SDL2 Tips, Tricks, and Workarounds
November 5th, 2013It only took me about 3 days to get my framework working with all my targets: Windows, Mac, Linux, iOS and Android. I took an extra day to write my own simple mixer for WAV and OGG audio. And there might have been another day in there for other cleanup / fixes. So maybe 1 week total.
You can get SDL2 here. If you’ve used SDL before, you’ll find that it’s pretty similar, only it seems to work better, and now it’s got slick iOS and Android support.
You will want to follow the SDL 1.2 to 2.0 Migration Guide. And then for Android and iOS, read the README-android.txt and README-ios.txt as included in the source zip. I’m just writing up some tips here to help you along the way. I’m writing this with OpenGLES 1.1 in mind.
SDL2 for Windows, Mac, Linux
– Initializing your Window must be done in the right order. Basically, I call SDL_CreateWindow first, then SDL_GL_CreateContext. If SDL_CreateWindow fails with my preferred settings, I chose more fail-safe settings. After setting the mode I use SDL_GetWindowSize and SDL_GetWindowFlags to see if I got what I wanted. If things aren’t quite right, I use SDL_SetWindowFullscreen and SDL_SetWindowSize to try and request them again. (I always go out of fullscreen, set size, (maybe go into fullscreen), then set size again.
– As of SDL 2.0.1, Mac Retina displays do not work consistently. There is a flag for this “SDL_WINDOW_ALLOW_HIGHDPI” but I’ve found that if you ever change mode, or do anything, things seem to fall apart. If you aren’t messing around much, it might just work well enough for you.
– glu doesn’t seem to work anymore. So I had to switch over to using glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP,GL_TRUE); to generate my mipmaps. I examine the glGetString(GL_VERSION)) to make sure it’s >= 1.4 before doing that. And if I’m not on a new enough GL, I just don’t use mipmaps at all.
– Use stb_image to load your images. (I didn’t do that just for SDL2, but it’s a great tip!)
SDL2 for Android
I can’t believe how smoothly this went. (Recall, 2 months for NDK, 1 week for Marmalade .. this port took 1 DAY!!!)
– Follow README.android for the details.
– in AndroidManfiest.xml in the “activity” tag add android:configChanges=”orientation” , keeps your app from crashing. You may use other orientation tags to keep it in a single orientation, or whatever.
– If you use sockets, be sure to add these permissions “android.permission.INTERNET”
– Use SDL_rwops to read your own data files (which you have placed in the “assets” folder of your Android project.)
– If you want to use SDL2_mixer, you may need to edit SDL2_mixer/Android.mk and disable a few things.
– If you are using single touch, your mouse SDL code might work already, otherwise add support for SDL_FINGER* events (and filter out the touch events from your mouse code if (e.motion.which == SDL_TOUCH_MOUSEID) { break; }
– Be sure to call SDL_SetTextInputRect before SDL_StartTextInput if you are using key input. SDL_SetTextInputRect let’s you specify where on the screen the text is appearing so that SDL2 can shift the screen to keep the virtual keyboard from overlapping it.
– On suspend / resume I had to pause my audio.
SDL2 for iOS
– Follow README.ios for the details.
– SDL_SetTextInputRect doesn’t work. So you’ll need to capture UIKeyboardWillShowNotification and shift your screen to keep the virtual keyboard from overlapping it on your own.
– Unlike in Android, the orientation won’t change between portrait and landscape UNLESS you add SDL_WINDOW_RESIZABLE to your SDL_CreateWindow flags.
– Even though I added in SDL_iPhoneSetAnimationCallback, I got crashes when suspending my app. I also had to use SDL_SetEventFilter to capture SDL_APP_WILLENTERBACKGROUND and SDL_APP_DIDENTERFOREGROUND to set a flag to tell my callback to start / stop doing its thing.
Custom Mixer?
You might be able to use SDL2_mixer on all platforms. I did try it out for Android, and got it working pretty easily. However, I decided to write up my own mixer using SDL2 to stream the output. (This ended up only taking a few hours, and it keeps me from having to have SDL2_mixer as an additional dependency on all platforms.)
I used SDL_LoadWAV to load wav files and stb_vorbis to load and stream ogg files.
Good luck!
-Phil