OpenGL & 2D & SDL

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[32
32];
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;

}

Daniel Vogel wrote:

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.

as Sam pointed out it displays a white screen with the Mesa software
renderer. I tested with a G400 and a TNT and it only works with a TNT.
The problem is glTexImage2D - if I use gluBuild2DMipmaps it works… I
guess it is a bug in Mesa :frowning: And from my experience with ClanLib it
should also work on Windows.–
Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right

Daniel Vogel wrote:

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.

as Sam pointed out it displays a white screen with the Mesa software
renderer. I tested with a G400 and a TNT and it only works with a TNT.
The problem is glTexImage2D - if I use gluBuild2DMipmaps it works… I
guess it is a bug in Mesa :frowning: And from my experience with ClanLib it
should also work on Windows.

Interesting… hmm. What version of Mesa are you using? I’ve wanted to get
my name on the Mesa contributors list for a while… :slight_smile:

Daniel Vogel My opinions may have changed,

Nicholas

Nicholas Vining “While you’re out there struggling
vining at pacificcoast.net with your computer, I’m naked,
icq: 20872003 clueless, and feeling good!”
- Ratbert

----- Original Message -----
From: 666@grafzahl.de (Daniel Vogel)
To: sdl at lokigames.com
Date: Sunday, March 05, 2000 6:14 PM
Subject: Re: [SDL] OpenGL & 2D & SDL

Daniel Vogel <666 at grafzahl.de> schrieb am 06 Mar 2000:

Daniel Vogel wrote:

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.

as Sam pointed out it displays a white screen with the Mesa software
renderer. I tested with a G400 and a TNT and it only works with a TNT.
The problem is glTexImage2D - if I use gluBuild2DMipmaps it works… I

The problem is: Many OpenGL implementations (3dfx comes to mind :slight_smile:
don’t support texture sizes larger than 256x256.

Use GL_PROXY_TEXTURE_2D to check if your implementation suits your
texturing needs and chop off the higher mipmap levels accordingly.

  • Andreas–
    Probably one of the smallest 3D-Games in the world: http://www.gltron.org
    More than 60’000 Downloads of the latest version (0.53)

“Andreas Umbach” wrote:

The problem is: Many OpenGL implementations (3dfx comes to mind :slight_smile:
don’t support texture sizes larger than 256x256.

Use GL_PROXY_TEXTURE_2D to check if your implementation suits your
texturing needs and chop off the higher mipmap levels accordingly.

I know, but all implementations tested supported at least a min texture
size of 1024. BTW, GL_PROXY_TEXTURE_2D is broken with Mesa ;)–
Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right