Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualServant of the Lord

Posted 17 August 2012 - 11:33 PM

A simple way to load tiles and maps:
Have a file that lists each tile image, or have a large tilesheet, and make each tile be represented by an number ("../tiles/floors/grass/grass17.png" == id '543').

Have a map with, say, 5 layers of 100 by 100 tiles. (When learning something, I like to use fixed amounts like '5 layers' or '100 by 100 tiles', and once I understand it, then I make it capable of handling any amount of layers and any amount of tiles).

Have your map file store each layer as 100 by 100 integers, using the ID of each tile ('543' for 'grass17.png'), instead of the tile itself.
It'll look something like this:
543 342 468 343 123 1072 ...
343 123 108 514 342 468  ...
54 514 342 468 123 1072 ...

But 100 by 100. You can make the saved file comma separated, or space separated or whatever you like.

When your game starts, load all the tile images into an array. When drawing your map, for each tile ID in the map, use that ID to find the tile image in the array and draw that. If you use the grass17.png a hundred times in one map, you should still only have one copy of the image that you loaded once, and just used the same image to draw each of the hundred grass tiles in the map.

 

When drawing a tile map, you can go like this:

array Layer = array of 100 x 100 integers [b][color=#008080](tile IDs)[/color][/b]
array Map = array of 5 Layers
Map myMap = load_map();
array tileImages = array of loaded images

for each layer in myMap:
	 for each row in layer:
		  for each column in row:
			   tileID = myMap[layer]->[row][column];
			   xPos = column * TILE_WIDTH;
			   yPos = row * TILE_HEIGHT;
			   draw_image(tileImages[tileID], draw at: xPos, yPos);

This is a simple way of doing things. Once you understand that, there are improvements you can make, such as: loading the images not at startup, but only when needed. Freeing the images that aren't needed. Saving images when changing maps if both maps use the same image. Any number of layers. Any size of maps. Breaking large maps into smaller chunks of maps, and only loading the chunks that are currently visible.

PS: Your English is quite good. Posted Image

#1Servant of the Lord

Posted 17 August 2012 - 11:32 PM

A simple way to load tiles and maps:
Have a file that lists each tile image, or have a large tilesheet, and make each tile be represented by an number ("../tiles/floors/grass/grass17.png" == id '543').

Have a map with, say, 5 layers of 100 by 100 tiles. (When learning something, I like to use fixed amounts like '5 layers' or '100 by 100 tiles', and once I understand it, then I make it capable of handling any amount of layers and any amount of tiles).

Have your map file store each layer as 100 by 100 integers, using the ID of each tile ('543' for 'grass17.png'), instead of the tile itself.
It'll look something like this:
543 342 468 343 123 1072 ...
343 123 108 514 342 468  ...
54 514 342 468 123 1072 ...

But 100 by 100. You can make the saved file comma separated, or space separated or whatever you like.

When your game starts, load all the tile images into an array. When drawing your map, for each tile ID in the map, use that ID to find the tile image in the array and draw that. If you use the grass17.png a hundred times in one map, you should still only have one copy of the image that you loaded once, and just used the same image to draw each of the hundred grass tiles in the map.

 

When drawing a tile map, you can go like this:

array Layer = array of 100 x 100 integers [b][color=#008080](tile IDs)[/color][/b]
array Map = array of 5 Layers
Map myMap = load_map();
array tileImages = array of loaded images

for each layer in myMap:
	 for each row in layer:
		  for each column in row:
               tileID = myMap[layer]->[row][column];
               xPos = column * TILE_WIDTH;
               yPos = row * TILE_HEIGHT;
    		   draw_image(tileImages[tileID], draw at: xPos, yPos);

This is a simple way of doing things. Once you understand that, there are improvements you can make, such as: loading the images not at startup, but only when needed. Freeing the images that aren't needed. Saving images when changing maps if both maps use the same image. Any number of layers. Any size of maps. Breaking large maps into smaller chunks of maps, and only loading the chunks that are currently visible.

PARTNERS