![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
I am attempting to write a side scroller using SDL. First I am experimenting with creating a map using two dimensional arrays. The array I am currently using is an integer array or 5 by 10. Value of 0 means that it represents the sky, 1 represents grass, and 2 represents dirt.
Here is how the array looks: {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2,2,2}
};The problem: Somewhere in my code is a bug which throws off the position of the tiles :mad: ![]() The grass should be flat across the whole thing. Here is my code: #include <SDL.h>
#include <iostream>
const int TILESIZE = 32;
const int LEVELHEIGHT = 15;
const int LEVELWIDTH = 100;
const int SKY = 0;
const int GRASS = 1;
const int DIRT = 2;
SDL_Surface* screen;
SDL_Surface* sky;
SDL_Surface* grass;
SDL_Surface* dirt;
SDL_Event event;
int map [10][5];
class Tile
{
private:
int type;
bool solid;
SDL_Surface* image;
SDL_Rect box;
public:
Tile(int x, int y, bool s, int t, SDL_Surface* img);
SDL_Surface* getImg(){return image;}
SDL_Rect getBox(){return box;}
bool IsSolid(){return solid;}
};
Tile::Tile(int x, int y, bool s, int t, SDL_Surface* img)
{
box.x = x;
box.y = y;
box.w = TILESIZE;
box.h = TILESIZE;
solid = s;
type = t;
image = img;
}
void MakeMap()
{
for(int y = 0; y<5;y++)
{
for(int x = 0; x<10;x++)
{
if(y <=2)
{
map[y][x] = 0;
}
if(y == 3)
{
map[y][x] = 1;
}
if(y == 4)
{
map[y][x] = 2;
}
}
}
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1 )
{
return false;
}
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
atexit(SDL_Quit);
return true;
}
SDL_Surface* LoadBitmap( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
void ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination,SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset );
}
void LoadMap(Tile* TileM[5][10])
{
for(int y = 0; y<5;y++)
{
for(int x= 0; x<10;x++)
{
switch(map[y][x])
{
case 0:
TileM[y][x] = new Tile(x*TILESIZE,y*TILESIZE,false,0,sky);
break;
case 1:
TileM[y][x] = new Tile(x*TILESIZE,y*TILESIZE,true,1,grass);
break;
case 2:
TileM[y][x] = new Tile(x*TILESIZE,y*TILESIZE,true,2,dirt);
break;
}
}
}
}
void DrawMap(Tile* TileM[5][10])
{
for(int y = 0; y<5; y++)
{
for(int x = 0; x<10; x++)
{
ApplySurface(TileM[y][x]->getBox().x,TileM[y][x]->getBox().y,TileM[y][x]->getImg(),screen);
}
}
}
int main(int argc, char** args)
{
bool quit = false;
Tile* TileMap[5][10];
if(!init())
{
return 1;
}
dirt = LoadBitmap("C:/Dirt.bmp");
grass = LoadBitmap("C:/Grass.bmp");
sky = LoadBitmap("C:/Sky.bmp");
MakeMap();
LoadMap(TileMap);
while(!quit)
{
while(SDL_PollEvent(&event ))
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}
DrawMap(TileMap);
if( SDL_Flip( screen ) == -1 )
{
return 2;
}
}
return 0;
}Thanks in advance |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
void DrawMap(Tile* TileM[5][10]) void DrawMap(Tile** TileM) |
|
|
|
|
|
#3 |
|
Professional Programmer
|
I figured out what I did wrong. It was the declaration of the array.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,192
Rep Power: 5
![]() |
Care to enlighten further? I'm assuming that the problem is that the dimensions of one of your arrays were in the wrong order ([10][5] when it should have been [5][10]).
|
|
|
|
|
|
#5 |
|
Professional Programmer
|
Yes the dimensions were in reverse order. At this point I am trying to figure out the best way to store a map so that it would be easy for editing. I will propably rewrite most of the code.
|
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Quote:
<!-- Standard XML Header here (I am a comment)-->
<level>
<!-- Define Values for terrain types -->
<types>
<sky>0</sky>
<grass>1</grass>
<dirt>2</dirt>
</types>
<!-- Our actual map data -->
<tiles width="10" height="5">
<row>0, 0, 0, 0, 0, 0, 0, 0, 0, 0</row>
<row>0, 0, 0, 0, 0, 0, 0, 0, 0, 0</row>
<row>0, 0, 0, 0, 0, 0, 0, 0, 0, 0</row>
<row>1, 1, 1, 1, 1, 1, 1, 1, 1, 1</row>
<row>2, 2, 2, 2, 2, 2, 2, 2, 2, 2</row>
</tiles>
</level>Now XML is not always the way to go, it can be overly verbose and is considered heavy wieght for many tasks. An advantage is others can easily write tools that work with your formats because they don't have to write a custom parser. Good luck on you project. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|