Initialize one or more SDL subsystems (SDL_Init )
The SDL_Init function should be the very first function that is called. If this function is not called, no other SDL function will operate. When you call this function, you specify which of the SDL subsystems you intend to use in the application. The identifiers for each of the subsystems are shown below.
Identifier Subsystem
SDL_INIT_TIMER The timer subsystem will be initialized.
SDL_INIT_AUDIO The audio subsystem will be initialized.
SDL_INIT_VIDEO The video subsystem will be initialized.
SDL_INIT_CDROM The CD-ROM subsystem will be initialized.
SDL_INIT_JOYSTICK The joystick subsystem will be initialized.
SDL_INIT_EVERYTHING All subsystems will be initialized.
You might be tempted to always use SDL_INIT_EVERYTHING.
Shut down all SDL subsystems (SDL_Quit)
Since you now know how to initialize SDL subsystems, it is time to take a look at how to shut them down. This is done using the SDL_Quit function. The first function, SDL_Quit, is extremely easy to use. It takes no parameter and returns no value. It simply shuts down all SDL subsystems. As such, it is typically the last thing you call in your program
Sample code to initialize and quit SDL
//initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//rest of program goes here
//shut down SDL
SDL_Quit();
Setting the Display Mode
The first surface you need to create in any SDL application is the display surface. This is the only surface that the user of your application will actually see. To set up the display surface, you call SDL_SetVideoMode.
SDL_Surface *SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags);
The width and height parameters, for instance, specify how wide and how tall you want the screen or window to be, respectively. The bpp parameter tells SDL how many bits per pixel you want the surface to have. The
flags parameter is a number of bit flags ored together that tell SDL exactly how you want your surface created.
The various flags and brief descriptions of them:
Bit SDL_SWSURFACE
Flag meaning: The surface is to be created in the main memory.
Bit SDL_HWSURFACE
Flag meaning: The surface is to be created in the video memory.
Bit SDL_ASYNCBLIT
Flag meaning: You want to use asynchronous blitting.
Bit SDL_ANYFORMAT
Flag meaning: You want to use the pixel format of the actual display surface.
Bit SDL_HWPALETTE
Flag meaning: You want to use all 256 colors of the palette.
Bit SDL_NOFRAME
Flag meaning: If windowed, you do not want to have the standard window decoration around the display surface. In full-screen, this is the default.
Bit SDL_OPENGLBLIT
Flag meaning: You are using SDL with OpenGL but would like to render with SDL.
Similarly,
Bit Flag meaning
SDL_DOUBLEBUF You want to use a double buffer.
SDL_FULLSCREEN You want the application to be full-screen.
SDL_OPENGL You are using SDL with OpenGL.
SDL_RESIZABLE You want a resizable window.
Now, here you are going to write a SDL based program just to create a SDL window.
#include "SDL/SDL.h"
int main( int argc, char* argv[] )
{
//Initialize SDL (SDL_Init Initializes one or more SDL subsystems.
SDL_Init( SDL_INIT_EVERYTHING );
//Set up SDL window
SDL_SetVideoMode( 600, 400, 32, SDL_SWSURFACE );
//Pause for 4 secs
SDL_Delay( 4000 );
//Quit SDL
SDL_Quit();
return 0;
}
In the above program, the SDL screen stays only for a time period of 4000 ms.
To make the SDL window to be displayed unless some event occurs ( such as unless exit/quit button is pressed), a few more functions are to be understood first.
The SDL_Event Structure
The one event structure to rule them all: SDL_Event! This is the main event structure (the only one received by the various event functions), and it contains all other types of events.
typedef union{
Uint8 type;
SDL_ActiveEvent active;
SDL_KeyboardEvent key;
SDL_MouseMotionEvent motion;
SDL_MouseButtonEvent button;
SDL_JoyAxisEvent jaxis;
SDL_JoyBallEvent jball;
SDL_JoyHatEvent jhat;
SDL_JoyButtonEvent jbutton;
SDL_ResizeEvent resize;
SDL_ExposeEvent expose;
SDL_QuitEvent quit;
SDL_UserEvent user;
SDL_SywWMEvent syswm;
} SDL_Event;
As you can see, this is not actually a structure—it is a union, so each of the event structures occupies the same memory. This is why the type parameter is always included as the first member of an event structure. When you read in an SDL_Event, you check its type member, which tells you what event it is and thus which other member you should be looking at. This is one of the best ways to handle input.
Polling
One method of gathering input is to periodically poll the event queue. If you find that no event has occurred, have the program do something else for a moment and then check for events again. This is more typical for game applications, especially those that have background processing such as animation. To check to see whether an event has occurred without waiting for one to occur, you use the SDL_PollEvent function.
int SDL_PollEvent(SDL_Event *event);
Here comes the program that shows up with SDL screen and exits only when the quit button is pressed.
#include <sdl/sdl.h>
int main(int argc, char* argv[])
{
char done;
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
//Set display mode
SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
done = 0;
SDL_Event event;
while(done!=1)
{
//check for polling under the event
while(SDL_PollEvent(&event))
{
//check if the event type is quit
if(event.type ==SDL_QUIT)
{
done=1;
}
}
}
//quit sdl
SDL_Quit();
return 0;
}
The source codes of this lesson are available for download at
http://www.mediafire.com/?q0it8l9yr0gnt4z
Nice Post !! Thanks :)
ReplyDelete