Updated testshader for the SDL 2.0 API
authorSam Lantinga <slouken@libsdl.org>
Thu, 27 Jun 2013 11:44:07 -0700
changeset 7323 590e596a8efc
parent 7322 5b94da2650a6
child 7324 0b4494cd6412
Updated testshader for the SDL 2.0 API
test/testshader.c
--- a/test/testshader.c	Thu Jun 27 11:27:19 2013 -0700
+++ b/test/testshader.c	Thu Jun 27 11:44:07 2013 -0700
@@ -11,15 +11,6 @@
 */
 /* This is a simple example of using GLSL shaders with SDL */
 
-#if 1 /* FIXME: Rework this using the 2.0 API */
-#include <stdio.h>
-
-int main(int argc, char *argv[])
-{
-    printf("FIXME\n");
-    return 0;
-}
-#else
 #include "SDL.h"
 
 #ifdef HAVE_OPENGL
@@ -315,11 +306,8 @@
     }
 
     /* Save the alpha blending attributes */
-    saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
     SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
-    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
-        SDL_SetAlpha(surface, 0, 0);
-    }
+    SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
 
     /* Copy the surface into the GL texture image */
     area.x = 0;
@@ -329,9 +317,7 @@
     SDL_BlitSurface(surface, &area, image, &area);
 
     /* Restore the alpha blending attributes */
-    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
-        SDL_SetAlpha(surface, saved_flags, saved_alpha);
-    }
+    SDL_SetSurfaceAlphaMod(surface, saved_alpha);
 
     /* Create an OpenGL texture for the image */
     glGenTextures(1, &texture);
@@ -368,7 +354,7 @@
 }
 
 /* The main drawing function. */
-void DrawGLScene(GLuint texture, GLfloat * texcoord)
+void DrawGLScene(SDL_Window *window, GLuint texture, GLfloat * texcoord)
 {
     /* Texture coordinate lookup, to make it simple */
     enum {
@@ -425,12 +411,13 @@
     glDisable(GL_TEXTURE_2D);
 
     // swap buffers to display, since we're double buffered.
-    SDL_GL_SwapBuffers();
+    SDL_GL_SwapWindow(window);
 }
 
 int main(int argc, char **argv)
 {
     int done;
+    SDL_Window *window;
     SDL_Surface *surface;
     GLuint texture;
     GLfloat texcoords[4];
@@ -442,14 +429,18 @@
     }
 
     /* Create a 640x480 OpenGL screen */
-    if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) {
-        fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
+    window = SDL_CreateWindow( "Shader Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL );
+    if ( !window ) {
+        fprintf(stderr, "Unable to create OpenGL window: %s\n", SDL_GetError());
         SDL_Quit();
         exit(2);
     }
 
-    /* Set the title bar in environments that support it */
-    SDL_WM_SetCaption("Shader Demo", NULL);
+    if ( !SDL_GL_CreateContext(window)) {
+        fprintf(stderr, "Unable to create OpenGL context: %s\n", SDL_GetError());
+        SDL_Quit();
+        exit(2);
+    }
 
     surface = SDL_LoadBMP("icon.bmp");
     if ( ! surface ) {
@@ -469,7 +460,7 @@
     }
     done = 0;
     while ( ! done ) {
-        DrawGLScene(texture, texcoords);
+        DrawGLScene(window, texture, texcoords);
 
         /* This could go in a separate function */
         { SDL_Event event;
@@ -503,6 +494,5 @@
 }
 
 #endif /* HAVE_OPENGL */
-#endif
 
 /* vi: set ts=4 sw=4 expandtab: */