PageRenderTime 37ms CodeModel.GetById 29ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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