Problem swapping tiles from curser tile to map tile
Posted: Sat Aug 19, 2006 2:56 am
I am currently developing a 2d map editor to use as a tool for a project i'm working on. A vector of CTile objects, which hold all the tile attributes, is used to draw the map.
this is the class for CTile, tilenum is used to determine the tiles texture. This number is used inside a core::rect to determine the position of the tile in the main tile list image.
Now, my problem. I have another function, which is a member of the CMap class, called addTile(CTile* current_tile, int layer, core::position2d<s32> mouse):
The function addTile() is supposed to check the mouse position against the tiles in the map layer depending on which layer is currently active. But for some reason nothing happens. The only data being changed is the tile number. current_tile is the tile currently selected by the user in the tile list.
and lastly of course my drawing method.
I'm really lost as to what could be wrong, any help would be great.
Please ask if I should explain anything further.
Code: Select all
class CTile
{
public:
int getx() { return x; }
int gety() { return y; }
int gettilenum() { return tilenum; }
void settilenum(int ntilenum) { tilenum = ntilenum; }
void setx( int nx ) { x = nx; }
void sety( int ny ) { y = ny; }
private:
int x;
int y;
int tilenum;
};
Now, my problem. I have another function, which is a member of the CMap class, called addTile(CTile* current_tile, int layer, core::position2d<s32> mouse):
Code: Select all
class CMap
{
public:
CMap();
void swapTile(CTile* current_tile, int layer, int x, int y);
void setTileFromTileList(CMap* currentmap, CTile* current_tile,
core::position2d<s32> mouse);
vector<CTile> map; //layer 1
vector<CTile> map_l2; //layer 2
vector<CTile> map_l3; //layer 3
vector<CTile> map_e; //event layer
vector<CTile> getMap() { return map; }
CTileListTile tilelist;
vector< core::position2d<s32> > tilepositions;
void Default(CMap* _map, video::IVideoDriver* driver, int x, int y);
void setname(char new_name[MAX_PATH]);
void setmapx(int nx) { sizex = nx; }
void setmapy(int ny) { sizey = ny; }
void settilenum( int ntilenum ) { tilenum = ntilenum; }
int gettilenum() { return tilenum; }
void addTile(CTile* current_tile, int layer, core::position2d<s32> mouse);
int getmapx() { return sizex; }
int getmapy() { return sizey; }
char* getname() { return name; }
bool CMap::DrawMap(CMap* _map,
video::IVideoDriver* driver);
void gettilelist(IrrlichtDevice* device, video::IVideoDriver* driver);
void drawtilelist(CMap* currentmap, IGUIEnvironment* env, int wx, int wy, video::IVideoDriver* driver);
bool cleanup(CMap* currentmap, CTile* current_tile);
void ExportMap(char* szFile, CMap* currentmap);
private:
int tilenum;
char name[MAX_PATH];
int sizex;
int sizey;
int eventnum;
};
Code: Select all
void CMap::addTile(CTile* current_tile, int layer, core::position2d<s32> mouse)
{
int winx = WINX;
int winy = WINY;
core::rect<s32> window_clip(-32,-17,winx - 53,winy - 15);
switch(layer)
{
case 1:
if(window_clip.isPointInside(mouse))
{
for(int i = 0; i<map.size(); i++)
{
if((mouse.X + (main_scrolloffset_H))/32 == map[i].getx() && (mouse.Y + (main_scrolloffset_V))/32 == map[i].gety())
{
map[i].settilenum(current_tile->gettilenum());
}
}
}
break;
case 2:
if(window_clip.isPointInside(mouse))
{
for(int i = 0; i<map_l2.size(); i++)
{
if((mouse.X + (main_scrolloffset_H))/32 == map_l2[i].getx() && (mouse.Y + (main_scrolloffset_V))/32 == map_l2[i].gety())
{
map_l2[i].settilenum(current_tile->gettilenum());
}
}
}
break;
case 3:
if(window_clip.isPointInside(mouse))
{
for(int i = 0; i<map_l3.size(); i++)
{
if((mouse.X + (main_scrolloffset_H))/32 == map_l3[i].getx() && (mouse.Y + (main_scrolloffset_V))/32 == map_l3[i].gety())
{
map_l3[i].settilenum(current_tile->gettilenum());
}
}
}
break;
};
}
Code: Select all
//draws the current map to the main window
bool CMap::DrawMap(CMap* _map, video::IVideoDriver* driver)
{
//this is the current tilenum
int layer1tile = 0;
int layer2tile = 0;
int layer3tile = 0;
video::ITexture* image = tilelist.getImage();
if(image == NULL)
return false;
//window dimensions
int winx = WINX;
int winy = WINY;
//the size of the viewable window to clip the map against
core::rect<s32> window_clip(-32,-17,winx - 53,winy - 15);
//draw layers
for(int i = 0; i <= _map->map.size()-1; i++)
{
int x = _map->map[i].getx();
int y = _map->map[i].gety();
//position of the tile to be drawn
core::position2d<s32> pos(x*32 - main_scrolloffset_H,
y*32 + toolbar_offsety - main_scrolloffset_V);
if(window_clip.isPointInside(pos))
{
//these are the tile numbers for the 3 layers to be checked so no two same tiles are drawn over eachother.
layer1tile = _map->map[i].gettilenum();
layer2tile = _map->map_l2[i].gettilenum();
layer3tile = _map->map_l3[i].gettilenum();
core::rect<s32> rect1((layer1tile - 1)*32,0,layer1tile*32,32);
core::rect<s32> rect2((layer2tile - 1)*32,0,layer2tile*32,32);
core::rect<s32> rect3((layer3tile - 1)*32,0,layer3tile*32,32);
if(layer1tile == layer2tile || layer1tile == layer3tile)
{
driver->draw2DImage(tilelist.getImage(), pos,
rect2, 0,
video::SColor(255,255,255,255), true);
driver->draw2DImage(tilelist.getImage(), pos,
rect3, 0,
video::SColor(255,255,255,255), true);
}
else if(layer3tile == layer2tile)
{
driver->draw2DImage(tilelist.getImage(), pos,
rect3, 0,
video::SColor(255,255,255,255), true);
}else
{
driver->draw2DImage(tilelist.getImage(), pos,
rect1, 0,
video::SColor(255,255,255,255), true);
driver->draw2DImage(tilelist.getImage(), pos,
rect2, 0,
video::SColor(255,255,255,255), true);
driver->draw2DImage(tilelist.getImage(), pos,
rect3, 0,
video::SColor(255,255,255,255), true);
}
}
}
return true;
}
Please ask if I should explain anything further.