GLES1 sample working
authorJavier S. Pedro <javier@javispedro.com>
Tue, 2 Mar 2010 19:32:54 +0000 (20:32 +0100)
committerJavier S. Pedro <javier@javispedro.com>
Tue, 2 Mar 2010 19:32:54 +0000 (20:32 +0100)
sdlgles/src/SDL_gles.c
sdlgles/src/SDL_gles.h
sdlgles/test/gles1.c

index 186eb15..c28dfdf 100644 (file)
@@ -189,6 +189,7 @@ void SDL_GLES_Quit()
 int SDL_GLES_SetVideoMode()
 {
        SDL_SysWMinfo info;
+       EGLBoolean res;
 
        SDL_VERSION(&info.version);
        if (SDL_GetWMInfo(&info) != 1) {
@@ -198,15 +199,21 @@ int SDL_GLES_SetVideoMode()
 
        /* Destroy previous surface, if any. */
        if (egl_surface != EGL_NO_SURFACE) {
+               /* Ensure the surface is not the current one,
+                * thus freeing memory earlier. */
+               eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+                        EGL_NO_CONTEXT);
                eglDestroySurface(egl_display, egl_surface);
                egl_surface = EGL_NO_SURFACE;
        }
 
-       /* No current context? Quietly defer surface creation. */
+       /* No current context? Quietly defer surface creation.
+        * Surface will be created on the call to MakeCurrent. */
        if (!cur_context) {
                return 0;
        }
 
+       /* Create the new window surface. */
        egl_surface = eglCreateWindowSurface(egl_display, cur_context->egl_config,
                (EGLNativeWindowType)info.info.x11.window, NULL);
        if (egl_surface == EGL_NO_SURFACE) {
@@ -215,6 +222,18 @@ int SDL_GLES_SetVideoMode()
                return -2;
        }
 
+       /* New surface created. Make it active. */
+       assert(cur_context && cur_context->egl_context != EGL_NO_CONTEXT);
+       res = eglMakeCurrent(egl_display, egl_surface, egl_surface,
+               cur_context->egl_context);
+
+       if (!res) {
+               SDL_SetError("EGL failed to change current surface: %s",
+                       get_error_string(eglGetError()));
+               cur_context = NULL;
+               return -2;
+       }
+
        return 0;
 }
 
@@ -263,29 +282,15 @@ int SDL_GLES_MakeCurrent(SDL_GLES_Context* c)
        SDL_GLES_ContextPriv *context = (SDL_GLES_ContextPriv*)c;
        int res;
 
-       if (context) {
-               cur_context = context;
-
-               res = SDL_GLES_SetVideoMode();
-               if (res != 0) return res; /* Surface (re-)creation failed. */
-
-               res = eglMakeCurrent(egl_display, egl_surface, egl_surface,
-                       context->egl_context);
+       cur_context = context;
 
-               if (!res) {
-                       SDL_SetError("EGL failed to make current: %s",
-                               get_error_string(eglGetError()));
-                       cur_context = NULL;
-                       return -2;
-               }
-       } else {
-               eglMakeCurrent(egl_display,     EGL_NO_SURFACE, EGL_NO_SURFACE,
-                       EGL_NO_CONTEXT);
-               cur_context = NULL;
-               SDL_GLES_SetVideoMode(); /* Will clear current surface. */
-       }
+       /* SDL_GLES_SetVideoMode() will appropiately clear the current context
+        * (and surface), then create a new surface matching the selected context
+        * config and make both the surface and the context the active ones. */
+       res = SDL_GLES_SetVideoMode();
+       if (res != 0) return res; /* Surface (re-)creation failed. */
 
-       // TODO Update attrib_list
+       /* TODO Update attrib_list. Make SDL_GLES_GetAttribute work. */
 
        switch (gl_version) {
                case SDL_GLES_VERSION_1_1:
index 9f74339..a079601 100644 (file)
@@ -46,10 +46,9 @@ extern DECLSPEC int SDLCALL SDL_GLES_Init(SDL_GLES_Version version);
   */
 extern DECLSPEC void SDLCALL SDL_GLES_Quit();
 
-/* Equivalent to SDL/GL ones. Untested. */
 /** Call before calling GetProcAddress. Dynamically loads a GLES library.
   * @param path full path to the library to load, or leave as NULL to load
-  *            the default GL ES library to the version specified in SDL_GLES_Init().
+  *            the default GL ES library (version as specified in SDL_GLES_Init()).
   * @return 0 if everything went OK.
   */
 extern DECLSPEC int SDLCALL SDL_GLES_LoadLibrary(const char *path);
index 704031a..6d844f0 100644 (file)
 static SDL_Surface *screen;
 static SDL_GLES_Context *context;
 
-static float box_step = 0.1f;
+static float box_step = 0.05f;
 
 static const float w = 0.28f, h = 0.4f;
-static float x = -1.0f, y = 0.0f;
+static float x = -0.5f, y = 0.0f;
 static float box_v[4*3];
+static bool fullscreen = false;
 
 static Uint32 tick(Uint32 interval, void* param)
 {
@@ -30,7 +31,7 @@ static Uint32 tick(Uint32 interval, void* param)
        e.type = SDL_VIDEOEXPOSE;
 
        x += box_step;
-       if (x >= 1.0f || x <= -1.0f) {
+       if ((x + w/2)  >= 1.0f || (x - w/2) <= -1.0f) {
                box_step *= -1.0f;
        }
 
@@ -42,11 +43,27 @@ static Uint32 tick(Uint32 interval, void* param)
        box_v[6] = x1; box_v[7] = y2;  box_v[8] = z;
        box_v[9] = x2; box_v[10] = y2; box_v[11] = z;
 
-       SDL_PushEvent(&e);
+       SDL_PushEvent(&e); /* Since SDL calls timers in another thread, we cannot
+                                                 call rendering functions from here. */
 
        return interval;
 }
 
+static void toggle_fullscreen()
+{
+       int res;
+
+       fullscreen = !fullscreen;
+
+       screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE |
+               (fullscreen ? SDL_FULLSCREEN : 0));
+       assert(screen);
+
+       res = SDL_GLES_SetVideoMode();
+       if (res != 0) puts(SDL_GetError());
+       assert(res == 0);
+}
+
 int main()
 {
        int res;
@@ -59,14 +76,17 @@ int main()
        screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
        assert(screen);
 
-       SDL_TimerID timer = SDL_AddTimer(10, tick, NULL);
+       SDL_ShowCursor(SDL_DISABLE);
+
+       SDL_TimerID timer = SDL_AddTimer(50, tick, NULL);
        assert(timer != NULL);
 
        context = SDL_GLES_CreateContext();
        assert(context);
 
-       SDL_GLES_MakeCurrent(context);
-       
+       res = SDL_GLES_MakeCurrent(context);
+       assert(res == 0);
+
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
 
@@ -85,6 +105,9 @@ int main()
                                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
                                SDL_GLES_SwapBuffers();
                                break;
+                       case SDL_MOUSEBUTTONDOWN:
+                               toggle_fullscreen();
+                               break;
                }
        }