cross-platform game engine development with sdl 2.0

28
inequation.org inequation.org WGK 2013 · September 7th, 2013 WGK 2013 · September 7th, 2013 Cross-platform game engine development with SDL 2.0 Leszek Godlewski Freelance Programmer [email protected]

Upload: leszek-godlewski

Post on 10-May-2015

6.232 views

Category:

Technology


17 download

DESCRIPTION

Lecture from the WGK 2013 game development conference The SDL library (Simple DirectMedia Layer) - known as "the open source response to DirectX" - lets you forget about all the boilerplate in game development on platforms ranging from Windows, through Linux and Mac OS X, to iOS and Android. While still in active development, version 2.0 of SDL provides new and improved functionality, including touch input and force feedback support. It also ships with the Steam Linux Library. This lecture provides an overview of the library's capabilities and some useful tricks.

TRANSCRIPT

inequation.orginequation.org

WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013

Cross-platform game engine development with SDL 2.0

Leszek GodlewskiFreelance [email protected]

AgendaAgenda

2 inequation.org2 inequation.org

● Who is this guy?

● Introduction to SDL

● Event pump

● New features of SDL 2.0

● Questions

Who is this guy?Who is this guy?

3 inequation.org3 inequation.org

Freelance Programmer (Sep 2013 – onwards)● inequation.org● Unannounced project

Generalist Programmer, The Farm 51 (Mar 2010 – Aug 2013)● thefarm51.com● Painkiller Hell & Damnation

(2012-2013; Win/Linux/X360/PS3)● Deadfall Adventures (2011-2013;

Win/X360)● A few unannounced projects

Who is this guy?Who is this guy?

4 inequation.org4 inequation.org

Cross-platform work( de facto – Linux)→

● github.com/inequation● Toy projects, personal utilities,

university stuff● Games as well!

● AC-130● Crystal Space● idTech 3-based games● Painkiller Hell & Damnation

● Linux as preferred OS for 7+ years● Former open-source evangelist

● Not anymore – lost interest

AgendaAgenda

5 inequation.org5 inequation.org

● Who is this guy?

● Introduction to SDL

● Event pump

● New features of SDL 2.0

● Questions

What is SDL?What is SDL?

6 inequation.org6 inequation.org

Simple DirectMedia LayerThe cross-platform, open-source „DirectX”

● Written by Sam Lantinga (ex-Loki, ex-Blizzard, currently Valve), Ryan Gordon (legendary porter) et al.

● Provides:● API in C with mappings to other languages● Timers and threads● Thread-safe input and events (incl. Unicode and IME)

● Polled and event-based APIs (incl. filtering)● Window management (incl. OpenGL [ES] contexts)● Audio (very barebone)● Accelerated 2D graphics (simple drawing, blitting)● More!

Benefits of SDLBenefits of SDL

7 inequation.org7 inequation.org

Many supported platforms

● Windows XP+● Linux 2.6+● Mac OS X 10.5+● Android 2.3.3+● iOS 4.0.1+● Many more: Free/Open/NetBSD, Solaris, Playstation Portable,

Haiku, Pandora...

Yup, mobiles!

Benefits of SDLBenefits of SDL

8 inequation.org8 inequation.org

All platform details hidden away under a unified interface!

(truly one codebase shared by all of them → write once, compile anywhere)

Benefits of SDLBenefits of SDL

9 inequation.org9 inequation.org

Extremely simple – goodbye, boilerplate!int main(int argc, char** argv) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) return 1; SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_Surface *bmp = SDL_LoadBMP("hello.bmp"); SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp); SDL_FreeSurface(bmp);

SDL_RenderClear(ren); SDL_RenderCopy(ren, tex, NULL, NULL); SDL_RenderPresent(ren);

SDL_Delay(2000); SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return 0;}

MonoGame-SDL2MonoGame-SDL2

10 inequation.org10 inequation.org

Hey indies!

● MonoGame: an XNA implementation on top of Mono● MG-SDL2: backend for MonoGame by Ethan Lee (Twitter: @flibitijibibo)

● Shipped and WIP games:● Capsized● FEZ● Gateways

http://github.com/flibitijibibo/MonoGame

Sample SDL game: AC-130Sample SDL game: AC-130

11 inequation.org11 inequation.org

AgendaAgenda

12 inequation.org12 inequation.org

● Who is this guy?

● Introduction to SDL

● Event pump

● New features of SDL 2.0

● Questions

Event pumpEvent pump

13 inequation.org13 inequation.org

SDL encapsulates hardware, system, window management and input events

● SDL_QUIT, SDL_APP_TERMINATING, SDL_APP_LOWMEMORY, SDL_APP_WILLENTERBACKGROUND, SDL_APP_DIDENTERBACKGROUND, SDL_APP_WILLENTERFOREGROUND, SDL_APP_DIDENTERFOREGROUND, SDL_WINDOWEVENT, SDL_SYSWMEVENT, SDL_KEYDOWN, SDL_KEYUP, SDL_TEXTEDITING, SDL_TEXTINPUT, SDL_MOUSEMOTION, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL, SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED, SDL_FINGERDOWN, SDL_FINGERUP, SDL_FINGERMOTION, SDL_DOLLARGESTURE, SDL_DOLLARRECORD, SDL_MULTIGESTURE, SDL_CLIPBOARDUPDATE, SDL_DROPFILE, SDL_USEREVENT

●SDL_PeepEvents(), SDL_PollEvents(), SDL_WaitEvent()...

● Filtering: simple, by type (SDL_EventState()) or complex with callbacks (SDL_EventFilter)

● Custom user events - SDL_RegisterEvents()

Event pumpEvent pump

14 inequation.org14 inequation.org

● Probably the single coolest feature!● One switch in a loop takes care of almost everything● Typical SDL application structure:

● Subsystem initialization● Event pump● Cleanup● That's it!

Sample SDL game: AC-130Sample SDL game: AC-130

15 inequation.org15 inequation.org

<AC-130 main loop breakdown>

github.com/inequation/ac130

AgendaAgenda

16 inequation.org16 inequation.org

● Who is this guy?

● Introduction to SDL

● Event pump

● New features of SDL 2.0

● Questions

Current state of SDL 2Current state of SDL 2

17 inequation.org17 inequation.org

● First stable release, 2.0.0, announced August 11th● Present in the Steam Linux Runtime since early RCs● Valve games and others rely on it

● Documentation is slightly outdated● Ongoing effort, gaps being quickly filled● You might be misled to believe a feature is missing...● When in doubt, read header file comments or generate your own docs

What's new in SDL 2What's new in SDL 2

18 inequation.org18 inequation.org

● OpenGL 3+ support● Incl. context sharing (finally!)

● OpenGL ES support● Haptic (force) feedback● Game controller API● Android and iOS support● Touch input with gestures● Support for multiple displays, windows, audio devices● IME and proper Unicode support● Pluggable 2D renderer backends● Portable atomic operations● Power management● Rudimentary UI (message boxes etc.)● Clipboard & drag-and-drop support

OpenGL context sharingOpenGL context sharing

19 inequation.org19 inequation.org

Finally – yay for multithreaded/multi-window rendering! ☺

● Undocumented... ☹ FIXED as of August 11th● Only mention in Google is the May 2012 commit that introduces the feature

● Needs an SDL-OpenGL attribute set before window creation:

SDL_GL_SetAttribute( SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);● Voila - all contexts created from now on with SDL_GL_CreateContext() will share data with the currently bound context (buffers, shaders etc.)!

OpenGL context sharingOpenGL context sharing

20 inequation.org20 inequation.org

Gotcha: pre-RC4 SDL2 keeps a global reference to the current window/GL context

pair

● Care must be taken when unbinding contexts (e.g. when killing worker threads)!// …SDL_GL_MakeCurrent(NULL, NULL);// most SDL_GL_* calls made on any thread// will crash now :(● Solution: simply bind back to the main window and context instead of unbinding

SDL_GL_MakeCurrent(MainWindow, MainGLCtx);

OpenGL context sharingOpenGL context sharing

21 inequation.org21 inequation.org

Gotcha: pre-RC4 SDL2 keeps a global reference to the current window/GL context

pair

RC4 and newer (incl. 2.0.0 stable) – nothing to worry about ☺

● Some distros still ship <= RC4● Ship your own or use Steam Linux Runtime

(fixed on 11 Jul 2013, rev. 50211a1fd557)

Full-screen quirksFull-screen quirks

22 inequation.org22 inequation.org

Gotcha: full-screen in Linux is hacky ☹● X11 doesn't really support „true” full-screen windows● Actually just a decoration-less window●SDL_WINDOW_FULLSCREEN uses an XRandR hack

● Change display mode while maintaining desktop size● Crop the „viewport”● Side effect: „viewport” follows mouse and may scroll away from app window... Bad! ☹

●SDL_WINDOW_FULLSCREEN_DESKTOP simply creates a desktop-sized window, no XRandR magic● Render to offscreen buffer (FBO), then blit with scaling● This is what Valve does!

Game controller API: SDL_GameController*Game controller API: SDL_GameController*

23 inequation.org23 inequation.org

Unified gamepad/joystick handling● Layout modelled after the Xbox 360 controller

● Built by Valve with Steam Big Picture in mind● Button/axis mapping support for other controllers

● Kind of a built-in equivalent to Xpadder● Bindings may be imported/exported via strings● Popular controllers have built-in mappings

● Hotplug support – yay!● May be polled for state or handled with events● API very similar to the regular joystick API, but buttons and axes are identified by enumerations instead●SDL_GameControllerAxis●SDL_GameControllerButton

Game controller API: SDL_GameController*Game controller API: SDL_GameController*

24 inequation.org24 inequation.org

Gotcha: joystick/game controller API interop● GC API is implemented on top of the SDL joystick API● SDL GC subsystem initialization implies joystick subsystem initialization

● Regular joystick events are issued for GCs as well!● This means double events for the axes, buttons, even hotplugs!

● Don't need joystick hats, arbitrary axes etc.? Just filter out joystick events altogether

● Otherwise you can identify the device type with SDL_IsGameController()

Haptic feedback: SDL_Haptic*Haptic feedback: SDL_Haptic*

25 inequation.org25 inequation.org

● Powerful API with more than just rumbles● Remember arcade racing machines that blocked the steering wheel and pedals?

● Haptic devices may be stand-alone (e.g. rumble motors in mobile devices) or derived from joysticks with SDL_HapticOpenFromJoystick()

● Simplified API for rumbles

TakeawayTakeaway

26 inequation.org26 inequation.org

● Battle-proven low-level abstraction layer● Ships with the Steam Linux Runtime● Support for many platforms, including Windows, Linux, Mac, mobiles and some handheld consoles

● Provides a universal event pump● Integrates well with OpenGL● Exposes game controllers and force feedback● Open-source!

libsdl.orgbugzilla.libsdl.org

inequation.orginequation.org

Questions?

[email protected]

WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013

inequation.orginequation.org

Thank you!

[email protected]@TheIneQuation

WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013