Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JClayton

Window Updating Problem

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!