1. ## Magic Numbers

Hi,

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,

Code:
```  // 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);```
This code sets a bitmap image of a saucer to the middle of the screen.

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.

2. 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.

How would I use the message box to return a variable value?

Thanks

4. Something like
Code:
```stringstream message;
message << "Variable foo is " << foo;
MessageBox(NULL, message.c_str(), MB_OK);```
Edit: Note I'm typing from memory, so check it first.

5. Originally Posted by tabstop
Something like
Code:
```stringstream message;
message << "Variable foo is " << foo;
MessageBox(NULL, message.c_str(), MB_OK);```
Edit: Note I'm typing from memory, so check it first.
Cool, thanks.

What should I #include?

How does the stringstream and its corresponding variable message work?

Thanks

6. 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

7. 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.

8. Shouldn't that be
Code:
`message.str().c_str()`
c_str() is not a member of std::stringstream.

and MessageBox has 4 parameters
Code:
`MessageBox(HWND, Caption, Title, nShowCmd)`

9. Originally Posted by Raigne
Shouldn't that be
Code:
`message.str().c_str()`
c_str() is not a member of std::stringstream.

and MessageBox has 4 parameters
Code:
`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.

10. Originally Posted by Salem
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.
Hi,

I got the message box working but this method seems very interesting.

How do you use the debugger and breakpoints. How does this work?

Thanks

11. 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.

12. Originally Posted by strokebow
Hi,

I got the message box working but this method seems very interesting.

How do you use the debugger and breakpoints. How does this work?

Thanks
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.

I still pop up windows though or use printf()/cout to get values. Obviously I should use the debugger though...

13. 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.