Download presentation
Presentation is loading. Please wait.
Published byTimothy Daniels Modified over 9 years ago
1
Lecture 02: Intro to SDL Topics: Downloading / Installing SDL Linking (in general and for SDL) SDL basics References: http://www.libsdl.org/cgi/docwiki.fcg/SDL_API http://www.sdltutorials.com/ http://www.libsdl.org/intro.en/toc.html
2
Getting SDL http://www.libsdl.org Download => SDL 1.2 (as of 6/6/2013) Under Development Libraries get either – SDL-devel-1.2.15-VC.zip (for Visual C++) – SDL-devel-1.2.15-mingw32.tar.gz (for Netbeans/Code::Blocks + MinGW) Extract the files (I'll assume you put them at c:\SDL1.2
3
// Lots of stuff... Build Steps #include #include "my_func.h" int main() { int x = f(15); cout << x << endl; } Pre-processor main.cpp iostream int f(int); my_func.h int f(int x) { return x * 3; } my_func.cpp // Lots of stuff... int f(int); int main() { int x = f(15); cout << x << endl; } int f(int); int f(int x) { return x * 3; } [not visible to user]
4
main.obj my_func.obj Build Steps Compiler // Lots of stuff... int f(int); int main() { int x = f(15); cout << x << endl; } int f(int); int f(int x) { return x * 3; } [Not visible to user] % Machine code mov ax, $0x0110735A cmp ax, #0 jne $0x73599920 %... % Machine code %... push #57 inc ax %... stdcpp_runtime.lib % Machine code %... SDL.lib % Machine code %... Linker
5
my_prog.exe Build Steps % Machine code %... Linker Build Time Run Time DirectX9.dll % Machine code %... Operating System RAM CPU HDD User
6
…as it relates to SDL When you download SDL, you get – A bunch of.h files in /include – Static Library files (.lib for VC++,.a for MinGW) – Runtime libraries (.dll) We need to 1.Statically link to the.lib files (using the IDE) 2.Make sure the.dll is available to the program at run-time Easily done by putting it in the same directory as the executable
7
Example: Minimal SDL program [Do this on the computer] – Create the project – Create a main program which creates a 640x480 window. – The window stays open until the user clicks the close button or presses escape
8
Basic SDL commands SDL_Init(int params) – Initializes one or more components of SDL Must be called before using them. – params is a bitwise combination of SDL_INIT_VIDEO SDL_INIT_AUDIO … – to initialize more than one sub-system, use a bitwise or. SDL_Quit() – Shuts down all sub-systems. – Make sure you're really quitting (e.g. at end of main)
9
Double Buffering When you create a window (SDL_SetVideoMode), you actually create two surfaces – One surface is what is showing on the monitor – The other is where all drawing is taking place [Reasons for double-buffering] To "swap" buffers (make your drawings visible) – SDL_Flip(SDL_Surface * window);
10
SDL Surfaces An image, basically – colors values for a rectangular grid of pixels color depth / encoding, memory size may be on GPU or RAM We just "own" a pointer to a surface – The memory is managed by SDL – We (indirectly) create / destroy that memory with SDL calls (not using new / delete) Created by several commands – SDL_Surface * SDL_SetVideoMode(int w, int h, int depth, int flags); Don't free this surface – SDL_Quit will do that for you. – SDL_Surface * SDL_LoadBMP(char * fname); Free this surface up with SDL_FreeSurface(SDL_Surface * surf); – SDL_Surface * SDL_CreateRGBSurface(int flags, int w, int h, int depth, int rmask, int gmask, int bmask, int amask); Free this surface up with SDL_FreeSurface(SDL_Surface * surf); –…–…
11
SDL Surfaces, cont. (0,0) +x 800 600 (799,599) (0,599) (400,300)
12
SDL Surfaces, cont. Colors in SDL – A single integer – Depending on display (window) format, we'll use some bits for Red, Green, and Blue – int SDL_MapRGB(SDL_PixelFormat * fmt, char r, char g, char b); Often, get fmt from the window->format field, where window is the SDL_Surface from SDL_SetVideoMode) Color Key – Makes one (and only one) color transparent – SDL_SetColorKey(SDL_Surface * surf, SDL_SRCCOLORKEY, int color);
13
Blitting skull burlap SDL_Surface * burlap, * window, * skull; SDL_Rect srect, drect; window = SDL_SetVideoMode(800,600,16,SDL_SWSURFACE); burlap = SDL_LoadBMP("burlap_bag.bmp"); skull = SDL_LoadBMP("pirate.bmp"); SDL_ColorKey(skull, SDL_SRCCOLORKEY, SDL_MapRGB(window->format, 100, 100, 255); srect.x = 0; srect.y = 0; srect.w = skull->w / 2; srect.h = skull->h; drect.x = 320 – srect.w / 2; drect.y = 50; drect.w = srect.w; drect.h = srect.h; SDL_BlitSurface(skull, &srect, burlap, &drect); SDL_BlitSurface(burlap, NULL, window, NULL); window 100 75 480 640 (270,50)
14
SDL Events A "message" sent by OS to our app – In response to user activity [Event Queue] SDL_Event objects To read one event from the queue – int SDL_PollEvent(SDL_Event * evt);
15
SDL Events, cont. The type field of the SDL_Event structure is always filled in. Depending on the type, additional fields may be filled in as well typeAdditional fieldsDescription SDL_QUITuser pressed quit button SDL_KEYDOWNkeyuser pressed down on key SDL_KEYUPkeyuser released a previously pressed key SDL_MOUSEBUT TONUP buttonuser released a previously pressed mouse button …
16
Time in SDL Framerate variability – Machine / Processor / GPU configuration – Amount of other code in application When doing animation, we could – Move a constant amount per update Bad, because of variable frame rates – Insert Pauses and move a constant amount per update Better, but we could be doing something productive… – Move a variable amount per update Higher frame-rate = less movement per update Lower frame-rate = more movement per update But…to the user, it always looks constant
17
Variable movement rates int SDL_GetTicks(); Returns # of milliseconds since last update General animation process start_time = SDL_GetTicks(); loop: Erase old frame buffer dt = (SDL_GetTicks() – start_time) / 1000.0; start_time = SDL_GetTicks(); Update all variables move by rate * dT Re-draw the frame buffer Flip back-buffer=>front-buffer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.