maek gaem iii: sdl ● simple directmedia layer ● input/graphics/audio using opengl ● not nearly...

13
MAEK GAEM III: SDL Simple DirectMedia Layer Input/Graphics/Audio using OpenGL Not nearly as difficult as OpenGL Cross Platform and Open Sauce Bindings for over 20 languages

Upload: delphia-potter

Post on 13-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

MAEK GAEM III: SDL

● Simple DirectMedia Layer● Input/Graphics/Audio using OpenGL● Not nearly as difficult as OpenGL● Cross Platform and Open Sauce● Bindings for over 20 languages

Page 2: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Download/Setup

● http://www.libsdl.org/download-1.2.php● How to get it all set up will depend on your

IDE. ● Once you build your game, you (usually) just

need to package it with SDL.dll

Page 3: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Get it Up

● We have to do some initialization before we can do anything serious

● SDL_Init( SDL_INIT_VIDEO ); That was easy

● You can initialize a number of subsystems with this method (video, audio, timer, etc.)

Page 4: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Screen Initialization● Nearly every video-oriented method using SDL

requires passing in a pointer to a surface object.● SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );

● But what does that mean?

● Firstly, the screen resolution's width and height● Then the bits per pixel. Cool! 0 is the current

default for the user's computer● All the extra flags go last. You can look 'em up

Page 5: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Drawing Boring Rectangles● You can fill simple rectangles with color● SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)

● First arg: Your surface from earlier● Second Arg: The Rectangle you where you want to

color the screen. NULL will fill the entire screen in ● Third Arg: Use SDL_MapRGB (or SDL_MapRGBA if

you want to do alpha blending). I'll go into detail in a sec.

Page 6: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

SDL_MapRGB

● Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);

● Last three arguments are pretty obvious. Just enter the Red Green and Blue values (from 0 to 255) to get the color you want.

● What's this PixelFormat shit?

It's an attribute of your screen object. Just pass in a screen->format and you'll be good

to go.

Page 7: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Rectangle Elaboration

● There's not too much to talk about. You need rectangles for drawing to the screen, but you can use them for whatever you want, be it collisions, keeping track of player position, whatever.

● Make one like this: SDL_Rect rectongle;● Has an attribute for x & y position, and width & height● rectongle.x = 200; rectongle.y = 30;

● rectongle.w = 20; rectongle.h = 20;

Page 8: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Fuck Colors, I Want Pictures!

● You can add textures as surface objects, but load in pictures using SDL_LoadBMP.

● LoadBMP just takes a filepath. Pretty easy, yeah?

● Ex: SDL_Surface* pic = SDL_LoadBMP(“poop.bmp”)

● There's a couple things to do now: Set transparency, and draw it to our surface

Page 9: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Sprite Sheet

● A spritesheet is a collection of pictures in a single image used in animation to simulate movement.

● When drawing to the screen, you

select a rectangular area on the

sheet you wish to draw● You should also select a color for

the background to make transparent

Page 10: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Transparency● Here's How: SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);

● As you know by now, pass in the surface pointer● Pass in SDL_SRCCOLORKEY to the second arg

(const)● That just means that the next argument is going

to be the color we want to be transparent● For the last arg, use MapRGB to pick a

transparent color. Pick something that you aren't using in the actual image.

● Magenta is a common one (RGB 255, 0, 255)

Page 11: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Shit it to the Screen● SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

● *src is a pointer to the source surface (your image) and *dst is the desination surface (screen object)

● *srcrect is the source Rectangle. This is the area inside the image that you want to draw.

● *dstrect is the destination Rectangle. This is where you want to draw it on the screen

Page 12: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Quick Notes

● If using doublebuffering, make sure to call SDL_Flip(SDL_Surface* screen) after every time you draw to the screen. This swaps the video buffers.

● Also call SDL_FreeSurface(SDL_Surface* surface) to cleanup after use. Do this for every surface you create

Page 13: MAEK GAEM III: SDL ● Simple DirectMedia Layer ● Input/Graphics/Audio using OpenGL ● Not nearly as difficult as OpenGL ● Cross Platform and Open Sauce ●

Input● SDL sends KEY_DOWN and KEY_UP events, so here's

what we're going to do:SDL_Event event;

if (SDL_PollEvent(&event)) //poll for a key event

{

if (event.type == SDL_KEYDOWN) //if key is pressed or released, handle it

{

HandleEvent(event);

}

if (event.type == SDL_KEYUP)

{

HandleEvent(event);

}

}