splitting up a texture

Started by
1 comment, last by petrusss 20 years, 5 months ago
Hi! I''m making a tile-based game, and I want to know how I easiest split up a texture (the tileset) into small chunks (tiles, 32x32). I''ve come up with one idea, but it''s kinda weird; mapping the whole tileset to each tile when drawing, but with S and T values deppending on where the tile''s position in the tileset is. PS: I''m using OpenGL.
Wehhhhhhhhhhooooooooo!!!!!!!
Advertisement
That''s actually not as wierd an idea as you might think. I''ve used that method for all sorts of things, from bitmapped fonts to sprites and tiles.

Josh
vertexnormal AT linuxmail DOT org

Check out Golem: Lands of Shadow, an isometrically rendered hack-and-slash inspired equally by Nethack and Diablo.
Thats how I do it, say you have a texture that is a power of two (64x64,128x128,256x256,etc) and it is made up of 32x32 tile images, taking into consideration that s and t can only have values between 0.0f and 1.0f, you need to do quite some work.

First you need to find out how many tiles are inside your texture, for this you need to find how many tiles wide the texture is, and then raise it to the power of 2 so:
TileWidth = ImageWidth/32;TolalTileCount = TileWidth^2;  

Now you need to scale the value of TileWidth to a 0.0f to 1.0f range and find out the Texel lenght:
floatTileWidth = float(1.0f/TileWidth);floatTexelLenght = float(1.0/ImageWidth);  

Now that you have that, how do you find the 4 values that make the tile?
s1 = (TileWanted%TileWidth)*floatTileWidtht1 = (TileWanted/TileWidth)*floatTileWidths2 = (TileWanted%TileWidth)*floatTileWidtht2 = ((TileWanted/TileWidth)*floatTileWidth)+(floatTileWidth-floatTexelLenght)s3 = ((TileWanted%TileWidth)*floatTileWidth)+(floatTileWidth-floatTexelLenght)t3 = ((TileWanted/TileWidth)*floatTileWidth)+(floatTileWidth-floatTexelLenght)s4 = ((TileWanted%TileWidth)*floatTileWidth)+(floatTileWidth-floatTexelLenght)t4 = (TileWanted/TileWidth)*floatTileWidth  


You MUST check that TileWanted is NOT greater than TotalTiles and you have to substract TexelWidth so the tile doesnt take the next tile's first line.

I dont have my code here, so ALL that is from memory, should get you started tho.
Oh yeah! and TileWanted first posible value is of course 0, not 1.

Proud aedGUI developer.

[edited by - Kwizatz on October 22, 2003 1:50:20 PM]

This topic is closed to new replies.

Advertisement