Simple tile engine.

Started by
7 comments, last by luxm 17 years, 6 months ago
Hi all. After i failed with my 1st game(well you can take a look at "game programming section") , im about to start a new game. But this time i'll start by collecting information. 1)How can i design a simple tile map? and how can i save&load the position of every object from a file?. In my non-finished game i was loading the data like this:

int i=-1;
string temp;
int PATH_X[512];
int PATH_Y[512];
while !eof

{
i++;
PATH_X=str2int(scan_datax(getlie,temp));
PATH_Y=str2int(scan_datay(getlie,temp));
//id,x,y
create_object
(
i,
str2int(scan_datax(getlie,temp)),
str2int(scan_datay(getlie,temp))
);

}


the output was something like this: xxxxxxxxxxx xxxYYYYxxxx xxxyyyyyyxx xxxyyyyyyyy xxxxxxxxxxx I learned that its better to use 2d arrays. Whats the right way to do this? 2) And a last question, how can i detect collision? thaNks!!edit: Im working with SDL & C++..if that helps!! [Edited by - luxm on September 28, 2006 7:55:35 PM]
Advertisement
for array I use
myclass *DATA;
or
int *DATA like in your example
then
// create data buffer
DATA = new[width*height];

x,y data is
int value = DATA[y*m_width+x];
or
DATA[y*m_width+x] = 1; // or any other value U use

and don't forget to

delete []DATA;

to release memory.
In this example you can use any width and height you like not just 512, 512 like in your example, and with this you are saving a bit of memory.
Nice , thanks.

And how about the indexing?
I have the feeling that by just updating the array with the path x & y , wont work.
There must be a conversion from standard x,y to tile x,y. right??
hmmm.. no one/?
i have done my own simple tile engine like i load nums of images from tileset from file to vector like this(file):
0 1 1 2 1 1 0
0 0 1 2 1 0 0
0 0 0 2 0 0 0

then i convert it from vector to [mapx][mapy] array
and now i only draw it to screen in loop and adding camera poses.

i never read anything about how to program this, its only come from my head and its working :p
Quote:Original post by lulul
i have done my own simple tile engine like i load nums of images from tileset from file to vector like this(file):
0 1 1 2 1 1 0
0 0 1 2 1 0 0
0 0 0 2 0 0 0

then i convert it from vector to [mapx][mapy] array
and now i only draw it to screen in loop and adding camera poses.

i never read anything about how to program this, its only come from my head and its working :p

Can you post a small example how you load/save them?
ehm, what save load? :p

load map from file?
void LoadMap(int i){    vector<int> file;    int data;    char path[128];    sprintf(path, "Data/map%d.txt", i);    ifstream in(path);    while(in >> data)    file.push_back(data);    for(int i=0, x=0, y=0; i<MAPX*MAPY; i++, x++)    {        if(x>MAPX-1) y++, x=0;        map[x][y] = file;    }}


and this draws my map
void DrawMap(){    SDL_Rect tilePos,tile;    tilePos.w = Tileset.tilesize;    tilePos.h = Tileset.tilesize;    tile.w = Tileset.tilesize;    tile.h = Tileset.tilesize;    for(int x=0; x<SCREEN_WIDTH/Tileset.tilesize; x++)    {        for(int y=0; y<SCREEN_HEIGHT/Tileset.tilesize; y++)        {            tilePos.x = x*Tileset.tilesize;            tilePos.y = y*Tileset.tilesize;            tile.x = map[x + (int)Camera.x / Tileset.tilesize][y + (int)Camera.y / Tileset.tilesize] % (Tileset.tilesetsize / Tileset.tilesize) * Tileset.tilesize;            tile.y = map[x + (int)Camera.x / Tileset.tilesize][y + (int)Camera.y / Tileset.tilesize] / (Tileset.tilesetsize / Tileset.tilesize) * Tileset.tilesize;            SDL_BlitSurface(Tileset.image, &tile, screen, &tilePos);        }    }}


and thats all
dont use it, made by noob :pp
Check out the articles section here on Gamedev. I found this one pretty quickly.

*CLICKY*

[edit]
Ack! Change the include <*stream.h> to just <*stream>. Someone needs to change that.
Evillive2
Wah...i thought this thread was dead!!
Thanks for the replies! , i hope i wont need anything else..

This topic is closed to new replies.

Advertisement