Here's some code :
Rendering system stuff
Code: Select all
RenderSystem::RenderSystem()
{
PlD = new Vector2( 20, 40 );// Size of the image
PlTex = this->LoadIMG( "yay.bmp" );
}
void RenderSystem::BindTexture( GLuint Texture )
{
glBindTexture(GL_TEXTURE_2D, Texture);
}
void RenderSystem::PrepareScene( )
{
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glColor3f( 1.0, 1.0, 1.0 );
glBegin (GL_QUADS);
}
void RenderSystem::RenderScene( Rect *POS, Rect *SRC )
{
glTexCoord2f( SRC->x / SRC->w, SRC->y / SRC->h);
glVertex2f(POS->x,POS->y);
glTexCoord2f( ( SRC->x + SRC->w ) / SRC->w, SRC->y / SRC->h);
glVertex2f(POS->x + POS->w, POS->y);
glTexCoord2f( ( SRC->x + SRC->w ) / SRC->w, ( SRC->y + SRC->h ) / SRC->h);
glVertex2f(POS->x + POS->w, POS->y + POS->h);
glTexCoord2f( SRC->x / SRC->w, ( SRC->y + SRC->h ) / SRC->h);
glVertex2f(POS->x, POS->y + POS->h);
}
void RenderSystem::EndScene( )
{
glEnd();
glPopMatrix();
}
GLuint RenderSystem::LoadIMG( std::string Name )
{
glEnable(GL_TEXTURE_2D);
SDL_Surface *Surface = NULL;
GLenum texture_format = NULL;
GLuint tex = 0;
GLint nOfColors = 0;
if ( (Surface = IMG_Load(Name.c_str())) ) {
if ( !isPowerOfTwo(Surface->w) ) {
printf("image.bmp's width is not a power of 2\n");
}
if ( !isPowerOfTwo(Surface->h) ) {
printf("image.bmp's height is not a power of 2\n");
}
nOfColors = Surface->format->BytesPerPixel;
if (nOfColors == 4) // contains an alpha channel
{
if (Surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (Surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glEnable(GL_TEXTURE_2D);
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, Surface->w, Surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, Surface->pixels );
}
else
{
std::cout<<"\nNOT LOADED\n";
}
SDL_FreeSurface(Surface);
return tex;
}
Code: Select all
Sprite::Sprite( GLuint Tex, Vector2 *Dims, Vector2 *SoC )
{
M_Tex = Tex; // texture
M_Dims = Dims; // dimensions of my texture
M_SoC = SoC; // Size of one clip
Box = new Rect( 0.0, 0.0, M_SoC->x, M_SoC->y );
}
void Sprite::Draw( int Part )
{
AssetManager::GetRSInstance()->BindTexture( M_Tex );
CBox = new Rect( Part * M_SoC->x, M_SoC->y, Part * M_SoC->x + M_SoC->x, M_SoC->y );
AssetManager::GetRSInstance()->PrepareScene();
AssetManager::GetRSInstance()->RenderScene( Box, CBox );
AssetManager::GetRSInstance()->EndScene();
}
Player
Code: Select all
Player::Player()
{
RS = AssetManager::GetRSInstance();// RenderSystem
...
m_SoC = new Vector2( 16, 16 ); // Size of one Clip
SPR = new Sprite( RS->PlTex, RS->PlD, m_SoC );
...
}
void Player::Show(SDL_Rect camera,int MAX_X,int MAX_Y,int LAYER,int xOff,int yOff)
{
..
SPR->Draw(0);
..
}