6e6a8a5e3a77e62f4718fe6fd4a0b137534fbb26
[sdlhildon] / sdlgles / test / attrib.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 bool fullscreen = false;
22
23 static const float vertices[] ={-0.6f, -0.6f, -0.5f, /* Square 1 */
24                                                                  0.3f, -0.6f, -0.5f,
25                                                                 -0.6f,  0.8f,  0.1f,
26                                                                  0.3f,  0.8f,  0.1f,
27                                                                  0.3f,  0.8f,  0.1f, /* Degenerate vertices */
28                                                                 -0.3f, -0.6f,  0.5f,
29                                                                 -0.3f, -0.6f,  0.5f, /* Square 2 */
30                                                                  0.6f, -0.6f,  0.5f,
31                                                                 -0.3f,  0.8f, -0.1f,
32                                                                  0.6f,  0.8f, -0.1f,
33                                                                 };
34 static const float colors[] =  { 1.0f,  0.0f,  0.0f, 1.0f,
35                                                                  1.0f,  0.0f,  0.0f, 1.0f,
36                                                                  1.0f,  0.0f,  0.0f, 1.0f,
37                                                                  1.0f,  0.0f,  0.0f, 1.0f,
38                                                                  0.0f,  0.0f,  0.0f, 1.0f,
39                                                                  0.0f,  0.0f,  1.0f, 1.0f,
40                                                                  0.0f,  0.0f,  1.0f, 1.0f,
41                                                                  0.0f,  0.0f,  1.0f, 1.0f,
42                                                                  0.0f,  0.0f,  1.0f, 1.0f,
43                                                                  0.0f,  0.0f,  1.0f, 1.0f,
44                                                                 };
45
46 static void draw()
47 {
48         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49         glDrawArrays(GL_TRIANGLE_STRIP, 0, 10);
50         SDL_GLES_SwapBuffers();
51 }
52
53 static void toggle_fullscreen()
54 {
55         int res;
56
57         fullscreen = !fullscreen;
58
59         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE |
60                 (fullscreen ? SDL_FULLSCREEN : 0));
61         assert(screen);
62
63         res = SDL_GLES_SetVideoMode();
64         assert(res == 0);
65
66         draw();
67 }
68
69 int main()
70 {
71         int res, value = 0;
72         res = SDL_Init(SDL_INIT_VIDEO);
73         assert(res == 0);
74
75         res = SDL_GLES_Init(SDL_GLES_VERSION_1_1);
76         assert(res == 0);
77
78         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
79         assert(screen);
80
81         SDL_WM_SetCaption("SDLgles attrib test", "SDLgles v1 test");
82         SDL_ShowCursor(SDL_DISABLE);
83
84 #define ENABLE_DEPTH_BUFFER 1
85
86 #ifdef ENABLE_DEPTH_BUFFER
87         /* With a depth buffer, the red square will be partially over the blue sq. */
88         res = SDL_GLES_SetAttribute(SDL_GLES_DEPTH_SIZE, 4);
89         assert(res == 0);
90
91         res = SDL_GLES_GetAttribute(SDL_GLES_DEPTH_SIZE, &value);
92         assert(res == 0);
93         assert(value == 4);
94         printf("Wanted depth buffer size is %d\n", value);
95 #else
96         /* Without a depth buffer, the blue square will be over the red square. */
97         printf("Wanted depth buffer size is %d\n", 0);
98 #endif
99
100         context = SDL_GLES_CreateContext();
101         assert(context);
102
103         res = SDL_GLES_MakeCurrent(context);
104         assert(res == 0);
105
106         value = 0;
107         res = SDL_GLES_GetAttribute(SDL_GLES_DEPTH_SIZE, &value);
108         assert(res == 0);
109         printf("Depth buffer size is %d\n", value);
110
111         glMatrixMode(GL_MODELVIEW);
112         glLoadIdentity();
113
114         glEnable(GL_DEPTH_TEST);
115         glClearColor(0.0f, 0.2f, 0.0f, 0.0f);
116         glEnableClientState(GL_VERTEX_ARRAY);
117         glEnableClientState(GL_COLOR_ARRAY);
118         glVertexPointer(3, GL_FLOAT, 0, vertices);
119         glColorPointer(4, GL_FLOAT, 0, colors);
120
121         draw();
122
123         SDL_Event event;
124         while (SDL_WaitEvent(&event)) {
125                 switch (event.type) {
126                         case SDL_QUIT:
127                                 goto quit;
128                         case SDL_MOUSEBUTTONDOWN:
129                                 toggle_fullscreen();
130                                 break;
131                 }
132         }
133
134 quit:
135         SDL_GLES_DeleteContext(context);
136
137         SDL_GLES_Quit();
138         SDL_Quit();
139
140         return 0;
141 }
142