gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Levels

#34014 - wiz - Wed Jan 12, 2005 7:12 pm

Hi folks,

Lets say we have levels as source files and its pretty simple:

level1.c
#define LEVEL_TIME 60
const u8 LEVEL_TILES[] = { 0, 0, 0, 0, 1, 2, 1, 2, 3 ......

level2.c
.?..

What would be a good method of this? It would not be possible to define LEVEL_TIME again for level2, and the array couldnt have the same name in each file.

Currently I can only think of having:

#define LEVEL1_TIME 60

...

if (LEVEL == LEVEL1) LEVEL_TIME = LEVEL1_TIME;

but if a game had 100 levels that would be horrid!!

Suggestions?

Thank you very much!!

#34016 - Celeryface - Wed Jan 12, 2005 7:26 pm

Hmm, you could have an array of level times in your source file, ordered sequentially from 0 - last level.

const int levelTimes[] = { 60, 100, 120, ... };

:)

OR

If your level times are always increasing you could do it this way, you could have a variable in your game that holds the time and calculates it per level.

#define LEVEL_TIME 60 (in your level source file)

LevelTime = LEVEL_TIME + (10*currentLevel); (in your game source file)


Last edited by Celeryface on Wed Jan 12, 2005 7:29 pm; edited 1 time in total

#34017 - wiz - Wed Jan 12, 2005 7:29 pm

Hi thanks for your reply,

I did think of that but then the same question goes for the const level array:

const u8 LEVEL1_TILES[] = { 0, 0, 0, 0, 1, 2, 1, 2, 3 ......
const u8 LEVEL2_TILES[] = { 2, 2, 2, 2, 1, 2, 1, 2, 3 ......
const u8 LEVEL3_TILES[] = { 3, 3, 3, 3, 1, 2, 1, 2, 3 ......
.. +100 levels

How do you access the array you want at runtime? with if statements? ouch

#34018 - Celeryface - Wed Jan 12, 2005 7:30 pm

wiz wrote:
Hi thanks for your reply,

I did think of that but then the same question goes for the const level array:

const u8 LEVEL1_TILES[] = { 0, 0, 0, 0, 1, 2, 1, 2, 3 ......
const u8 LEVEL2_TILES[] = { 2, 2, 2, 2, 1, 2, 1, 2, 3 ......
const u8 LEVEL3_TILES[] = { 3, 3, 3, 3, 1, 2, 1, 2, 3 ......
.. +100 levels

How do you access the array you want at runtime? with if statements? ouch


I edited the post again. Does the other suggestion help? :)

#34019 - wiz - Wed Jan 12, 2005 7:36 pm

Hi again :)

Not all levels would be increased time. Lets forget about the level-time, that was because I was thinking of #defines as well.

The 'tile array' for each level could be quite large so its the same question with this, how can you access the one you want? I'm guessing that you DONT name the arrays as their level number: "const u8 LEVEL1_TILES[] = { ........" but some other magic way ;)

Hope you understand what I mean,

Cheers

#34022 - Miked0801 - Wed Jan 12, 2005 8:06 pm

Create an array named per level as you suggest (lvl1array[]={}; lvl2array[]={};...) then create a second table that contains pointers to the first:
Code:

const LVL_ARRAY_TYPE *masterTab[] = {lvl1array, lvl2array, ...};


Access the master tab to get your data. The advantage of this is that all levels need not be the same size.

#34024 - Zhila - Wed Jan 12, 2005 8:12 pm

I'm not much into C++, but, I assume you could always to a 2 dimensional array, instead of 100 single dimensional arrays. I'm not exactly sure how the syntax works itself, but I'd assume something similar to this would work:
Code:

const u8 LEVEL_TILES[100,] = { { 0, 0, 0, 0, 1, 2, 1, 2, 3, ... },
                               {2, 2, 2, 2, 1, 2, 1, 2, 3, ...},
                               {3, 3, 3, 3, 1, 2, 1, 2, 3, ...),
                               +100 levels

_________________
Current high scores on Super Mario 64 DS:
Shell Smash - 50230
Wanted - 140

#34030 - ScottLininger - Wed Jan 12, 2005 9:44 pm

Pointers are definitely the answer. I like using structs or organize everything about the level...

Code:

// First dump out your variable-length data

const u8 level0Map[] = { 1, 2, 3 ... };
const u8 level1Map[] = { 1, 2, 3 ... };


// then define a struct for your maps

typedef struct structLevelData { 
    u16 tileWidth;
    u16 tileHeight;
    u16 timeLimit;
    u8* mapData;   // the star makes it a pointer
} typeLevelData;


// then you can create an array of Levels, and
// assign your pointers to the map data above

const typeLevelData allLevels[2] = {
    {
        50,                // tileWidth
        80,                // tileHeight
        120,                // timeLimit
        level0Map    // pointer to your map Data      
    } , {    
        99,                // tileWidth
        52                // tileHeight
        60,                // timeLimit
        level1Map    // pointer to your map Data
    }
};

// now you can get at your data using the following syntax

// to get at the timeLimit of your first map
timeValue = allLevels[0].timeLimit;

// to get at the 56th element of mapData on your second map
tileValue = allLevels[1].mapData[55];



That's the way many people do it, because it makes the end syntax so clean. It's a little bit of work to set up properly, but once that's out of the way, you're golden.

This is essentially the same solution that Mike suggested above, but the extra struct allows you to track other stuff about your levels all in one place.

Cheers,

-Scott

#34044 - sgeos - Wed Jan 12, 2005 11:02 pm

You might want to write a tool that holds stage data in a custom format and can spit out the C/ASM code for you.

-Brendan

#34048 - wiz - Wed Jan 12, 2005 11:41 pm

Thats brilliant!

Thanks everyone, once again. I had a feeling it had something to do with pointers.

:)

#34056 - dagamer34 - Thu Jan 13, 2005 3:24 am

Working with 100 levels is a daunting task in the first place! :D
_________________
Little kids and Playstation 2's don't mix. :(

#34060 - sgeos - Thu Jan 13, 2005 3:46 am

dagamer34 wrote:
Working with 100 levels is a daunting task in the first place! :D

Hence the tool. Creating stages in source code is OK if you only need to make a couple, but once you hit the double digits an encoder is called for- even if it is just a simple curses program.

-Brendan