Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 1st, 2006, 7:18 PM   #1
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Angry Array Problems?

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
Wizard1988 is offline   Reply With Quote
Old Jul 1st, 2006, 8:02 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
void DrawMap(Tile* TileM[5][10])
This looks somewhat odd. Shouldn't it be:
void DrawMap(Tile** TileM)
However, C++ has a lot of alternate ways of doing things, so it could be that your function is perfectly valid.
Arevos is offline   Reply With Quote
Old Jul 1st, 2006, 8:11 PM   #3
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
I figured out what I did wrong. It was the declaration of the array.
Wizard1988 is offline   Reply With Quote
Old Jul 1st, 2006, 8:18 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
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]).
grumpy is offline   Reply With Quote
Old Jul 1st, 2006, 8:28 PM   #5
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
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.
Wizard1988 is offline   Reply With Quote
Old Jul 2nd, 2006, 6:03 PM   #6
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Quote:
Originally Posted by Wizard1988
At this point I am trying to figure out the best way to store a map so that it would be easy for editing.
A simple text format should suit you well. XML is quite trendy now and there are some easy to use and lightweight parsers like TinyXML (which I use extsively) which make working with it a breeze. You could create a file that looked like this:

<!-- 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.
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC