Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Loading Levels


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 David.M   Members   -  Reputation: 731

Like
0Likes
Like

Posted 12 February 2013 - 07:27 PM

I've done some searching but couldn't find a topic that was quite what I wanted so I apologize if this has been asked multiple times.

 

I'm currently working on a 2D platformer game and am contemplating how to deal with loading levels. I'm using LibGDX as my framework. I was wondering what you guys prefer to do for handling this. I might like to add a level editor eventually so I was thinking about having a simple text file with the maps and different numbers representing different tiles, i.e.

0022200000
0000121000
0111012200
1110020010
0111210011
0120101010

Where 0, 1, and 2 represent different tile images. Then I could just load it in one level at a time. What are my other alternatives? How do you prefer loading levels? Thanks,

 

David



Sponsor:

#2 jellyfishchris   Members   -  Reputation: 300

Like
0Likes
Like

Posted 12 February 2013 - 08:54 PM

I mean with the example you gave, you could just use a .bmp file and represent red as 1 white as 0 and blue as 2.



#3 Khatharr   Crossbones+   -  Reputation: 3044

Like
0Likes
Like

Posted 12 February 2013 - 09:07 PM

When working indexed tiles in the past I usually started out with a small tileset and implemented a loader that would read from text files as you described. Once I was happy with the runtime I'd create a map editor that saved to a simple binary format something like this:

str MAPF - first 4 bytes are a fourcc to verify that the file is the correct format
uint32 length - indicating length of the rest of the file (total file length minus 8) so I can read the rest into memory easily

uint32 width - in tiles
uint32 height
uint32 layers
uint32 zero - for alignment

Then it's just raw byte-width tile id's, row followed by row.

At runtime the dimensions would be loaded into a struct and that would be fed to a loader function that turned each width x height bytes into a layer and spat out the resulting map object. Internally the map class just stored each layer in the form of the data which had been read. I also supported animated tiles, so the map included some functionality for dealing with that. By associating the map object with a tileset texture it could be prompted to render at any x,y offset. In D3D it's simple to do because the D3DXSprite batcher can do the sorting for you, but in an API that doesn't have that you'd want to sort by tile to cut rendering time.

'Tiled' is in vogue at the moment. It's a nice little tool. You may want to look at that before going hardcore. I believe there's a lib for loading Tiled maps as well, though I don't know what it loads into. Honestly if I were to do this again I'd go the Tiled route.

http://www.mapeditor.org/
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#4 ProvenDantheman   Members   -  Reputation: 111

Like
0Likes
Like

Posted 12 February 2013 - 09:09 PM

I mean with the example you gave, you could just use a .bmp file and represent red as 1 white as 0 and blue as 2.

Why waste 54 bytes on a header? Just use raw data.

 

to save:

 

FILE * fileLevel;

fileLevel = fopen(NAME,"w");

fileLevel = fopen(NAME,"a");

for(int i = 0; i < LEVELHEIGHT; i++)

{

for(int j = 0; j < LEVELWIDTH; j++)

{

fputc((char)LEVELDATAARRAY[i][j],fileLevel);

}

}

fclose(fileLevel);

 

to open:

 

FILE * fileLevel;

fileLevel = fopen(NAME,"r");

for(int i = 0; i < LEVELHEIGHT; i++)

{

for(int j = 0; j < LEVELWIDTH; j++)

{

LEVELDATAARRAY[i][j] = fgetc(fileLevel);

}

}

fclose(fileLevel);


Edited by ProvenDantheman, 12 February 2013 - 09:11 PM.


#5 jellyfishchris   Members   -  Reputation: 300

Like
0Likes
Like

Posted 12 February 2013 - 09:34 PM


I mean with the example you gave, you could just use a .bmp file and represent red as 1 white as 0 and blue as 2.

Why waste 54 bytes on a header? Just use raw data.

 

to save:

 

FILE * fileLevel;

fileLevel = fopen(NAME,"w");

fileLevel = fopen(NAME,"a");

for(int i = 0; i < LEVELHEIGHT; i++)

{

for(int j = 0; j < LEVELWIDTH; j++)

{

fputc((char)LEVELDATAARRAY[i][j],fileLevel);

}

}

fclose(fileLevel);

 

to open:

 

FILE * fileLevel;

fileLevel = fopen(NAME,"r");

for(int i = 0; i < LEVELHEIGHT; i++)

{

for(int j = 0; j < LEVELWIDTH; j++)

{

LEVELDATAARRAY[i][j] = fgetc(fileLevel);

}

}

fclose(fileLevel);

 

Easier to modify, easy to visually see etc. Im ok with wasting 54bytes on a header file, when I have those added benefits!



#6 acamer01   Members   -  Reputation: 382

Like
0Likes
Like

Posted 12 February 2013 - 09:54 PM

You can try checking out Programming Role Playing Games With DirectX by Jim Adams (2nd Ed) It uses DX9 but it help you to get started.


The key to immortality is to live a life worth remembering.

#7 Krohm   Crossbones+   -  Reputation: 3261

Like
0Likes
Like

Posted 13 February 2013 - 01:44 AM

Back in time I used... believe it or not... OpenOffice Calc. Exporting to .csv. It worked quite well. In retrospect, it worked better than my current approach which is giving me headaches right now.

The cool thing about using CSV is that you can have many, many tiles, with many, many properties and they'll still align as you want them to when opened in Calc. By contrast, a pure txt file would quickly require a lot of padding to align on number digits and extra annotations.

 

That said, I prefer binary blobs for runtime.


Edited by Krohm, 13 February 2013 - 01:45 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS