PDA

View Full Version : spritesheets



ragzzy-R
04-07-2013, 07:40 PM
Hi,
Im writing a small 2D engine(well lets not say engine maybe a bunch of basic functions).
Im having some problem loading spritesheets.

first what i did was i loaded images in bmp format and textured it into quads with specified size.It was perfectly working.But then I wanted to load other formats too like png and jpeg(png mostly)so I searched the internet and found SDL_Image.and successfully loaded the png.but when I bind the texture to quad.it doesn't draw the image rather it just draw something like madman scratching with chalk on a black board.

so my questions are:

1) is there a specific way to bind textures with formats other than bmp?
2)also is there any other better way to draw sprites or images for a 2D game? other than binding them to quads??

thanks in advance!! please help me!!

ragzzy-R
04-07-2013, 08:09 PM
i changed the internal format of glTextureImage2D to to RGBA and now the image is rendering but the alpha is not present.it is rendered like first the background map is rendered.then when the character is rendered it is rendered within a black rectangle. its like instead of alpha background black background is rendered.

mobeen
04-07-2013, 10:38 PM
Hi,
Just to be sure, have you enabled alpha blending ? Once again it is always helpful to show us your code on how you are rendering the objects.


glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//draw your transparent quads here
glDisable(GL_BLEND);

ragzzy-R
04-07-2013, 11:01 PM
actually i dont need to draw an entire quad transparent.let me tell it clearly first.

1)I have a blue background PNG.which i draw first.No transperancy nothing.
2)then i draw an image with buildings in that image except the buildings rest are transparent.(understood).
3)now the scene should render something like blue background beyond buildings.


I'll post the code without blending enabled.cuz i am not sure where to add the blending code.


this is my main file.


#include "untitledGame.h"
#include "GameContainer.h"
#include "AppGame.h"



untitledGame::untitledGame(void)
{
}

void untitledGame::init() {
ss.loadImage("untitled.png");
//man.loadImage("man.png");
map.loadImage("map.png");

}

void untitledGame::update() {

}

void untitledGame::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

ss.drawImage(0,0,800,600);
map.drawImage(0,0,800,600);
SDL_GL_SwapBuffers();
}


untitledGame::~untitledGame(void)
{
}


int main(int argc,char** argv) {

untitledGame ug;
AppGame apg;
apg.initializeEverything(800,600,false);
return apg.startGame(ug);
}


this is loadImage and drawImage.basically they are just making the texture and binding them to quads.


#include "Images.h"



Images::Images(void)
{
}

int Images::loadImage(const char* filename) {
glBindTexture( GL_TEXTURE_2D, texture[0] );
SDL_Surface *TextureImage[1];
if ( ( TextureImage[0] = IMG_Load(filename ) ) )
{


glGenTextures( 1, &texture[0] );


glBindTexture( GL_TEXTURE_2D, texture[0] );


glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[0]->w,
TextureImage[0]->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, TextureImage[0]->pixels );


glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}


if ( TextureImage[0] )
SDL_FreeSurface( TextureImage[0] );

return 0;
}
int Images::drawImage(float x,float y,int width,int height) {
glBindTexture(GL_TEXTURE_2D,texture[0]);
glBegin(GL_QUADS);

glTexCoord2f( 0.0f, 1.0f ); glVertex3f( x, y, 0 );
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( width, y, 0 );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( width,height, 0 );
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( x,height, 0 );

glEnd();
return 0;
}
Images::~Images(void)
{
}