SDL 2.0 Tutorial Index

Deprecation Notice:

I've been updating the lessons now that SDL2 is officially released, please visit my new site on Github that has updated lessons and improved code.


 Welcome!

The goal of the following tutorials is to provide you with an introduction to SDL 2.0 and game design topics and concepts in C++. It is assumed throughout the tutorials that you have a reasonable amount of knowledge in C++ with an understanding of at a minimum arrays, vectors, control structures, functions and pointers.

If you find yourself having trouble understanding the code throughout the tutorials feel free to comment on the tutorial or grab one of the books from this excellent list on StackOverflow.

If you want to see the full source or download the assets for the tutorials you may download them from Github. But never copy!

The SDL 2.0 documentation is also available for reading on the online wiki.

Tutorial Index:

Lesson 0: Setting up SDL
Lesson 1: Hello World!
Lesson 2: Don't Put Everything in Main
Lesson 3: SDL Extension Libraries 
Lesson 4: Event Driven Programming
Lesson 5: Clipping Sprite Sheets  
Lesson 6: True Type Fonts with SDL_ttf
Lesson 7: Taking Advantage of Classes
Lesson 8: Timers

I've been tied up with classes lately but hopefully I can get some time and put up some more "soon" heh.

22 comments:

  1. Thank you so much for these tutorials! They are really useful :)

    ReplyDelete
  2. Thanks for make it easy for newbies!! :)

    ReplyDelete
  3. Can I translate this tutorial into Chinese~? :)

    ReplyDelete
  4. Hi TwinkleBear, I have a game I was working on back in 2004 that I have recently revived. It was written in SDL 1.2 For about a month now I have catching up and coding in SDL 1.2.15 along with SDL_mixer/image/ttf/net.

    When it came time to port to OSX (I recently upgraded to Mountain Lion), I got it to run perfectly in Windowed mode. But all calls to set the screen to fullscreen would fail (SDL_SetVideoMode with fullscreen flag). I tried many things to fix this issue and much googling didn't really help me find the solution.. Except for..

    SDL2. I tried installing SDL2 into my Mountain Lion Mac and fullscreen is working :) However, this would be an incredible upgrade dontchu think? To modify my whole game .. It's definitely feasable. My game is on 0.01 state haha but it does have an engine and lots of code.. I dunno maybe about 50 source files??? Overestimated?? We'll see..

    Anyways, I cannot find much information, especially REVIEWs on SDL2, what the status of it is. I would really like to know if the switch from 1.2 to 2 is worth it. and if SDL2 or SDL in general has a bright future. I don't want to dedicate my precious time to a game coded in a library that will suck in a year or 2.

    :) Thanks in advance
    Michael

    ReplyDelete
    Replies
    1. SDL2 is nearing release from what I hear, or at least is in a state where everything is usable and works. As far as the difficulty of migrating it depends on how modular your code is. I think the wiki has some information on migrating code from 1.2.

      Compared to SDL1.2 SDL2 brings some pretty serious improvements like OpenGL based rendering instead of the old software renderer making it much faster, along with gamepad support and some other nice goodies.

      As far as future-proofing, everything goes out of date eventually, but I think SDL2 will remain useful for a while. If you want more power/control/graphics features you can always drop down to raw OpenGL calls.

      Delete
  5. Thank you for your tutorial. I've been translating this tutorial into Chinese. The translation is here: http://adolfans.github.com/sdltutorialcn/
    BTW, SDL_mixer can be compiled with Visual Studio now if you undefine MP3_MUSIC in preprocessor settings. The problem is that the mp3 playing module of SDL_mixer depends on smpeg, but smpeg depends on SDL1.2. I've tried to make these libraries work together but SDL_mixer still cannot play .mp3 files. I think it is SDL_mixer for SDL2.0 doesn't support .mp3 playback.

    ReplyDelete
    Replies
    1. The translation looks great! Thanks for the tip on SDL_mixer too, I wonder if it'd be possible to compile/setup smpeg with SDL2.

      Delete
  6. Great tutorial.

    I have this simple code here that create an opengl context and every gl code that I write gives me an error. Here's the code: http://pastebin.com/kQnjD8Dr
    Here's the error: error LNK2019: unresolved external symbol __imp__glGetString@4 referenced in function _SDL_main

    I have visual express 2012 and opengl works with glut and freeglut, but it doesn't with SDL2. These errors show up for every gl function I add, so this means that for some reason it doesn't load opengl.

    Do you have a clue why this is happening ? Also, can we get some opengl SDL 2 tutorials?

    Thank you

    ReplyDelete
    Replies
    1. Did you add OpenGL32.lib to your linker dependencies? For OpenGL stuff with SDL2 I actually use GLEW to load my OpenGL functions since with SDL it appears you need to do it yourself (ie. set up the function pointers with SDL_GetProcAddress) for a lot of more modern functions and it's a huge pain. GLEW takes care of all that really simply instead. So I end up using SDL for opening a window and OpenGL context and glew for getting OpenGL functions, and linking against glew32.lib, OpenGL32.lib and SDL.lib (+ SDLmain.lib on Windows) and include glew.h instead of SDL_opengl.h.

      I've found this is a lot easier to get setup. If you want to take a look at how the project is setup you can peek at my OpenGL wrapper project: https://github.com/Twinklebear/OpenGL-Wrapper which you really REALLY should not take as good practice at all. I'm still learning OpenGL myself, so this is pretty awful, but it will show how to get an OpenGL context with extensions up and running with SDL. This function: https://github.com/Twinklebear/OpenGL-Wrapper/blob/master/src/window.cpp#L11-50 shows creation of an OpenGL context with SDL2.

      As far as SDL2 OpenGL tutorials I guess I could show how to get an OpenGL context setup with SDL2, but beyond that it's just regular OpenGL so any good OpenGL tutorial or book will be fine. There's plenty of great OpenGL learning material out there and I don't think I could really match their quality, so I'd just recommend to learn OpenGL from them.

      If you want me to point you to some OpenGL books/tutorials lemme know and I'll comment with some links.

      Delete
    2. Honestly I just want a library that creates this window and gives me support for mouse/keyboard inputs. The rest I want to do with OpenGL and OpenAL. Freeglut was working fine, do you know any other lib that does this?

      Delete
    3. Ah then GLFW may be what you're after, it's similar to GLUT but has a more modern design and still has active developers. GLUT is pretty old/outdated these days, I'm not familiar with FreeGLUT but if it's just an open source implementation of GLUT the same can probably be said for it.

      Check out GLFW here: http://www.glfw.org/

      Delete
    4. freeGLUT is basically GLUT but with updates likes being able to compile for 64bit and fixes. Ok I will try GLFW, thanx

      Delete
  7. I find these tutorials useful, can you post something about tiling

    ReplyDelete
    Replies
    1. Lazy Foo's Tiling lesson is decent, the idea is the same for SDL2 except you'd be drawing/clipping with a SDL_Texture as covered previously. Here's the link: http://lazyfoo.net/SDL_tutorials/lesson29/index.php

      Delete
  8. Could you do a tut for multithreading? I think it's necessary for haptic feedback effects and loading screens.

    ReplyDelete
    Replies
    1. Multithreading is a really complex topic that I don't think I could cover very well. I've found the book C++ Concurrency in Action, which covers the C++11 multithreading features, to be very helpful though. You can check it out here: http://www.manning.com/williams/

      SDL does also have cross-platform threading features but if C++11 is available then the new threading library there is really powerful, and if not the concepts are the same.

      Delete
  9. thank you for all ! I was searching for a tutorial about SDL 2 but i was hard to find it.

    ReplyDelete
  10. Thank you!
    This is amazing: SDL2 is really popular framework, but information about it is so hard to find... Thank you for doing this tutorials!

    ReplyDelete
  11. Thank you very much! These are great tutorials.

    ReplyDelete
  12. These are awesome tutorials, they have helped me a lot *-----*

    ReplyDelete