Skip to main content
gamedev.net
🔒 Locked

Window Updating Problem

Started by JClayton May 6, 2004 at 6:58 PM 2 replies 300+ views
Original Post
JClayton
JClayton
I am writing an OpenGL renderer and I have found an unusual problem... the window is only updated when windows needs to send a message (i.e. mouse movement/press and key presses). I am using a windows wrapper class with a message router. I will include the relavant code below: Windows Wrapper Functions:
/*****************************************************************************
 CWND::MainLoop()

 Window''s main loop that receives and translates messages as well as calling
 the window''s main logic loop.
*****************************************************************************/
LRESULT CWND::MainLoop()
{  
  // Main message loop

  
  MSG msg;
    
  while(GetMessage(&msg, NULL, 0, 0) > 0)
  {   
    // Translate & dispatch to even quene

    TranslateMessage(&msg);
    DispatchMessage(&msg);
    
    // Main game logic cycle

    GameCycle(this);
  }
}

/*****************************************************************************
 CWND::MessageRouter(HWND, UINT, WPARAM, LPARAM)

 Routes a message to the correct class instance (static)
*****************************************************************************/
LRESULT CALLBACK CWND::MessageRouter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  CWND *p_wnd = 0;
    
  if(msg == WM_NCCREATE)
  {    
    // retrieve Window instance from window creation data and associate

    LPCREATESTRUCT cs = reinterpret_cast<LPCREATESTRUCT>(lParam);
    
    SetWindowLong(hwnd, GWL_USERDATA, (long)(cs->lpCreateParams));
	  
	  // retrieve associated Window instance

	  p_wnd = reinterpret_cast<CWND*>(GetWindowLong(hwnd, GWL_USERDATA));
	  
    // save window handle

    if(p_wnd)
    {
      p_wnd->SetHWND(hwnd);
    }
  }
  else
  {
    // retrieve associated Window instance

    p_wnd = reinterpret_cast<CWND*>(GetWindowLong(hwnd, GWL_USERDATA));
  }
  
  if(p_wnd)
  {    
    // retrieve the associated message handler

    t_messageIterator i = p_wnd->GetMessageHandler(msg);
    
    // test iterator for validity

    if(i != p_wnd->GetEndItr())
        return (i->second)(p_wnd, hwnd, wParam, lParam); 
  }
    
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
Program Main loop:
int WINAPI WinMain(HINSTANCE instance, HINSTANCE PrevInstance, LPSTR cmdLine, 
    int showCmd)
{  
  // Ask The User Which Screen Mode They Prefer

	if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",
      MB_YESNO | MB_ICONQUESTION) == IDNO)
	{
		g_fullscreen = false;						// Windowed Mode

	}
  
  // initialize the timer

  if(!g_timer.Init())
      { MessageBox(NULL, "Timer not supported", "ERROR", MB_OK); }
  
  // construct the class

  COGLWND *p_window = new COGLWND("A Window", 800, 600, 32, g_fullscreen, GameCycle);
   
  // Create a window

  p_window->CreateWnd(showCmd, instance);
  
  // register the message handlers

  p_window->RegisterDefaultHandlers();
  
  // Setup the pixel format

  p_window->InitPixelFormat(32);
  
  // enable openGL

  p_window->EnableOGL();
  p_window->InitOGL();
  p_window->CreateBitmapFont("Impact", 30);
  
  // Enter the window''s main loop

  p_window->MainLoop();
  
  // clean up 

  p_window->ClearFont(0);
  delete p_window;
  p_window = NULL;
}
NOTE: The main loop is sloppy I know... its only temporary
JClayton
JClayton
By updating I mean the scene is not rendering unless a message is getting sent.
vTheHero
vTheHero
You should use PeekMessage() instead of GetMessage(). GetMessage() is a blocking call so it will stop the program until a message happens. PeekMessage() does not block so you can check to see if a message exists and then do the normal GetMessage(),TranslateMessage(),DispatchMessage() code but if a message isn''t available your program execution continues normally.

-Dan
JClayton
JClayton
Thank you. Just for future reference... I modified the pump to the following:

while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ))
{
if(!GetMessage(&msg, NULL, 0, 0 ) < 0)
{
break;
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}

GameCycle(this);
}


This cycle seems more efficient because it still allows the main cycle if there isn''t a message while still utalizing the return and error checking power of GetMessage().

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.