ID:1681140
 
Hey guys,

I have a C++ question for you. I've looked everywhere for information on this, but can't seem to find anything.

I am working on a console game and would like to have multiple levels (maps) in the game. The problem I'm having is that I can't think of a good way to do an efficient drawMap() function that would work for multiple levels. My issues are that 1. you cannot use the assignment operator to populate arrays (my map is a char array), so I can't simply change a global gameMap[][] and 2. I can't think of a way to account for any number of maps one could possibly have within one function.

I am new to C++ and so am probably overlooking something obvious. Does anybody have suggestions?
You should be loading your maps from a file, not hard-coding them into a static array. If your maps can be different sizes, you'll want to dynamically allocate the array that stores the values. Also, it's usually easier to create a one-dimensional array and just do a little arithmetic to convert a 2D coordinate into an index.

If you want to copy one array of POD data into another, you can use memcpy() (after verifying the source data fits in the destination array).

If you aren't comfortable doing memory management yourself, and are willing to (potentially) take a small performance hit, you could just use a std::vector (or whatever equivalent your game engine might offer)

Instead of storing your map data in a global variable, it might be better to wrap it in a map class that includes functions for loading the data. Then you would store a pointer to an instance of that map class in a game class that manages the current game session. The game class would call your drawMap function and pass in the current map object.

If you're new to C++ (or programming in general, by the sound of it), it might be too early to be looking at console game development. PC game development is usually easier (and without the devkit/licensing fees).
In response to DarkCampainger
DarkCampainger wrote:
If you're new to C++ (or programming in general, by the sound of it), it might be too early to be looking at console game development. PC game development is usually easier (and without the devkit/licensing fees).

Agreed with everything else in the post-- I think there might be a slight misunderstanding here, though. =p Pretty sure he means console as in command line/console window/console application
In response to DarkCampainger
You know, I've never really had any problems using std::vector when it comes to performance. Unless you're trying to develop something on the large scale or you don't optimize the functions that call to a vector object, then you might have some issues, but overall, as long as you pay attention, it's pretty simple.

Onto the topic at hand, DarkCampainger pretty much has most of the bases covered. The goal of building a usable game engine is to make it as soft-coded as possible. Designing code that reads from files that contain all of the necessary information makes your job a million times easier on the back-end when it comes down to making big changes.

But that's just me.
In response to Audeuro
Audeuro wrote:
Pretty sure he means console as in command line/console window/console application

That... would make a whole lot more sense! :x
In response to DarkCampainger
DarkCampainger wrote:
You should be loading your maps from a file, not hard-coding them into a static array. If your maps can be different sizes, you'll want to dynamically allocate the array that stores the values. Also, it's usually easier to create a one-dimensional array and just do a little arithmetic to convert a 2D coordinate into an index.

If you want to copy one array of POD data into another, you can use memcpy() (after verifying the source data fits in the destination array).

If you aren't comfortable doing memory management yourself, and are willing to (potentially) take a small performance hit, you could just use a std::vector (or whatever equivalent your game engine might offer)

Instead of storing your map data in a global variable, it might be better to wrap it in a map class that includes functions for loading the data. Then you would store a pointer to an instance of that map class in a game class that manages the current game session. The game class would call your drawMap function and pass in the current map object.

If you're new to C++ (or programming in general, by the sound of it), it might be too early to be looking at console game development. PC game development is usually easier (and without the devkit/licensing fees).

I redesigned my program so that it reads maps from a file into a char array, which is a member of a map class. This solves all of my problems - thanks for the suggestions!
I've been previously working on a C++ game using the SFML library to handle my graphics. I also came across the problem of not having to hard code anything and maintain promising performance at the same time (given the fact I'm redisplaying around 100ish sprites per second). So what I ended up doing is dabbing into some XML which is quite similar to what your doing and added a simple loop in the main game loop to convert/parse content in the XML file into sprites.

Note: Keep in mind collision had to be programmed individually.
In response to Wissam
Wissam wrote:
I've been previously working on a C++ game using the SFML library to handle my graphics. I also came across the problem of not having to hard code anything and maintain promising performance at the same time (given the fact I'm redisplaying around 100ish sprites per second). So what I ended up doing is dabbing into some XML which is quite similar to what your doing and added a simple loop in the main game loop to convert/parse content in the XML file into sprites.

Note: Keep in mind collision had to be programmed individually.

Hmm...sounds like something I might like to explore. I'm fairly certain that learning about programming will never end - it's an academic pursuit!