I'm working on a basic tile-based side-scrolling engine. Here's the code so far. (I know there are errors in array usage. I'm trying to fix them.)
Level.h
#ifndef LEVEL_H
#define LEVEL_H
//This is the starting number for sprite ID's
//that are used in level drawing.
#define LEVEL_SPRITE_BEGIN 8,000
#include <vector>
#include "Typedefs.h"
class cLevel
{
protected:
//Level descriptors
uInt levelLength;
uInt levelHeight;
uInt numOfLayers;
uInt tileWidth;
uInt tileHeight;
//The RGB values for transparency
uChar transparentRed;
uChar transparentGreen;
uChar transparentBlue;
//Variables for scrolling
int levelPosX;
int levelOffsetX;
//Array of bool's for checking whether
//or not a layer has been defined.
bool layerIsDefined[19];
public:
std::vector<uInt*> tileMaps;
cLevel ( uInt length, uInt height, uInt tileW, uInt tileH, uInt numLayer ) {
levelLength = length;
levelHeight = height;
numOfLayers = numLayers;
}
//Assign an array of uInt's to a layer.
void DefineLayer ( uInt* tileArray, uInt layer );
//Draw the specified layer. If layer == 0, all layers
//will be drawn in order.
void DrawMap ( int xPos, int yPos, uInt layer );
};
#endif
Level Drawing.cpp
#include "DarkGDK.h"
#include "Typedefs.h"
#include "Level.h"
void cLevel::DefineLayer ( uInt* mapArray, uInt layer )
{
if ( layerIsDefined[layer-1] )
return;
else
{
if ( tileMaps.size ( ) < layer )
tileMaps.resize ( layer );
tileMaps[layer-1] = mapArray;
layerIsDefined[layer-1] = true;
}
}
void cLevel::DrawMap ( int xPos, int yPos, uInt layer )
{
//Draw all layers if layer == 0.
if ( layer == 0 )
{
for ( int layer_i = 0; layer_i < (layer-1); layer_i++ )
{
if ( layerIsDefined[layer_i] )
{
//Drawing loop
uInt* layerArray = tileMaps[layer_i];
int i = 0;
for ( int x = 0; x < levelLength; x++ )
{
for ( int y = 0; y < levelHeight; y++ )
{
dbSprite ( LEVEL_SPRITE_BEGIN + i, xPos + (x*tileWidth),
yPos + (y*tileHeight), layerArray[x][y] );
i++;
}
}
//End of drawing loop
}
}
}
//Otherwise, draw the specified layer.
else
{
if ( layerIsDefined[layer-1] )
{
//Drawing loop
uInt* layerArray = tileMaps[layer-1];
int i = 0;
for ( int x = 0; x < levelLength; x++ )
{
for ( int y = 0; y < levelHeight; y++ )
{
dbSprite ( LEVEL_SPRITE_BEGIN + i, xPos + (x*tileWidth),
yPos + (y*tileHeight), layerArray[x][y] );
i++;
}
}
//End of drawing loop
}
}
}
And Main.cpp
#include <DarkGDK.h>
#include "Typedefs.h"
#include "Sprite Resources.h"
#include "Level.h"
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbSetDisplayMode ( 640, 480, 32 );
dbSetDir ( "Data\\" );
LoadTileResources ( );
cLevel level ( 50, 15, 32, 32, 1 );
uInt layerOne[50][15] =
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
uInt* layerOnePtr = layerOne;
level.DefineLayer ( layerOne, 1 );
level.DrawMap ( 0, 0, 0 );
while ( LoopGDK ( ) )
{
dbSync ( );
}
return;
}
I'm not sure how to correct the errors in array logic. I'd like to use the class in this manner (passing arrays of uInt's).
Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo