SimpleGL  1.1.0
A framework for platform independent rendering
application.cpp
Go to the documentation of this file.
1 
9 
10 #include <GLFW/glfw3.h>
11 
13 
14 namespace sgl {
15 
17  targetUps = 60;
18  targetFps = 60;
19  running = false;
20  }
21 
23  /* Delete window */
24  delete window;
25 
26  /* Terminate GLFW */
28  }
29 
31  init();
32  mainLoop();
33  dispose();
34  }
35 
38  running = true;
39  }
40 
42  /* Initialize time accumulator and update interval */
43  float accumulator = 0.0f;
44  float interval = 1.0f / float(targetUps);
45 
46  /* Main Loop */
47  while (running) {
48  /* Get delta time and update the time accumulator */
49  float delta = TimeUtil::getDelta();
50  accumulator += delta;
51 
52  /* Handle events */
55 
56  /* Update application if enough time has passed */
57  while (accumulator >= interval) {
58  updateScene(interval);
60  accumulator -= interval;
61  }
62 
63  /* Calculate alpha value for interpolation */
64  float alpha = accumulator / interval;
65 
66  /* Clear screen and render scene */
67  clear();
68  renderScene(alpha);
70 
71  /* Update time utility to set FPS and UPS if a whole second has passed */
73 
74  /* Show informations in the title */
76 
77  /* Swap buffers and poll events */
78  window->update();
79 
80  /* Synchronize to the target FPS */
82 
83  /* Check if application should close */
84  if (window->shouldClose()) {
85  running = false;
86  }
87  }
88  }
89 
91  /* Nothing to do here */
92  }
93 
94  double TimeUtil::lastLoopTime = 0.0;
95  float TimeUtil::timeCount = 0.0f;
96  int TimeUtil::fps = 0;
97  int TimeUtil::fpsCount = 0;
98  int TimeUtil::ups = 0;
99  int TimeUtil::upsCount = 0;
100 
101  TimeUtil::TimeUtil(void) {
102  /* Nothing to do here */
103  }
104 
105  void TimeUtil::init() {
106  lastLoopTime = getTime();
107  }
108 
109  double TimeUtil::getTime() {
110  return glfwGetTime();
111  }
112 
114  /* Delta time is the elapsed time since the last loop and now */
115  double now = getTime();
116  float delta = float(now - lastLoopTime);
117  lastLoopTime = now;
118 
119  /* Increment the time counter by the elapsed time */
120  timeCount += delta;
121 
122  return delta;
123  }
124 
126  /* Update FPS and UPS if a whole second has passed */
127  if (timeCount > 1.0f) {
128  /* Update FPS */
129  fps = fpsCount;
130  fpsCount = 0;
131 
132  /* Update UPS */
133  ups = upsCount;
134  upsCount = 0;
135 
136  /* Subtract one second of the time counter */
137  timeCount -= 1.0f;
138  }
139  }
140 
142  fpsCount++;
143  }
144 
146  upsCount++;
147  }
148 
150  /* If FPS is 0 return the value of the counter */
151  return fps > 0 ? fps : fpsCount;
152  }
153 
155  /* If UPS is 0 return the value of the counter */
156  return ups > 0 ? ups : upsCount;
157  }
158 
160  return lastLoopTime;
161  }
162 }
static void incUPS()
Increments the UPS counter by one.
~Application(void)
Destroys the application.
Definition: application.cpp:22
void update()
Convenience method to swap buffers and poll events in one call.
Definition: glfw_window.cpp:99
void sync(int fps)
Syncs the window framerate to specified FPS.
void showInfoTitle()
Shows informations in the title.
Definition: glfw_window.cpp:77
void run()
Starts the application.
Definition: application.cpp:30
virtual void renderScene(float alpha)=0
This method is intended to used for drawing the scene.
static void clearEvents()
Clears the event list, this should be called after every loop.
Definition: event.cpp:23
void mainLoop()
The main loop is a fixed timestep loop.
Definition: application.cpp:41
Window * window
The window of this application.
Definition: application.hpp:38
static void init()
Initializes the time utility.
virtual void updateScene(float delta)=0
This method is intended to update objects of the application.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
Application(void)
Initializes a new application.
Definition: application.cpp:16
static float getDelta()
Returns the time that have passed since the last loop.
static void incFPS()
Increments the FPS counter by one.
bool shouldClose()
Checks the close flag for the window.
virtual void clear()=0
This method is intended to clear the color and depth buffers.
static int getFPS()
Returns the current frames per second.
int targetUps
Stores the desired UPS.
Definition: application.hpp:32
static void update()
Updates FPS and UPS if a whole second has passed.
virtual void handleEvents(std::vector< Event * > events)=0
This method is intended to handle events like user input.
bool running
Tells if the application is running.
Definition: application.hpp:29
static int getUPS()
Returns the current updates per second.
static double getLastLoopTime()
Returns the last loop time.
virtual void dispose()
Disposes the application.
Definition: application.cpp:90
void init()
Initializes the application.
Definition: application.cpp:36
static void terminate()
Terminates GLFW, this should only be called at the end of the application.
static std::vector< Event * > getEvents()
Returns the list of events.
Definition: event.cpp:13
int targetFps
Stores the desired FPS.
Definition: application.hpp:35
static double getTime()
Returns the application time in seconds.