Sign in to follow this  
RojanShrestha

Map Loading

Recommended Posts

[color=#1C2837][size=2]I have a framework set up for a 2D side scrolling game, with a constant background that is scrolled, and my player can now move around in the scene but now I want to place my certain sprites in certain exact locations of the game world. How do I do that.. I don't have any idea about this (please describe from basic)... [/size][/color]

Share this post


Link to post
Share on other sites
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?

Share this post


Link to post
Share on other sites
It might help to know what you're using; which language, and which engines? Also, when you say sprite, to you mean any graphical sprite, or moving character or monster sprites? That, and it depends on your setup, and how your world is stored, I suppose. How do you place the objects that your played encounters? Do you have a separate editor?

I did something similar not too long ago.

I had a tile-based setup, and a separate editor program in which I set the tiles to certain values in order to create various objects in the game. When I wanted to place things like mobs or items, I right-clicked on the position I wanted them at, and stored the mouse's position in a vector of positions for that item type (i.e. I had three mob types, and a vector for positions of mobs of each type). To change the type of mob I was placing, I had the space key change a value "MobType" to a value of between 0 and 2, going back to 0 if it hit 3.

Then I added those positions to my save file (a text tile). The save file consisted of a grid of letters representing tile types, followed by a list of integers, which were broken up by strings that would tell the game what the integers represented. It then created the mob at that position when I loaded the map up in the same program; for me, X and Y coordinates were parameters in the constructor for the mob class, so I didn't need to do anything else other than let the mob class constructor place the mob.

For example:

tilemap
aaaa
abaa
abaa
aaaa
player
3
7
mobs
3
15
6
17
12
6

where every pair of integers after "mobs" is interpreted as the XY coordinates of a mob.

What have you tried so far? What problems are you facing?

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?
[/quote]
hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??

Share this post


Link to post
Share on other sites
[quote name='Saldan' timestamp='1313657092' post='4850662']
It might help to know what you're using; which language, and which engines? Also, when you say sprite, to you mean any graphical sprite, or moving character or monster sprites? That, and it depends on your setup, and how your world is stored, I suppose. How do you place the objects that your played encounters? Do you have a separate editor?

I did something similar not too long ago.

I had a tile-based setup, and a separate editor program in which I set the tiles to certain values in order to create various objects in the game. When I wanted to place things like mobs or items, I right-clicked on the position I wanted them at, and stored the mouse's position in a vector of positions for that item type (i.e. I had three mob types, and a vector for positions of mobs of each type). To change the type of mob I was placing, I had the space key change a value "MobType" to a value of between 0 and 2, going back to 0 if it hit 3.

Then I added those positions to my save file (a text tile). The save file consisted of a grid of letters representing tile types, followed by a list of integers, which were broken up by strings that would tell the game what the integers represented. It then created the mob at that position when I loaded the map up in the same program; for me, X and Y coordinates were parameters in the constructor for the mob class, so I didn't need to do anything else other than let the mob class constructor place the mob.

For example:

tilemap
aaaa
abaa
abaa
aaaa
player
3
7
mobs
3
15
6
17
12
6

where every pair of integers after "mobs" is interpreted as the XY coordinates of a mob.

What have you tried so far? What problems are you facing?
[/quote]

Thanks for your reply!! By the way I am using c++ with SFML library. Its not the player or monsters that I want, I want objects like walls, etc... to be positioned... and no I don't have an editor ( not yet )
I like your idea of storing the position of the objects in the next lines... will give it a try... but I think it is absolutely necessary to build an editor for this?? (Please correct me if I am wrong)

Share this post


Link to post
Share on other sites
[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?
[/quote]
hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1313674903' post='4850758']
[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?
[/quote]
hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?
[/quote]
I didn't quite get the second method... could you please be more descriptive..

Share this post


Link to post
Share on other sites
[quote name='newbie_gamer' timestamp='1313677795' post='4850782']
[quote name='ryan20fun' timestamp='1313674903' post='4850758']
[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?
[/quote]
hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?
[/quote]
I didn't quite get the second method... could you please be more descriptive..
[/quote]

ill combine it with the way Saldan did his.

the first line of the file could be a nuber specyfiing how many objects are to be places, or read till EOF.

then read in till you find a tab, this would be the X position of the objecy ->
then read in till you find another tab, this will be the Y position of the object ->
then read in till new line( end of the current line ), this would be a int or string indicating what object it is supposed to be.

here is some code i used to load a entity into a game i made.
[code]
int numberofframes = 1;
float width = 1;
float height = 1;
float fsx = 1;
float fsy = 1;
float alpha = 0;
wstring ws;

char *buf = new char[20];
memset(buf, 0, 20);

fstream fs(Path);

if(!fs)
{
wstring ws(L"File Not Found");
ws.append(L"\n");
ws.append(Path);
ws.append(L"\n");
ws.append(L"I will probably crash when you click Ok");
MessageBox(NULL, ws.c_str(), L"Error", MB_OK);
MessageBox(NULL, L"Noooooooooooooo", L"Help", MB_OK);
HRESULT r = MessageBox(NULL, L"Continue \?", L"Why you looking at me \?", MB_OKCANCEL);
if(r == IDCANCEL)
{
MsgProc(hWnd, WM_DESTROY, NULL, NULL); // PostQuitMessage(0);
_isquiting = true;
}
}

fs.getline(buf, 20, '\t');
width = (float)atof(buf); // width

fs.getline(buf, 20, '\n');
height = (float)atof(buf); // height

fs.getline(buf, 20, '\t');
fsx = (float)atof(buf); // frame size x

fs.getline(buf, 20, '\n');
fsy = (float)atof(buf); // frame size y

fs.getline(buf, 20, '\n');
numberofframes = (int)atof(buf); // number of frames

fs.getline(buf, 20, '\n');
alpha = (float)atof(buf); // alpha

fs.getline(buf, 20, '\n');
string s;
s.append(mediafolder);
s.append(buf);
ws.append(s.begin(), s.end()); // path to sprite sheet

delete []buf;
sprites.push_back(AnimatedSprite(D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha), numberofframes, PosX, PosY, Type, (UINT)width, (UINT)height, fsx, fsy, ws.c_str()));
[/code]

it would expect a file formated like this
[code]
320 384
64 64
29
1
donut.bmp
[/code]

if you did not read the comments in the code:
320 is the width of the image
384 is the height of the image
64 is the horizontal frame size of the animation
64 is the vertical frame size of the animation
29 is how many animations the sprite has
1 is the alpha value, IE is black tranparent ?

understand ?

you could do a similar thing, just make the first line tell the parser how many entities exist.
instread of seperating it by relevance( and because this is map data ) just seperate each entry( for an individual object ) in the file by a tab and do it that way.

it should be simple to addapt the code i pasted into something remotly usable for the level data.

:) :) :)

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1313679346' post='4850790']
[quote name='newbie_gamer' timestamp='1313677795' post='4850782']
[quote name='ryan20fun' timestamp='1313674903' post='4850758']
[quote name='newbie_gamer' timestamp='1313674038' post='4850750']
[quote name='ryan20fun' timestamp='1313657069' post='4850661']
you cald use a *simple* text file for the level.

example
00100203000345
12990043003040
12045065643212

where 0 would be blank, 1 through 9 would mean some type of object

and that is a reall simple file format, the XNA sidscroller project uses that.

to save it, you could start by by building a coordinate based container of some kind and fill that with what entity and position -> then write it starting at 0,0 and move along the x axis and write the values of the object, then move onto next line.

make sense ?
[/quote]
hey!! I thought of making the levels the same way... but even though I can set each character read from a file to represent a certain item... How do I get the location of the item on the screen.??
[/quote]

right now i can think of two ways.

one, is to have the sprite no larger then X size, and each entry in the text file represents that block on screen.

two, more complicated.
first store the how many entries X and Y are in the file.
read in till tab, denotes X position -> read again till tab, denots Y position -> read till newline, denots entity type or 0 for none.
repeat till you have read in all the specified Y lines.

was that clear ?
[/quote]
I didn't quite get the second method... could you please be more descriptive..
[/quote]

ill combine it with the way Saldan did his.

the first line of the file could be a nuber specyfiing how many objects are to be places, or read till EOF.

then read in till you find a tab, this would be the X position of the objecy ->
then read in till you find another tab, this will be the Y position of the object ->
then read in till new line( end of the current line ), this would be a int or string indicating what object it is supposed to be.

here is some code i used to load a entity into a game i made.
[code]
int numberofframes = 1;
float width = 1;
float height = 1;
float fsx = 1;
float fsy = 1;
float alpha = 0;
wstring ws;

char *buf = new char[20];
memset(buf, 0, 20);

fstream fs(Path);

if(!fs)
{
wstring ws(L"File Not Found");
ws.append(L"\n");
ws.append(Path);
ws.append(L"\n");
ws.append(L"I will probably crash when you click Ok");
MessageBox(NULL, ws.c_str(), L"Error", MB_OK);
MessageBox(NULL, L"Noooooooooooooo", L"Help", MB_OK);
HRESULT r = MessageBox(NULL, L"Continue \?", L"Why you looking at me \?", MB_OKCANCEL);
if(r == IDCANCEL)
{
MsgProc(hWnd, WM_DESTROY, NULL, NULL); // PostQuitMessage(0);
_isquiting = true;
}
}

fs.getline(buf, 20, '\t');
width = (float)atof(buf); // width

fs.getline(buf, 20, '\n');
height = (float)atof(buf); // height

fs.getline(buf, 20, '\t');
fsx = (float)atof(buf); // frame size x

fs.getline(buf, 20, '\n');
fsy = (float)atof(buf); // frame size y

fs.getline(buf, 20, '\n');
numberofframes = (int)atof(buf); // number of frames

fs.getline(buf, 20, '\n');
alpha = (float)atof(buf); // alpha

fs.getline(buf, 20, '\n');
string s;
s.append(mediafolder);
s.append(buf);
ws.append(s.begin(), s.end()); // path to sprite sheet

delete []buf;
sprites.push_back(AnimatedSprite(D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, alpha), numberofframes, PosX, PosY, Type, (UINT)width, (UINT)height, fsx, fsy, ws.c_str()));
[/code]

it would expect a file formated like this
[code]
320 384
64 64
29
1
donut.bmp
[/code]

if you did not read the comments in the code:
320 is the width of the image
384 is the height of the image
64 is the horizontal frame size of the animation
64 is the vertical frame size of the animation
29 is how many animations the sprite has
1 is the alpha value, IE is black tranparent ?

understand ?

you could do a similar thing, just make the first line tell the parser how many entities exist.
instread of seperating it by relevance( and because this is map data ) just seperate each entry( for an individual object ) in the file by a tab and do it that way.

it should be simple to addapt the code i pasted into something remotly usable for the level data.

:) :) :)
[/quote]


Thank you for such a detailed reply. I think I get it some what... will definitely give it a try!! [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img][img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img]

Share this post


Link to post
Share on other sites
There is a fairly simple example in NetHack --- which also has source available for your enjoyment.


It is a 2D dungeon crawler, but the requirements for maps are not dramatically different.


They have a simple [url="http://nethackwiki.com/wiki/Dungeon.def_format"]Dungeon file format[/url] that gives everything you need in that style of game.

Some example levels are [url="http://nethackwiki.com/wiki/Source:NetHack_3.4.3/dat/tower.des"]Vlad the Impaler's Tower[/url], the [url="http://nethackwiki.com/wiki/Source:NetHack_3.4.3/dat/mines.des"]gnomish mines[/url], and so on. They include rules for the randomly-generated portions of dungeon, plus rules for specifically-generated areas as well. Some maps mix random areas and fixed areas.

Share this post


Link to post
Share on other sites
Or, you could use xml, and use tinyxml to parse it. Check here for an example tutorial: [url="http://ken.ath.cx/kens_code_pit/2010/12/06/tinyxml-tutorial-simple-parsing-2/"]My link[/url]

It could look something like this:
[code]
<MapLayout>
<Level1>
<Object type="Block" x="10" y="120" />
<Enemy type="Zombie" x="100" y="10" />
<Enemy type="Boss1" x="10000" y="20" />
</Level1>
</MapLyout>

[/code]

then, in another file you may have entities.xml, like this
[code]
<Entities>
<Enemies>
<Zombie Sprite="Zombie.jpg" Health="100" Strength="10" />
</Enemies>
<Objects>
<Block Sprite="Block.jpg" Moveable="no" />
</Objects>
</Entities>
[/code]

If you do go this route, I'd suggest googling tinyxml tutorial, and looking all over gamedev for help. You may even want to get familiar with xml terms as well.

Share this post


Link to post
Share on other sites

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