[SOLVED] Best Way to load a level

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

[SOLVED] Best Way to load a level

Post by 0x0000000 »

Hi all! I have been working on a engine for the past few months, and It has finally come to point where level loading is required.
I was chatting with Falco awhile ago about what would be the best way to load a tilemap.

The way I am currently doing is that I have a fixed 2D array of tiles(100x100), and it loads a number into the ID of the tile, something like this:
while(!map.eof()) {
        map >> map_t[currentTileX][currentTileY].ID;
        currentTileX++;
        if(currentTileX >= width) {
            currentTileX = 0;
            currentTileY++;
        }
    }
But the problem with this is that the map file would have to be something like this:

Code: Select all

1
1
1
2
1
2
2
2
1
Which can be very hard to type; since I am the only one working on this project, I haven't made a Level Editor yet.
So my question is, is there any other way to load a map?

Something like this maybe?:

Code: Select all

1111111111
1112222221
1111111111
Falco suggested that I use binary files, which I will look into and try to use.
But in the mean time, any suggestions would be great!
Last edited by 0x0000000 on Sun Feb 17, 2013 5:12 am, edited 1 time in total.
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: Best Way to load a level

Post by 0x0000000 »

OK! I have just learned how to use binary files, but the format is now like this:

Code: Select all

111112311211111122212112222122121
I was hoping it to be something like the last example in the above post.

Any ideas guys?
Perhaps on how to remove a \n when parsing a binary file?

EDIT: I just learned about fscanf()! Funny because Falco mentioned it when we were chatting, perhaps this will solve my problem.
Last edited by 0x0000000 on Sun Feb 17, 2013 4:29 am, edited 1 time in total.
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
BugInTheSYS
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 88
Joined: Mon Feb 07, 2011 4:16 pm
Current Project: Vintage Roads
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, Delphi
Location: Pinneberg, Germany
Contact:

Re: Best Way to load a level

Post by BugInTheSYS »

0x0000000 wrote:Perhaps on how to remove a \n when parsing a binary file?
What do you mean by 'remove a \n?'
Do you want the newline character to delimit the tile rows? Or do you want to ignore it?

If all you want is to ignore it while parsing, then I suppose you work your way around by checking if the current character is a newline character, and if so, increment neither currentTileX nor currentTileY, but just literally ignore the newline character. (Hint: The std::ifstream class even has a member function for that.)

Edit: You would probably just forward the get pointer of the ifstream, which is even simpler.

Hope I could help.
Someday, everything will go to /dev/null. - Bug's prophecy 13:37
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: Best Way to load a level

Post by 0x0000000 »

BugInTheSYS wrote:
0x0000000 wrote:Perhaps on how to remove a \n when parsing a binary file?
What do you mean by 'remove a \n?'
Do you want the newline character to delimit the tile rows? Or do you want to ignore it?

If all you want is to ignore it while parsing, then I suppose you work your way around by checking if the current character is a newline character, and if so, increment neither currentTileX nor currentTileY, but just literally ignore the newline character. (Hint: The std::ifstream class even has a member function for that.)

Hope I could help.
I wanted to ignore the \n, so this:

Code: Select all

111111221
111112221
111112222
Would be something like this:

Code: Select all

111111221111112221111112222
when being read in from the Engine.

And also, I just learned about fscanf(), which seems to suit my needs, but thanks for the help anyways! :) :mrgreen:
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: Best Way to load a level

Post by 0x0000000 »

I figured out what I should do!
I used fscanf() and what BugInTheSys told me to do.

The final result is:
for (int i = 0; i < width * height; i++) {
            fscanf(map, "%c", &curTile);
            if(curTile != '\n' && curTile != ' ') {
                map_t[currentTileX][currentTileY].ID = curTile;
                cerr << map_t[currentTileX][currentTileY].ID;
                currentTileX++;
                if(currentTileX >= width) {
                    currentTileX = 0;
                    currentTileY++;
                }
            }
        }
And the visual result:
Image

Anyways, thank you Falco and BugInTheSys :worship:
I couldn't have done it without you :) (great now I sound like Dora)
Last edited by 0x0000000 on Sun Feb 17, 2013 6:34 am, edited 2 times in total.
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
BugInTheSYS
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 88
Joined: Mon Feb 07, 2011 4:16 pm
Current Project: Vintage Roads
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, Delphi
Location: Pinneberg, Germany
Contact:

Re: [SOLVED] Best Way to load a level

Post by BugInTheSYS »

Open the img tag, and paste the image URL in between (img) and (/img) (with square brackets!)
Someday, everything will go to /dev/null. - Bug's prophecy 13:37
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: [SOLVED] Best Way to load a level

Post by 0x0000000 »

BugInTheSYS wrote:Open the img tag, and paste the image URL in between (img) and (/img) (with square brackets!)
I see, but does that mean I can't upload pictures from my own computer?
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
BugInTheSYS
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 88
Joined: Mon Feb 07, 2011 4:16 pm
Current Project: Vintage Roads
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, Delphi
Location: Pinneberg, Germany
Contact:

Re: [SOLVED] Best Way to load a level

Post by BugInTheSYS »

You can use an intermediate image hosting service, like tinypic or any of these services.
Someday, everything will go to /dev/null. - Bug's prophecy 13:37
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: [SOLVED] Best Way to load a level

Post by 0x0000000 »

BugInTheSYS wrote:You can use an intermediate image hosting service, like tinypic or any of these services.
Oh ok, but let me make a awesome map first ;)
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: [SOLVED] Best Way to load a level

Post by 0x0000000 »

Uploaded the image! Look at my previous post ;)
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
lalacomun
VS Setup Wizard
VS Setup Wizard
Posts: 114
Joined: Wed Dec 28, 2011 10:18 pm
Favorite Gaming Platforms: psx, nintendo ds, gameboy advance, xbox 360, ps2
Programming Language of Choice: C++
Location: Argentina
Contact:

Re: [SOLVED] Best Way to load a level

Post by lalacomun »

Now your goal is to handle multiple layers, like tiles, objects, collision etc...
Just a tip for your engine, i dont recomend having a "Collision layer" it uses more RAM and more HDD space, you may say that 1 mb of ram doesnt matter but when you are working with low resources hardware that meg of ram makes the difference :) i recommend you that each tile have a simple boolean value that sets if its colidable or not, this way you can have something like "colliders" like the ES engine that sets what part of the tile is colidable ex: upper half of tile, left half tile etc...atleast thats the way i handle collision in my engine ;)
Image
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: [SOLVED] Best Way to load a level

Post by 0x0000000 »

lalacomun wrote:Now your goal is to handle multiple layers, like tiles, objects, collision etc...
Just a tip for your engine, i dont recomend having a "Collision layer" it uses more RAM and more HDD space, you may say that 1 mb of ram doesnt matter but when you are working with low resources hardware that meg of ram makes the difference :) i recommend you that each tile have a simple boolean value that sets if its colidable or not, this way you can have something like "colliders" like the ES engine that sets what part of the tile is colidable ex: upper half of tile, left half tile etc...atleast thats the way i handle collision in my engine ;)
That sounds reasonable!

The only problem is that I have no f*cking clue how to do collision resolution :|
But thanks for the advice!

Do you maybe know any sites that teach collision resolution?
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
lalacomun
VS Setup Wizard
VS Setup Wizard
Posts: 114
Joined: Wed Dec 28, 2011 10:18 pm
Favorite Gaming Platforms: psx, nintendo ds, gameboy advance, xbox 360, ps2
Programming Language of Choice: C++
Location: Argentina
Contact:

Re: [SOLVED] Best Way to load a level

Post by lalacomun »

I can teach you ;) do you have skype or something?? or china just block it??? if thats the case you can check out the LazyFoo tuts or here is a great tut about collision:



;)
Image
User avatar
0x0000000
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Mon Feb 11, 2013 11:46 pm
Current Project: Pokemans
Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
Programming Language of Choice: C/++
Location: Memory Address 0x0000000 (in UTF-9 notation)
Contact:

Re: [SOLVED] Best Way to load a level

Post by 0x0000000 »

I have Skype, add me: altan.haan ;)
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
User avatar
DistortedLance
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Fri Jul 22, 2011 12:36 pm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: [SOLVED] Best Way to load a level

Post by DistortedLance »

lalacomun wrote:Now your goal is to handle multiple layers, like tiles, objects, collision etc...
Just a tip for your engine, i dont recomend having a "Collision layer" it uses more RAM and more HDD space, you may say that 1 mb of ram doesnt matter but when you are working with low resources hardware that meg of ram makes the difference :) i recommend you that each tile have a simple boolean value that sets if its colidable or not, this way you can have something like "colliders" like the ES engine that sets what part of the tile is colidable ex: upper half of tile, left half tile etc...atleast thats the way i handle collision in my engine ;)
There are, of course, disadvantages with this approach. It's not quite as flexible when it comes to having multiple tiles with the same graphic but with different collision bounds, so depending on the scenario, a separate collision layer could easily be reasonable. You could do various things like bit-masking the collision type in the "tile index" (if you already had spare bits available) rather than fixing the collision types when loading the tilesheet if you wanted specific collisions per-layer, or just read in an additional value per map-location if you wanted this additional flexibility. If you're using the bit-mask approach, there's no additional memory usage, while the per-location method is a relatively small number of kilobytes. It's probably better for him to decide what the realistic needs of the game would be, rather than simply choosing a method for a constraint that might not necessarily apply.
Post Reply