Hello guys! I am not doing fanzy 3d stuff.. I am trying to draw a simple quad with a texture using SDL + OpenGl!
The problem is that my quad is white.. but I should have the texture on it! uh? :|
I don't get error messages when I load the SDL_Surface!
Thank you very much! :)
Here is the source code:
main.cpp
#include "game_header.hpp"
#ifdef _WIN32
#undef main
#endif
LogFile LOG("logfile.log");
int main(int argc, char **argv)
{
SDL_Surface *screen;
bool running(true);
init_SDL_OGL( screen, 640, 480 );
GL_Texture* texture = new GL_Texture( "galaxien.bmp" );
Quad quad( texture );
SDL_Event event;
while( running == true )
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
running = false;
break;
case SDL_QUIT:
running = false;
break;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
quad.draw( 300, 300 );
SDL_GL_SwapBuffers();
}
delete texture;
return 0;
}
game_header.hpp
#include "eng_header.hpp"
eng_header.hpp
#include <SDL.h>
#include <SDL_opengl.h>
#include "eng_logfile.hpp"
#include "eng_init.hpp"
#include "eng_texture.hpp"
#include "eng_quad.hpp"
initSDL_OGL.hpp
#ifndef __ENG_INIT_HPP__
#define __ENG_INIT_HPP__
bool init_SDL_OGL( SDL_Surface* screen, const int w, const int h );
#endif
initSDL_OGL.cpp
#include "eng_header.hpp"
bool init_SDL_OGL( SDL_Surface* screen, const int w, const int h )
{
// SDL:
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
LOG << "Could not init SDL" << ENDL;
exit( 1 );
}
atexit( SDL_Quit );
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
screen = SDL_SetVideoMode( w, h, 0, SDL_OPENGL);
if ( !screen )
{
LOG << "Couldn't set video mode!" << ENDL;
exit(1);
}
// OGL:
glViewport(0, 0, screen->w, screen->h);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
return true;
}
eng_texture.hpp
#ifndef __ENG_TEXTURE_HPP__
#define __ENG_TEXTURE_HPP__
class GL_Texture
{
public:
GL_Texture( void );
GL_Texture( const char* filename );
~GL_Texture( void );
bool init( const char* filename );
bool initTexture( void );
int getW( void ) const{ return w; }
int getH( void ) const{ return h; }
unsigned getTexture( void ) const{ return texture; }
private:
SDL_Surface* sprite;
unsigned texture;
int w, h;
};
#endif
eng_texture.cpp
#include "eng_header.hpp"
GL_Texture::GL_Texture( void )
:w(0), h(0), sprite( NULL )
{
}
GL_Texture::GL_Texture( const char* filename )
:w(0), h(0), sprite( NULL )
{
init( filename );
initTexture();
}
// inits the SDl-surface
bool GL_Texture::init( const char* filename )
{
if( sprite != NULL )
{
SDL_FreeSurface( sprite );
sprite = NULL;
}
// Load the BMP file into a surface
sprite = SDL_LoadBMP( filename );
if ( sprite == NULL )
{
LOG << "Couldn't load " << filename << " -- " << ENDL;
return false;
}
if( SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(sprite->format, 255, 0, 255)) < 0)
{
LOG << "Couldn't set the ColorKey for " << filename << " -- " << ENDL;
return false;
}
w = sprite->w;
h = sprite->h;
return true;
}
bool GL_Texture::initTexture( void )
{
SDL_Surface* conv;
// convert to 32-bit RGBA format
conv = SDL_CreateRGBSurface( SDL_SWSURFACE, sprite->w, sprite->h, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
SDL_BlitSurface( sprite, 0, conv, 0);
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NICEST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NICEST );
glPixelStorei( GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel );
glTexImage2D( GL_TEXTURE_2D, 0, 3, conv->w, conv->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
SDL_FreeSurface(conv);
}
GL_Texture::~GL_Texture( void )
{
if( sprite )
{
SDL_FreeSurface( sprite );
sprite = NULL;
}
}
quad.hpp
#ifndef __ENG_QUAD_HPP__
#define __ENG_QUAD_HPP__
class Quad
{
public:
Quad( GL_Texture* const texture );
void draw( const int x, const int y );
private:
GL_Texture* texture;
};
#endif
quad.cpp
#include "eng_header.hpp"
Quad::Quad( GL_Texture* const texture2 )
:texture( texture2 )
{
}
void Quad::draw( const int x, const int y )
{
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture->getTexture() );
// Don't use texture-w / h later
const int t_w = texture->getW();
const int t_h = texture->getH();
glBegin( GL_QUADS );
glTexCoord2f( 0, 0 ); glVertex2i( x, y ); // x -- y
glTexCoord2f( 1, 0 ); glVertex2i( x + t_w, y ); // x+w -- y
glTexCoord2f( 1, 1 ); glVertex2i( x + t_w, y + t_h ); // x+w -- y+h
glTexCoord2f( 0, 1 ); glVertex2i( x, y + t_h ); // x -- y+h
glEnd();
}
[Edited by - rakoon2 on September 3, 2004 8:25:12 PM]