SDLGLES initial import
[sdlhildon] / sdlgles / test / gles1.c
1 /* gles1 - a simple SDL_gles OpenGL|ES 1.1 sample
2  *
3  * This file is in the public domain, furnished "as is", without technical
4  * support, and with no warranty, express or implied, as to its usefulness for
5  * any purpose.
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include <assert.h>
12
13 #include <SDL.h>
14 #include <SDL_gles.h>
15
16 #include <GLES/gl.h>
17
18 static SDL_Surface *screen;
19 static SDL_GLES_Context *context;
20
21 static float box_step = 0.1f;
22
23 static const float w = 0.28f, h = 0.4f;
24 static float x = -1.0f, y = 0.0f;
25 static float box_v[4*3];
26
27 static Uint32 tick(Uint32 interval, void* param)
28 {
29         SDL_Event e;
30         e.type = SDL_VIDEOEXPOSE;
31
32         x += box_step;
33         if (x >= 1.0f || x <= -1.0f) {
34                 box_step *= -1.0f;
35         }
36
37         const float x1 = x - w/2, y1 = y - h/2;
38         const float x2 = x + w/2, y2 = y + h/2;
39         const float z = 0.5f;
40         box_v[0] = x1; box_v[1] = y1;  box_v[2] = z;
41         box_v[3] = x2; box_v[4] = y1;  box_v[5] = z;
42         box_v[6] = x1; box_v[7] = y2;  box_v[8] = z;
43         box_v[9] = x2; box_v[10] = y2; box_v[11] = z;
44
45         SDL_PushEvent(&e);
46
47         return interval;
48 }
49
50 int main()
51 {
52         int res;
53         res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
54         assert(res == 0);
55
56         res = SDL_GLES_Init(SDL_GLES_VERSION_1_1);
57         assert(res == 0);
58
59         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
60         assert(screen);
61
62         SDL_TimerID timer = SDL_AddTimer(10, tick, NULL);
63         assert(timer != NULL);
64
65         context = SDL_GLES_CreateContext();
66         assert(context);
67
68         SDL_GLES_MakeCurrent(context);
69         
70         glMatrixMode(GL_MODELVIEW);
71         glLoadIdentity();
72
73         glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
74         glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
75         glEnableClientState(GL_VERTEX_ARRAY);
76         glVertexPointer(3, GL_FLOAT, 0, box_v);
77
78         SDL_Event event;
79         while (SDL_WaitEvent(&event)) {
80                 switch (event.type) {
81                         case SDL_QUIT:
82                                 goto quit;
83                         case SDL_VIDEOEXPOSE:
84                                 glClear(GL_COLOR_BUFFER_BIT);
85                                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
86                                 SDL_GLES_SwapBuffers();
87                                 break;
88                 }
89         }
90
91 quit:
92         SDL_GLES_DeleteContext(context);
93
94         SDL_GLES_Quit();
95         SDL_Quit();
96
97         return 0;
98 }