Loading a graphic... long awaited!

You must understand the Game Editor concepts, before post here.

Loading a graphic... long awaited!

Postby Fuzzy » Mon Jul 02, 2007 4:05 am

Its pretty rough, but i felt if I didnt hurry up and post it I never would.

How to load a uncompressed 24 bit TGA file.
THis first part is the header. We wont bother with it in the simple loader. Its just so that you can expand your knowledge.
Code: Select all
#define BYTE unsigned char
#define WORD unsigned short int

struct TGAheader { // we are not using this. its only to show you how the header is set up.
    BYTE IDlen; // Leave this at zero... you dont need it.
    BYTE MapType; // We want true color(24+ bits) so set this to 0, as we dont have a palette image
    BYTE ImageType; // Always use number 2.
    WORD FirstCMapEntry; // We dont have a pallete, so its ignored. See Map Type. Zero
    WORD ColorMapLength; // Ignored along with FirstCMapEntry. Zero
    BYTE CMapEntrySize; // Also ignored.. No palette. Zero
    WORD ImageXOrigin; // 0,0 is upper left? then use zero here.
    WORD ImageYOrigin; // 0,0 is upper left? then use zero here.
    WORD ImageWidth; // If you dont know this....
    WORD ImageHeight; // then there is not point in explaining them
    BYTE BitsPerPixel; // 24 for 24 bit, 32 if you use Alpha channel.
    BYTE DescriptorBits; // Set at 32 for 24 bit, and 40 for 32 bit.(I dont think this really matters though)
                 };


This is the code portion we will use.
Code: Select all
#define BYTE unsigned char
#define WORD unsigned short int

BYTE red; BYTE green; BYTE blue; // temp color variables

WORD Height;
WORD Width;
WORD TMP;
char fp;

int CheckTGAheader()
{
    FILE *fp=fopen("C:\\Here\\Be\\Dragons.tga", "rb"); // open the file.
    if(fgetc(fp) != 0) return -1; // ID tag = bad
    if(fgetc(fp) != 0) return -1; // A palette? We dont like you.
    if(fgetc(fp) != 2) return -1; // Compression? No thanks.
    fseek(fp, 12, SEEK_SET); // go tot he twelfth position from the beginning.
    TMP = fgetc(fp) << 8; // read a BYTE into a temp variable push it over 8 bits
    Height = fgetc(fp)+TMP; // read a BYTE into Height and add the temp variable
    TMP = fgetc(fp) << 8; // read a BYTE into a temp variable push it over 8 bits...AGAIN
    Width = fgetc(fp)+TMP; // Read the height Byte and add TMP.
    fseek(fp, 17, SEEK_SET); // Zoom to the 17th byte from the beginning
    if(fgetc(fp) != 24) return -1; // Its not 24 bit...no good.
    fclose(fp); // Close the file.
    return 0; // Return a zero.. All is well.
}
 
char image[16][16*3]; // whatever size the image is.. one array per image.
// learn about malloc to do allocate arrays on the fly
int ReadTGA()
{
    int i = 0; int j = 0;
    FILE *fp=fopen("C:\\Here\\Be\\Dragons.tga", "rb"); // open the file again.
    fseek(fp, 18, SEEK_SET); // skip the header
    for(i = 0; i <Height; i++)
    {
        for(j = 0; j <Width; j++)
        {
            blue = fgetc(fp); // read off in reverse order
            green = fgetc(fp); // green is normal
            red = fgetc(fp); // remember that the stored order is bgr not rgb
            image[i][j] = red; // set the order to rgb
            image[i][j+1] = green; // green is center
            image[i][j+2] = blue; // last color
        }
    }
    fclose(fp); // close the file
    return 0; // all is well
 
}



Read it over. if you dont understand how it works, ask me here. it contains the concept of error checking, which is why you have the functions as ints and not void. make a int var, and in your code, go

int CheckOp = 0; // short for check operation

CheckOp = CheckTGAheader();

then you check the variable CheckOp. if CheckOp is NOT zero, something went wrong.

after that, you load the field
CheckOp = 0; // set it back to zero
CheckOp = ReadTGA(); // attempt to read the tga, returning an error code

and check it again.. if its not zero, you have a problem.
Last edited by Fuzzy on Mon Jul 02, 2007 5:51 pm, edited 1 time in total.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby Sgt. Sparky » Mon Jul 02, 2007 4:04 pm

Amazing! :D:D:D
you describe it well, I will try this out someday soon. :D :D :D :D :D :D :D :D :D
1+ point!
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby Game A Gogo » Tue Jul 03, 2007 1:20 am

umm, to tired to understand xD but here is a point my dear friend!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron