secsPerFramewhich is equal to 20 ms.).
loopSlotvariable) 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.
glfwSwapIntervalmethod (if we set it to 2, we would get 30 FPS).
Windowallowing some basic parameterization of its characteristics (such as title and size). That
Windowclass will also provide a method to detect key presses which will be used in our game loop:
Windowclass 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
glfwSetWindowSizeCallbackmethod. 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.
Rendererclass which will handle our game render logic. By now, it will just have an empty
initmethod and another method to clear the screen with the configured clear colour:
IGameLogicwhich 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.
GameEnginewhich will contain our game loop code. This class will implement to hold the game loop:
vSyncparameter allows us to select if we want to use v-sync or not. You can see we implement the run method of our
GameEngineclass which will contain our game loop:
GameEngineclass provides a
runmethod 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.
GameEngineclass just delegates the input and update methods to the
IGameLogicinstance. In the render method it delegates also to the
IGameLogicinstance and updates the window.
GameEngineinstance and run it.
rendermethod will just clear the window with that colour.
rendermethod 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.
Windowclass before the window is created: