Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Petty little thing about TGA loader source.....

This topic is 5561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is just a note to NeHe about a few lines in the TGA loader source that have been nagging at me for awhile now.
texture->width  = tga.header[1] * 256 + tga.header[0];
texture->height = tga.header[3] * 256 + tga.header[2];
It's bad form to do this arithmetically IMHO. You can either do it logically, or use a typecast on the pointer, and rely on the endian order to do the byteswap for you. For example:
//Obtaining value using logical shift to promote second byte,

//and binary OR to combine with first byte:

texture->width  = (tga.header[1] << 8) | tga.header[0];
texture->height = (tga.header[3] << 8) | tga.header[2];

//Obtaining value using typecast on pointer, and relying

//on little endian byte order to perform byteswap.

texture->width  = *((short*)&(tga.header[0]));
texture->height = *((short*)&(tga.header[2]));
The second method is rather experimental for me, and I don't know if it'd be considered messy or bad form by more experienced programmers, but the logical method shown in the first example is a much better way to obtain the values than using multpilcation and addition IMO, and even though speed is not any real issue in this perticular instance, I think it would be significantly faster if the two were compared. Note that this is not meant as criticism, merely as a suggestion for improvement. EDIT: This damn thing keeps screwing up my source tags. [edited by - Nemesis2k2 on September 23, 2003 8:28:32 PM]

Share this post

Link to post
Share on other sites
Actually, NeHe''s method, unlike your second one, is cross-platform, since it doesn''t assume anything about the endianness of the target platform.

How appropriate. You fight like a cow.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!