- added null checks for map editor

This commit is contained in:
Mark Vejvoda
2011-01-29 21:44:09 +00:00
parent f2467faebe
commit d4370675be
2 changed files with 264 additions and 232 deletions

View File

@@ -489,8 +489,10 @@ void MainWindow::onPaint(wxPaintEvent &event) {
if(panel) panel->Update(); if(panel) panel->Update();
if(menuBar) menuBar->Update(); if(menuBar) menuBar->Update();
if(program && glCanvas) {
program->renderMap(glCanvas->GetClientSize().x, glCanvas->GetClientSize().y); program->renderMap(glCanvas->GetClientSize().x, glCanvas->GetClientSize().y);
glCanvas->SwapBuffers(); glCanvas->SwapBuffers();
}
event.Skip(); event.Skip();
} }

View File

@@ -155,12 +155,13 @@ Program::Program(int w, int h) {
Program::~Program() { Program::~Program() {
delete map; delete map;
map = NULL;
} }
int Program::getObject(int x, int y) { int Program::getObject(int x, int y) {
int i=(x - ofsetX) / cellSize; int i=(x - ofsetX) / cellSize;
int j= (y + ofsetY) / cellSize; int j= (y + ofsetY) / cellSize;
if (map->inside(i, j)) { if (map && map->inside(i, j)) {
return map->getObject(i,j); return map->getObject(i,j);
} }
else { else {
@@ -171,7 +172,7 @@ int Program::getObject(int x, int y) {
int Program::getResource(int x, int y) { int Program::getResource(int x, int y) {
int i=(x - ofsetX) / cellSize; int i=(x - ofsetX) / cellSize;
int j= (y + ofsetY) / cellSize; int j= (y + ofsetY) / cellSize;
if (map->inside(i, j)) { if (map && map->inside(i, j)) {
return map->getResource(i,j); return map->getResource(i,j);
} }
else { else {
@@ -181,27 +182,27 @@ int Program::getResource(int x, int y) {
// TODO: move editor-specific code from shared_lib to here. // TODO: move editor-specific code from shared_lib to here.
void Program::glestChangeMapHeight(int x, int y, int Height, int radius) { void Program::glestChangeMapHeight(int x, int y, int Height, int radius) {
map->glestChangeHeight((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, Height, radius); if(map) map->glestChangeHeight((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, Height, radius);
} }
void Program::pirateChangeMapHeight(int x, int y, int Height, int radius) { void Program::pirateChangeMapHeight(int x, int y, int Height, int radius) {
map->pirateChangeHeight((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, Height, radius); if(map) map->pirateChangeHeight((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, Height, radius);
} }
void Program::changeMapSurface(int x, int y, int surface, int radius) { void Program::changeMapSurface(int x, int y, int surface, int radius) {
map->changeSurface((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, static_cast<MapSurfaceType>(surface), radius); if(map) map->changeSurface((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, static_cast<MapSurfaceType>(surface), radius);
} }
void Program::changeMapObject(int x, int y, int object, int radius) { void Program::changeMapObject(int x, int y, int object, int radius) {
map->changeObject((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, object, radius); if(map) map->changeObject((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, object, radius);
} }
void Program::changeMapResource(int x, int y, int resource, int radius) { void Program::changeMapResource(int x, int y, int resource, int radius) {
map->changeResource((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, resource, radius); if(map) map->changeResource((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, resource, radius);
} }
void Program::changeStartLocation(int x, int y, int player) { void Program::changeStartLocation(int x, int y, int player) {
map->changeStartLocation((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, player); if(map) map->changeStartLocation((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, player);
} }
void Program::setUndoPoint(ChangeType change) { void Program::setUndoPoint(ChangeType change) {
@@ -240,22 +241,23 @@ bool Program::redo() {
} }
void Program::renderMap(int w, int h) { void Program::renderMap(int w, int h) {
renderer.renderMap(map, ofsetX, ofsetY, w, h, cellSize, grid); if(map) renderer.renderMap(map, ofsetX, ofsetY, w, h, cellSize, grid);
} }
void Program::setRefAlt(int x, int y) { void Program::setRefAlt(int x, int y) {
map->setRefAlt((x - ofsetX) / cellSize, (y + ofsetY) / cellSize); if(map) map->setRefAlt((x - ofsetX) / cellSize, (y + ofsetY) / cellSize);
} }
void Program::flipX() { void Program::flipX() {
map->flipX(); if(map) map->flipX();
} }
void Program::flipY() { void Program::flipY() {
map->flipY(); if(map) map->flipY();
} }
void Program::mirrorX() { // copy left to right void Program::mirrorX() { // copy left to right
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
for (int i = 0; i < w/2; i++) { for (int i = 0; i < w/2; i++) {
@@ -276,8 +278,10 @@ void Program::mirrorX() { // copy left to right
} }
} }
} }
}
void Program::mirrorY() { // copy top to bottom void Program::mirrorY() { // copy top to bottom
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
for (int i = 0; i < w; i++) { for (int i = 0; i < w; i++) {
@@ -298,8 +302,10 @@ void Program::mirrorY() { // copy top to bottom
} }
} }
} }
}
void Program::mirrorXY() { // copy leftbottom to topright, can handle non-sqaure maps void Program::mirrorXY() { // copy leftbottom to topright, can handle non-sqaure maps
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
if (h==w) { if (h==w) {
@@ -339,8 +345,10 @@ void Program::mirrorXY() { // copy leftbottom to topright, can handle non-sqaure
} }
} }
} }
}
void Program::rotatecopyX() { void Program::rotatecopyX() {
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
for (int i = 0; i < w/2; i++) { for (int i = 0; i < w/2; i++) {
@@ -361,8 +369,10 @@ void Program::rotatecopyX() {
} }
} }
} }
}
void Program::rotatecopyY() { void Program::rotatecopyY() {
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
for (int i = 0; i < w; i++) { for (int i = 0; i < w; i++) {
@@ -383,8 +393,10 @@ void Program::rotatecopyY() {
} }
} }
} }
}
void Program::rotatecopyXY() { void Program::rotatecopyXY() {
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
int sw=w/h; if(sw<1) sw=1; // x-squares per y int sw=w/h; if(sw<1) sw=1; // x-squares per y
@@ -414,8 +426,10 @@ void Program::rotatecopyXY() {
} }
} }
} }
}
void Program::rotatecopyCorner() { // rotate top left 1/4 to top right 1/4 void Program::rotatecopyCorner() { // rotate top left 1/4 to top right 1/4
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH(); int h=map->getH();
if (h==w) { if (h==w) {
@@ -455,9 +469,11 @@ void Program::rotatecopyCorner() { // rotate top left 1/4 to top right 1/4
} }
} }
} }
}
void Program::shiftLeft() { void Program::shiftLeft() {
if(map) {
int w=map->getW()-1; int w=map->getW()-1;
int h=map->getH(); int h=map->getH();
for (int i=0; i<w; i++) for (int i=0; i<w; i++)
@@ -466,7 +482,9 @@ void Program::shiftLeft() {
for (int i = 0; i < map->getMaxFactions(); ++i) // move players for (int i = 0; i < map->getMaxFactions(); ++i) // move players
map->changeStartLocation(map->getStartLocationX(i)-1, map->getStartLocationY(i), i); // it allready check limits map->changeStartLocation(map->getStartLocationX(i)-1, map->getStartLocationY(i), i); // it allready check limits
} }
}
void Program::shiftRight() { void Program::shiftRight() {
if(map) {
int w=map->getW()-1; int w=map->getW()-1;
int h=map->getH(); int h=map->getH();
for (int i=w; i>0; i--) for (int i=w; i>0; i--)
@@ -476,7 +494,9 @@ void Program::shiftRight() {
if (map->getStartLocationX(i) != 0 || map->getStartLocationY(i) != 0) // don't move the unset ones if (map->getStartLocationX(i) != 0 || map->getStartLocationY(i) != 0) // don't move the unset ones
map->changeStartLocation(map->getStartLocationX(i)+1, map->getStartLocationY(i), i); // it allready check limits map->changeStartLocation(map->getStartLocationX(i)+1, map->getStartLocationY(i), i); // it allready check limits
} }
}
void Program::shiftUp() { void Program::shiftUp() {
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH()-1; int h=map->getH()-1;
for (int i=0; i<w; i++) for (int i=0; i<w; i++)
@@ -485,7 +505,9 @@ void Program::shiftUp() {
for (int i = 0; i < map->getMaxFactions(); ++i) // move players for (int i = 0; i < map->getMaxFactions(); ++i) // move players
map->changeStartLocation(map->getStartLocationX(i), map->getStartLocationY(i)-1, i); // it allready check limits map->changeStartLocation(map->getStartLocationX(i), map->getStartLocationY(i)-1, i); // it allready check limits
} }
}
void Program::shiftDown() { void Program::shiftDown() {
if(map) {
int w=map->getW(); int w=map->getW();
int h=map->getH()-1; int h=map->getH()-1;
for (int i=0; i<w; i++) for (int i=0; i<w; i++)
@@ -495,55 +517,62 @@ void Program::shiftDown() {
if (map->getStartLocationX(i) != 0 || map->getStartLocationY(i) != 0) // don't move the unset ones if (map->getStartLocationX(i) != 0 || map->getStartLocationY(i) != 0) // don't move the unset ones
map->changeStartLocation(map->getStartLocationX(i), map->getStartLocationY(i)+1, i); // it allready check limits map->changeStartLocation(map->getStartLocationX(i), map->getStartLocationY(i)+1, i); // it allready check limits
} }
}
void Program::randomizeMapHeights() { void Program::randomizeMapHeights() {
map->randomizeHeights(); if(map) map->randomizeHeights();
} }
void Program::randomizeMap() { void Program::randomizeMap() {
map->randomize(); if(map) map->randomize();
} }
void Program::switchMapSurfaces(int surf1, int surf2) { void Program::switchMapSurfaces(int surf1, int surf2) {
map->switchSurfaces(static_cast<MapSurfaceType>(surf1), static_cast<MapSurfaceType>(surf2)); if(map) map->switchSurfaces(static_cast<MapSurfaceType>(surf1), static_cast<MapSurfaceType>(surf2));
} }
void Program::reset(int w, int h, int alt, int surf) { void Program::reset(int w, int h, int alt, int surf) {
undoStack.clear(); undoStack.clear();
redoStack.clear(); redoStack.clear();
map->reset(w, h, (float) alt, static_cast<MapSurfaceType>(surf)); if(map) map->reset(w, h, (float) alt, static_cast<MapSurfaceType>(surf));
} }
void Program::resize(int w, int h, int alt, int surf) { void Program::resize(int w, int h, int alt, int surf) {
map->resize(w, h, (float) alt, static_cast<MapSurfaceType>(surf)); if(map) map->resize(w, h, (float) alt, static_cast<MapSurfaceType>(surf));
} }
void Program::resetFactions(int maxFactions) { void Program::resetFactions(int maxFactions) {
map->resetFactions(maxFactions); if(map) map->resetFactions(maxFactions);
} }
bool Program::setMapTitle(const string &title) { bool Program::setMapTitle(const string &title) {
if(map) {
if (map->getTitle() != title) { if (map->getTitle() != title) {
map->setTitle(title); map->setTitle(title);
return true; return true;
} }
}
return false; return false;
} }
bool Program::setMapDesc(const string &desc) { bool Program::setMapDesc(const string &desc) {
if(map) {
if (map->getDesc() != desc) { if (map->getDesc() != desc) {
map->setDesc(desc); map->setDesc(desc);
return true; return true;
} }
}
return false; return false;
} }
bool Program::setMapAuthor(const string &author) { bool Program::setMapAuthor(const string &author) {
if(map) {
if (map->getAuthor() != author) { if (map->getAuthor() != author) {
map->setAuthor(author); map->setAuthor(author);
return true; return true;
} }
}
return false; return false;
} }
@@ -553,7 +582,7 @@ void Program::setOfset(int x, int y) {
} }
void Program::incCellSize(int i) { void Program::incCellSize(int i) {
if(map) {
int minInc = 2 - cellSize; int minInc = 2 - cellSize;
if (i < minInc) { if (i < minInc) {
@@ -563,6 +592,7 @@ void Program::incCellSize(int i) {
ofsetX -= (map->getW() * i) / 2; ofsetX -= (map->getW() * i) / 2;
ofsetY += (map->getH() * i) / 2; ofsetY += (map->getH() * i) / 2;
} }
}
void Program::resetOfset() { void Program::resetOfset() {
ofsetX = 0; ofsetX = 0;
@@ -576,7 +606,7 @@ bool Program::setGridOnOff() {
} }
void Program::setMapAdvanced(int altFactor, int waterLevel) { void Program::setMapAdvanced(int altFactor, int waterLevel) {
map->setAdvanced(altFactor, waterLevel); if(map) map->setAdvanced(altFactor, waterLevel);
} }
void Program::loadMap(const string &path) { void Program::loadMap(const string &path) {
@@ -586,7 +616,7 @@ void Program::loadMap(const string &path) {
} }
void Program::saveMap(const string &path) { void Program::saveMap(const string &path) {
map->saveToFile(path); if(map) map->saveToFile(path);
} }
}// end namespace }// end namespace