Skip to main content
gamedev.net
🔒 Locked

tga/bitmap loading

Started by ryacketta 5 replies 850+ views
Original Post
ryacketta
ryacketta
Folks, I have a minor question compared to the others I have read around town I have a tga/bmp/jpg (what have you) file that has 15 images (0 - 14 in array speak) that I would like to load in for a tiled game. I thought it would be best to have 1 file vice 15 seperate files to load, my question comes in the loading code. How would I load the tga/bmp/jpg (what have you) file so that I could access each image (0 - 14)? I would like to beable to access each image as thus texture[0] thru texture[14] this would corrolate to the tile to be used. Tile 0 would be water, 1 sand, 2 grass etc.. Sorry if this seems to be such a simplistic question for some, I have just begund my DX9 adventures. Regards and Happy Holidays! -Ron
jon723
jon723
Well, in the function that you have to load the images have it take an array of strings, where the name of the image is an element in the array. That way you can go through each name and load them all in one shot
www.lefthandinteractive.net
KabOm
KabOm
Hi!
ok, I''m no programming guru, but I''ll tell you what I think sounds good:

I think I would load the image to an offscreen buffer with the size of your image. I suppose that the tiles are the same size. So I would make some constants for this tileset, something like this:


  
// GLOBALS for an animation consisting of 33 tiles

int TILE_WIDTH = 92; //width of each tile

int TILE_HEIGHT = 102; // height of each tile

int TILE_X_DEST = 94; // the X coordinate where you want to put it on screen

int TILE_Y_DEST = 378; // the X coordinate where you want to put it on screen

int TILE_FRAMES = 33;
int TILE_MAX_FPS = 50;


and in your case you would probably want to make some constants for the different materials:


  
int GRASS = 0; //if grass is the first tile in your bmp

int WATER = 1; //if water is the second tile in your bmp

int ROCK = 2; //if rock is the third tile in your bmp



then when you would want to load let say WATER from your tile you would only have to do like this:

  
RECT imgSrc, imgDest;
//Setting the rectangular area that will be copied from a surface

SetRect(&imgSrc, (TILE_WIDTH * WATER), 0, (TILE_WIDTH * WATER + TILE_WIDTH), TILE_HEIGHT);
//Setting the rectangular area where the copied surface area specified above will be put

SetRect(&imgSrc, TILE_X_DEST, TILE_Y_DEST, (TILE_X_DEST + TILE_WIDTH), (TILE_Y_DEST + TILE_HEIGHT));

// copying the specified area from lpddsOffscreenBuffer to the specified area in lpddsPrimary surface

lpddsPrimary->Blt(&imgDest, lpddsOffscreenBuffer, &imgSrc, DDBLT_WAIT, NULL);


And the TILE_X_DEST and TILE_Y_DEST I would change during each loop when loading a map.

I made a function for loading an animation according to the way I tried to describe above:


  
// this function is recieving the surface you are loading a tile from, the surface you are blitting it to

// it also recieves the width/height of the tiles you are loading, the desired x/y position, fps lock, and max numbers of frames of the animation

// this is why I like to use many globals for the tileset

bool BlitAnimation(LPDIRECTDRAWSURFACE7 lpddsSrc, LPDIRECTDRAWSURFACE7 lpddsDest, int nWidth, int nHeight, int nXDest, int nYDest, int nNumFrames, int nFpsCap, int nYCoordStart)
{
// some static variables to track the old tile, frame fps... or what so ever...

static int nCurrentPic, lastFrameAnimation, fpsCounter;
RECT destImage, srcImage;

// a simple fps lock

if((GetTickCount() - fpsCounter) < nFpsCap)
{
return(TRUE);
}

if(!nCurrentPic)
{
nCurrentPic = 0;
}

// loads depending on nCurrentPi a different tile is loaded... this is where the globals for WATER, GRASS, ROCK etc. would come handy

SetRect(&srcImage, (nWidth*nCurrentPic), nYCoordStart, (nWidth*nCurrentPic + nWidth), (nYCoordStart + nHeight) );
SetRect(&destImage, nXDest, nYDest, (nXDest + nWidth), (nYDest + nHeight));
lpddsDest->Blt(&destImage, lpddsSrc, &srcImage, DDBLT_WAIT, NULL);

fpsCounter = GetTickCount();
nCurrentPic++;

// if the current tile shown on screen is greater than the max number of tiles, then we start over again

if(nCurrentPic > nNumFrames)
nCurrentPic = 0;

return(TRUE);
}

The function above I call every frame... in your case i think I would have a function that would be called every time in your loop for loading a map. Something like this:


  
for(int y=0; y<=MAP_HEIGHT_IN_TILES; y++)
{
for(int x=0; x<=MAP_WIDTH_IN_TILES; x++)
{
//depending on what tilesize you would use... set the TILE_X_DEST and TILE_Y_DEST

//if tilesize is 32*32 pixels


TILE_X_DEST = (TILE_WIDTH * x);
TILE_Y_DEST = (TILE_HEIGHT * y);

// now make a call for a function similar to my animation function

LoadTextureTile(.....);
}
}


So, anyway... I load the tiles to the offscreen buffer and when I want to use them for something, (in my case it is an animation), I would just call for the function that loads the tiles from the offscreen buffer and blits them to the primarySurface or whatever.

And in your case the map loading i told you would automaticly know what x/y position the tiles should be put on

Only thing you would need would be a map file with an array that describes what material you want and where.

Oh well... I did my best, so don''t flame me now


KaBooOM!
ryacketta
ryacketta
quote:
Original post by jon723
Well, in the function that you have to load the images have it take an array of strings, where the name of the image is an element in the array. That way you can go through each name and load them all in one shot


Would that not be more resource intensive? 15 load calls etc?
would it not be more efficient to load a single file and in some way (mentioned by the other poster?) to store the images for usage later.

-Ron
Smurfwow
Smurfwow
isn''t this what texture coordinates are for?

I don''t understand the theory behind generating tex coordinates... but im sure this is why they exist.
Pipo DeClown
Pipo DeClown
enum Type{ Rock=0, Water, Grass };

Water is now 1, Grass is now 2.

.lick
Pipo DeClown
Pipo DeClown
btw, I load them in my engine like:

0 1 2 3
4 5 6 7

you specify the num_X 4 and num_Y 2. when you have a bigger file:

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14


num_X 5 and num_Y 3

then you can do the math in your engine like i do...
do you want my mapengine?



.lick

[edited by - Pipo DeClown on January 1, 2003 8:57:20 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.