5425ecd39beb3c545b0ae32b7607b7c75f3659b9
[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.025f;
22
23 static const float w = 0.28f, h = 0.4f;
24 static float x = -0.5f, y = 0.0f;
25 static float box_v[4*3];
26 static bool fullscreen = false;
27
28 static Uint32 tick(Uint32 interval, void* param)
29 {
30         SDL_Event e;
31         e.type = SDL_VIDEOEXPOSE;
32
33         x += box_step;
34         if ((x + w/2)  >= 1.0f || (x - w/2) <= -1.0f) {
35                 box_step *= -1.0f;
36         }
37
38         const float x1 = x - w/2, y1 = y - h/2;
39         const float x2 = x + w/2, y2 = y + h/2;
40         const float z = 0.5f;
41         box_v[0] = x1; box_v[1] = y1;  box_v[2] = z;
42         box_v[3] = x2; box_v[4] = y1;  box_v[5] = z;
43         box_v[6] = x1; box_v[7] = y2;  box_v[8] = z;
44         box_v[9] = x2; box_v[10] = y2; box_v[11] = z;
45
46         SDL_PushEvent(&e); /* Since SDL calls timers in another thread, we cannot
47                                                   call rendering functions from here. */
48
49         return interval;
50 }
51
52 static void toggle_fullscreen()
53 {
54         int res;
55
56         fullscreen = !fullscreen;
57
58         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE |
59                 (fullscreen ? SDL_FULLSCREEN : 0));
60         assert(screen);
61
62         res = SDL_GLES_SetVideoMode();
63         if (res != 0) puts(SDL_GetError());
64         assert(res == 0);
65 }
66
67 int main()
68 {
69         int res;
70         res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
71         assert(res == 0);
72
73         res = SDL_GLES_Init(SDL_GLES_VERSION_1_1);
74         assert(res == 0);
75
76         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
77         assert(screen);
78
79         SDL_WM_SetCaption("SDLgles v1 test");
80         SDL_ShowCursor(SDL_DISABLE);
81
82         SDL_TimerID timer = SDL_AddTimer(30, tick, NULL);
83         assert(timer != NULL);
84
85         context = SDL_GLES_CreateContext();
86         assert(context);
87
88         res = SDL_GLES_MakeCurrent(context);
89         assert(res == 0);
90
91         glMatrixMode(GL_MODELVIEW);
92         glLoadIdentity();
93
94         glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
95         glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
96         glEnableClientState(GL_VERTEX_ARRAY);
97         glVertexPointer(3, GL_FLOAT, 0, box_v);
98
99         SDL_Event event;
100         while (SDL_WaitEvent(&event)) {
101                 switch (event.type) {
102                         case SDL_QUIT:
103                                 goto quit;
104                         case SDL_VIDEOEXPOSE:
105                                 glClear(GL_COLOR_BUFFER_BIT);
106                                 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
107                                 SDL_GLES_SwapBuffers();
108                                 break;
109                         case SDL_MOUSEBUTTONDOWN:
110                                 toggle_fullscreen();
111                                 break;
112                 }
113         }
114
115 quit:
116         SDL_GLES_DeleteContext(context);
117
118         SDL_GLES_Quit();
119         SDL_Quit();
120
121         return 0;
122 }