• SDL2 para Android

De VGAFIB

Instalar SDK/NDK como en SFML

hg clone http://hg.libsdl.org/SDL SDL
mv SDL/android-project .
mv SDL android-project/jni/
cd android-project/jni/

poner tus sources en android-project/jni/src/Android.mk

Ejemplo de main (FUNCIONA!!! :D)

#include "GLES2/gl2.h"
#include "SDL.h"
#include <stdio.h>

int SDL_main(int argc, char *argv[]) {

	SDL_Window *window;                    // Declare a pointer

	SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

	// Create an application window with the following settings:
	window = SDL_CreateWindow(
		"An SDL2 window",                  // window title
		SDL_WINDOWPOS_UNDEFINED,           // initial x position
		SDL_WINDOWPOS_UNDEFINED,           // initial y position
		640,                               // width, in pixels
		480,                               // height, in pixels
		SDL_WINDOW_OPENGL                  // flags - see below
	);

	// Check that the window was successfully made
	if (window == NULL) {
		// In the event that the window could not be made...
		printf("Could not create window: %s\n", SDL_GetError());
		return 1;
	}


	// Create an OpenGL context associated with the window.
	SDL_GLContext glcontext = SDL_GL_CreateContext(window);

	int frame = 0;
	while(true) {
		SDL_Event event; /* Event structure */
		/* Check for events */
		while(SDL_PollEvent(&event)){  /* Loop until there are no events left on the queue */
			switch(event.type){  /* Process the appropiate event type */
				default: /* Report an unhandled event */
					printf("Event!\n");
			}
		}

		// now you can make GL calls.
		glClearColor(frame%2,0,0,1);
		glClear(GL_COLOR_BUFFER_BIT);
		SDL_GL_SwapWindow(window);
        frame++;
	}

	// Once finished with OpenGL functions, the SDL_GLContext can be deleted.
	SDL_GL_DeleteContext(glcontext);

	// Close and destroy the window
	SDL_DestroyWindow(window);

	// Clean up
	SDL_Quit();
	return 0;
}

Nota: ES IMPORTANTE llamar a SDL_PollEvent cada frame. PollEvent hace cosas para restaurar el contexto de OpenGL cuando cambias de aplicacion y vuelves. Si no lo llamas se te rompera al cambiar de aplicacion. Tambien es el encargado de bloquear el game loop cuando la aplicacion esta en background, cosa importante para no malgastar battery life.

(en el dir jni/)
ndk-build
cd ..
ant debug

o si quieres instalar

ant debug install