From 98c8cab708d6e4711fb9556f360c94db81d38367 Mon Sep 17 00:00:00 2001 From: titison Date: Sun, 9 May 2021 12:21:49 +0200 Subject: [PATCH] MapEditor: Reworked resize function --- source/glest_map_editor/main.cpp | 8 +-- source/glest_map_editor/program.cpp | 4 +- source/glest_map_editor/program.h | 2 +- source/shared_lib/include/map/map_preview.h | 2 +- source/shared_lib/sources/map/map_preview.cpp | 68 +++++-------------- 5 files changed, 23 insertions(+), 61 deletions(-) diff --git a/source/glest_map_editor/main.cpp b/source/glest_map_editor/main.cpp index a8ad5cd7..f3a98e9d 100644 --- a/source/glest_map_editor/main.cpp +++ b/source/glest_map_editor/main.cpp @@ -920,16 +920,12 @@ void MainWindow::onMenuEditResize(wxCommandEvent &event) { SimpleDialog simpleDialog; simpleDialog.addValue("Width", intToStr(program->getMap()->getW()),"(must be 16,32,64,128,256,512...)"); simpleDialog.addValue("Height", intToStr(program->getMap()->getH()),"(must be 16,32,64,128,256,512...)"); - simpleDialog.addValue("Surface", "1","(surface material for new area around map)"); - simpleDialog.addValue("Altitude", "10","(surface height for new area around map)"); - if (!simpleDialog.show("Resize - expand around, shrink to topleft")) return; + if (!simpleDialog.show("Resize")) return; try { program->resize( strToInt(simpleDialog.getValue("Width")), - strToInt(simpleDialog.getValue("Height")), - strToInt(simpleDialog.getValue("Altitude")), - strToInt(simpleDialog.getValue("Surface"))); + strToInt(simpleDialog.getValue("Height"))); } catch (const exception &e) { MsgDialog(this, ToUnicode(e.what()), wxT("Exception"), wxOK | wxICON_ERROR).ShowModal(); diff --git a/source/glest_map_editor/program.cpp b/source/glest_map_editor/program.cpp index b0411fc0..ccc672d7 100644 --- a/source/glest_map_editor/program.cpp +++ b/source/glest_map_editor/program.cpp @@ -584,8 +584,8 @@ void Program::reset(int w, int h, int alt, int surf) { if(map) map->reset(w, h, (float) alt, static_cast(surf)); } -void Program::resize(int w, int h, int alt, int surf) { - if(map) map->resize(w, h, (float) alt, static_cast(surf)); +void Program::resize(int w, int h) { + if(map) map->resize(w, h); } void Program::resetFactions(int maxFactions) { diff --git a/source/glest_map_editor/program.h b/source/glest_map_editor/program.h index 4dcd15e9..8f1ab081 100644 --- a/source/glest_map_editor/program.h +++ b/source/glest_map_editor/program.h @@ -134,7 +134,7 @@ public: //map ops void reset(int w, int h, int alt, int surf); - void resize(int w, int h, int alt, int surf); + void resize(int w, int h); void resetFactions(int maxFactions); void setRefAlt(int x, int y); void flipX(); diff --git a/source/shared_lib/include/map/map_preview.h b/source/shared_lib/include/map/map_preview.h index 21083db5..e50883bb 100644 --- a/source/shared_lib/include/map/map_preview.h +++ b/source/shared_lib/include/map/map_preview.h @@ -206,7 +206,7 @@ public: void copyXY(int x, int y, int sx, int sy); // destination x,y = source sx,sy void swapXY(int x, int y, int sx, int sy); void reset(int w, int h, float alt, MapSurfaceType surf); - void resize(int w, int h, float alt, MapSurfaceType surf); + void resize(int w, int h); void resetFactions(int maxFactions); void randomizeHeights(bool withReset,int minimumHeight, int maximumHeight, int chanceDevider, int smoothRecursions); void importMapHeights(unsigned char* data); diff --git a/source/shared_lib/sources/map/map_preview.cpp b/source/shared_lib/sources/map/map_preview.cpp index af7d0e63..ad1903a5 100644 --- a/source/shared_lib/sources/map/map_preview.cpp +++ b/source/shared_lib/sources/map/map_preview.cpp @@ -579,7 +579,7 @@ void MapPreview::reset(int w, int h, float alt, MapSurfaceType surf) { hasChanged = true; } -void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) { +void MapPreview::resize(int w, int h) { if (w < MIN_MAP_CELL_DIMENSION || h < MIN_MAP_CELL_DIMENSION) { char szBuf[8096]=""; snprintf(szBuf,8096,"Size of map must be at least %dx%d",MIN_MAP_CELL_DIMENSION,MIN_MAP_CELL_DIMENSION); @@ -593,18 +593,6 @@ void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) { throw megaglest_runtime_error(szBuf); } - if (alt < MIN_MAP_CELL_HEIGHT || alt > MAX_MAP_CELL_HEIGHT) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Height must be in the range %d-%d",MIN_MAP_CELL_HEIGHT,MAX_MAP_CELL_HEIGHT); - throw megaglest_runtime_error(szBuf); - } - - if (surf < st_Grass || surf > st_Ground) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Surface must be in the range %d-%d",st_Grass,st_Ground); - throw megaglest_runtime_error(szBuf); - } - int oldW = this->w; int oldH = this->h; this->w = w; @@ -612,46 +600,24 @@ void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) { //this->maxFactions = maxFactions; //create new cells - //Cell **oldCells = cells; std::vector > oldCells = cells; - //cells = new Cell*[w]; - cells.resize(w); - for (int i = 0; i < w; i++) { - //cells[i] = new Cell[h]; - cells[i].resize(h); - for (int j = 0; j < h; j++) { - cells[i][j].height = alt; - cells[i][j].object = 0; - cells[i][j].resource = 0; - cells[i][j].surface = surf; - } - } - - int wOffset = w < oldW ? 0 : (w - oldW) / 2; - int hOffset = h < oldH ? 0 : (h - oldH) / 2; - //assign old values to cells - for (int i = 0; i < oldW; i++) { - for (int j = 0; j < oldH; j++) { - if (i + wOffset < w && j + hOffset < h) { - cells[i+wOffset][j+hOffset].height = oldCells[i][j].height; - cells[i+wOffset][j+hOffset].object = oldCells[i][j].object; - cells[i+wOffset][j+hOffset].resource = oldCells[i][j].resource; - cells[i+wOffset][j+hOffset].surface = oldCells[i][j].surface; - } - } - } - for (int i = 0; i < maxFactions; ++i) { - startLocations[i].x += wOffset; - startLocations[i].y += hOffset; - } - - //delete old cells - //if (oldCells != NULL) { - // for (int i = 0; i < oldW; i++) - // delete [] oldCells[i]; - // delete [] oldCells; - //} + cells.resize(w); + for (int i = 0; i < w; i++) { + cells[i].resize(h); + for (int j = 0; j < h; j++) { + int from_x = (i*oldW)/w; + int from_y = (j*oldH)/h; + cells[i][j].height = oldCells[from_x][from_y].height; + cells[i][j].object = oldCells[from_x][from_y].object; + cells[i][j].resource = oldCells[from_x][from_y].resource; + cells[i][j].surface = oldCells[from_x][from_y].surface; + } + } + for (int i = 0; i < maxFactions; ++i) { + startLocations[i].x *= (float)w/oldW; + startLocations[i].y *= (float)h/oldH; + } hasChanged = true; }