/src/Example_RendererSGUsage/ApplicationLoop.h

http://github.com/Akranar/daguerreo · C Header · 116 lines · 80 code · 23 blank · 13 comment · 6 complexity · 664fc3fb87281b47f452d7a3f71e4e43 MD5 · raw file

  1. #ifndef _APPLICATION_LOOP_H_
  2. #define _APPLICATION_LOOP_H_
  3. #include "ExLib_WindowEvents.h"
  4. #include <Core_DoubleTimer.h>
  5. //*****************************************************************************
  6. // Note:
  7. // -> The reason this class exists and why it is a templated class
  8. // -> RendererBase is likely a member of GAME.
  9. // -> In OpenGL:
  10. // -> RendererBase's constructor initializes GLEW
  11. // -> Requires OpenGL Context Already Created.
  12. // -> Means WindowEvents::NewRenderContext
  13. // -> must already have been called.
  14. // -> With GAME as template parameter,
  15. // -> Can call GAME's constructor after RenderContext Created.
  16. //*****************************************************************************
  17. template <class GAME>
  18. class ApplicationLoop
  19. {
  20. double frames_per_second;
  21. bool show_fps_in_title;
  22. public:
  23. ApplicationLoop();
  24. virtual ~ApplicationLoop();
  25. void Run();
  26. inline double GetFPS() const;
  27. inline void ShowFPSInTitle(bool value);
  28. };
  29. template <class GAME>
  30. ApplicationLoop<GAME>::ApplicationLoop()
  31. :
  32. show_fps_in_title(true)
  33. {
  34. }
  35. template <class GAME>
  36. ApplicationLoop<GAME>::~ApplicationLoop()
  37. {
  38. }
  39. template <class GAME>
  40. inline double ApplicationLoop<GAME>::GetFPS() const
  41. {
  42. return frames_per_second;
  43. }
  44. template <class GAME>
  45. inline void ApplicationLoop<GAME>::ShowFPSInTitle(bool value)
  46. {
  47. show_fps_in_title = value;
  48. }
  49. template <class GAME>
  50. void ApplicationLoop<GAME>::Run()
  51. {
  52. WindowEvents * window = new WindowEvents(800, 600);
  53. window->Open();
  54. WindowPixelFormat pixel_format;
  55. //pixel_format.SetAll(8, 8, 8, 8, 24, 8, 1, 4);
  56. pixel_format.SetAll(8, 8, 8, 8, 24, 8, 0, 1);
  57. RenderContext * render_context = window->NewRenderContext(&pixel_format);
  58. window->MakeCurrentDraw(render_context);
  59. window->SetVSync(true);
  60. char title[100];
  61. DoubleTimer timer;
  62. timer.StartTimer();
  63. int frames = 0;
  64. GAME game;
  65. game.Init(window);
  66. window->Show();
  67. double start_time = timer.GetTimeInSeconds();
  68. while (WindowEvents::ProcessEvents())
  69. {
  70. game.Update(timer.GetTimeInSeconds(), window);
  71. if (window->IsVisible() && window->IsAlive())
  72. {
  73. game.Draw();
  74. window->FlipBuffers();
  75. }
  76. frames++;
  77. if ((timer.GetTimeInSeconds() - start_time) > 1.0)
  78. {
  79. frames_per_second = double(frames) / (timer.GetTimeInSeconds() - start_time);
  80. start_time = timer.GetTimeInSeconds();
  81. frames = 0;
  82. if (show_fps_in_title && window->IsVisible())
  83. {
  84. sprintf (title, "FPS: %f", frames_per_second);
  85. window->SetTitle(title);
  86. }
  87. }
  88. }
  89. game.Deinit();
  90. delete render_context;
  91. delete window;
  92. }
  93. #endif