Loading TGA files in C++ for use with OpenGL

November 7, 2010

Among the many samples and examples in the leaked PS3 SDK there is a function for recording screenshots in tga format. After finding it I decided to simply rewrite it to essentially work in reverse so that I could load TGA files and use them as textures for PSGL games. It was simple to do and works beautifully on PC. But for some reason it fails to correctly read the width and height of the images when I run it on PS3. It seems to work fine if I hard code in what width and height to use but it refuses to correctly read that from the tga files despite it doing so on Windows. I’ve wasted away the last two days trying to get it to work and have failed to do so. Can anyone see anything wrong with the bellow source code that could explain the failure?


GLuint loadtga(char* filename,bool mipmap){

unsigned char header[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned char bpp = 32;
unsigned char id = 8;
unsigned short width;
unsigned short height;
unsigned char *pPixels = NULL;
FILE *fp = NULL;

fp = fopen(filename,"r");

fread(header,sizeof(unsigned char),12,fp);
fread(&width,sizeof(unsigned short),1,fp);
fread(&height,sizeof(unsigned short),1,fp);
fread(&bpp,sizeof(unsigned char),1,fp);
fread(&id,sizeof(unsigned char),1,fp);

pPixels = new unsigned char[width * height * 4];
fread(pPixels, 4, width * height, fp);
fclose(fp);

GLuint texName;
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texName);
glBindTexture(GL_TEXTURE_2D,texName);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
if(mipmap==0){
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if(mipmap==1){
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,width,height,
GL_BGRA,GL_UNSIGNED_BYTE,pPixels);
}
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,
0,GL_BGRA,GL_UNSIGNED_BYTE,pPixels);

delete pPixels;

return texName;

}

More then getting it to work I just really want to know WHY it isn’t working already.


Cubicle Shooter v0.4 and source code – PS3

October 31, 2010

Another updated version of Cubicle Shooter.

As an added bonus having this installed will allow the text (including your score and high score) to load and be correctly displayed on screen for Dont Get Crushed v0.1.

Notable changes:
Improved textures for enemies.
Enemies are now animated.
Enemies die when shot.
Enemies are infinitely re-spawning.
There is now a textured and deformable ground (this differs from the destructible walls and is instead more reminiscent of the ground deformation in fracture)
The debug text has been replaced with larger and more legible sprite based text
The rate of fire has been doubled
The maximum number of cuboids that walls can be sub divided into has been doubled
Game saving and loading has once again been updated to reflect the changes

Performance:
The games performance has gotten both better and worse. The load time is significantly longer (this is largely to blame on the textures) and the starting frame rate is lower (the ground layer is mostly to blame for this). But on the upside I have gotten things running more efficiently especially the destructible walls and the frame rate doesn’t take that harsh drop around the 2700 cuboid mark anymore. so you can break apart the walls more now before the frame rate becomes unbearable.

Controls:
Move – Left analog stick
Aim – Right analog stick
Shooter – R1
Jump – X
Save – Select
Load – Start

Download the PS3 version
Alternate links

Download the PS3 version’s Source Code
Alternate links


TheyDoNotDie R15

February 22, 2010

You can shoot again. If you shoot a zombie they die. If a zombie gets too far away they are repositioned closer by. The darkness around the border of the screen and the cloud shadows or whatever you want to call is back.When you get too close to a zombie the borders of the screen turn red and the camera zooms in. When you or a zombie is injured blood is dropped. I’ve worked back in on screen text but unlike with the older releases this time text is textured onto polygons so it scales perfectly with the resolution of the screen. I think that is pretty much everything that has changed. Still not as complete as the twelfth release but its getting there.

Download Links for Windows PC version


TheyDoNotDie R14 – Now with zombies and music.. Again!

February 14, 2010

Its still not as complete as R12 but I’m getting it there.

Rather then running in a 640×480 window it goes full screen now and uses whatever your desktop resolution is. I have the environment rendering properly (still no objects like crates and cars but the tile map is going fine). The roofs of buildings fade in and out of view like they did before. RekcahDam‘s music has made a return. Zombies are back (they are drawn on screen and move towards you). I also have the basics of the box2d stuff working so your movement, the movement of the zombies and all the collision detection (no more walking through walls) is physics based once again.

Without the darkness around the edges of the screen and the cloud shadows everything looks really bright and flat but despite its lack of completion there are already a few ways in which its looking better then R12. I redid some of the sprites (though they aren’t drastically different). I finished fixing the problems of the edges of the textures for map tiles so it shows the full tile right up to the edge without it bleeding over and incorrectly showing edges of unrelated tiles (nobody ever seemed to complain about it but before the switch there would for example at times have lines through roofs of buildings where you could see in when you shouldn’t have been able to). Also as a direct result of switching over to OpenGL there are no longer any problems of overlapping sprites rendering incorrectly (something that I spent forever trying to fix but never could before switching).

Download Links for Windows PC version


TheyDoNotDie R13 – Now more incomplete then ever!

February 2, 2010

Its a well established fact that DirectX users are always the first to get themselves killed and eaten by zombies.

So anyways I’m starting fresh and making the game with SDL and OpenGL. No more DarkGDK or DirectX.

Why this is a good thing:

I’ve never used OpenGL before and new things are interesting things which means that new things are good things. The more interested I am in a project and the more I feel like I’m learning or doing something new then the more likely I am to spend time on that project.

It will be portable. Because it used DirectX and even more so because it was using DirectX through DarkGDK the game was and always would be restricted to being a being a windows only game. That isn’t the case with SDL or OpenGL. Linux and Mac users rejoice. I could end up making it available on pretty much anything.

It will run faster. Its hard to judge by how much right now but DarkGDK isn’t exactly known for being speedy. Better performance means that it will run on even more lower end computer or alternatively will run the same on the same computers with even more zombies and even more stuff going on.

Why this a bad thing:

It will probably take a little time just to get back to the same level of semi complete that I was at previously. Nowhere near as long as it did the first time around though and I will at least be returning to working on it now which I wouldn’t be super interested in doing if I had to continue carrying the baggage of the way it was previously made.

Proof that I have returned to working on it:

Download links for PC version