Original Post
Hiya I am currently making a game in C++ with SDL. It's 2D and so far I've managed to make a map and get a player to move around it. My problem comes when I try to add in collision detection with the surrounding tiles. Here is my code: Map.h (contains all classes to do with showing the map) Map.cpp This is where my collision detection is, and this causes the problems: At the moment if the player can move in a direction is totally random (or so it seems to me), although then he gets to the edge of the map, where the game promptly crashes. I am sure that this crash is due to trying to access non-exsistant places in the vector, but I don't know why my checks are stopping it. Any help would be much apprecieated.
#ifndef MAP_H
#define MAP_H
#include "Includes.h"
class Tile : public CSprite{
public:
bool passable;
void WipeBG();
void clearBG();
Tile(CSpriteBase *base, SDL_Surface *screen, bool passable);
void tDescribe();
void setOld(int x, int y);
};
class Layer{
public:
std::vector< std::vector<int> > Tiles;
std::vector< Tile* > TileSet;
};
class Map{
public:
std::string name;
std::string description;
std::vector< Layer > Layers;
void Draw(int x, int y);
int OldOffsetX, OldOffsetY;
Map();
};
#endif
#include "Includes.h"
Map::Map(){
OldOffsetX = 0;
OldOffsetY = 0;
}
void Map::Draw(int OffSetX, int OffSetY){
for(int lay = 0; lay < Layers.size(); lay ++)
{
for(int row = 0; row < Layers[lay].Tiles.size(); row ++)
{
for(int col = 0; col < Layers[lay].Tiles[row].size(); col++)
{
int curTile = this->Layers[lay].Tiles[row][col];
this->Layers[ lay ].TileSet[ curTile ]->setOld((col*TileWidth) + OldOffsetX, (row*TileHeight) + OldOffsetY);
this->Layers[ lay ].TileSet[ curTile ]->set((col*TileWidth) + OffSetX, (row*TileHeight) + OffSetY);
this->Layers[ lay ].TileSet[ curTile ]->WipeBG();
this->Layers[ lay ].TileSet[ curTile ]->draw();
}
}
}
this->OldOffsetX = OffSetX;
this->OldOffsetY = OffSetY;
}
Tile::Tile(CSpriteBase *base, SDL_Surface *screen, bool pass){
mSpriteBase = base;
if(mSpriteBase->mBuilt)
{
if(mSpriteBase->mNumframes>1) mAnimating=1;
mBackreplacement =
SDL_DisplayFormat(mSpriteBase->mAnim[0].image);
}
mScreen = screen;
mFrame = 0;
mDrawn = 0;
mLastupdate = 0;
passable = pass;
}
void Tile::clearBG()
{
if(mDrawn==1)
{
SDL_Rect dest;
dest.x = mOldX;
dest.y = mOldY;
dest.w = mSpriteBase->mW;
dest.h = mSpriteBase->mH;
SDL_FillRect(mBackreplacement, NULL, 0 );
//SDL_BlitSurface(mBackreplacement, NULL, mScreen, &dest);
}
}
void Tile::WipeBG()
{
SDL_Rect backSpace;
backSpace.x = mOldX;
backSpace.y = mOldY;
backSpace.w = this->GetW();
backSpace.h = this->GetH();
SDL_FillRect(mScreen, &backSpace, 0);
}
void Tile::tDescribe(){
printf("\n\tX and Y: %d, %d", this->X(), this->Y());
printf("\n\tOld X and Y: %d, %d", this->mOldX, this->mOldY);
}
void Tile::setOld(int x, int y){
mOldX = x;
mOldY = y;
}
void Player::Move(Direction dir, Map const *place){
int col = (GlobalX / TileWidth);
int row = (GlobalY / TileHeight);
int lay = GlobalLevel;
if(m_movingDir == None)
{
int nextTile;
if(dir==Up){
if(col-1 < 0)
return;
nextTile = place->Layers[lay].Tiles[row][col-1];
}
if(dir==Down){
if(col+1 >= place->Layers[lay].Tiles[row].size())
return;
nextTile = place->Layers[lay].Tiles[row][col+1];
}
if(dir==Right){
if(row+1 >= place->Layers[lay].Tiles.size())
return;
nextTile = place->Layers[lay].Tiles[row+1][col];
}
if(dir==Left){
if(row-1 < 0)
return;
nextTile = place->Layers[lay].Tiles[row-1][col];
}
if(place->Layers[ lay ].TileSet[ nextTile ]->passable)
m_movingDir = dir;
}
}