Web Design,Programming,Mobile,iOS/Android,GameDev,IT Businness,eCommerce,Social Media


Saturday, 29 December 2012

SDL Windows Programming - Creating your first Window


In this text we will create our first windows program using SDL ( Simple Direct Media Layer ). SDL is low level graphics api and can be used as a windowing platform to create window programs or as a graphics layer to create small games. It can be very powerful if its used correctly.

In this blog post i discuss the steps to configure SDL with Visual Studio. Follow the steps and if you are all set up come back. If you already configured SDL you are now ready to create your first window.

#include <sdl/SDL.h>
int main(int argc, char *argv[])
  int            WindowWidth = 720;
  int            WindowHeight = 480;
  SDL_Event      Event;
  SDL_Surface    *Screen;
  bool           Running = true;
  Screen = SDL_SetVideoMode(WindowWidth, WindowHeight, 32, SDL_SWSURFACE);
  SDL_WM_SetCaption("My First Window", "First Window");
         case SDL_QUIT : 
          Running = false;
  return 0;

The code above creates simple blank window. Lets explain the code piece by piece. The first line includes the sdl library. Inside main function we create some variables. WindowWidth and WindowHeight define the windows width and height. Windows are static and they wait for an event to happen before they do something. Events can be different based on users input. For exampleand an event can be a mouse click, key press etc. And with SDL_Event Event we create a variable and with it we can handle all events that can happen inside our window. SDL_Surface *Screen creates a pointer variable that will hold our screen. The screen is the area inside window borders. We need a variable to manipulate this screen, for displaying different things we want and flipping the screens.

When programming with graphics there is a buffer in the background that prepares our screen to display stuff we want. A buffer is like a container that fills the screen with pixels we want to display. SDL_Flip() function then flips the current screen with the buffer that has been prepared and in this moment the buffer becomes the actual screen while the screen we had previously becomes the buffer. And again the buffer prepares other pixels for display based on our input.Without the buffer the screen will prepare the pixels and also display them in the same moment and what we would see in first moment is our laggy screen with messed pixels.

And the last variable is a boolean variable which will control the state of our program. If this variable turns to false this means our program has terminated and we call functions to exit the SDL and close the window. Based on different users event we make this variable to false if we want to terminate the program. For example if a user clicks the X (exit button) then we make this variable to false and the program terminates.

As we said earlier we have a variable Event that checks for user events to happen and variable that controls the state of our program. This variables are inside while loop which will be run when SDL is intialized correctly and will end until the variable Running is set to false.

SDL_PollEvent is a way to check for events that can happen inside our window. switch(Event.type) checks for the event type and if this event is SDL_QUIT it means that the user has clicked the X button and we should terminate the program. And as we said we close the program by turning the Running variable into false. When this variable is set to false the first while loop will see that Running is set to false and will skip everything that is inside the brackes.

After the first while loop is SDL_Quit(); function which will quit SDL and free all resources that are used by SDL. The result from the code above should look like this

SDL Windows Programming

Written by

Thank you for taking your time to browse through our blog. We try to build as much rich content base as we can. We focus in modern technologies, computer science design and business.


Post a Comment


© 2013 smartnoob . All rights resevered. Designed by Templateism