[SDL] Are there any working SDL Android examples?

Dimitris Zenios dimitris.zenios at gmail.com
Tue Jul 26 02:05:21 PDT 2011


#include "SDL.h"

int main(int argc, char* argv[])
{
        SDL_Window* window;
        SDL_Renderer* renderer;

        // Initialize SDL.
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
                return 1;

        // Create the window where we will draw.
        window = SDL_CreateWindow("SDL_RenderClear",
                        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                        512, 512,
                        SDL_WINDOW_SHOWN);

        // We must call SDL_CreateRenderer in order for draw calls to
affect this window.
        renderer = SDL_CreateRenderer(window, -1, 0);

        // Select the color for drawing. It is set to red here.
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

        whiile(!quit) {
        // Clear the entire screen to our selected color.
        SDL_RenderClear(renderer);

        // Up until now everything was drawn behind the scenes.
        // This will show the new, red contents of the window.
        SDL_RenderPresent(renderer);

         // Give us time to see the window.
        SDL_Delay(50);
        }





        // Always be sure to clean up
        SDL_Quit();
        return 0;
}

On Tue, Jul 26, 2011 at 11:53 AM, William Dyce <wilbefast at gmail.com> wrote:
> Dimitris: never actually used the renderer before. The documentation is
> pretty good though. I'm guessing SDL_RENDERER_ACCELERATED isn't going to
> work, any suggestions for what kind?
>
> Just trying to get Code::Blocks to compile my project using SDL 1.3 rather
> than 1.2 so I can do some tests on Linux as well as Android. This is looking
> good so far though guys - thanks very much for all of you help :)
>
> William
>
> On 26 July 2011 18:28, Dimitris Zenios <dimitris.zenios at gmail.com> wrote:
>>
>> William you can create a renderer for the window and do render present
>> on the renderer.hat way you will not need the opengl code.You will use
>> the SDL rendering api
>>
>> On Tue, Jul 26, 2011 at 10:54 AM, William Dyce <wilbefast at gmail.com>
>> wrote:
>> > You beauty :D
>> >
>> > I combined the Dimitris and Ryan's examples (attached): I've never been
>> > so
>> > happy to see a red rectangle :P The next step is getting an image to
>> > draw,
>> > then I'll give input a shot. That is, after I do a bit of probing to
>> > figure
>> > out why it's working now when it wasn't before. I think it may be that
>> > we're
>> > now calling OpenGL functions like glClearColor and SDL_GL_SwapWindow
>> > rather
>> > than the higher-level SDL_FillRect, and SDL_Flip.
>> >
>> > Any reason why SDL_Flip might not be working? This all has me very
>> > curious... I'll need to get SDL 1.3 set up so I can test things on Linux
>> > as
>> > well - pity you can't just be a noob install it through Synaptic (which
>> > is
>> > what I've generally done for 1.2).
>> >
>> > William
>> >
>> > On 26 July 2011 17:09, Ryan C. Gordon <icculus at icculus.org> wrote:
>> >>
>> >>> 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.
>> >>
>> >> _______________________________________________
>> >> SDL mailing list
>> >> SDL at lists.libsdl.org
>> >> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>> >
>> >
>> > _______________________________________________
>> > SDL mailing list
>> > SDL at lists.libsdl.org
>> > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>> >
>> >
>> _______________________________________________
>> SDL mailing list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>



More information about the SDL mailing list