[SDL] Are there any working SDL Android examples?

Ryan C. Gordon icculus at icculus.org
Tue Jul 26 00:09:54 PDT 2011


> Dimitris: so avoid legacy functions like SDL_SetVideoMode?

In theory, if all you want is the 1.2 API, SDL_SetVideoMode() _should_ 
do the right thing...it just wraps the 1.3 API with some limitations 
(like the inability to have multiple windows, etc).

That being said, the 1.2 emulation is not perfect in places, so you're 
probably safer using the 1.3 API directly if you can.

> I've actually been unable to find any example code that uses
> SDL_CreateWindow, even in the SDL1.3 examples directory. Could you give
> me an example, and is there anything I should try to strip out?

SDL/test/testgles.c

Sadly, all the important parts are all abstracted out to common.c (where 
it handles multiple windows and all sorts of options you don't care 
about, etc), but you should be able to find what you need easily enough.

Using the SDL 1.3 API isn't much more complicated that 1.2:

   // this is untested, so forgive minor typos, please...

   // just like 1.2 (but DO check return values!)
   SDL_Init(SDL_INIT_VIDEO);
   SDL_GL_LoadLibrary(NULL);

   // Make a window...
   win = SDL_CreateWindow("My Application",
             SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
             640, 480,
             SDL_WINDOW_FULLSCREEN | SDL_WINDOW_SHOWN |
             SDL_WINDOW_OPENGL);

   // Create a GL context for that window...
   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
   // etc...

   // Make the major version 2 if you want GLES2 on Android.
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
   SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
   ctx = SDL_GL_CreateContext(win);
   SDL_GL_MakeCurrent(win, ctx);

   // You might want this to turn on/off vsync...
   SDL_GL_SwapInterval(1);  // 1 == vsync, 0 == no vsync.

   // Now you can draw with OpenGL calls.

   // Use this instead of SDL 1.2's SDL_GL_SwapBuffers()...
   SDL_GL_SwapWindow(win);

   // At shutdown, you clean up...
   SDL_GL_MakeCurrent(NULL, NULL);
   SDL_GL_DeleteContext(ctx);
   SDL_DestroyWindow(win);

   // And finally, just like 1.2...
   SDL_Quit();


Some of this was just being pedantic (you don't actually have to do the 
SDL_GL_MakeCurrent() calls), but that's about all you need to know about 
SDL's video subsystem to move a 1.2 OpenGL app to 1.3.

That being said, I can't account for bugs in our Android port. If you 
find one, please report it.

I was really worried for a long time about moving from the 1.2 API, 
which I'm extremely comfortable with, to 1.3, but the API is 
surprisingly comfortable too...it feels easy like 1.2, but without 1.2's 
limitations.

--ryan.




More information about the SDL mailing list