Updated testshader for the SDL 2.0 API

This commit is contained in:
Sam Lantinga 2013-06-27 11:44:07 -07:00
parent 54060becd3
commit a61d6e4500

View file

@ -11,15 +11,6 @@
*/ */
/* This is a simple example of using GLSL shaders with SDL */ /* 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" #include "SDL.h"
#ifdef HAVE_OPENGL #ifdef HAVE_OPENGL
@ -315,11 +306,8 @@ SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
} }
/* Save the alpha blending attributes */ /* Save the alpha blending attributes */
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
SDL_GetSurfaceAlphaMod(surface, &saved_alpha); SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) { SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
SDL_SetAlpha(surface, 0, 0);
}
/* Copy the surface into the GL texture image */ /* Copy the surface into the GL texture image */
area.x = 0; area.x = 0;
@ -329,9 +317,7 @@ SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
SDL_BlitSurface(surface, &area, image, &area); SDL_BlitSurface(surface, &area, image, &area);
/* Restore the alpha blending attributes */ /* Restore the alpha blending attributes */
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) { SDL_SetSurfaceAlphaMod(surface, saved_alpha);
SDL_SetAlpha(surface, saved_flags, saved_alpha);
}
/* Create an OpenGL texture for the image */ /* Create an OpenGL texture for the image */
glGenTextures(1, &texture); glGenTextures(1, &texture);
@ -368,7 +354,7 @@ void InitGL(int Width, int Height) // We call this right afte
} }
/* The main drawing function. */ /* 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 */ /* Texture coordinate lookup, to make it simple */
enum { enum {
@ -425,12 +411,13 @@ void DrawGLScene(GLuint texture, GLfloat * texcoord)
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
// swap buffers to display, since we're double buffered. // swap buffers to display, since we're double buffered.
SDL_GL_SwapBuffers(); SDL_GL_SwapWindow(window);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int done; int done;
SDL_Window *window;
SDL_Surface *surface; SDL_Surface *surface;
GLuint texture; GLuint texture;
GLfloat texcoords[4]; GLfloat texcoords[4];
@ -442,14 +429,18 @@ int main(int argc, char **argv)
} }
/* Create a 640x480 OpenGL screen */ /* Create a 640x480 OpenGL screen */
if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) { window = SDL_CreateWindow( "Shader Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL );
fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError()); if ( !window ) {
fprintf(stderr, "Unable to create OpenGL window: %s\n", SDL_GetError());
SDL_Quit(); SDL_Quit();
exit(2); exit(2);
} }
/* Set the title bar in environments that support it */ if ( !SDL_GL_CreateContext(window)) {
SDL_WM_SetCaption("Shader Demo", NULL); fprintf(stderr, "Unable to create OpenGL context: %s\n", SDL_GetError());
SDL_Quit();
exit(2);
}
surface = SDL_LoadBMP("icon.bmp"); surface = SDL_LoadBMP("icon.bmp");
if ( ! surface ) { if ( ! surface ) {
@ -469,7 +460,7 @@ int main(int argc, char **argv)
} }
done = 0; done = 0;
while ( ! done ) { while ( ! done ) {
DrawGLScene(texture, texcoords); DrawGLScene(window, texture, texcoords);
/* This could go in a separate function */ /* This could go in a separate function */
{ SDL_Event event; { SDL_Event event;
@ -503,6 +494,5 @@ main(int argc, char *argv[])
} }
#endif /* HAVE_OPENGL */ #endif /* HAVE_OPENGL */
#endif
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */