test/testshader.c
changeset 7323 590e596a8efc
parent 7191 75360622e65f
child 7324 0b4494cd6412
equal deleted inserted replaced
7322:5b94da2650a6 7323:590e596a8efc
     9   including commercial applications, and to alter it and redistribute it
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    10   freely.
    11 */
    11 */
    12 /* This is a simple example of using GLSL shaders with SDL */
    12 /* This is a simple example of using GLSL shaders with SDL */
    13 
    13 
    14 #if 1 /* FIXME: Rework this using the 2.0 API */
       
    15 #include <stdio.h>
       
    16 
       
    17 int main(int argc, char *argv[])
       
    18 {
       
    19     printf("FIXME\n");
       
    20     return 0;
       
    21 }
       
    22 #else
       
    23 #include "SDL.h"
    14 #include "SDL.h"
    24 
    15 
    25 #ifdef HAVE_OPENGL
    16 #ifdef HAVE_OPENGL
    26 
    17 
    27 #include "SDL_opengl.h"
    18 #include "SDL_opengl.h"
   313     if (image == NULL) {
   304     if (image == NULL) {
   314         return 0;
   305         return 0;
   315     }
   306     }
   316 
   307 
   317     /* Save the alpha blending attributes */
   308     /* Save the alpha blending attributes */
   318     saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
       
   319     SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
   309     SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
   320     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   310     SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
   321         SDL_SetAlpha(surface, 0, 0);
       
   322     }
       
   323 
   311 
   324     /* Copy the surface into the GL texture image */
   312     /* Copy the surface into the GL texture image */
   325     area.x = 0;
   313     area.x = 0;
   326     area.y = 0;
   314     area.y = 0;
   327     area.w = surface->w;
   315     area.w = surface->w;
   328     area.h = surface->h;
   316     area.h = surface->h;
   329     SDL_BlitSurface(surface, &area, image, &area);
   317     SDL_BlitSurface(surface, &area, image, &area);
   330 
   318 
   331     /* Restore the alpha blending attributes */
   319     /* Restore the alpha blending attributes */
   332     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   320     SDL_SetSurfaceAlphaMod(surface, saved_alpha);
   333         SDL_SetAlpha(surface, saved_flags, saved_alpha);
       
   334     }
       
   335 
   321 
   336     /* Create an OpenGL texture for the image */
   322     /* Create an OpenGL texture for the image */
   337     glGenTextures(1, &texture);
   323     glGenTextures(1, &texture);
   338     glBindTexture(GL_TEXTURE_2D, texture);
   324     glBindTexture(GL_TEXTURE_2D, texture);
   339     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   325     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   366 
   352 
   367     glMatrixMode(GL_MODELVIEW);
   353     glMatrixMode(GL_MODELVIEW);
   368 }
   354 }
   369 
   355 
   370 /* The main drawing function. */
   356 /* The main drawing function. */
   371 void DrawGLScene(GLuint texture, GLfloat * texcoord)
   357 void DrawGLScene(SDL_Window *window, GLuint texture, GLfloat * texcoord)
   372 {
   358 {
   373     /* Texture coordinate lookup, to make it simple */
   359     /* Texture coordinate lookup, to make it simple */
   374     enum {
   360     enum {
   375         MINX,
   361         MINX,
   376         MINY,
   362         MINY,
   423         glUseProgramObjectARB(0);
   409         glUseProgramObjectARB(0);
   424     }
   410     }
   425     glDisable(GL_TEXTURE_2D);
   411     glDisable(GL_TEXTURE_2D);
   426 
   412 
   427     // swap buffers to display, since we're double buffered.
   413     // swap buffers to display, since we're double buffered.
   428     SDL_GL_SwapBuffers();
   414     SDL_GL_SwapWindow(window);
   429 }
   415 }
   430 
   416 
   431 int main(int argc, char **argv)
   417 int main(int argc, char **argv)
   432 {
   418 {
   433     int done;
   419     int done;
       
   420     SDL_Window *window;
   434     SDL_Surface *surface;
   421     SDL_Surface *surface;
   435     GLuint texture;
   422     GLuint texture;
   436     GLfloat texcoords[4];
   423     GLfloat texcoords[4];
   437 
   424 
   438     /* Initialize SDL for video output */
   425     /* Initialize SDL for video output */
   440         fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
   427         fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
   441         exit(1);
   428         exit(1);
   442     }
   429     }
   443 
   430 
   444     /* Create a 640x480 OpenGL screen */
   431     /* Create a 640x480 OpenGL screen */
   445     if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL ) {
   432     window = SDL_CreateWindow( "Shader Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL );
   446         fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
   433     if ( !window ) {
       
   434         fprintf(stderr, "Unable to create OpenGL window: %s\n", SDL_GetError());
   447         SDL_Quit();
   435         SDL_Quit();
   448         exit(2);
   436         exit(2);
   449     }
   437     }
   450 
   438 
   451     /* Set the title bar in environments that support it */
   439     if ( !SDL_GL_CreateContext(window)) {
   452     SDL_WM_SetCaption("Shader Demo", NULL);
   440         fprintf(stderr, "Unable to create OpenGL context: %s\n", SDL_GetError());
       
   441         SDL_Quit();
       
   442         exit(2);
       
   443     }
   453 
   444 
   454     surface = SDL_LoadBMP("icon.bmp");
   445     surface = SDL_LoadBMP("icon.bmp");
   455     if ( ! surface ) {
   446     if ( ! surface ) {
   456         fprintf(stderr, "Unable to load icon.bmp: %s\n", SDL_GetError());
   447         fprintf(stderr, "Unable to load icon.bmp: %s\n", SDL_GetError());
   457         SDL_Quit();
   448         SDL_Quit();
   467     } else {
   458     } else {
   468         printf("Shaders not supported!\n");
   459         printf("Shaders not supported!\n");
   469     }
   460     }
   470     done = 0;
   461     done = 0;
   471     while ( ! done ) {
   462     while ( ! done ) {
   472         DrawGLScene(texture, texcoords);
   463         DrawGLScene(window, texture, texcoords);
   473 
   464 
   474         /* This could go in a separate function */
   465         /* This could go in a separate function */
   475         { SDL_Event event;
   466         { SDL_Event event;
   476             while ( SDL_PollEvent(&event) ) {
   467             while ( SDL_PollEvent(&event) ) {
   477                 if ( event.type == SDL_QUIT ) {
   468                 if ( event.type == SDL_QUIT ) {
   501     printf("No OpenGL support on this system\n");
   492     printf("No OpenGL support on this system\n");
   502     return 1;
   493     return 1;
   503 }
   494 }
   504 
   495 
   505 #endif /* HAVE_OPENGL */
   496 #endif /* HAVE_OPENGL */
   506 #endif
       
   507 
   497 
   508 /* vi: set ts=4 sw=4 expandtab: */
   498 /* vi: set ts=4 sw=4 expandtab: */