Simple message problem...

This is a discussion on Simple message problem... within the C++ Programming forums, part of the General Programming Boards category; I've been reading a book about how computer games are made, and I came across the "PeekMessage" command for windows ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Simple message problem...

    I've been reading a book about how computer games are made, and I came across the "PeekMessage" command for windows programming, and the book said to write it as below, but my compiler (Dev-C++, if that helps) wont accept it when I try use it to process input and logic etc, so, is it a problem with the code, or is it something else???

    Code:
    while(1)
    	{
    	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{ 
    		// test if this is a quit
            if (msg.message == WM_QUIT)
               break;
    	
    		// translate any accelerator keys
    		TranslateMessage(&msg);
    
    		// send the message to the window proc
    		DispatchMessage(&msg);
    		} // end if
        
        // main game processing goes here

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    This is how I always do it:

    Code:
    while (GetMessage (&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    for ( ;; ) {
    
        if ( PeekMessage ( &msg, NULL, 0, 0, PM_REMOVE ) ) { 
    	
            if ( msg.message == WM_QUIT ) break;
    	
    	TranslateMessage ( &msg );
    	DispatchMessage ( &msg );
            
        }
        
        else {
    
            // program code
    
        }
    
    }
    Now make sure your compiling it as a Win32 app and not a console app.

    Are you getting any errors or what?

    [note] The for loop is just a programming preference, while(1) does the same thing.
    What is C++?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Here is how I've always done it. (It's very close to Vicious' example).

    Code:
    	CGame myGame;
    	// All that windows stuff that is necessary goes here
    	myGame.Initialize(); // perform initialization to game objects, and openGL/directX
    	while(!done)
    	{
    		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	
    		{
    			if (msg.message==WM_QUIT)				
    			{
    				done=true;					
    			}
    			else						
    			{
    				TranslateMessage(&msg);			
    				DispatchMessage(&msg);			
    			}
    		}
    		else								
    		{
    			delta = calculateDelta();
    			myGame.Render(delta);
    		}
    	}
    	myGame.Close();
    I create an object (CGame in the above code) to do all the game related stuff. That way it is easy to separate it from the windows initialization stuff. The delta parameter that is passed to the render function is the time elapsed since the last time myGame.Render() was called. You will know why this is necessay as you get further into game programming.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>the time elapsed since the last time myGame.Render() was called
    Can't you calculate that from within Render()? I mean, store the time either as a member variable or as a static variable?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Gets() problem
    By akira181 in forum C++ Programming
    Replies: 17
    Last Post: 05-25-2006, 03:28 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 01:22 AM
  4. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 12:09 PM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

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