Hello. I have a bit of a problem, so a little help from someone more experienced would be appreciated. :-)
I've been using SDL for a few months, and have gotten quite good at it and started making my first game. But now I want my game to use hardware accelerated rendering because software rendering won't cut it any more, so I started looking into OpenGL.
Since I have some experience in SDL and kind of know what I'm doing with it, but know nothing about OpenGL, I tried to find the simplest way possible to convert my SDL surfaces to OpenGL textures and render them with OpenGL.
So I found THIS and THIS from sdltutorials.com
Following these instructions, I altered my Initialization and rendering functions from this:
bool init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) return false;
screen = SDL_SetVideoMode( SCREEN_WIDTH , SCREEN_HEIGHT , BITS_PER_PIXEL , SDL_HWSURFACE | SDL_DOUBLEBUF );
if( screen == NULL ) return false;
if( Mix_OpenAudio( 44100 , MIX_DEFAULT_FORMAT , 2 , 512 ) < 0 ) return false;
SDL_WM_SetCaption( "Test!" , NULL );
return true;
}
SDL_Surface * load( std::string file )
{
SDL_Surface * loaded = NULL;
SDL_Surface * optimized = NULL;
loaded = IMG_Load( file.c_str() );
if( loaded != NULL )
{
optimized = SDL_DisplayFormatAlpha( loaded );
SDL_FreeSurface( loaded );
}
return optimized;
}
void apply_surface( int x , int y , SDL_Surface * get = NULL , SDL_Surface * give = NULL , SDL_Rect * clips = NULL )
{
SDL_Rect offsets;
offsets.x = x;
offsets.y = y;
SDL_BlitSurface( get , clips , give , &offsets );
}
void apply_surface( int x , int y , SDL_Surface * get , SDL_Surface * give , int clips_x , int clips_y , int w , int h )
{
SDL_Rect offsets;
offsets.x = x;
offsets.y = y;
SDL_Rect clips;
clips.x = clips_x;
clips.y = clips_y;
clips.w = w;
clips.h = h;
SDL_BlitSurface( get , &clips , give , &offsets );
}
...To this:
bool init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) return false;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,2);
screen = SDL_SetVideoMode( SCREEN_WIDTH , SCREEN_HEIGHT , BITS_PER_PIXEL , SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL );
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH , SCREEN_HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
if( screen == NULL ) return false;
if( Mix_OpenAudio( 44100 , MIX_DEFAULT_FORMAT , 2 , 512 ) < 0 ) return false;
SDL_WM_SetCaption( "OpenGL Test!" , NULL );
return true;
}
SDL_Surface * load( std::string file )
{
SDL_Surface * loaded = NULL;
loaded = IMG_Load( file.c_str() );
return loaded;
}
void apply_surface( int x , int y , SDL_Surface * get = NULL )
{
GLuint textureID = 0;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
int Mode = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, Mode, get->w, get->h, 0, Mode, GL_UNSIGNED_BYTE, get->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, textureID);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x+get->w, y);
glTexCoord2f(1, 1);
glVertex2f(x+get->w, y+get->h);
glTexCoord2f(0, 1);
glVertex2f(x, y+get->h);
glEnd();
glDeleteTextures(1, &textureID);
glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
}
void apply_surface( int x , int y , SDL_Surface * get , int clips_x , int clips_y , int w , int h )
{
GLuint textureID = 0;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
int Mode = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, Mode, get->w, get->h, 0, Mode, GL_UNSIGNED_BYTE, get->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, textureID);
int cx = clips_x / get->w;
int cy = clips_y / get->h;
int cw = clips_x+w / get->w;
int ch = clips_y+h / get->h;
glBegin(GL_QUADS);
glTexCoord2f(cx, cy);
glVertex2f(x, y);
glTexCoord2f(cx+cw, cy);
glVertex2f(x+get->w, y);
glTexCoord2f(cx+cw, cy+ch);
glVertex2f(x+get->w, y+get->h);
glTexCoord2f(cx, cy+ch);
glVertex2f(x, y+get->h);
glEnd();
glDeleteTextures(1, &textureID);
glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
}
...But all I see when I run it is a black screen with a few glitchy rectangles flying by diagonally, and the program runs at 4 frames per second.
I will not lie, I have absolutely no idea what I'm actually doing, I just want an easy and fast way to have hardware acceleration without having to change too much of my code. I do want to learn how to properly use OpenGL eventually, but not before I at least finish my first game.
If someone can offer some advice on what I'm doing wrong, I would really appreciate it. :-)
Thank you in advance. :-)