Jump to content
  • Advertisement
Sign in to follow this  
luxm

Simple tile engine.

This topic is 4369 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=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]

Share this post


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





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
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.

Share this post


Link to post
Share on other sites
Wah...i thought this thread was dead!!
Thanks for the replies! , i hope i wont need anything else..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!