I assign each type of block an ID number and then store all the information for the level in a text file that has a block type, location, and size for each block. Then I made a level designer so that I would not have to type all these values in manually.
Here is the code for the level loader and a sample level is attached.
void loadlevel(int);
void loadlevel(int level){
char temp[21];//=new char[12];
int temp2[3];
temp[0]='l';
temp[1]='e';
temp[2]='v';
temp[3]='e';
temp[4]='l';
temp[5]='s';
temp[6]='/';
temp[7]='/';
temp[8]='l';
temp[9]='e';
temp[10]='v';
temp[11]='e';
temp[12]='l';
temp[13]=(char)((int)level/100+48);
temp[14]=(char)((int)level/10+48);
temp[15]=(char)((int)level+48);
temp[16]='.';
temp[17]='t';
temp[18]='x';
temp[19]='t';
temp[20]='\0';
ifstream levelload(temp);
levelload>>end;
count=0;
while(count<end){
levelload>>temp2[0];
if(temp2[0]<specials){//for static collision blocks and non colision blocks
floorblks[count]=nextsprite;
levelload>>temp2[1];
levelload>>temp2[2];
dbSprite(nextsprite,temp2[1],temp2[2],temp2[0]);
levelload>>temp2[1];
levelload>>temp2[2];
dbSizeSprite(nextsprite,temp2[1],temp2[2]);
nextsprite++;
count++;
}else{
//for loading specials no specials yet
}
}
}
Of course you need an image for each of the ID's which come first on each line of the level file. Oh, and I have been keeping my levels in a folder called levels, so that explains the
temp[0]='l';
temp[1]='e';
temp[2]='v';
temp[3]='e';
temp[4]='l';
temp[5]='s';
temp[6]='/';
temp[7]='/';
.
Also, here is the code for my level designer.
Main.cpp:
// Dark GDK - The Game Creators - www.thegamecreators.com
// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application
// whenever using Dark GDK you must ensure you include the header file
#include "methods.h"
int off[2]={0};
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
loadobjects();
mouse=nextsprite;
dbSprite(mouse,dbMouseX(),dbMouseY(),1);
dbHideSprite(mouse);
nextsprite++;
dbSizeSprite(mouse,2,2);
dbDisableEscapeKey();
// our main loop
loadold();
while ( LoopGDK ( ) )
{
dbSprite(mouse,dbMouseX(),dbMouseY(),1);
if(dbKeyState(59)&&end<199){
addblock();
}
if(dbKeyState(210)){
offset[1]=0;
offset[0]=0;
}
if(dbKeyState(60) && currentblock==-1 && resize==-1 && dbMouseClick() && mousestate==0){
mousestate=1;
count=0;
while(count<end){
if(dbSpriteCollision(mouse,blocks[count])){
dbCloneSprite(blocks[count],nextsprite);
blocks[end]=nextsprite;
currentblock=end;
nextsprite++;
end++;
break;
}
count++;
}
}
if(currentblock!=-1 && dbKeyState(211)){
dbDeleteSprite(blocks[currentblock]);
count=currentblock+1;
while(count<end){
blocks[count-1]=blocks[count];
count++;
}
end--;
currentblock=-1;
}
if(currentblock!=-1){
dbSprite(blocks[currentblock],dbMouseX()-off[0],dbMouseY()-off[1],dbSpriteImage(blocks[currentblock]));
}
if(dbMouseClick()==0){
mousestate=0;
}
if(dbMouseClick()&& mousestate==0 && currentblock!=-1 && resize==-1 && dbShiftKey()==0){
mousestate=1;
off[0]=0;
off[1]=0;
currentblock=-1;
}
if(dbMouseClick()&& mousestate==0 && currentblock==-1 && resize==-1 && dbShiftKey()==0){
mousestate=1;
count=0;
while(count<end){
if(dbSpriteCollision(mouse,blocks[count])){
off[1]=dbMouseY()-dbSpriteY(blocks[count]);
off[0]=dbMouseX()-dbSpriteX(blocks[count]);
currentblock=count;
}
count++;
}
}
if(dbShiftKey() && dbMouseClick()&& mousestate==0 && currentblock==-1 && resize==-1){
mousestate=1;
count=0;
while(count<end){
if(dbSpriteCollision(mouse,blocks[count])){
resize=count;
}
count++;
}
}
if(resize!=-1){
/*if(dbLeftKey() && dbSpriteWidth(blocks[resize])>5){
dbSizeSprite(blocks[resize],dbSpriteWidth(blocks[resize])-1,dbSpriteHeight(blocks[resize]));
}
if(dbRightKey()){
dbSizeSprite(blocks[resize],dbSpriteWidth(blocks[resize])+1,dbSpriteHeight(blocks[resize]));
}
if(dbDownKey()){
dbSizeSprite(blocks[resize],dbSpriteWidth(blocks[resize]),dbSpriteHeight(blocks[resize])+1);
}
if(dbUpKey() && dbSpriteHeight(blocks[resize])>5){
dbSizeSprite(blocks[resize],dbSpriteWidth(blocks[resize]),dbSpriteHeight(blocks[resize])-1);
}*/
dbSizeSprite(blocks[resize],dbMouseX()-dbSpriteX(blocks[resize]),dbMouseY()-dbSpriteY(blocks[resize]));
if(dbSpriteHeight(blocks[resize])<5){
dbSizeSprite(blocks[resize],dbSpriteWidth(blocks[resize]),5);
}
if(dbSpriteWidth(blocks[resize])<5){
dbSizeSprite(blocks[resize],5,dbSpriteHeight(blocks[resize]));
}
}
if(dbReturnKey()){
resize=-1;
}
if(resize==-1){
if(dbLeftKey()){
count=0;
while(count<end){
dbSprite(blocks[count],dbSpriteX(blocks[count])+10,dbSpriteY(blocks[count]),dbSpriteImage(blocks[count]));
count++;
}
offset[0]-=10;
}
if(dbRightKey()){
count=0;
while(count<end){
dbSprite(blocks[count],dbSpriteX(blocks[count])-10,dbSpriteY(blocks[count]),dbSpriteImage(blocks[count]));
count++;
}
offset[0]+=10;
}
if(dbDownKey()){
count=0;
while(count<end){
dbSprite(blocks[count],dbSpriteX(blocks[count]),dbSpriteY(blocks[count])-10,dbSpriteImage(blocks[count]));
count++;
}
offset[1]+=10;
}
if(dbUpKey()){
count=0;
while(count<end){
dbSprite(blocks[count],dbSpriteX(blocks[count]),dbSpriteY(blocks[count])+10,dbSpriteImage(blocks[count]));
count++;
}
offset[1]-=10;
}
}
if(dbKeyState(15)){
count=0;
while(count<end){
dbSprite(blocks[count],dbSpriteX(blocks[count])+offset[0],dbSpriteY(blocks[count])+offset[1],dbSpriteImage(blocks[count]));
count++;
}
offset[0]=0;
offset[1]=0;
}
dbText(0,0,dbStr(end));
// update the screen
dbSync ( );
}
ofstream levelsave("level.txt");
levelsave<<end<<endl;
count=0;
while(count<end){
levelsave<<dbSpriteImage(blocks[count])<<" "<<dbSpriteX(blocks[count])+offset[0]<<" "<<dbSpriteY(blocks[count])+offset[1]<<" "<<dbSpriteWidth(blocks[count])<<" "<<dbSpriteHeight(blocks[count])<<endl;
count++;
}
// return back to windows
return;
}
methods.h
#include "DarkGDK.h"
using namespace std;
#include <fstream>
//#include <string>
int nextsprite=1,blocks[200],currentblock=-1,end=0,images=0,count=0,nextpos=0,mouse,mousestate,resize=-1,offset[2];
void loadobjects(void);
void loadobjects(void){
dbSetImageColorKey(255,255,255);
char* name=new char[60];
ifstream loadfiles("rec\\blocks.txt");
loadfiles.getline(name,60);
while(loadfiles.fail()==false){
images++;
dbLoadImage(name,images);
loadfiles.getline(name,60);
}
//delete loadfiles;
/*
images++;
dbLoadImage("rec\\bdcr.bmp",images);
images++;
dbLoadImage("rec\\darkrock.bmp",images);
*/
while(count<images){
count++;
dbSprite(nextsprite,0,0,count);
dbHideSprite(nextsprite);
nextsprite++;
}
}
void addblock(void);
void addblock(void){
count=0;
nextpos=0;
while(count<images){
count++;
dbShowSprite(count);
dbSprite(count,nextpos,0,count);
dbSizeSprite(count,100,100);
nextpos+=105;
/*count++;
dbShowSprite(count);
dbSprite(count,nextpos,0,count);
dbSizeSprite(count,100,100);
nextpos+=105;
*/
}
bool done=false;
while(done==false && LoopGDK ( )){
dbSprite(mouse,dbMouseX(),dbMouseY(),1);
if(dbEscapeKey()){
count=0;
while(count<images){
count++;
dbHideSprite(count);
}
break;
}
if(dbLeftKey()){
count=0;
while(count<images){
count++;
dbSprite(count,dbSpriteX(count)-10,0,count);
}
}
if(dbRightKey()){
count=0;
while(count<images){
count++;
dbSprite(count,dbSpriteX(count)+10,0,count);
}
}
if(dbMouseClick()){
count=0;
while(count<images){
count++;
if(dbSpriteCollision(count,mouse)){
blocks[end]=nextsprite;
dbSprite(nextsprite,dbMouseX(),dbMouseY(),count);
currentblock=end;
end++;
nextsprite++;
count=0;
while(count<images){
count++;
dbHideSprite(count);
}
done=true;
mousestate=1;
break;
}
}
}
dbSync();
}
}
void loadold(void);
void loadold(void){
ifstream levelload("level.txt");
int tempload[4]={0};
levelload>>tempload[0];
if(tempload[0]>0){
count=0;
while(count<tempload[0]){
levelload>>tempload[1]>>tempload[2]>>tempload[3];
dbSprite(nextsprite,tempload[2],tempload[3],tempload[1]);
levelload>>tempload[1]>>tempload[2];
dbSizeSprite(nextsprite,tempload[1],tempload[2]);
blocks[end]=nextsprite;
nextsprite++;
end++;
count++;
}
//delete levelload;
}
}
Also attached is a readme for the designer.
Good luck with designer though the code is quite messy. You'd be better off making your own for the most part than trying to interpret mine. I don't even bother with it anymore. It just works.
Considering that though, you could just use my designer!
Important: To use any of this source code you must run in release mode.