hi`
today I found some time to write that test app I promised. Okay, it is
not as fancy as I intended to write it but it serves its purpose:
showing a way to use SDL for normal blitting and OpenGL for the final
blit.–
Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right
-------------- next part --------------
// gcc testgl.c -o testgl -lSDL -lpthread -lGL -lGLU
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#define RMASK 0xFF000000
#define GMASK 0x00FF0000
#define BMASK 0x0000FF00
#define AMASK 0x00000000
/* black and white tiles /
int white[3232];
int black[32*32];
int main( int argc, char* argv[] )
{
/* Variable declaration at the beginning of a scope - that seriously sucks about C */
SDL_Surface *screen;
SDL_Surface *black_tile;
SDL_Surface *white_tile;
SDL_Rect pos;
GLuint texture;
Uint32 time;
int done = 0;
int frames = 0;
int x,y;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
/* Initialize the display. */
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if ( SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL /*| SDL_FULLSCREEN*/ ) == NULL )
{
fprintf( stderr, "Couldn't set GL mode: %s\n", SDL_GetError() );
exit(1);
}
memset(white, 0xFF, sizeof(white));
memset(black, 0x00, sizeof(black));
screen = SDL_CreateRGBSurface( SDL_SWSURFACE, 640, 480, 32, RMASK, GMASK, BMASK, AMASK );
black_tile = SDL_CreateRGBSurfaceFrom( black, 32, 32, 32, 4*32, RMASK, GMASK, BMASK, AMASK );
white_tile = SDL_CreateRGBSurfaceFrom( white, 32, 32, 32, 4*32, RMASK, GMASK, BMASK, AMASK );
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
{
/* Why can't I pass NULL if I don't want the texture to be initialized? */
void* dummy = malloc(1024*512*4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, dummy);
free(dummy);
}
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 480.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
time = SDL_GetTicks();
while (!done)
{
GLenum gl_error;
char* sdl_error;
SDL_Event event;
gl_error = glGetError();
if( gl_error != GL_NO_ERROR )
{
fprintf( stderr, "testgl: OpenGL error: %d\n", gl_error );
}
sdl_error = SDL_GetError();
if( sdl_error[0] != '\0' )
{
fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
SDL_ClearError();
}
pos.x = 0;
pos.y = 0;
pos.w = 32;
pos.h = 32;
for (y=0; y<480; y+=32)
{
for (x=0; x<640; x+=32)
{
pos.x = x;
pos.y = y;
if ((x+y)%64)
SDL_BlitSurface(white_tile, NULL, screen, &pos);
else
SDL_BlitSurface(black_tile, NULL, screen, &pos);
}
}
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 640, 480, GL_RGBA, GL_UNSIGNED_BYTE, screen->pixels);
/* Take a deep breath. */
SDL_Delay(20);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0, 0.0);
glVertex2i( 0, 0);
glTexCoord2f(640/1024.0, 0.0);
glVertex2i( 640, 0);
glTexCoord2f(0.0, 480/512.0);
glVertex2i( 0, 480);
glTexCoord2f(640/1024.0, 480/512.0);
glVertex2i( 640, 480);
glEnd();
/* Swap buffers */
SDL_GL_SwapBuffers();
frames++;
/* Check if there's a pending event. */
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYDOWN:
case SDL_QUIT:
done = 1;
}
}
}
printf("fps : %f\n", frames * 1000.0 / (SDL_GetTicks() - time));
return 0;
}