Cross-platform game engine development with SDL 2.0

Leszek Godlewski
Leszek GodlewskiEngine Programmer at Flying Wild Hog Cracow
inequation.orginequation.org
WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013
Cross-platform game engine
development with SDL 2.0
Leszek Godlewski
Freelance Programmer
lg@inequation.org
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 Layer
The 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.org
bugzilla.libsdl.org
inequation.orginequation.org
Questions?
lg@inequation.org
WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013
inequation.orginequation.org
Thank you!
inequation.org
lg@inequation.org
@TheIneQuation
WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013
1 of 28

Recommended

6LoWPAN: An Open IoT Networking Protocol by
6LoWPAN: An Open IoT Networking Protocol6LoWPAN: An Open IoT Networking Protocol
6LoWPAN: An Open IoT Networking ProtocolSamsung Open Source Group
14.2K views37 slides
Problem Solving by
Problem Solving Problem Solving
Problem Solving Alfiramita Hertanti
593 views7 slides
CCNA Router Startup and Configuration by
CCNA Router Startup and ConfigurationCCNA Router Startup and Configuration
CCNA Router Startup and ConfigurationDsunte Wilson
11.6K views46 slides
How to configure static nat on cisco routers by
How to configure static nat on cisco routersHow to configure static nat on cisco routers
How to configure static nat on cisco routersIT Tech
30.1K views4 slides
Mikrotik Hotspot With Queue Tree BW Management by
Mikrotik Hotspot With Queue Tree BW ManagementMikrotik Hotspot With Queue Tree BW Management
Mikrotik Hotspot With Queue Tree BW Managementgopartheredbuff
12.7K views32 slides
Biznet guideline configuration mikrotik router by
Biznet guideline configuration mikrotik routerBiznet guideline configuration mikrotik router
Biznet guideline configuration mikrotik routerarimayawulantara
670 views11 slides

More Related Content

What's hot

Cisco Commands by
Cisco CommandsCisco Commands
Cisco CommandsFredrick Hall
1.6K views8 slides
Install mikrotik di virtualbox by
Install mikrotik di virtualboxInstall mikrotik di virtualbox
Install mikrotik di virtualboxMusanif Efendi
1.5K views7 slides
BGP on mikrotik by
BGP on mikrotikBGP on mikrotik
BGP on mikrotikAchmad Mardiansyah
6.7K views35 slides
Openwrt wireless by
Openwrt wirelessOpenwrt wireless
Openwrt wireless晓东 杜
1.1K views53 slides
Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ... by
Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ...Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ...
Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ...I Putu Hariyadi
371 views17 slides
Juniper mpls best practice part 1 by
Juniper mpls best practice   part 1Juniper mpls best practice   part 1
Juniper mpls best practice part 1Febrian ‎
10.1K views22 slides

What's hot(20)

Install mikrotik di virtualbox by Musanif Efendi
Install mikrotik di virtualboxInstall mikrotik di virtualbox
Install mikrotik di virtualbox
Musanif Efendi1.5K views
Openwrt wireless by 晓东 杜
Openwrt wirelessOpenwrt wireless
Openwrt wireless
晓东 杜1.1K views
Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ... by I Putu Hariyadi
Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ...Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ...
Pembahasan Solusi Soal UTS Semester Genap 2023 - Praktikum Jaringan Komputer ...
I Putu Hariyadi371 views
Juniper mpls best practice part 1 by Febrian ‎
Juniper mpls best practice   part 1Juniper mpls best practice   part 1
Juniper mpls best practice part 1
Febrian ‎10.1K views
CCNA Security Lab 9 - Enabling SSH and HTTPS access to Cisco IOS Routers - CLI by Hoàng Hải Nguyễn
CCNA Security Lab 9 - Enabling SSH and HTTPS access to Cisco IOS Routers - CLICCNA Security Lab 9 - Enabling SSH and HTTPS access to Cisco IOS Routers - CLI
CCNA Security Lab 9 - Enabling SSH and HTTPS access to Cisco IOS Routers - CLI
Project calico - introduction by Hazzim Anaya
Project calico - introductionProject calico - introduction
Project calico - introduction
Hazzim Anaya526 views
Module 10: CDB Subscribers by Tail-f Systems
Module 10: CDB SubscribersModule 10: CDB Subscribers
Module 10: CDB Subscribers
Tail-f Systems4.9K views
Home Automation Benchmarking Report by Synack
Home Automation Benchmarking ReportHome Automation Benchmarking Report
Home Automation Benchmarking Report
Synack5.6K views
Rider S Manual K 1200 S 2008 by edum
Rider S Manual K 1200 S 2008Rider S Manual K 1200 S 2008
Rider S Manual K 1200 S 2008
edum3.8K views
How to Configure NetFlow v5 & v9 on Cisco Routers by SolarWinds
How to Configure NetFlow v5 & v9 on Cisco RoutersHow to Configure NetFlow v5 & v9 on Cisco Routers
How to Configure NetFlow v5 & v9 on Cisco Routers
SolarWinds19.5K views
Ifupdown2: Network Interface Manager by Cumulus Networks
Ifupdown2: Network Interface ManagerIfupdown2: Network Interface Manager
Ifupdown2: Network Interface Manager
Cumulus Networks2.5K views
Understanding Non Blocking I/O with Python by Vaidik Kapoor
Understanding Non Blocking I/O with PythonUnderstanding Non Blocking I/O with Python
Understanding Non Blocking I/O with Python
Vaidik Kapoor2.3K views
Troubleshooting BGP Juniper Examples by Salachudin Emir
Troubleshooting BGP Juniper ExamplesTroubleshooting BGP Juniper Examples
Troubleshooting BGP Juniper Examples
Salachudin Emir6.9K views

Viewers also liked

Jogos em Perl by
Jogos em PerlJogos em Perl
Jogos em Perlgarux
1.2K views29 slides
Gamedev-grade debugging by
Gamedev-grade debuggingGamedev-grade debugging
Gamedev-grade debuggingLeszek Godlewski
1.1K views32 slides
El barrroco by
El barrrocoEl barrroco
El barrrocoespejodeoesed
132 views12 slides
Suir img by
Suir imgSuir img
Suir imgRocío Citroni
305 views4 slides
One Year of Porting - Post-mortem of two Linux/SteamOS launches by
One Year of Porting - Post-mortem of two Linux/SteamOS launchesOne Year of Porting - Post-mortem of two Linux/SteamOS launches
One Year of Porting - Post-mortem of two Linux/SteamOS launchesLeszek Godlewski
31.8K views44 slides
El presidencialismo mexicano antes y después by
El presidencialismo mexicano antes y después El presidencialismo mexicano antes y después
El presidencialismo mexicano antes y después espejodeoesed
156 views7 slides

Viewers also liked(20)

Jogos em Perl by garux
Jogos em PerlJogos em Perl
Jogos em Perl
garux1.2K views
One Year of Porting - Post-mortem of two Linux/SteamOS launches by Leszek Godlewski
One Year of Porting - Post-mortem of two Linux/SteamOS launchesOne Year of Porting - Post-mortem of two Linux/SteamOS launches
One Year of Porting - Post-mortem of two Linux/SteamOS launches
Leszek Godlewski31.8K views
El presidencialismo mexicano antes y después by espejodeoesed
El presidencialismo mexicano antes y después El presidencialismo mexicano antes y después
El presidencialismo mexicano antes y después
espejodeoesed156 views
Linux as a gaming platform - Errata by Leszek Godlewski
Linux as a gaming platform - ErrataLinux as a gaming platform - Errata
Linux as a gaming platform - Errata
Leszek Godlewski16.5K views
CriminalEFS-PowerPoint by Jenn Amabile
CriminalEFS-PowerPointCriminalEFS-PowerPoint
CriminalEFS-PowerPoint
Jenn Amabile221 views
Social Media For Busy Entrepreneurs and Small Businesses by Fikriyyah George
Social Media For Busy Entrepreneurs and Small Businesses Social Media For Busy Entrepreneurs and Small Businesses
Social Media For Busy Entrepreneurs and Small Businesses
Fikriyyah George304 views
Linux as a gaming platform, ideology aside by Leszek Godlewski
Linux as a gaming platform, ideology asideLinux as a gaming platform, ideology aside
Linux as a gaming platform, ideology aside
Leszek Godlewski24.4K views
Хипстеры в энтерпрайзе by Aleksandr Tarasov
Хипстеры в энтерпрайзеХипстеры в энтерпрайзе
Хипстеры в энтерпрайзе
Aleksandr Tarasov1.8K views
каталог керасис by Nastasik
каталог керасискаталог керасис
каталог керасис
Nastasik3.4K views
Advanced Linux Game Programming by Leszek Godlewski
Advanced Linux Game ProgrammingAdvanced Linux Game Programming
Advanced Linux Game Programming
Leszek Godlewski74.9K views
Service Discovery. Spring Cloud Internals by Aleksandr Tarasov
Service Discovery. Spring Cloud InternalsService Discovery. Spring Cloud Internals
Service Discovery. Spring Cloud Internals
Aleksandr Tarasov1.7K views

Similar to Cross-platform game engine development with SDL 2.0

Don't Give Credit: Hacking Arcade Machines by
Don't Give Credit: Hacking Arcade MachinesDon't Give Credit: Hacking Arcade Machines
Don't Give Credit: Hacking Arcade MachinesMichael Scovetta
7.9K views60 slides
Porting the Source Engine to Linux: Valve's Lessons Learned by
Porting the Source Engine to Linux: Valve's Lessons LearnedPorting the Source Engine to Linux: Valve's Lessons Learned
Porting the Source Engine to Linux: Valve's Lessons Learnedbasisspace
3K views90 slides
OpenGL Introduction. by
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.Girish Ghate
7K views156 slides
Programming with OpenGL by
Programming with OpenGLProgramming with OpenGL
Programming with OpenGLSyed Zaid Irshad
832 views22 slides
Embedded Graphics Drivers in Mesa (ELCE 2019) by
Embedded Graphics Drivers in Mesa (ELCE 2019)Embedded Graphics Drivers in Mesa (ELCE 2019)
Embedded Graphics Drivers in Mesa (ELCE 2019)Igalia
514 views36 slides
Developing games and graphic visualizations in Pascal by
Developing games and graphic visualizations in PascalDeveloping games and graphic visualizations in Pascal
Developing games and graphic visualizations in Pascalmichaliskambi
7 views60 slides

Similar to Cross-platform game engine development with SDL 2.0(20)

Don't Give Credit: Hacking Arcade Machines by Michael Scovetta
Don't Give Credit: Hacking Arcade MachinesDon't Give Credit: Hacking Arcade Machines
Don't Give Credit: Hacking Arcade Machines
Michael Scovetta7.9K views
Porting the Source Engine to Linux: Valve's Lessons Learned by basisspace
Porting the Source Engine to Linux: Valve's Lessons LearnedPorting the Source Engine to Linux: Valve's Lessons Learned
Porting the Source Engine to Linux: Valve's Lessons Learned
basisspace3K views
Embedded Graphics Drivers in Mesa (ELCE 2019) by Igalia
Embedded Graphics Drivers in Mesa (ELCE 2019)Embedded Graphics Drivers in Mesa (ELCE 2019)
Embedded Graphics Drivers in Mesa (ELCE 2019)
Igalia514 views
Developing games and graphic visualizations in Pascal by michaliskambi
Developing games and graphic visualizations in PascalDeveloping games and graphic visualizations in Pascal
Developing games and graphic visualizations in Pascal
michaliskambi7 views
SDL2 Game Development VT Code Camp 2013 by Eric Basile
SDL2 Game Development VT Code Camp 2013SDL2 Game Development VT Code Camp 2013
SDL2 Game Development VT Code Camp 2013
Eric Basile6K views
LMG Lightning Talks - SFO17-205 by Linaro
LMG Lightning Talks - SFO17-205LMG Lightning Talks - SFO17-205
LMG Lightning Talks - SFO17-205
Linaro303 views
Introduction to Internet of Things Hardware by Daniel Eichhorn
Introduction to Internet of Things HardwareIntroduction to Internet of Things Hardware
Introduction to Internet of Things Hardware
Daniel Eichhorn5.4K views
Woden 2: Developing a modern 3D graphics engine in Smalltalk by ESUG
Woden 2: Developing a modern 3D graphics engine in SmalltalkWoden 2: Developing a modern 3D graphics engine in Smalltalk
Woden 2: Developing a modern 3D graphics engine in Smalltalk
ESUG1.8K views
Smartphone++ by mharkus
Smartphone++Smartphone++
Smartphone++
mharkus3.3K views
13th kandroid OpenGL and EGL by Jungsoo Nam
13th kandroid OpenGL and EGL13th kandroid OpenGL and EGL
13th kandroid OpenGL and EGL
Jungsoo Nam3.4K views
Raspberry Pi - HW/SW Application Development by Corley S.r.l.
Raspberry Pi - HW/SW Application DevelopmentRaspberry Pi - HW/SW Application Development
Raspberry Pi - HW/SW Application Development
Corley S.r.l.4.2K views
Yocto and IoT - a retrospective by Open-RnD
Yocto and IoT - a retrospectiveYocto and IoT - a retrospective
Yocto and IoT - a retrospective
Open-RnD1.7K views
Is Android the New Embedded Linux? at AnDevCon VI by Opersys inc.
Is Android the New Embedded Linux? at AnDevCon VIIs Android the New Embedded Linux? at AnDevCon VI
Is Android the New Embedded Linux? at AnDevCon VI
Opersys inc.803 views
mloc.js 2014 - JavaScript and the browser as a platform for game development by David Galeano
mloc.js 2014 - JavaScript and the browser as a platform for game developmentmloc.js 2014 - JavaScript and the browser as a platform for game development
mloc.js 2014 - JavaScript and the browser as a platform for game development
David Galeano1.2K views
Iot with-the-best & VSCP by Ake Hedman
Iot with-the-best & VSCPIot with-the-best & VSCP
Iot with-the-best & VSCP
Ake Hedman512 views
Ake hedman why we need to unite and why vscp is a solution to a problem by WithTheBest
Ake hedman  why we need to unite and why vscp is a solution to a problemAke hedman  why we need to unite and why vscp is a solution to a problem
Ake hedman why we need to unite and why vscp is a solution to a problem
WithTheBest104 views
pcDuino Presentation at SparkFun by Jingfeng Liu
pcDuino Presentation at SparkFunpcDuino Presentation at SparkFun
pcDuino Presentation at SparkFun
Jingfeng Liu2.4K views
Is Android the New Embedded Linux? at AnDevCon V by Opersys inc.
Is Android the New Embedded Linux? at AnDevCon VIs Android the New Embedded Linux? at AnDevCon V
Is Android the New Embedded Linux? at AnDevCon V
Opersys inc.500 views

Recently uploaded

ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...Jasper Oosterveld
27 views49 slides
Democratising digital commerce in India-Report by
Democratising digital commerce in India-ReportDemocratising digital commerce in India-Report
Democratising digital commerce in India-ReportKapil Khandelwal (KK)
20 views161 slides
Evolving the Network Automation Journey from Python to Platforms by
Evolving the Network Automation Journey from Python to PlatformsEvolving the Network Automation Journey from Python to Platforms
Evolving the Network Automation Journey from Python to PlatformsNetwork Automation Forum
17 views21 slides
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院IttrainingIttraining
69 views8 slides
MVP and prioritization.pdf by
MVP and prioritization.pdfMVP and prioritization.pdf
MVP and prioritization.pdfrahuldharwal141
37 views8 slides
Data Integrity for Banking and Financial Services by
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial ServicesPrecisely
29 views26 slides

Recently uploaded(20)

ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely29 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2218 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays17 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc72 views

Cross-platform game engine development with SDL 2.0

  • 1. inequation.orginequation.org WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013 Cross-platform game engine development with SDL 2.0 Leszek Godlewski Freelance Programmer lg@inequation.org
  • 2. AgendaAgenda 2 inequation.org2 inequation.org ● Who is this guy? ● Introduction to SDL ● Event pump ● New features of SDL 2.0 ● Questions
  • 3. 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
  • 4. 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
  • 5. AgendaAgenda 5 inequation.org5 inequation.org ● Who is this guy? ● Introduction to SDL ● Event pump ● New features of SDL 2.0 ● Questions
  • 6. What is SDL?What is SDL? 6 inequation.org6 inequation.org Simple DirectMedia Layer The 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!
  • 7. 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!
  • 8. 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)
  • 9. 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; }
  • 10. 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
  • 11. Sample SDL game: AC-130Sample SDL game: AC-130 11 inequation.org11 inequation.org
  • 12. AgendaAgenda 12 inequation.org12 inequation.org ● Who is this guy? ● Introduction to SDL ● Event pump ● New features of SDL 2.0 ● Questions
  • 13. 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()
  • 14. 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!
  • 15. Sample SDL game: AC-130Sample SDL game: AC-130 15 inequation.org15 inequation.org <AC-130 main loop breakdown> github.com/inequation/ac130
  • 16. AgendaAgenda 16 inequation.org16 inequation.org ● Who is this guy? ● Introduction to SDL ● Event pump ● New features of SDL 2.0 ● Questions
  • 17. 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
  • 18. 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
  • 19. 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.)!
  • 20. 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);
  • 21. 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)
  • 22. 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!
  • 23. 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
  • 24. 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()
  • 25. 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
  • 26. 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.org bugzilla.libsdl.org
  • 27. inequation.orginequation.org Questions? lg@inequation.org WGK 2013 · September 7th, 2013WGK 2013 · September 7th, 2013