Archived

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

PlagueViper

How to take 32x32 chunks from a 512x512 BMP

Recommended Posts

My tile-based game now has 32x32 px bmps for each tile. It would be more effective from me to make like a 512x512 pic with lots of 32x32 tiles. My Q is, how would I extract these from the big file.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Draw it out on paper and it might be easier to see.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

  
glBindTexture(GL_TEXTURE_2D, MyBigTexture);

const float tilesize=1/32.0;

glBegin(GL_QUADS);
glTexCoord2f(tilesize*tileID,tilesize*tileid); glVertex3f(0,0,0);
glTexCoord2f(tilesize*tileID+tilesize,tilesize*tileid); glVertex3f(0,0,0);
glTexCoord2f(tilesize*tileID+tilesize,tilesize*tileid+tilesize); glVertex3f(0,0,0);
glTexCoord2f(tilesize*tileID,tilesize*tileid+tilesize); glVertex3f(0,0,0);
glEnd();


Learn the basics of texturing.

Share this post


Link to post
Share on other sites
You can calculate the two-dimensional position in a 1 dimensional array, at (x,y) by saying array[y*width+x]

Here is some pseudocode. It's C code but I call it pseudocode because I have't tested it and its a good bet that there are some errors and it won't compile, but I think it serves to illistrate the solution to your problem.
(note: this code wouldn't work if the image doesn't perfectly divide with the given tilesize.)

[pseudocode]



#define TILESIZE 32;
#define TILEAREA (TILESIZE*TILESIZE)

int i, x, y, row, n_tiles, n_tiles_x, n_tiles_y;
pixel **tiles, *src, *dst;

n_tiles_x = width / TILESIZE;
n_tiles_y = height / TILESIZE;
n_tiles = n_tiles_x * n_tiles_y;
tiles = (pixel**)malloc(n_tiles * sizeof(pixel*));

for(i = 0; i < n_tiles; n++)
{
tiles[ i ] = (pixel*)malloc(TILEAREA * sizeof(pixel));
}

for(y = 0; y < n_tiles_y; y++)
{
for(x = 0; x < n_tiles_x; x++)
{
// get a pointer to the tile we need to copy to
dst = tiles[y * n_tiles_x + x];
// figure out where we start copying from the big image
// "data" is the pointer to the "big image"
src = data[y * n_tiles_x * TILEAREA + x * TILESIZE];
// copy the tile from the large image to the small, row by row
for(row = 0; row < TILESIZE; row++)
{
memcpy(dst, src, TILESIZE*sizeof(pixe));
// go to the next row of the big image
src += width;
// go to the next row of the tile
dst += TILESIZE;
}
}
}

[/pseudocode]

Now the "tiles" array would have pointers to each of the individual tiles.
There are faster ways to do this i'm sure, but I think this is good for starters, because its easy to understand (imho, sorry if its not )

I hope that helps.
good luck!

EDIT: As other people have pointed out, if you are using a 3d API for your game, simply changing the texcoords is best.

EDIT2: Doh, obviously you are using a 3d API, this is the opengl forum. That's what I get for browsing the "active topics" page
If you ever do software rendering, my post may still help though.
Good luck again!

[edited by - Melekor on May 26, 2003 8:44:35 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Raduprv
Not that I want to be mean, but not figuring that by yourself, even after being given some pointers is lazyness.

<a href="http://hme.sf.net" target=new>Height Map Editor</a> | <a href="http://www.eternal-lands.com" target=new>Eternal Lands</a> | <a href="http://fud.sf.net">Fast User Directory</a>

<SPAN CLASS=editedby>[edited by - Raduprv on May 26, 2003 8:59:10 PM]</SPAN>

<SPAN CLASS=editedby>[edited by - Raduprv on May 26, 2003 8:59:39 PM]</SPAN>



Oh, that''s not mean. *sigh* I agree, though.

ph34r the anon''s! ^_^

Share this post


Link to post
Share on other sites