1 /* gles1 - a simple SDL_gles OpenGL|ES 1.1 sample
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
16 #include <GLES2/gl2.h>
18 static SDL_Surface *screen;
19 static SDL_GLES_Context *context;
21 static GLuint program;
22 static bool fullscreen = false;
24 static GLuint compile_shader(GLenum type, const char *src)
26 GLuint shader = glCreateShader(type);
30 glShaderSource(shader, 1, &src, NULL);
31 glCompileShader(shader);
32 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
40 const char vertex_shader_src[] =
41 "attribute vec4 pos;\n"
44 " gl_Position = pos;\n"
47 const char fragment_shader_src[] =
48 "precision mediump float;\n"
51 " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
54 GLuint vertex_shader = compile_shader(GL_VERTEX_SHADER, vertex_shader_src);
55 GLuint fragment_shader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_src);
57 program = glCreateProgram();
60 glAttachShader(program, vertex_shader);
61 glAttachShader(program, fragment_shader);
63 glBindAttribLocation(program, 0, "pos");
66 glLinkProgram(program);
67 glGetProgramiv(program, GL_LINK_STATUS, &linked);
70 glClearColor(0.0f, 0.0f, 0.2f, 0.0f);
75 GLfloat tri_v[] = {0.0f, 0.5f, 0.0f,
79 glViewport(0, 0, screen->w, screen->h);
80 glClear(GL_COLOR_BUFFER_BIT);
81 glUseProgram(program);
83 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, tri_v);
84 glEnableVertexAttribArray(0);
86 glDrawArrays(GL_TRIANGLES, 0, 3);
88 SDL_GLES_SwapBuffers();
91 static void toggle_fullscreen()
95 fullscreen = !fullscreen;
97 screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE |
98 (fullscreen ? SDL_FULLSCREEN : 0));
101 res = SDL_GLES_SetVideoMode();
102 if (res != 0) puts(SDL_GetError());
111 res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
114 res = SDL_GLES_Init(SDL_GLES_VERSION_2_0);
117 screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
120 SDL_WM_SetCaption("SDLgles v2 test", "SDLgles v2 test");
121 SDL_ShowCursor(SDL_DISABLE);
123 context = SDL_GLES_CreateContext();
124 if (!context) puts(SDL_GetError());
127 res = SDL_GLES_MakeCurrent(context);
134 while (SDL_WaitEvent(&event)) {
135 switch (event.type) {
138 case SDL_MOUSEBUTTONDOWN:
145 SDL_GLES_DeleteContext(context);