‘rta’ has the right answer: you need to surround the struct in the correct #pragma pack options. You also need to post in an on-topic forum, or just read the source of one of the 103 (last count) TGA readers/writers available through a Google search on the web.
oh god, dont start with the “this isn’t related” crap. See, by the time you get into the advanced stuff, you know how to work opengl, its just the theories and stuff. So, i think a topic on how to save TGA’s is appropiate.
Anyways, thanks, the header I was using was wrong or something.
Following everyone’s posts there are only a couple things I see as being wrong.
One (please correct me if I am wrong) I don’t believe TGA’s are in BGRA format. I thought they were actually RGBA. But I haven’t looked at my TGA loader in a really really long time. And I’m not home to take a look. either way the code should still work it will just look weird ( :
The second thing. I don’t believe that you should have to align the struct and or allocated image data. It would be nicer to do so but shouldn’t be necessary. I don’t see why fwrite would care either way. Performance is probably significantly better with aligned data.
I am impressed to see that you set image descriptor equal to 8 (for the 8 bits of alpha). That is good but you missed one thing that i don’t think you are aware of.
TGA’s are meant to be platform independant. The height and width are specified as low byte then high byte. I would change your code as follows.
/* CHANGE TO */
I don’t believe that you should have to align the struct
The alignment can cause severe problems. Memory access is faster on 4 byte boundary than on 2 and 1 byte boundary. A compiler can align the data members so that they are stored on 4 byte boundary. That means that a structure may be padded with extra bytes so that the elements in the structure can be accessed faster (at the cost of greater memory usage). Problem occurs when you try to write the structure directly to a file, passing a pointer to the structure and the size of the structure. First, the sizeof operator will return the size of the structure including the size of the pad bytes, and when you write to the file, these pad bytes are also written. You solve this by either save each datamember separately, or by forcing the compiler to align the structure to 1 byte boudary.