Im following a book on game programming.... but the guy uses numbers which I can't understand why he has them and how they relate. He doesn't explain his code at all. In particular,
This code sets a bitmap image of a saucer to the middle of the screen.
// Set the initial saucer position and speed - Middle of the screen
g_iSaucerX = 250 - (g_pSaucer->GetWidth() / 2);
g_iSaucerY = 200 - (g_pSaucer->GetHeight() / 2);
Now, GetWidth simply returns m_iWidth. This is set to 640 and 480 for the height.
So, in trying to understand this I changed g_iSaucer to -70: what I believed the calculation to be equal to. This caused the image to begin at the extreme left of the window :(
Anyone know what the 250 is?
Also, when I do programmes with console I do a quick cout << to display any variables I'm unsure what they contain at a particular moment. How can I do the equivalent in the Win32 API. this would be a very handy technique for understanding code and also debugging.
Thanks for your time
Perhaps you could pop a MessageBox with the values of the variables.
I'm guessing (I like guessing) that that g_pSaucer's getWidth would return the width of the thing you're drawing, not the screen itself. So if the window is 500 units wide, then half of that would be 250, and if the thing is centered you'd take off half of that to get the left position.
Thanks for your reply.
I understand your point.
How would I use the message box to return a variable value?
Edit: Note I'm typing from memory, so check it first.
message << "Variable foo is " << foo;
MessageBox(NULL, message.c_str(), MB_OK);
Originally Posted by tabstop
What should I #include?
How does the stringstream and its corresponding variable message work?
stringstream is in <sstream>. You should be able to look it up in whatever book you happen to have -- it's just a stream interface into a string variable, more or less
Learn to use a debugger + breakpoints + watch windows
A far more useful skill long term than popping up a window, or continually editing the code with random cout statements.
Shouldn't that be
c_str() is not a member of std::stringstream.
and MessageBox has 4 parameters
MessageBox(HWND, Caption, Title, nShowCmd)
Right, there's an extra .str() in there to get the string object, and then it's .c_str() after that. And I did forget about the title argument.
Originally Posted by Raigne
Originally Posted by Salem
I got the message box working but this method seems very interesting.
How do you use the debugger and breakpoints. How does this work?
A Visual studio example would be
Right-click in the left margin of an executable statement, then choose "insert breakpoint".
Then do something like run->debug
Then shortly, your program will run, then stop at the breakpoint.
The rest is reading the manual, and trying stuff out to see what it does.
Basically, you just tell the debugger to stop at a point. There you can examine some local varables for example and see what is going wrong. Faster than popping up windows and more details.
Originally Posted by strokebow
I still pop up windows though or use printf()/cout to get values. Obviously I should use the debugger though...
That code puts the bitmap at 250,200. F9 on MSVS (MSVC 6 key mappings) will set a breakpoint or you can click on the line number (if shown) in the left margin.
My settings are:
F9 - set breakpoint
F10 - step over
F11 - step into
I use MSVS 2003 at work but have noticed that MSVS 2005 and 2008 have more features in the debugger. In 2005 you have a break at function feature as well as several different types of breakpoint filters.