{"id":1365,"date":"2013-11-05T13:29:28","date_gmt":"2013-11-05T19:29:28","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/?p=1365"},"modified":"2013-11-05T13:29:28","modified_gmt":"2013-11-05T19:29:28","slug":"sdl2-tips-tricks-and-workarounds","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2013\/11\/05\/sdl2-tips-tricks-and-workarounds\/","title":{"rendered":"SDL2 Tips, Tricks, and Workarounds"},"content":{"rendered":"<p>It 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.<\/p>\n<p>You can get SDL2 <a href=\"http:\/\/libsdl.org\/\">here<\/a>.  If you&#8217;ve used SDL before, you&#8217;ll find that it&#8217;s pretty similar, only it seems to work better, and now it&#8217;s got slick iOS and Android support.<\/p>\n<p>You will want to follow the <a href=\"http:\/\/wiki.libsdl.org\/MigrationGuide\">SDL 1.2 to 2.0 Migration Guide<\/a>.  And then for Android and iOS, read the README-android.txt and README-ios.txt as included in the source zip.  I&#8217;m just writing up some tips here to help you along the way.  I&#8217;m writing this with OpenGLES 1.1 in mind.<\/p>\n<p><strong>SDL2 for Windows, Mac, Linux<\/strong><\/p>\n<p>&#8211; 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&#8217;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.<\/p>\n<p>&#8211;  As of SDL 2.0.1, Mac Retina displays do not work consistently.  There is a flag for this &#8220;SDL_WINDOW_ALLOW_HIGHDPI&#8221; but I&#8217;ve found that if you ever change mode, or do anything, things seem to fall apart.  If you aren&#8217;t messing around much, it might just work well enough for you. <\/p>\n<p>&#8211; glu doesn&#8217;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&#8217;s >= 1.4 before doing that.  And if I&#8217;m not on a new enough GL, I just don&#8217;t use mipmaps at all.<\/p>\n<p>&#8211; Use <a href=\"http:\/\/nothings.org\/stb_image.c\">stb_image<\/a> to load your images. (I didn&#8217;t do that just for SDL2, but it&#8217;s a great tip!)<\/p>\n<p><strong>SDL2 for Android<\/strong><\/p>\n<p><i>I can&#8217;t believe how smoothly this went. (Recall, 2 months for NDK, 1 week for Marmalade .. this port took 1 DAY!!!)<\/i><\/p>\n<p>&#8211; Follow README.android for the details.<\/p>\n<p>&#8211; in AndroidManfiest.xml in the &#8220;activity&#8221; tag add android:configChanges=&#8221;orientation&#8221; , keeps your app from crashing.  You may use other orientation tags to keep it in a single orientation, or whatever.<\/p>\n<p>&#8211; If you use sockets, be sure to add these permissions &#8220;android.permission.INTERNET&#8221;<\/p>\n<p>&#8211; Use <a href=\"http:\/\/wiki.libsdl.org\/SDL_RWops\">SDL_rwops<\/a> to read your own data files (which you have placed in the &#8220;assets&#8221; folder of your Android project.)<\/p>\n<p>&#8211; If you want to use SDL2_mixer, you may need to edit SDL2_mixer\/Android.mk and disable a few things.<\/p>\n<p>&#8211; 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; }<\/p>\n<p>&#8211; Be sure to call SDL_SetTextInputRect before SDL_StartTextInput if you are using key input.  SDL_SetTextInputRect let&#8217;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.<\/p>\n<p>&#8211; On suspend \/ resume I had to pause my audio.<\/p>\n<p><strong>SDL2 for iOS<\/strong><\/p>\n<p>&#8211; Follow README.ios for the details.<\/p>\n<p>&#8211; SDL_SetTextInputRect doesn&#8217;t work.  So you&#8217;ll need to capture UIKeyboardWillShowNotification and shift your screen to keep the virtual keyboard from overlapping it on your own.<\/p>\n<p>&#8211; Unlike in Android, the orientation won&#8217;t change between portrait and landscape UNLESS you add SDL_WINDOW_RESIZABLE to your SDL_CreateWindow flags.<\/p>\n<p>&#8211; 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.<\/p>\n<p><strong>Custom Mixer?<\/strong><\/p>\n<p>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.)<\/p>\n<p>I used SDL_LoadWAV to load wav files and <a href=\"http:\/\/www.nothings.org\/stb_vorbis\/\">stb_vorbis<\/a> to load and stream ogg files.<\/p>\n<p>Good luck!<br \/>\n-Phil<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,6],"tags":[],"class_list":["post-1365","post","type-post","status-publish","format-standard","hentry","category-c","category-development"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/1365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=1365"}],"version-history":[{"count":4,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":1369,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/1365\/revisions\/1369"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}