SDL_WaitEvent — Waits indefinitely for the next available event.
-- Waits indefinitely for the next available event.
SDL_PushEvent — Pushes an event onto the event queue
-- Pushes an event onto the event queue
SDL_SetEventFilter — Sets up a filter to process all events before they are posted
+> -- Sets up a filter to process all events before they are posted
to the event queue.
SDLdoc (The SDL Documentation Project) was formed to completely rewrite the SDL documentation and to keep it continually up to date. The team consists completely of volunteers ranging from people working with SDL in their spare time to people who use SDL in their everyday working lives.
For the moment these examples are taken directly from the old SDL documentation. By the 1.2 release these examples should hopefully deal with most common SDL programming problems.
The first step in using a joystick in a SDL program is to initialize the Joystick subsystems of SDL. This done by passing the QueryingQuerying
If we have reached this point then we can safely assume that the SDL library has been initialized and that the Joystick subsystem is active. We can now call some video and/or sound functions to get things going before we need the joystick. Eventually we have to make sure that there is actually a joystick to work with. It's wise to always check even if you know a joystick will be present on the system because it can also help detect when the joystick is unplugged. The function used to check for joysticks is Opening a Joystick and Receiving Joystick EventsOpening a Joystick and Receiving Joystick Events
SDL's event driven architecture makes working with joysticks a snap. Joysticks can trigger 4 different types of events:
That takes care of the controls that you can count on being on every joystick under the sun, but there are a few extra things that SDL can support. Joyballs are next on our list, they are alot like axis with a few minor differences. Joyballs store relative changes unlike the the absolute postion stored in a axis event. Also one trackball event contains both the change in x and they change in y. Our case for it is as follows:
case SDL_JOYHATMOTION: /* Handle Hat Motion */
- if ( event.jhat.hat & SDL_HAT_UP )
+ if ( event.jhat.value & SDL_HAT_UP )
{
/* Do up stuff here */
}
- if ( event.jhat.hat & SDL_HAT_LEFT )
+ if ( event.jhat.value & SDL_HAT_LEFT )
{
/* Do left stuff here */
}
- if ( event.jhat.hat & SDL_HAT_RIGHTDOWN )
+ if ( event.jhat.value & SDL_HAT_RIGHTDOWN )
{
/* Do right and down together stuff here */
}
@@ -674,6 +677,7 @@ CLASS="NAVFOOTER"
>
It should make it a lot easier to understand this tutorial is you are familiar with the data types involved in keyboard access, so I'll explain them first.
The SDL library is designed to make it easy to write games that run on Linux, *BSD, MacOS, Win32 and BeOS using the various native high-performance media interfaces, (for video, audio, etc) and presenting a single source-code level API to your application. SDL is a fairly low level API, but using it, completely portable applications can be written with a great deal of flexibility.
Video is probably the most common thing that SDL is used for, and
so it has the most complete subsystem. Here are a few
@@ -109,8 +112,8 @@ CLASS="SECT2"
CLASS="SECT2"
>Initializing the Video DisplayInitializing the Video Display
This is what almost all SDL programs have to do in one way or
another.
If you have a preference for a certain pixel depth but will accept any
other, use SDL_SetVideoMode with SDL_ANYFORMAT as below. You can also
@@ -190,8 +193,8 @@ CLASS="SECT2"
CLASS="SECT2"
>Loading and Displaying a BMP FileLoading and Displaying a BMP File
The following function loads and displays a BMP file given as
argument, once SDL is initialised and a video mode has been set.
The following two functions can be used to get and set single
pixels of a surface. They are carefully written to work with any depth
@@ -398,6 +401,7 @@ CLASS="NAVFOOTER"
>
SDL_Surface *image;
- SDL_Rect dest;
- int ncolors, i;
- SDL_Color *colors;
-
- /* Load the BMP file into a surface */
- image = SDL_LoadBMP("sample.bmp");
- if ( image == NULL ) {
- fprintf(stderr, "Couldn't load sample.bmp: %s\n",
- SDL_GetError());
- return;
- }
-
- /* Set the display colors -- SDL_SetColors() only does something on
- palettized displays, but it doesn't hurt anything on HiColor or
- TrueColor displays.
- If the display colors have already been set, this step can be
- skipped, and the library will automatically map the image to
- the current display colors.
- */
- if ( image->format->palette ) {
- ncolors = image->format->palette->ncolors;
- colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
- memcpy(colors, image->format->palette->colors, ncolors);
- }
- else {
- int r, g, b;
-
- /* Allocate 256 color palette */
- ncolors = 256;
- colors = (SDL_Color *)malloc(ncolors*sizeof(SDL_Color));
-
- /* Set a 3,3,2 color cube */
- for ( r=0; r<8; ++r ) {
- for ( g=0; g<8; ++g ) {
- for ( b=0; b<4; ++b ) {
- i = ((r<<5)|(g<<2)|b);
- colors[i].r = r<<5;
- colors[i].g = g<<5;
- colors[i].b = b<<6;
- }
- }
- }
- /* Note: A better way of allocating the palette might be
- to calculate the frequency of colors in the image
- and create a palette based on that information.
- */
- }
- /* Set colormap, try for all the colors, but don't worry about it */
- SDL_SetColors(screen, colors, 0, ncolors);
- free(colors);
-
- /* Blit onto the screen surface */
- dest.x = 0;
- dest.y = 0;
- dest.w = image->w;
- dest.h = image->h;
- SDL_BlitSurface(image, NULL, screen, &dest);
-
- SDL_UpdateRects(screen, 1, &dest);
-
- /* Free the allocated BMP surface */
- SDL_FreeSurface(image);
- return;