OnUpdate/OnRender Question

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • OnUpdate/OnRender Question

      So in my continued struggle to port the system developed in this book over to mac/open gl (in hopes of creating a 2d action game) I have encountered another problem:

      Most of the core functions (VOnRender, VOnUpdate, etc.) in GameCodeApp are called through DirectX callbacks. As far as I know, OpenGL (i am using AGL, not GLUT) doesn't have anything like that.

      What is the best way to do this on my own? While I was building up all the different classes I was just having a timer fire every millisecond and call VOnUpdate and then VOnRender but clearly that is wrong (the elapsedTime between timer shots goes from one millisecond to five hundredths of a second once VOnRender is put in the mix -- this doesn't seem right).
    • RE: OnUpdate/OnRender Question

      Win32 & DirectX is something of a nightmare - there is a HUGE wad of code that handles Windows messages, lost resources, etc.

      I've never coded for Mac or OpenGL - but the heart of your main loop should look something like this:

      Source Code

      1. HRESULT BaseGame::ProcessNextFrame()
      2. {
      3. // Figure how much time has passed since the last time
      4. m_currTick = timeGetTime();
      5. if (m_currTick == m_lastUpdate)
      6. return S_OK;
      7. VOnUpdate(m_currTick - m_lastUpdate);
      8. m_lastUpdate = m_currTick;
      9. // It is time to draw ?
      10. if( m_runFullSpeed || ( (m_currTick - m_lastDraw) > SCREEN_REFRESH_RATE) )
      11. {
      12. if (S_OK == VOnRender())
      13. {
      14. // record the last successful render
      15. m_lastDraw = m_currTick;
      16. }
      17. }
      18. return S_OK;
      19. }
      Display All


      I grabbed that from the 1st edition - that had a much simpler main loop and didn't use all the DirectX framework stuff.

      The only thing this doesn't do is handle the Mac equivalent of the message pump - so you'll want to do that in a loop. In Win32 it would look a little like this:

      Source Code

      1. while( msg.message!=WM_QUIT )
      2. {
      3. // Part 1: Do idle work until a message is seen in the message queue
      4. while (!PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
      5. {
      6. g_App.MainLoop();
      7. }
      8. // Part 2: Pump messages until the queue is empty
      9. do
      10. {
      11. if (GetMessage(&msg, NULL, NULL, NULL))
      12. {
      13. TranslateMessage(&msg);
      14. DispatchMessage(&msg);
      15. }
      16. } while (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE));
      17. }
      Display All


      A few critical points to note are:
      1. If there are no messages for the message pump to process it should not wait for them to arrive.
      2. You should be able to frame limit your renderer.
      3. The call to the game simulation - VOnUpdate(), should happen as fast as the loop can tick.
      Mr.Mike
      Author, Programmer, Brewer, Patriot