Jump to content
  • Advertisement
Sign in to follow this  

TGA files

This topic is 4089 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

In tutorial 32 it explains how to make TGA files. I couldn't follow this expination and was wondering if anyone could explain it a little better for me?

Share this post

Link to post
Share on other sites
This is a good explanation, very simple:

Share this post

Link to post
Share on other sites
Here's some C++ code to output a sample TGA with transparency, using the information on Paul Bourke's website:

#include <fstream>
using std::ofstream;
using std::endl;

#include <ios>
using std::ios_base;

int main(void)
// Assumes char is 1 byte, short is 2 bytes.
char temp_c = 0;
short temp_s = 0;
ofstream out("test.tga", ios_base::binary);

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // idlength
out.write(&temp_c, sizeof(temp_c)); // colourmaptype

temp_c = 2;
out.write(&temp_c, sizeof(temp_c)); // datatypecode (2 == uncompressed RGB(A) data)

temp_s = 0;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // colourmaporigin
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // colourmaplength

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // colourmapdepth

temp_s = 0;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // x_origin
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // y_origin

temp_s = 128;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // width (128 pixels wide)

temp_s = 64;
out.write(reinterpret_cast<const char *>(&temp_s), sizeof(temp_s)); // height (64 pixels tall)

temp_c = 32;
out.write(&temp_c, sizeof(temp_c)); // bitsperpixel (32 bits per pixel)

temp_c = 0;
out.write(&temp_c, sizeof(temp_c)); // imagedescriptor

unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 128;
unsigned char a = 0;

// output 128*64*4 bytes of data
for(short y = 0; y < 64; y++)
// For each line:
r += 2; // Increase red by 2
g = 0; // Reset green to 0
a = 255; // Reset alpha to 255 (fully opaque)

for(short x = 0; x < 128; x++)
// For each pixel in each line:
g++; // Increase green by 1
a--; // Decrease alpha by 1

out.write(reinterpret_cast<const char *>(&r), sizeof(r));
out.write(reinterpret_cast<const char *>(&g), sizeof(g));
out.write(reinterpret_cast<const char *>(&b), sizeof(b));
out.write(reinterpret_cast<const char *>(&a), sizeof(a));

return 0;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!