SDL Gamer

Search this site

SDLgamer Menu‎ > ‎

Lesson 3. Create a window

After setup SDL in Dev-C++ , let's see how simple it is to create a window !    At the beginning , the most difficult part is to setup an environment for a successful compilation.    The programming part is relative simple , since it is just making a logic flow.    Once you can make it , then you are on the half way of success !

Lesson 3-1
In Dev-C++ , select File -> New -> Project
Click on "Empty Project" , then type in SDLWin for project name.

Lesson 3-2
Create a folder "ATest" and save the project in there.

Lesson 3-3
After saved the project file , click on "ICON New" and select "Source File  CTRL+ N ".
Then Yes for add new file to the current project.
You should see a blank file attaching to Project SDLWin on left hand side.
Select File -> Save As CTRL + F12 -> give it a name "test.cpp"

Lesson 3-4
Type in the following program code and save it.
Or you can download this attachment and Add it to Project SDLWin.

 #include "SDL.h"

int main(int argc, char *argv[])

SDL_Surface *screen;


if( SDL_Init(SDL_INIT_VIDEO) < 0 ) exit(1);

SDL_WM_SetCaption("SDL Window", NULL);

screen = SDL_SetVideoMode( 640 , 480 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);

SDL_FillRect(screen , NULL , 0x66FF00);


SDL_Delay( 5000 );

return 0;

Lesson 3-5
Select Execute -> Compile & Run F9

You should be able to see this window pops up for 5 sec.
If you see it , that's a good good sign.      That means you win !
You made a window for 5 sec !!!

Lesson 3-6
That's quite simple , isn't it?
So , let's check the document of SDL to see what we did.
The document locate in :

#include "SDL.h"
Since we are going to use the function that defined in SDL.h , thus we include
it here.

SDL_Surface *screen;
SDL_Surface's represent areas of "graphical" memory, memory that can be drawn to.
Later , we will fill the color in the window , so it needs a SDL Surface to store the color information.

SDL_Quit shuts down all SDL subsystems and frees the resources allocated to them. This should always be called before you exit.

if( SDL_Init(SDL_INIT_VIDEO) < 0 ) exit(1);
Initializes SDL. This should be called before all other SDL functions.
We can initialize video , audio , cd rom , joy stick , timer by their flag.   Since we only
use video function here , so only put in flag "SDL_INIT_VIDEO"

SDL_WM_SetCaption("SDL Window", NULL);
This is what the caption will show in the bar of a window.

screen = SDL_SetVideoMode( 640 , 480 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);
The attributes of the windows been set in here.         Width=640 , Height =480 , 32 bit per pixel
Check the document for more flags.
  SDL_DOUBLEBUF Enable hardware double buffering; only valid with SDL_HWSURFACE. Calling SDL_Flip will flip the buffers and update the screen. All drawing will take place on the surface that is not displayed at the moment. If double buffering could not be enabled then SDL_Flip will just perform a SDL_UpdateRect on the entire screen.
 SDL_HWSURFACE  Create the video surface in video memory
 SDL_ANYFORMAT  Normally, if a video surface of the requested bits-per-pixel (bpp) is not available, SDL will emulate one with a shadow surface. Passing SDL_ANYFORMAT prevents this and causes SDL to use the video surface, regardless of its pixel depth.

SDL_FillRect(screen , NULL , 0x66FF00);
This function performs a fast fill of the given rectangle with color. If dstrect is NULL, the whole surface will be filled with color.
The color format is 0xRRGGBB

Use this function to update screen.

SDL_Delay( 5000 );
Ask the system delay 5000 msec

Lesson 3 END

That's very amazing & impressive , isn't it?
The SDL almost wrap all heavy work and let user be able to create a window
in a minute !

Let's continue with the excited Lesson 4 : Load a bmp image file into a window

Copyright © 2009 bdragon All rights reserved.

Bdragon Ho,
Aug 3, 2009, 7:02 PM