SDL2 Game Development VT Code Camp 2013

  • 3,563 views
Uploaded on

VT Code Camp 2013 SDL2 Presentation

VT Code Camp 2013 SDL2 Presentation

More in: Sports , Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,563
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
26
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Game Development With SDL2
  • 2. Who Am I ? ● Eric Basile – Engineer at Hitpoint Studios. ● Worked On Win8 & iOS titles – Graduated From Champlain College in Burlington VT ● Game Programming BS – SDL has been my side project for the last year. – Website ● Portfolio : EricBasile.com ● Blog : FaceKeyboard.com – Contact ● Email : eric@ericbasile.com ● Twitter : @Samurai336
  • 3. What Is SDL ? ● SDL stands for Simple Directmedia Layer – Multimedia library that provides access to : ● Graphics, Sound, and Input devices. – Cross Platform ● Set up to be a layer for interfacing with platform & operating-system-specific functions. – Written in C ● Supported Platforms include : – Windows, Linux, OSX, Android, iOS – Open Source ● SDL2 uses zlib license
  • 4. History of SDL ● Created by Sam Lantinga at Loki Games 1998 – Used to port Doom to BeOS – SMPEG and OpenAL developed to work along side. ● SDL 1.3 Announced in 2008 (This would become 2.0) – Would use zlib license ● 1.2 and older use GNU LGPL ● SDL 2.0 officially announced July 14 2012 – Sam also announces he is joining valve software
  • 5. Whats new in 2 ● SDL v2.0.0 stable Released in August 11 2013 – Not backwards compatible with SDL 1.2 ● Most repos only have 1.2 right now – SDL2 Hardware accelerated ● 2D rendering using Direct3D, OpenGL or OpenGL ES or software rendering in background. – Official Android and iOS support – Support for multiple windows – Much Much more. ● Over view at: http://wiki.libsdl.org/MigrationGuide
  • 6. Who's Using It ? And Tons of Indie developers !!
  • 7. Whats using it AND SO MUCH MORE !!
  • 8. Talk About Build Systems ● Cmake great for cross platform build systems – Allows people to more or less use development environment of choice. – Cross Platform (Duh!) – Fairly straightforward scripting.
  • 9. Basic cmake syntax file(GLOB game_files src/*.c src/*.cpp src/*.h src/*.hpp) SOURCE_GROUP(Game FILES ${game_files}) add_executable(${App_Name} ${game_files} ) find_package(SDL2) include_directories( ${SDL2_INCLUDE_DIR} ${INCLUDE_DIRECTORIES} ) target_link_libraries(${App_Name} ${SDL2_LIBRARY}) Complete cmake available at : https://github.com/Samurai336/SDL2_PONG/blob/master/CMakeLists.txt set (App_Name "SDL2_PONG")
  • 10. Using Cmake ● Create Directory for your Project Build – Avoid creating folder in Source Directory ● Run cmake – Run cmake with path to directory containing CmakeLists.txt – $ cmake Path/to/folder/With/Cmake/List/
  • 11. Cmake gui
  • 12. Basic SDL Application Initialize SDL Handle Events Draw Clean up Handle logic
  • 13. Initializing SDL ● SDL_Init(uint32 flags) ; – We can pick and choose what parts of SDL we want to use (IE just Audio or just input). ● Available flags – SDL_INIT_TIMER, SDL_INIT_AUDIO, SDL_INIT_VIDEO, SDL_INIT_JOYSTICK, SDL_INIT_HAPTIC, SDL_INIT_GAMECONTROLLER, SDL_INIT_EVENTS, SDL_INIT_EVERYTHING. ● Use or for multiple flags (SDL_INIT_TIMER|SDL_INIT_AUDIO) ● We will use SDL_INIT_EVERYTHING. – Check things Started ok. if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("failed to initin"); }
  • 14. Create a window ● SDL_Window * MainWindow ; – Pointer to what will be our main window ● SDL_Window* SDL_CreateWindow(const char* title, int x,int y,int w,int h,Uint32 flags) – Will give us a pointer to a created window at with specified width, height, positon and title. – Flags for windows include: SDL_WINDOW_FULLSCREEN, SDL_WINDOW_BORDERLESS, SDL_WINDOW_RESIZABLE and more. MainWindow = SDL_CreateWindow("SDL2_PONG", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE );
  • 15. Create Render Context ● SDL_Renderer* Renderer ; – Pointer to our Renderer ● SDL_Renderer* SDL_CreateRenderer(SDL_Window* window, int index, Uint32 flags); – Creates a 2D rendering context for a windows ● Pointer to the window ● the index of the rendering driver to initialize (-1 to initialize the first one supporting the requested flags) ● Enumerated flags to specify rendering context. – Example : SDL_RENDERER_ACCELERATED for hardware accelerated SDL_CreateRenderer(MainWindow , -1, SDL_RENDERER_ACCELERATED );
  • 16. Extensions ● SDL Has Several Extensions Libraries – Used to Abstract lower level but common SDL functionality (Ex : Loading Compressed Image formats) – Extension Libraries Available: SDL_image, SDL_mixer,SDL_net, SDL_rtf, SDL_ttf. – We will be Covering: SDL_image & SDL_mixer.
  • 17. Starting Extensions ● Initializing Extensions – Image ● IMG_Init(int flags) – What format libs to load for Decoding Ex : IMG_INIT_JPG for jpg ● We will be using png – Mixer ● Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize); – We will use 44100 for frequency – MIX_DEFAULT_FORMAT for our format – 2 channels – Chunk size of 4096 ● More or less whats Default in the Documentation :P Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) IMG_Init(IMG_INIT_PNG);
  • 18. SDL Event Handling
  • 19. SDL_Event ● SDL_Event Handling – Popular for abstracting input and other system messages across OS's and platforms ● Can Be used independently – SDL_Init(SDL_INIT_EVENTS); ● Used for : – SDL_KeyboardEvent SDL_MouseButtonEvent SDL_MouseMotionEvent SDL_WindowEvent SDL_TouchFingerEvent and more...
  • 20. Getting Events ● Use SDL_PollEvent(SDL_Event* event) to get currently pending event. – Works nicely with while loops in the main loop. while (SDL_PollEvent(&event)) { // handle your event here }
  • 21. Check Event Type ● Get event type – Event->type ● Will tell you what this event is. – Ex: SDL_CommonEvent,SDL_QuitEvent, SDL_MouseButtonEvent, SDL_TouchEvent ● Works great in a Switch Statement switch(test_event->type) { case SDL_MOUSEMOTION: printf("We got a motion event.n"); printf("Current mouse position is: (%d, %d)n", test_event.motion.x, test_event.motion.y); break; default: printf("Unhandled Event!n"); break; } }
  • 22. Handling Event ● Use data from event Based on type – Event->key.keysym.sym == SDLK_DOWN ● Handles down arrow. – Keysym A structure that contains key information and sym is an SDL virtual key code. if(sym == SDLK_DOWN) { Player2.SetY(Player2.GetY() + 25); } else if(sym == SDLK_UP) { Player2.SetY(Player2.GetY() - 25); }
  • 23. 2D Drawing
  • 24. Cartesian plane in SDL +Y +X{0,0}
  • 25. Loading an Image as a texture ● SDL_texture* – Is a structure that contains an efficient, driver-specific representation of pixel data. ● Using SDL_image – SDL_Texture* IMG_LoadTexture(SDL_Renderer *renderer, const char *file) ● Pointer to the render context where we will display our image. ● Path to our file. ● 1.2 used SDL_Surface* OurTexture = IMG_LoadTexture(Renderer, "./ImageFile.png");
  • 26. Drawing Images With SDL_Renderer ● Will copy a portion of the texture to the current rendering target SDL_RenderCopyEx(Renderer, OurTexture, NULL, &DestR, 0.0, NULL, SDL_FLIP_NONE); DestR = {128,128,128,128}
  • 27. Sprite Sheet Animated Image Drawing ● Two SDL Rects ● SDL_Rects are just struct's with int's for X, Y Width and Height. – One for where on the sprite sheet and one for where to display in the windows render context. Here's our source rect parameter would be {128,128,128,128} SDL_RenderCopyEx(Renderer, OurTexture, &SrcR, &DestR, 0.0, NULL, SDL_FLIP_NONE);
  • 28. Sprite Sheet Animated Image DrawingDestR = {128,128,128,128}
  • 29. Drawing to The Screen ● SDL_RenderPresent(SDL_Renderer* renderer) – update the screen with rendering performed. ● SDL_RenderClear(SDL_Renderer* renderer) – clear the current rendering target with the drawing color. ● These Need to be done every frame to see what we have drawn on screen. SDL_RenderPresent(Renderer); SDL_RenderClear(Renderer);
  • 30. Drawing Simple shapes ● SDL_SetRenderDrawColor(SDL_Renderer* renderer,Uint8 r,Uint8 g,Uint8 b,Uint8 a) – Set our renderer's rgb draw color. ● SDL_RenderFillRect(SDL_Renderer* renderer,const SDL_Rect* rect) – Draws a rect in the passed render context ● Also Available in SDL are : – SDL_RenderDrawLine, SDL_RenderDrawLines, SDL_RenderDrawPoint, SDL_RenderDrawPoints, SDL_RenderDrawRect, SDL_RenderDrawRects. SDL_RenderFillRect(Renderer, &DestR);
  • 31. Sound
  • 32. Loading A sound File ● Using SDL_mixer – Mix_Chunk* SoundFile; ● Pointer to Audio Data in memory – Mix_Chunk *Mix_LoadWAV(const char *fname); ● Will load fname file into memory and return a pointer to it. Mix_Chunk* SoundFile = Mix_LoadWAV("./SoundFIle.wav") ;
  • 33. Playing Sounds ● Mix_PlayChannel (int channel, Mix_Chunk *chunk, int loops); – Channel we want to play on ● -1 to let SDL choose it for you – Pointer to our sound file – How many times to loop ● 0 for once and -1 for infinite. Mix_PlayChannel(-1, SoundFile, 0);
  • 34. Clean up ● Its Important to Clean all the assets you have loaded into memory. ● Assets – images ● SDL_DestroyTexture(OurTexture); – Sounds ● Mix_FreeChunk(SoundFile) ;
  • 35. Shutting Down SDL ● Renderer – SDL_DestroyRenderer(Renderer) ; ● Window – SDL_DestroyWindow(MainWindow) ; ● Image Extension – IMG_Quit(); ● Audio – Mix_CloseAudio() ; ● SDL – SDL_Quit() ;
  • 36. SDL2 Demo using Everything Discussed. Get It NOW !! $ git clone https://github.com/Samurai336/SDL2_PONG.git
  • 37. Reasons to use SDL beyond 2D games. ● Able to pick and choose parts of SDL to use – Input handler, Audio handler.. etc ● Used for 3D games – Great for creating OpenGL OpenGLes 1.0 & 2.0 and DirectX render contexts – Now you have a way to handle cross platform input ● Works on just about anything – Open source can compile it anywhere and add support yourself – Works on Windows, Linux, Mac OS X, iOS, Android – Sorta works on PSP and Raspberry Pi
  • 38. Resources ● Official website – http://www.libsdl.org/ – Documentation → http://wiki.libsdl.org/ ● SDL 1.2 tutorials and information – http://lazyfoo.net/SDL_tutorials/index.php – http://www.sdltutorials.com/ ● History – http://en.wikipedia.org/wiki/Simple_DirectMedia_Layer ● Game and company logos from – http://store.steampowered.com/