Sign in to follow this  

Simple tile engine.

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[I]=str2int(scan_datax(getlie,temp));
PATH_Y[I]=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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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[i];
}
}





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

Share this post


Link to post
Share on other sites

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this