secsPerFrame
which is equal to 20 ms.).loopSlot
variable) and we wait for that amount of time taking into consideration the time we spent in our loop. But instead of doing a single wait for the whole available time period we do small waits. This will allow other tasks to run and will avoid the sleep accuracy problems we mentioned before. Then, what we do is:
1. Calculate the time at which we should exit this wait method and start another iteration of our game loop (which is the variable endTime
).
2. Compare the current time with that end time and wait just one millisecond if we have not reached that time yet.glfwSwapInterval
method (if we set it to 2, we would get 30 FPS).Window
allowing some basic parameterization of its characteristics (such as title and size). That Window
class will also provide a method to detect key presses which will be used in our game loop:Window
class besides providing the initialization code also needs to be aware of resizing. So it needs to setup a callback that will be invoked whenever the window is resized. The callback will receive the width and height, in pixels, of the framebuffer (the rendering area, in this sample, the display area). If you want the width, height of the framebuffer in screen coordinates you may use the glfwSetWindowSizeCallback
method. Screen coordinates don't necessarily correspond to pixels (for instance, on a Mac with Retina display). Since we are going to use that information when performing some OpenGL calls, we are interested in pixels not in screen coordinates. You can get more information in the GLFW documentation.Renderer
class which will handle our game render logic. By now, it will just have an empty init
method and another method to clear the screen with the configured clear colour:IGameLogic
which will encapsulate our game logic. By doing this we will make our game engine reusable across different titles. This interface will have methods to get the input, to update the game state and to render game-specific data.GameEngine
which will contain our game loop code. This class will implement to hold the game loop:vSync
parameter allows us to select if we want to use v-sync or not. You can see we implement the run method of our GameEngine
class which will contain our game loop:GameEngine
class provides a run
method that will perform the initialization tasks and will run the game loop until our window is closed. A little bit note on threading. GLFW requires to be initialized from the main thread. Polling of events should also be done in that thread. Therefore, instead of creating a separate thread for the game loop, which is what you would see commonly in games, we will execute everything from the main thread.GameEngine
class just delegates the input and update methods to the IGameLogic
instance. In the render method it delegates also to the IGameLogic
instance and updates the window.GameEngine
instance and run it.render
method will just clear the window with that colour.render
method we get notified when the window has been resized in order to update the viewport to locate the center of the coordinates to the center of the window.The only OpenGL 3.x and 4.x contexts currently supported by OS X are forward-compatible, core profile contexts. The supported versions are 3.2 on 10.7 Lion and 3.3 and 4.1 on 10.9 Mavericks. In all cases, your GPU needs to support the specified OpenGL version for context creation to succeed.
Window
class before the window is created: