Meego Wiki
From MeeGo wiki
Jump to: navigation, search

Example application for Meego Harmattan N950 / libsdl1.2_1.2.14-0maemo5

/* gles1 - a simple SDL_gles OpenGL|ES 1.1 sample

gcc `sdl-config --cflags --libs` -o gles1 gles1.c -lEGL -lGLES_CM

 * From http://git.maemo.org/git?p=sdlhildon;a=blob_plain;f=sdlgles/test/gles2.c
 * Modifications from: http://git.maemo.org/git?p=sdlhildon;a=blob_plain;f=sdlgles/test/gles2.c
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

#include <SDL.h>

#include <GLES/gl.h>
 
static SDL_Surface *screen;

static float box_step = 0.025f;

static const float w = 0.28f, h = 0.4f;
static float x = -0.5f, y = 0.0f;
static float box_v[4*3];
 
static Uint32 tick(Uint32 interval, void* param)
{
	SDL_Event e;
	e.type = SDL_VIDEOEXPOSE;

	x += box_step;
	if ((x + w/2)  >= 1.0f || (x - w/2) <= -1.0f) {
		box_step *= -1.0f;
	}

	const float x1 = x - w/2, y1 = y - h/2;
	const float x2 = x + w/2, y2 = y + h/2;
	const float z = 0.5f;
	box_v[0] = x1; box_v[1] = y1;  box_v[2] = z;
	box_v[3] = x2; box_v[4] = y1;  box_v[5] = z;
	box_v[6] = x1; box_v[7] = y2;  box_v[8] = z;
	box_v[9] = x2; box_v[10] = y2; box_v[11] = z;

	SDL_PushEvent(&e); /* Since SDL calls timers in another thread, we cannot
						  call rendering functions from here. */

	return interval;
}

/* general OpenGL initialization function */
int initGL( GLvoid )
{
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);

    /* Enable Texture Mapping ( NEW ) */
    glEnable( GL_TEXTURE_2D );

    /* Enable smooth shading */
    glShadeModel( GL_SMOOTH );

    /* Set the background black */
    glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );

    /* Depth buffer setup */
    glClearDepthf( 1.0f );

    /* Enables Depth Testing */
    glEnable( GL_DEPTH_TEST );

    /* The Type Of Depth Test To Do */
    glDepthFunc( GL_LEQUAL );

    /* Really Nice Perspective Calculations */
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
    return 1;
}

int main()
{
	int res;
	res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
	assert(res == 0);

	initGL();

	SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 1);
	screen = SDL_SetVideoMode(0, 0, 0, SDL_OPENGLES | SDL_FULLSCREEN);
	assert(screen);

	SDL_WM_SetCaption("SDLgles v1 test", "SDLgles v1 test");
	SDL_ShowCursor(SDL_DISABLE);

	SDL_TimerID timer = SDL_AddTimer(30, tick, NULL);
	assert(timer != NULL);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
	glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, box_v);

	SDL_Event event;
	while (SDL_WaitEvent(&event)) {
		switch (event.type) {
			case SDL_QUIT:
				goto quit;
			case SDL_VIDEOEXPOSE:
				glClear(GL_COLOR_BUFFER_BIT);
				glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
				SDL_GL_SwapBuffers();
				break;
		}
	}

quit:
	SDL_Quit();

	return 0;
}
Personal tools