Thread: Level creation, loading, management etc

  1. #1
    Registered User
    Join Date
    Jun 2005

    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?

    Thanks for any help on the subject. I would greatly appreciate it.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    you may want to look into binary file I/O, and by extension, data structures:
    #include<iostream>	//for console I/O
    #include<fstream>	//for file I/O
    #include<cstdlib>	//for rand function
    #include<ctime>		//for time function
    struct myBoard	//to be saved to and read from a file
    	int layout[20][60];	//your board
    int main()
    	srand(static_cast<unsigned int>(time(0)));	//seed the PRNG
    	myBoard board;	//instantiate the struct
    	myBoard testboard;	//for testing purposes
    	//open a file for binary writing
    	std::ofstream ofile("",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(register short int x=0;x<20;x++)	//loop through the colums
    		for(register short int y=0;y<60;y++)	//loop through the rows
    	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("",std::ios::binary);
    	//read it into a different instance of the same struct (for testing)<char*>(&testboard),sizeof(testboard));
    	for(register short int x=0;x<20;x++)	//loop through the colums
    		for(register short int y=0;y<60;y++)	//loop through the rows
    			//if the boards match
    			else	//if there's a discrepancy between the original
    			{	//map and the one you just read
    				std::cerr<<"DATA CORRUPTION DETECTED\n";
    	infile.close();	//close the file
    	return 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.
    Last edited by major_small; 08-02-2005 at 11:12 PM. Reason: color-happy syntax highlighter
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. Dynamic Object Creation (and Management)...
    By Comrade_Yeti in forum C++ Programming
    Replies: 3
    Last Post: 07-31-2005, 01:44 PM
  3. Newton + Einstein were wrong!
    By Jez in forum A Brief History of
    Replies: 64
    Last Post: 12-14-2004, 02:24 PM
  4. Loading a new level
    By harry_p in forum Game Programming
    Replies: 1
    Last Post: 07-15-2002, 12:37 PM
  5. Loading Screen
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2002, 11:48 PM