Hi all! :)
[Update 2015-11-14: This article is out of date. Check out the latest version at Gigi Labs.]
Yesterday's article dealt with setting up SDL2 in Visual Studio. Today we're going to continue what we did there by showing an empty window and allowing the user to exit by pressing the X at the top-right of the window. This is very similar to the "SDL Quickstart for Linux: Empty Window" article I wrote almost three years ago on my other blog; however this article is for Windows and deals with SDL2, rather than SDL1.2.x.
It takes very little to show an empty window. Use the following code:
#include <SDL2/SDL.h>
int main(int argc, char ** argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * screen = SDL_CreateWindow("My SDL Empty Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Quit();
return 0;
}
We use SDL_Init() to initialise SDL, and tell it which subsystems we need - in this case video is enough. At the end, we use SDL_Quit() to clean up. It is possible to set up SDL_Quit with atexit(), as the SDL_Quit() documentation shows.
We create a window using SDL_CreateWindow(). This is quite different from how we used to do it in SDL 1.2.x. We pass it the window caption, initial coordinates where to put the window (not important in our case), window width and height, and flags (e.g. fullscreen).
If you try and run the code, it will work, but the window will flash for half a second and then disappear. You can put a call to SDL_Delay() to make it persist for a certain number of milliseconds:
SDL_Delay(3000);
Now, let's make the window actually remain until it is closed. Use the following code:
#include <SDL2/SDL.h>
int main(int argc, char ** argv)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * screen = SDL_CreateWindow("My SDL Empty Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
while (!quit)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Quit();
return 0;
}
The while (!quit) part is very typical in games and is in fact called a game loop. We basically loop forever, until the conditions necessary for quitting occur.
We use SDL_WaitEvent() to wait for an event (e.g. keypress) to happen, and we pass a reference to an SDL_Event structure. Another possibility is to use SDL_PollEvent(), which checks continuously for events and consumes a lot of CPU cycles (SDL_WaitEvent() basically just sleeps until an event occurs, so it's much more lightweight).
The event type gives you an idea of what happened. It could be a key press, mouse wheel movement, touch interaction, etc. In our case we're interested in the SDL_QUIT event type, which means the user clicked the window's top-right X button to close it.
We can now run this code, and the window remains until you close it:
Wasn't that easy? You can use this as a starting point to start drawing stuff in your window. Have fun, and come back again for more tutorials! :)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.