-
Notifications
You must be signed in to change notification settings - Fork 0
Creating an OpenGL window
In this tutorial, we will see how to create an OpenGL window using the SuperMaximo GameLibrary.
We will be initialising SDL, creating a window, and then update the window while checking for input. If we press the Escape key or click on the Close button, then the we want the program to close.
First off, we need to include the headers into our program. We want to include the headers that allow us to initialise SDL, create a window and handle input, which are the SMSDL, Display, and Input headers respectively. Make sure you've set the file inclusion options in your project correctly.
// C
#include <SuperMaximo_GameLibrary/SMSDL-C.h>
#include <SuperMaximo_GameLibrary/Display-C.h>
#include <SuperMaximo_GameLibrary/Input-C.h>
// Pascal
uses SMGL_SMSDL, SMGL_Input, SMGL_Display;Next, we need to initialise SDL. To do this, we use the initSDL function. Let's pass in SDL_INIT_EVERYTHING to initialise all SDL subsystems. At the end of the code we need to put the quitSDL function to close SDL.
// C
int main(void) {
initSDL(SDL_INIT_EVERYTHING);
quitSDL();
return EXIT_SUCCESS;
}
// Pascal
begin
initSDL(SDL_INIT_EVERYTHING);
quitSDL;
end.To initialise input, we use the initInput and quitInput functions, which take no arguments. Make sure these are inside init/quitSDL. Now we can initialise the display, which creates our OpenGL window. To do this, use the initDisplay function. This function takes six arguments; screen width, screen height, screen depth, framerate, fullscreen, window title. Screen width, and screen height are self explanatory. Screen depth is the maximum depth in 3D space an object can be at before clipping occurs. The framerate parameter determines the framerate that the game will run at. The parameter 'Fullscreen' takes a boolean value that determines whether the window should be fullscreen. Window title is the title that is displayed on the top of the window.
Let's have a window size of 800x600, with a framerate of 60 frames per second.
// C
int main(void) {
initSDL(SDL_INIT_EVERYTHING);
initInput();
initDisplay(800, 600, 1000, 60, SM_FALSE, "OpenGL window");
quitDisplay();
quitInput();
quitSDL();
return EXIT_SUCCESS;
}
// Pascal
begin
initSDL(SDL_INIT_EVERYTHING);
initInput;
initDisplay(800, 600, 1000, 60, SM_FALSE, 'OpenGL window');
quitDisplay;
quitInput;
quitSDL;
end.Now finally, lets put in a while loop that will refresh the window. It will also check if Escape is pressed or the close button is clicked, and if they are, then the program will stop. We use the keyPressed function, passing a keycode, to check if a key is pressed. The keycode for the Escape key is 27. We will also use the closeClicked function, which returns whether the close button has been clicked. To refresh the screen, we need to use the refreshScreen function, which takes no arguments.
// C
int main(void) {
initSDL(SDL_INIT_EVERYTHING);
initInput();
initDisplay(800, 600, 1000, 60, SM_FALSE, "OpenGL window");
while (!keyPressed(27) && !closeClicked()) {
refreshScreen();
}
quitDisplay();
quitInput();
quitSDL();
return EXIT_SUCCESS;
}
// Pascal
begin
initSDL(SDL_INIT_EVERYTHING);
initInput;
//We need to use the SM_TRUE and SM_FALSE booleans in Pascal
initDisplay(800, 600, 1000, 60, SM_FALSE, 'OpenGL window');
while (keyPressed(27) = SM_FALSE) and (closeClicked = SM_FALSE) do
begin
refreshScreen;
end;
quitDisplay;
quitInput;
quitSDL;
end.And that's it! Now we know how to initialise SDL, create an OpenGL window, and check for keyboard input!