Magic Numbers

This is a discussion on Magic Numbers within the Game Programming forums, part of the General Programming Boards category; Hi, Im following a book on game programming.... but the guy uses numbers which I can't understand why he has ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

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

    Thanks for your time

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    Thanks for your reply.
    I understand your point.

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

    Thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.
    Last edited by tabstop; 11-17-2008 at 07:43 AM.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    Quote Originally Posted by tabstop View Post
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Raigne View Post
    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. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    Quote Originally Posted by Salem View Post
    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. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by strokebow View Post
    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. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 05:03 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. the magic of MAGIC numbers
    By borko_b in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-27-2002, 03:36 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21