Thread: WinMain and return msg.wParam problems

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11

    WinMain and return msg.wParam problems

    Hi

    I'm a begining C++ programer. I'm working from a book called Beginning Game Programing by Johnathan Harbour. I'm up to using the DirectX API. I have the latest Direct X SDK, the book was written with 9.0b and beyond in mind. Im using Visual C++ 6.

    The program is meant to have a black background (windowed at 640 x 480) and have a sprite of a cat walking. All that happens is that the screen flicks up and then disapears. Its not meant to do this. I will go back and check for a return 0 instead of something else in my code but i dont think that is the problem...

    This is the code of my WinMain...

    Code:
    // Beging Game Programing
    // Chapter 7
    // winmain.cpp -- windows code framework
    
    
    
    // header files
    
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <time.h>
    #include <stdio.h>
    #include "dxgraphics.h"
    #include "game.h"
    
    
    
    //windows event call back function
    LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    	{
    		switch ( msg )
    
    		{
    		case WM_DESTROY:
    			//release the direct 3d device
    			if (d3ddev != NULL)
    				d3ddev->Release();
    
    			//release the direct 3d object
    			if (d3d != NULL)
    				d3d->Release();
    
    			//call the 'front end' shut-down function
    			Game_End(hWnd);
    
    			// tell windows to kill this program
    			PostQuitMessage(0);
    			return 0;
    		}
    		
    	return DefWindowProc (hWnd, msg, wParam, lParam);
     
    	}
    
    
    //helper function to set up the window properties
    ATOM MyRegisterClass(HINSTANCE hInstance)
    	{
    		//create the window class structure
    		WNDCLASSEX wc;
    		wc.cbSize = sizeof(WNDCLASSEX);
    
    		//fill the struct with info
    		wc.style = CS_HREDRAW | CS_VREDRAW;
    		wc.lpfnWndProc = (WNDPROC)WinProc;
    		wc.cbClsExtra = 0;
    		wc.cbWndExtra = 0;
    		wc.hInstance = hInstance;
    		wc.hIcon = NULL;
    		wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    		wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    		wc.lpszMenuName = NULL;
    		wc.lpszClassName = APPTITLE;
    		wc.hIconSm = NULL;
    
    //set up the window with the class info
    return RegisterClassEx(&wc);
    }
    
    
    //entry point for a windows program
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    
    {
    	MSG msg;
    	HWND hWnd;
    
    	//Register the class
    	MyRegisterClass(hInstance);
    
    	//set up the screen in windowed or full-screen mode?
    	DWORD style;
    	if (FULLSCREEN)
    		style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
    	else
    		style = WS_OVERLAPPED;
    
    	//Create new window
    	hWnd = CreateWindow(
    		APPTITLE,				//window class
    		APPTITLE,				//title bar
    		style,					//window style
    		CW_USEDEFAULT,			//x position of window
    		CW_USEDEFAULT,			//y position of window
    		SCREEN_WIDTH,			//width of the window
    		SCREEN_HEIGHT,			//height of the window
    		NULL,					//parent window
    		NULL,					//menu
    		hInstance,				//application instance
    		NULL);					//window parameters
    
    	//Was there an error creating the window?
    	if (!hWnd)
    		return false;
    
    	//display the window
    	ShowWindow(hWnd, nCmdShow);
    	UpdateWindow(hWnd);
    
    	if (!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN))
    		{
    		MessageBox(hWnd, "Error initializing Direct 3D", "Error", MB_OK);
    		return 0;
    		}
    	//initalize the game
    	if (!Game_Init(hWnd))
    		{
    			MessageBox(hWnd, "Error initializing the game", "Error", MB_OK);
    			return 0;
    		}
    
    	//main message loop
    	int done = 0;
    	
    	while (!done)
    		{
    			if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    			{
    				//look for quit message
    				if (msg.message == WM_QUIT)
    					done = 1;
    
    				//decode and pass messages on to WndProc
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    
    			else
    				//process game loop ('else' prevents running after close)
    				Game_Run(hWnd);
    
    			return 0;
    		}
    	return msg.wParam;
    
    }
    The one line that has a warning is
    return msg.wParam, just at the end. It says...
    warning C4700: local variable 'msg' used without having been initialized
    This is the only warning/error in my entire program and it doesnt work. I can post the other files if need be, but i think it is this one line.

    Any help would be appreciated, will go and check for other errors manualy now.

    Cheers
    Manaxter
    Last edited by Manaxter; 01-08-2006 at 02:20 AM. Reason: Forgot to add the last little polite bit

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    Ok, i must appologise...not a good start.

    I added a return 0 just after Game_Run(hWnd). Ooops.

    But, if you could tell me why i get that warning, that would be good!

    Cheers
    Manaxter

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The compiler is just telling you that the loop
    Code:
    while (!done)
    might not be executed and in this case
    Code:
    return msg.wParam;
    would return an uninitialized value.
    So I guess
    Code:
    do {
      ...
    } while ( ! done );
    Would be a better solution.
    Kurt

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Which is actually pretty stupid of the compiler. A simple flow analysis using the fact that nothing between done's initialization and the loop would prove that msg MUST be initialized (provided that PeekMessage assigns a value, which it cannot know, but should assume).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    this is what my message loop looks like:

    Code:
    MSG msg;
        while ( 1 ) 
        {
            // Did we recieve a message, or are we idling ?
            if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
            {
                if ( msg.message == WM_QUIT)
                {
                    break;
                }
                TranslateMessage( &msg );
                DispatchMessage ( &msg );
            } 
            else 
            {
                // Render the frame
                if ( m_pGraphics->GetDevice() != NULL && m_active )
                {
                    OnUpdateFrame();
                    OnRenderFrame();
                }
            } 
        }
    It compiles just fine. Also note that my DX is wrapped in classes so ignore the code for rendering.
    My Website
    010000110010101100101011
    Add Color To Your Code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM