I am using 32bit PNG texture image with transparency. The rest of the code works other then the texture appearing white.
this is my main code
Code:
#include "EzGame.h"
enum MOVEMENT { M_UP, M_DOWN , M_LEFT , M_RIGHT};
struct Link
{
MOVEMENT curMove;
int noUp , noDown , noLeft , noRight;
SDL_Rect curPos;
Link & operator=(const Link & rhs )
{
if (this != &rhs)
{
this->curMove = rhs.curMove;
this->curPos.h = rhs.curPos.h;
this->curPos.w = rhs.curPos.w;
this->curPos.x = rhs.curPos.x;
this->curPos.y = rhs.curPos.y;
this->noDown = rhs.noDown;
this->noLeft = rhs.noLeft;
this->noRight = rhs.noRight;
this->noUp = rhs.noUp;
}
return *this;
}
};
class Test_Game : public gamestate
{
public:
Test_Game() { initState = false;}
void update(Input_KeyBoard & kb, Input_Mouse & ms, gstate & Curstate )
{
if(!initState)
{
Link nPerson;
nPerson.curPos.x = 128;
nPerson.curPos.y = 128;
nPerson.curPos.h = 16;
nPerson.curPos.w = 16;
nPerson.curMove = M_DOWN;
nPerson.noUp = nPerson.noRight = nPerson.noDown = nPerson.noLeft = 0;
Data = nPerson;
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, 256, 256, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
initState = true;
}
if(!Data.empty())
{
Link Temp = boost::any_cast<Link>(Data);
if( kb.keyDown(SDLK_UP) )
{
Temp.curPos.y -= 5;
Temp.curMove = M_UP;
Temp.noUp += 1;
Temp.noDown = Temp.noLeft = Temp.noRight = 0;
}
if( kb.keyDown(SDLK_DOWN) )
{
Temp.curPos.y += 5;
Temp.curMove = M_DOWN;
Temp.noDown += 1;
Temp.noUp = Temp.noLeft = Temp.noRight = 0;
}
if( kb.keyDown(SDLK_LEFT) )
{
Temp.curPos.x -= 5;
Temp.curMove = M_LEFT;
Temp.noLeft += 1;
Temp.noDown = Temp.noUp = Temp.noRight = 0;
}
if( kb.keyDown(SDLK_RIGHT) )
{
Temp.curPos.x += 5;
Temp.curMove = M_RIGHT;
Temp.noRight += 1;
Temp.noDown = Temp.noLeft = Temp.noUp = 0;
}
Data = Temp;
}
if( kb.keyDown(SDLK_ESCAPE))
{
Curstate = gExit;
}
}
void render(EzDraw & Ez )
{
glClearColor(0, 0, 0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
Ez.Begin(0, GL_TEXTURE_2D, false);
SDL_Rect Rect;
Rect.h = Rect.w = 256; Rect.x = 0; Rect.y = 0;
Ez.DrawImage(Rect, 0.0 , 0.0 , 1.0);
Ez.End(GL_TEXTURE_2D);
Ez.Begin(1, GL_TEXTURE_2D|GL_BLEND, true);
int Which;
if(!Data.empty())
{
Link Temp = boost::any_cast<Link>(Data);
switch(Temp.curMove)
{
case M_LEFT:
Which = Temp.noLeft % 2;
if(Which == 0)
{
Ez.DrawImage(Temp.curPos, 0 , 0.03125 , .25 );
}
else
{
Ez.DrawImage(Temp.curPos, .25 , 0.03125 , .25 );
}
break;
case M_DOWN:
Which = Temp.noDown % 2;
if(Which == 0)
{
Ez.DrawImage(Temp.curPos, 0.484375 , 0.03125 , .25 );
}
else
{
Ez.DrawImage(Temp.curPos, 0.734375 , 0.03125 , .25 );
}
break;
case M_UP:
Which = Temp.noUp % 2;
if(Which == 0)
{
Ez.DrawImage(Temp.curPos, 0 , 0.359375 , .25 );
}
else
{
Ez.DrawImage(Temp.curPos, .25 , 0.359375 , .25 );
}
break;
case M_RIGHT:
Which = Temp.noRight % 2;
if(Which == 0)
{
Ez.DrawImage(Temp.curPos, .5 , 0.359375 , .25 );
}
else
{
Ez.DrawImage(Temp.curPos, 0.734375 , 0.359375 , .25 );
}
break;
}
}
Ez.End(GL_TEXTURE_2D|GL_BLEND );
}
};
bool Init_Game(void)
{
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}
if( SDL_SetVideoMode( 256 , 256 , 32 , SDL_OPENGL ) == NULL )
{
return false;
}
//Set caption
SDL_WM_SetCaption( "Test Game", NULL );
return true;
}
int main(int argc, char *argv[])
{
if(Init_Game())
{
cGame Tester(gWorld);
Tester._insert(gWorld, new Test_Game );
EzDraw TestDraw(2);
TestDraw.getImage("Scene.png");
TestDraw.getImage("Link.png");
Tester.run(TestDraw);
}
//Quit SDL
SDL_Quit();
return 0;
}
this is the definitions
Code:
#include "EzGame.h"
#include <cmath>
bool EzDraw::Begin(unsigned int select, GLenum flags , bool alpha )
{
if( select < Inc)
{
glEnable(flags);
glBindTexture(GL_TEXTURE_2D, textures[select]);
if(!alpha)
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glBegin( GL_QUADS );
OkDraw = true;
return true;
}
return false;
}
void EzDraw::End( GLenum flags )
{
if(OkDraw)
{
glEnd();
glDisable(flags);
OkDraw = false;
}
}
EzDraw::EzDraw(unsigned int N)
{
Inc = 0;
textures = new GLuint[ N ];
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures(N, &textures[ 0 ] );
}
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
static const Uint32 rmask = 0x000000FF;
static const Uint32 bmask = 0x0000FF00;
static const Uint32 gmask = 0x00FF0000;
static const Uint32 amask = 0xFF000000;
#else
static const Uint32 rmask = 0xFF000000;
static const Uint32 bmask = 0x00FF0000;
static const Uint32 gmask = 0x0000FF00;
static const Uint32 amask = 0x000000FF;
#endif
void EzDraw::getImage(const char * filename)
{
SDL_Surface * surface = IMG_Load(filename);
if(surface == NULL)
{
// could not get filename
return;
}
SDL_PixelFormat *format = surface->format;
Uint32 widthPow = surface->w;
Uint32 heightPow = surface->h;
// Create new empty surface.
SDL_Surface* newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA,
widthPow, heightPow, 32,
rmask, bmask, gmask, amask );
// Fill sprite with alpha.
unsigned int alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.h = heightPow;
rect.w = widthPow;
SDL_FillRect( newSurface, &rect, alpha);
surface->flags &= !SDL_SRCALPHA;
SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
// Copy image data to our new surface.
SDL_BlitSurface( surface, 0, newSurface, 0 );
// Bind the texture.
glBindTexture(GL_TEXTURE_2D, textures[Inc]);
// Convert surface to Open GL format.
gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
widthPow, heightPow, GL_RGBA,GL_UNSIGNED_BYTE,
newSurface->pixels);
// Free our temporary SDL buffers.
SDL_FreeSurface( surface );
SDL_FreeSurface( newSurface );
++Inc;
}
void EzDraw::DrawImage( SDL_Rect & rect, float sx , float sy , float sL )
{
float dx = (float)rect.x;
float dy = (float)rect.y;
glTexCoord2f(sx, sy);
glVertex2f(dx, dy);
glTexCoord2d(sx + sL, sy);
glVertex2f(dx + rect.w, dy);
glTexCoord2d(sx + sL, sy + sL);
glVertex2f(dx + rect.w, dy + rect.h);
glTexCoord2d(sx , sy + sL);
glVertex2f(dx, dy + rect.h);
}
Input_KeyBoard::Input_KeyBoard(void)
{
Uint32 iKey;
for( iKey = 0x00; iKey < 0xB1; iKey++ )
{
Keys_Pressed[ iKey ] = false;
}
}
void Input_KeyBoard::listen(SDL_Event & event )
{
switch(event.type)
{
case SDL_KEYDOWN:
Keys_Pressed[event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
Keys_Pressed[event.key.keysym.sym] = false;
break;
}
}
bool Input_KeyBoard::keyDown( SDLKey sym )
{
return Keys_Pressed[sym];
}
Input_Mouse::Input_Mouse(void)
{
g_MouseClicked = false;
g_MouseX = 0;
g_MouseY = 0;
}
void Input_Mouse::listen(SDL_Event & event )
{
switch(event.type)
{
//Mouse gathering information
case SDL_MOUSEMOTION:
g_MouseX = event.motion.x;
g_MouseY = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
button = event.button.button;
g_MouseClicked = true;
break;
case SDL_MOUSEBUTTONUP:
g_MouseClicked = false;
break;
}
}
bool Input_Mouse::isclick()
{
return g_MouseClicked;
}
bool Input_Mouse::ishover(int & x, int & y, int & h, int & w )
{
if( (g_MouseX >= x ) && (g_MouseX <= x + w ) )
if( (g_MouseY >= y) && ( g_MouseY <= y+h ) )
return true;
return false;
}
void Input_Mouse::mousebutton(click_t button )
{
if( button & SDL_BUTTON(1) )
button = left_click;
if( button & SDL_BUTTON(3) )
button = right_click;
}
bool Input_Mouse::isclick(int & x, int & y, int & h, int & w )
{
if( ishover(x,y,h,w) )
if( isclick() )
return true;
return false;
}
void Input_Mouse::scroller( unsigned int & value)
{
if( button == 4 )
++value;
if( button == 5 )
--value;
}
cGame:: cGame(gstate gs)
{
Curstate = gs;
}
void cGame::_insert( gstate Index , gamestate * perm )
{
gsList[Index] = perm ;
}
#define TICKS_PER_SECOND 50
#define SKIP_TICKS 1000 / TICKS_PER_SECOND
#define MAX_FRAMESKIP 100
void cGame::run(EzDraw & Ez)
{
Uint32 next_game_tick = SDL_GetTicks();
int loops;
gamestate * gMode = gsList[Curstate];
gstate temp = Curstate;
while( Curstate != gExit )
{
loops = 0;
while( (SDL_GetTicks() > next_game_tick) && (loops < MAX_FRAMESKIP) )
{
while ( SDL_PollEvent(&gevent) )
{
kb.listen(gevent);
mc.listen(gevent);
}
gMode->update(kb , mc , temp);
if( Curstate != temp )
{
gworld = gMode->getWorld();
Curstate = temp;
gMode = gsList[Curstate];
gMode->_Setup(gworld);
}
next_game_tick += SKIP_TICKS;
loops++;
}
gMode->render(Ez);
SDL_GL_SwapBuffers();
}
}
sorry for the long post of source code, but what did I do wrong?