6a60e89f57dbcd2f3a4d839dbf1e7ce90797829f
[sdlhildon] / sdlgles / test / gles2.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 <GLES2/gl2.h>
17
18 static SDL_Surface *screen;
19 static SDL_GLES_Context *context;
20
21 static GLuint program;
22 static bool fullscreen = false;
23
24 static GLuint compile_shader(GLenum type, const char *src)
25 {
26         GLuint shader = glCreateShader(type);
27         GLint compiled;
28
29         assert(shader != 0),
30         glShaderSource(shader, 1, &src, NULL);
31         glCompileShader(shader);
32         glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
33         assert(compiled);
34
35         return shader;
36 }
37
38 static void init()
39 {
40         const char vertex_shader_src[] =  
41                 "attribute vec4 pos;\n"
42                 "void main()\n"
43                 "{\n"
44                 "       gl_Position = pos;\n"
45                 "}\n";
46
47         const char fragment_shader_src[] =  
48                 "precision mediump float;\n"
49                 "void main()\n"
50                 "{\n"
51                 "       gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
52                 "}\n";
53
54         GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_src);
55         GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_src);
56
57         program = glCreateProgram();
58         assert(program);
59
60         glAttachShader(program, vertex_shader);
61         glAttachShader(program, fragment_shader);
62
63         glBindAttribLocation(program, 0, "pos");
64
65         GLint linked;
66         glLinkProgram(program);
67         glGetProgramiv(program, GL_LINK_STATUS, &linked);
68         assert(linked);
69
70         glClearColor(0.0f, 0.0f, 0.2f, 0.0f);
71 }
72
73 static void render()
74 {
75         GLfloat tri_v[] =       {0.0f,  0.5f, 0.0f,
76                                                 -0.5f, -0.5f, 0.0f,
77                                                  0.5f, -0.5f, 0.0f};
78
79         glViewport(0, 0, screen->w, screen->h);
80         glClear(GL_COLOR_BUFFER_BIT);
81         glUseProgram(program);
82
83         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, tri_v);
84         glEnableVertexAttribArray(0);
85
86         glDrawArrays(GL_TRIANGLES, 0, 3);
87
88         SDL_GLES_SwapBuffers();
89 }
90
91 static void toggle_fullscreen()
92 {
93         int res;
94
95         fullscreen = !fullscreen;
96
97         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE |
98                 (fullscreen ? SDL_FULLSCREEN : 0));
99         assert(screen);
100
101         res = SDL_GLES_SetVideoMode();
102         if (res != 0) puts(SDL_GetError());
103         assert(res == 0);
104
105         render();
106 }
107
108 int main()
109 {
110         int res;
111         res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
112         assert(res == 0);
113
114         res = SDL_GLES_Init(SDL_GLES_VERSION_2_0);
115         assert(res == 0);
116
117         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
118         assert(screen);
119
120         SDL_WM_SetCaption("SDLgles v2 test", "SDLgles v2 test");
121         SDL_ShowCursor(SDL_DISABLE);
122
123         context = SDL_GLES_CreateContext();
124         if (!context) puts(SDL_GetError());
125         assert(context);
126
127         res = SDL_GLES_MakeCurrent(context);
128         assert(res == 0);
129
130         init();
131         render();
132
133         SDL_Event event;
134         while (SDL_WaitEvent(&event)) {
135                 switch (event.type) {
136                         case SDL_QUIT:
137                                 goto quit;
138                         case SDL_MOUSEBUTTONDOWN:
139                                 toggle_fullscreen();
140                                 break;
141                 }
142         }
143
144 quit:
145         SDL_GLES_DeleteContext(context);
146
147         SDL_GLES_Quit();
148         SDL_Quit();
149
150         return 0;
151 }