Sdl programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

SDL Programming PowerPoint PPT Presentation


  • 51 Views
  • Uploaded on
  • Presentation posted in: General

SDL Programming. Introduction. Initialization. The first thing you must do is initialize SDL i nt SDL_Init ( SDL_INIT_EVERYTHING ) This will return -1 if it fails Various flags can be OR’ed together to enable various features In this case, we enable all features. Surfaces.

Download Presentation

SDL Programming

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Sdl programming

SDL Programming

Introduction


Initialization

Initialization

  • The first thing you must do is initialize SDL

    • intSDL_Init( SDL_INIT_EVERYTHING )

      • This will return -1 if it fails

      • Various flags can be OR’ed together to enable various features

      • In this case, we enable all features


Surfaces

Surfaces

  • A surface is an area where you can draw

  • The screen is one such surface

  • You can create other surfaces to hold images or to draw on

  • You can copy one surface to another

  • Surfaces have properties

    • Height

    • Width

    • Number of bits per pixel


Creating the screen

Creating the Screen

  • After initializing SDL, you create the screen

    • SDL_Surface *screen;

    • screen = SDL_SetVideoMode( winWidth, winHeight, screenDepth, SDL_HWSURFACE | SDL_DOUBLEBUF);

  • The screen depth is usually 32

  • If the screen cannot be created, NULL will be returned

  • When a surface is no longer needed it should be freed

    • SDL_FreeSurface(surface);


Setting a window title

Setting a Window Title

  • You can set the title in the top of the window

    • SDL_WM_SetCaption( “Title", NULL );

  • The title will be displayed in the window decoration area, usually at the top of the window


Sdl coordinates

SDL Coordinates

(100, 0)

(0, 0)

(0, 100)

(100, 100)

  • The origin of a window is the top-left corner

  • Y coordinates increase as we go down


Loading images

Loading Images

  • SDL can load images in the BMP format

  • There are extensions which can load other formats

  • These extensions must be downloaded and installed into your SDL directories

  • Loaded images are stored on surfaces

  • Remember, the screen itself is a surface

  • You can then copy them from one surface to another to make them appear on the screen


Loading images1

Loading Images

  • To load an image

    • SDL_Surface *image = SDL_LoadBMP(“file.bmp”);

      • This will either load the image or return NULL if it cannot be loaded

  • The trouble is that the image might not have the same format as the screen you want to display it on

  • You can convert it to the correct format as follows

    • SDL_Surface *displayImage = SDL_DisplayFormat(image);


Loading images2

Loading Images

  • These steps are done often enough to warrant creating a function or method for loading an image

    SDL_Surface* loadImage(const char* fileName) {

    SDL_Surface *tmp = NULL, *image = NULL;

    tmp = SDL_LoadBMP(fileName);

    if(tmp) {

    image = SDL_DisplayFormat(tmp);

    SDL_FreeSurface(tmp);}

    return image;

    }


Rendering images

Rendering Images

  • When you load an image

    • It is stored on a surface

  • This does not make the image visible

    • The image must be copied to the screen to be visible

  • We move an image using bit blitting

    • Bit block image transfer

    • This is done with the function SDL_BlitSurface


Sdl blitsurface

SDL_BlitSurface

  • intSDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)

    • Src – source surface

    • Srcrect – rectangle delimiting areas to copy or NULL for entire image

    • Dst – the destination surface

    • Dstrect – position to place on destination surface. If NULL image will be placed at top-left corner

    • Returns 0 on success


Sdl rect

SDL_Rect

  • This is a common structure used to indicate the size and position of rectangular areas

    typedefstruct{

    Sint16 x, y;

    Uint16 w, h;

    } SDL_Rect;


Event handling

Event Handling

  • Many events happen while a program is running

    • Key presses

    • Mouse movement

    • Window resizing

    • Window closing

  • Games are interested in these events and need to be able to find out when they occur

  • This can be done using

    • SDL_PollEvent(& event)


Sdl event

SDL_Event

typedef union{

Uint8 type; SDL_ActiveEvent active; SDL_KeyboardEvent key; SDL_MouseMotionEvent motion; SDL_MouseButtonEvent button; SDL_JoyAxisEventjaxis; SDL_JoyBallEventjball; SDL_JoyHatEventjhat; SDL_JoyButtonEventjbutton; SDL_ResizeEvent resize; SDL_ExposeEvent expose; SDL_QuitEvent quit; SDL_UserEvent user; SDL_SysWMEventsyswm; } SDL_Event;

  • This is used for all event types

  • Note that it is a union

  • The type field indicates which member of the union is to be used


Sdl event types structures

SDL Event Types & Structures


Sdl keyboardevent

SDL_KeyboardEvent

typedefstruct{

Uint8 type;

Uint8 state;

SDL_keysymkeysym;

} SDL_KeyboardEvent;


Sdl keysym

SDL_keysym

typedefstruct{ Uint8 scancode; SDLKey sym; SDLMod mod; Uint16 unicode; } SDL_keysym;


Sdl key symbols

SDL Key Symbols


Sdl key symbols1

SDL Key Symbols


Sdl key symbols2

SDL Key Symbols


Sdl key symbols3

SDL Key Symbols


Sdl key symbols4

SDL Key Symbols


Sdl key symbols5

SDL Key Symbols


Event processing loop

Event Processing Loop

while(SDL_PollEvent(&event)){ switch(event.type){ case SDL_KEYDOWN: if(event.key.keysym.sym==SDLK_LEFT) move_left(); break; . . . } }

Poll for events and process each of the events


The game loop

The Game Loop

bool quit = false;

SDL_Event event;

while( quit == false ) {

if( SDL_PollEvent( &event ) ) {

if( event.type == SDL_QUIT ) {

quit = true;

}

if( SDL_Flip( screen ) == -1 ) {

//return 1;

}

}

}

This processes eventsand keeps the windowon the screen untilthe X in the top rightof the window is clicked.


Drawing text

Drawing Text

  • SDL does not support TTF fonts as distributed

  • You can download the extension from

    • http://www.libsdl.org/projects/SDL_ttf/

  • Get the file

    • SDL_ttf-devel-2.0.10-VC.zip

  • Open the zip file and

    • Copy the file in include to the include directory for your SDL

    • Copy the files in lib to the lib directory for your SDL

  • You will need to copy the new DLLs to any project that wants to use text


Preparing to use text

Preparing to Use Text

  • Set the text color

    SDL_ColortextColor;

    textColor.r = textColor.g = textColor.b = 255;

  • Initialize the TTF extension

    if( TTF_Init() == -1 ) {

    return false;

    }

  • Load the font

    if( NULL == (font = TTF_OpenFont( "lazy.ttf", 28 )) ) {

    return false;

    }


Rendering the text

Rendering the Text

  • text is rendered to a newly created surface

  • You then copy this surface onto the surface where the text should appear

    textSurface = TTF_RenderText_Solid( font,

    "A-Maze-ing", textColor );

  • This surface is then blitted onto the destination surface


Colors

Colors

  • Colors are specified as

    • RGB with each value from 0 – 255

  • There are two different structures

    • An unsigned 32 bit int

    • An SDL_Color structure

  • SDL_Color has members

    • r, g, b

  • To create the 32 bit int

    unsigned int color = SD_MapRGB(screen->format,

    255, 255, 255);


Drawing lines rectangles

Drawing Lines & Rectangles

intSDL_DrawLine(SDL_Surface* dst,

int x1, int y1,

int x2, int y2,

Uint32 color)

intSDL_FillRect(SDL_Surface *dst,

SDL_Rect *dstrect,

Uint32 color)

  • Both functions return 0 on success


  • Login