Level creation, loading, management etc

This is a discussion on Level creation, loading, management etc within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. I'm relatively new to the programming world and I'm just starting out with some basic 2D games. I ...

Level creation, loading, management etc

Hey everyone. I'm relatively new to the programming world and I'm just starting out with some basic 2D games. I completed a Pong clone a few days ago using SDL and now I want to move onto something similar to the game BreakOut, if anyone is familiar with it.

The main hurdle for me is that I want this game to include a bunch of different levels with different layouts. For those who are unfamiliar with BreakOut, it's basically just hitting a ball off of a paddle upwards and hitting it off of a bunch of blocks to destroy them. So basically I want to load in different levels with different block layouts, backgrounds, colors etc. The only problem is that I have absolutely no idea how this can be accomplished

How are files saved in such a way that they can be read from and interpreted by a program? Say I wanted to store the level layout in an array. How can I store this array in a file in such a way that it can be read into my main program and then acted upon? I know there must be a way, since all professional games do it, but I'm just not sure how it's done. Do I need something like a level editor to accomplish this?

you may want to look into binary file I/O, and by extension, data structures:

Code:

#include<iostream> //for console I/O#include<fstream> //for file I/O#include<cstdlib> //for rand function#include<ctime> //for time functionstruct myBoard //to be saved to and read from a file
{
int layout[20][60]; //your board
};
int main()
{
srand(static_cast<unsignedint>(time(0))); //seed the PRNG
myBoard board; //instantiate the struct
myBoard testboard; //for testing purposes//open a file for binary writing
std::ofstream ofile("test.in",std::ios::trunc|std::ios::binary);
/*
* You would need to create a level generator - I'm using a simple
* one that just throws a random number into each spot - it would
* be no use to you.
*/for(registershortint x=0;x<20;x++) //loop through the colums
{
for(registershortint y=0;y<60;y++) //loop through the rows
{
board.layout[x][y]=rand()%10;
}
}
ofile.write(reinterpret_cast<char*>(&board),sizeof(board)); //write the struct
ofile.close(); //close the file/*
* now you need to create a program to read the file(s) you just wrote
* again, this code will only show you how to do it - you'll need to
* make some modifications to tailor it to your own uses
*///open a file for binary reading
std::ifstream infile("test.in",std::ios::binary);
//read it into a different instance of the same struct (for testing)
infile.read(reinterpret_cast<char*>(&testboard),sizeof(testboard));
for(registershortint x=0;x<20;x++) //loop through the colums
{
for(registershortint y=0;y<60;y++) //loop through the rows
{
//if the boards matchif(testboard.layout[x][y]==board.layout[x][y])
{
std::cout<<board.layout[x][y];
}
else//if there's a discrepancy between the original
{ //map and the one you just read
std::cerr<<"DATA CORRUPTION DETECTED\n";
break;
}
}
std::cout<<'\n';
}
infile.close(); //close the filereturn 0;
}

as you may or may not be able to pick up, structs can hold pretty much any data type (and not just one at a time, either). you can use an integer for the color, a string for the name, etc.