mapeditor from GAE ( removed macro stuff and so on )

This commit is contained in:
Titus Tscharntke
2010-03-07 17:25:42 +00:00
parent 06012e4804
commit 983d8475c9
8 changed files with 1815 additions and 998 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,16 @@
#ifndef _GLEST_MAPEDITOR_MAIN_H_ // ==============================================================
#define _GLEST_MAPEDITOR_MAIN_H_ // This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _MAPEDITOR_MAIN_H_
#define _MAPEDITOR_MAIN_H_
#include <string> #include <string>
#include <vector> #include <vector>
@@ -7,41 +18,88 @@
#include <wx/wx.h> #include <wx/wx.h>
#include <wx/glcanvas.h> #include <wx/glcanvas.h>
#include "program.h" #include "program.h"
#include "util.h"
using std::string; using std::string;
using std::vector; using std::vector;
using std::pair; using std::pair;
namespace Glest{ namespace MapEditor{ namespace MapEditor {
class GlCanvas; class GlCanvas;
enum BrushType {
btHeight,
btGradient,
btSurface,
btObject,
btResource,
btStartLocation
};
enum StatusItems {
siNULL_ENTRY,
siFILE_NAME,
siFILE_TYPE,
siBRUSH_TYPE,
siBRUSH_VALUE,
siBRUSH_RADIUS,
siCOUNT
};
const char *object_descs[] = {
"None (Erase)",
"Tree",
"Dead Tree",
"Stone",
"Bush",
"Water Object",
"Big/Dead Tree",
"Trophy Corpse",
"Statues",
"Big Rock",
"Invisible Blocking"
};
const char *resource_descs[] = {
"None (Erase)", "Gold", "Stone", "Custom", "Custom", "Custom"
};
const char *surface_descs[] = {
"Grass", "Alt. Grass", "Road", "Stone", "Ground"
};
// ===================================================== // =====================================================
// class MainWindow // class MainWindow
// ===================================================== // =====================================================
class MainWindow: public wxFrame{ class MainWindow: public wxFrame {
private: private:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
private: private:
static const string versionString; static const string versionString;
static const string winHeader; static const string winHeader;
static const int heightCount= 11; static const int heightCount = 11;
static const int surfaceCount= 5; static const int surfaceCount = 5;
static const int objectCount= 11; static const int objectCount = 11;
static const int resourceCount= 6; static const int resourceCount = 6;
static const int startLocationCount= 8; static const int startLocationCount = 8;
static const int radiusCount= 9; static const int radiusCount = 9;
private: private:
enum MenuId{ enum MenuId {
miFileLoad, miFileLoad,
miFileSave, miFileSave,
miFileSaveAs, miFileSaveAs,
miFileExit, miFileExit,
miEditUndo,
miEditRedo,
miEditReset, miEditReset,
miEditResetPlayers, miEditResetPlayers,
miEditResize, miEditResize,
@@ -58,12 +116,13 @@ private:
miMiscHelp, miMiscHelp,
miBrushHeight, miBrushHeight,
miBrushSurface= miBrushHeight + heightCount + 1, miBrushGradient = miBrushHeight + heightCount + 1,
miBrushObject= miBrushSurface + surfaceCount + 1, miBrushSurface = miBrushGradient + heightCount + 1,
miBrushResource= miBrushObject + objectCount + 1, miBrushObject = miBrushSurface + surfaceCount + 1,
miBrushStartLocation= miBrushResource + resourceCount + 1, miBrushResource = miBrushObject + objectCount + 1,
miBrushStartLocation = miBrushResource + resourceCount + 1,
miRadius= miBrushStartLocation + startLocationCount + 1 miRadius = miBrushStartLocation + startLocationCount + 1
}; };
private: private:
@@ -79,38 +138,47 @@ private:
wxMenu *menuMisc; wxMenu *menuMisc;
wxMenu *menuBrush; wxMenu *menuBrush;
wxMenu *menuBrushHeight; wxMenu *menuBrushHeight;
wxMenu *menuBrushGradient;
wxMenu *menuBrushSurface; wxMenu *menuBrushSurface;
wxMenu *menuBrushObject; wxMenu *menuBrushObject;
wxMenu *menuBrushResource; wxMenu *menuBrushResource;
wxMenu *menuBrushStartLocation; wxMenu *menuBrushStartLocation;
wxMenu *menuRadius; wxMenu *menuRadius;
string currentFile; string currentFile;
BrushType currentBrush;
int height; int height;
int surface; int surface;
int radius; int radius;
int object; int object;
int resource; int resource;
int startLocation; int startLocation;
int enabledGroup; ChangeType enabledGroup;
string fileName;
bool fileModified;
public: public:
MainWindow(); MainWindow();
~MainWindow(); ~MainWindow();
void init(); void init(string fname);
void onClose(wxCloseEvent &event); void onClose(wxCloseEvent &event);
void onMouseDown(wxMouseEvent &event); void onMouseDown(wxMouseEvent &event);
void onMouseMove(wxMouseEvent &event); void onMouseMove(wxMouseEvent &event);
void onPaint(wxPaintEvent &event); void onPaint(wxPaintEvent &event);
void onKeyDown(wxKeyEvent &e);
void onMenuFileLoad(wxCommandEvent &event); void onMenuFileLoad(wxCommandEvent &event);
void onMenuFileSave(wxCommandEvent &event); void onMenuFileSave(wxCommandEvent &event);
void onMenuFileSaveAs(wxCommandEvent &event); void onMenuFileSaveAs(wxCommandEvent &event);
void onMenuFileExit(wxCommandEvent &event); void onMenuFileExit(wxCommandEvent &event);
void onMenuEditUndo(wxCommandEvent &event);
void onMenuEditRedo(wxCommandEvent &event);
void onMenuEditReset(wxCommandEvent &event); void onMenuEditReset(wxCommandEvent &event);
void onMenuEditResetPlayers(wxCommandEvent &event); void onMenuEditResetPlayers(wxCommandEvent &event);
void onMenuEditResize(wxCommandEvent &event); void onMenuEditResize(wxCommandEvent &event);
@@ -127,6 +195,7 @@ public:
void onMenuMiscHelp(wxCommandEvent &event); void onMenuMiscHelp(wxCommandEvent &event);
void onMenuBrushHeight(wxCommandEvent &event); void onMenuBrushHeight(wxCommandEvent &event);
void onMenuBrushGradient(wxCommandEvent &event);
void onMenuBrushSurface(wxCommandEvent &event); void onMenuBrushSurface(wxCommandEvent &event);
void onMenuBrushObject(wxCommandEvent &event); void onMenuBrushObject(wxCommandEvent &event);
void onMenuBrushResource(wxCommandEvent &event); void onMenuBrushResource(wxCommandEvent &event);
@@ -139,21 +208,27 @@ public:
void uncheckBrush(); void uncheckBrush();
void uncheckRadius(); void uncheckRadius();
private:
bool isDirty() const { return fileModified; }
void setDirty(bool val=true);
void setExtension();
}; };
// ===================================================== // =====================================================
// class GlCanvas // class GlCanvas
// ===================================================== // =====================================================
class GlCanvas: public wxGLCanvas{ class GlCanvas: public wxGLCanvas {
private: private:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
public: public:
GlCanvas(MainWindow *mainWindow,int* args); GlCanvas(MainWindow *mainWindow, int *args);
void onMouseDown(wxMouseEvent &event); void onMouseDown(wxMouseEvent &event);
void onMouseMove(wxMouseEvent &event); void onMouseMove(wxMouseEvent &event);
void onKeyDown(wxKeyEvent &event);
private: private:
MainWindow *mainWindow; MainWindow *mainWindow;
@@ -163,7 +238,7 @@ private:
// class SimpleDialog // class SimpleDialog
// ===================================================== // =====================================================
class SimpleDialog: public wxDialog{ class SimpleDialog: public wxDialog {
private: private:
typedef vector<pair<string, string> > Values; typedef vector<pair<string, string> > Values;
@@ -185,7 +260,7 @@ public:
// class App // class App
// ===================================================== // =====================================================
class App: public wxApp{ class App: public wxApp {
private: private:
MainWindow *mainWindow; MainWindow *mainWindow;
@@ -195,8 +270,8 @@ public:
virtual int OnExit(); virtual int OnExit();
}; };
}}// end namespace }// end namespace
DECLARE_APP(Glest::MapEditor::App) DECLARE_APP(MapEditor::App)
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,16 @@
#ifndef _GLEST_MAPEDITOR_MAP_H_ // ==============================================================
#define _GLEST_MAPEDITOR_MAP_H_ // This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _MAPEDITOR_MAP_H_
#define _MAPEDITOR_MAP_H_
#include "util.h" #include "util.h"
#include "types.h" #include "types.h"
@@ -10,9 +21,11 @@ using Shared::Platform::int32;
using Shared::Platform::float32; using Shared::Platform::float32;
using Shared::Util::Random; using Shared::Util::Random;
struct MapFileHeader{ namespace MapEditor {
struct MapFileHeader {
int32 version; int32 version;
int32 maxPlayers; int32 maxFactions;
int32 width; int32 width;
int32 height; int32 height;
int32 altFactor; int32 altFactor;
@@ -22,88 +35,92 @@ struct MapFileHeader{
int8 description[256]; int8 description[256];
}; };
namespace Glest{ namespace MapEditor{
// =============================================== // ===============================================
// class Map // class Map
// =============================================== // ===============================================
class Map{ class Map {
public: public:
static const int maxHeight= 20; static const int maxHeight = 20;
static const int minHeight= 0; static const int minHeight = 0;
private: private:
struct Cell{ struct Cell {
int surface; int surface;
int object; int object;
int resource; int resource;
float height; float height;
}; };
struct StartLocation{ struct StartLocation {
int x; int x;
int y; int y;
}; };
Random random; Random random;
string title; string title;
string author; string author;
string desc; string desc;
string recScn; string recScn;
int type; int type;
int h; int h;
int w; int w;
int altFactor; int altFactor;
int waterLevel; int waterLevel;
Cell **cells; Cell **cells;
int maxPlayers; int maxFactions;
StartLocation *startLocations; StartLocation *startLocations;
int refAlt; int refAlt;
public: public:
Map(); Map();
~Map(); ~Map();
float getHeight(int x, int y) const; float getHeight(int x, int y) const;
int getSurface(int x, int y) const; int getSurface(int x, int y) const;
int getObject(int x, int y) const; int getObject(int x, int y) const;
int getResource(int x, int y) const; int getResource(int x, int y) const;
int getStartLocationX(int index) const; int getStartLocationX(int index) const;
int getStartLocationY(int index) const; int getStartLocationY(int index) const;
int getHeightFactor() const; int getHeightFactor() const;
int getWaterLevel() const; int getWaterLevel() const;
bool inside(int x, int y); bool inside(int x, int y);
void setRefAlt(int x, int y); void setRefAlt(int x, int y);
void setAdvanced(int altFactor, int waterLevel); void setAdvanced(int altFactor, int waterLevel);
void setTitle(const string &title); void setTitle(const string &title);
void setDesc(const string &desc); void setDesc(const string &desc);
void setAuthor(const string &author); void setAuthor(const string &author);
int getH() const {return h;} int getH() const {return h;}
int getW() const {return w;} int getW() const {return w;}
int getMaxPlayers() const {return maxPlayers;} int getMaxFactions() const {return maxFactions;}
string getTitle() const {return title;} string getTitle() const {return title;}
string getDesc() const {return desc;} string getDesc() const {return desc;}
string getAuthor() const {return author;} string getAuthor() const {return author;}
void changeHeight(int x, int y, int height, int radius); void glestChangeHeight(int x, int y, int height, int radius);
void pirateChangeHeight(int x, int y, int height, int radius);
void changeSurface(int x, int y, int surface, int radius); void changeSurface(int x, int y, int surface, int radius);
void changeObject(int x, int y, int object, int radius); void changeObject(int x, int y, int object, int radius);
void changeResource(int x, int y, int resource, int radius); void changeResource(int x, int y, int resource, int radius);
void changeStartLocation(int x, int y, int player); void changeStartLocation(int x, int y, int player);
void setHeight(int x, int y, float height);
void setSurface(int x, int y, int surface);
void setObject(int x, int y, int object);
void setResource(int x, int y, int resource);
void flipX(); void flipX();
void flipY(); void flipY();
void reset(int w, int h, float alt, int surf); void reset(int w, int h, float alt, int surf);
void resize(int w, int h, float alt, int surf); void resize(int w, int h, float alt, int surf);
void resetPlayers(int maxPlayers); void resetFactions(int maxFactions);
void randomizeHeights(); void randomizeHeights();
void randomize(); void randomize();
void switchSurfaces(int surf1, int surf2); void switchSurfaces(int surf1, int surf2);
void loadFromFile(const string &path); void loadFromFile(const string &path);
void saveToFile(const string &path); void saveToFile(const string &path);
public: public:
void resetHeights(int height); void resetHeights(int height);
@@ -112,6 +129,6 @@ public:
void applyNewHeight(float newHeight, int x, int y, int strenght); void applyNewHeight(float newHeight, int x, int y, int strenght);
}; };
}}// end namespace }// end namespace
#endif #endif

View File

@@ -1,132 +1,320 @@
// ==============================================================
// This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "program.h" #include "program.h"
#include "util.h" #include "util.h"
using namespace Shared::Util; using namespace Shared::Util;
namespace Glest{ namespace MapEditor{ namespace MapEditor {
////////////////////////////
// class UndoPoint
////////////////////////////
int UndoPoint::w = 0;
int UndoPoint::h = 0;
UndoPoint::UndoPoint()
: height(0)
, surface(0)
, object(0)
, resource(0)
, change(ctNone) {
w = Program::map->getW();
h = Program::map->getH();
}
/*
UndoPoint::UndoPoint(ChangeType change) {
w = Program::map->getW();
h = Program::map->getH();
init(change);
}*/
void UndoPoint::init(ChangeType change) {
this->change = change;
switch (change) {
// Back up everything
case ctAll:
// Back up heights
case ctHeight:
case ctGradient:
// Build an array of heights from the map
//std::cout << "Building an array of heights to use for our UndoPoint" << std::endl;
height = new float[w * h];
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
height[j * w + i] = Program::map->getHeight(i, j);
}
}
//std::cout << "Built the array" << std::endl;
if (change != ctAll) break;
// Back up surfaces
case ctSurface:
surface = new int[w * h];
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
surface[j * w + i] = Program::map->getSurface(i, j);
}
}
if (change != ctAll) break;
// Back up both objects and resources if either changes
case ctObject:
case ctResource:
object = new int[w * h];
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
object[j * w + i] = Program::map->getObject(i, j);
}
}
resource = new int[w * h];
for (int i = 0; i < w; ++i) {
for (int j = 0; j < h; ++j) {
resource[j * w + i] = Program::map->getResource(i, j);
}
}
break;
}
}
UndoPoint::~UndoPoint() {
delete [] height;
delete [] resource;
delete [] object;
delete [] surface;
}
void UndoPoint::revert() {
//std::cout << "attempting to revert last changes to " << undoID << std::endl;
switch (change) {
// Revert Everything
case ctAll:
// Revert Heights
case ctHeight:
case ctGradient:
// Restore the array of heights to the map
//std::cout << "attempting to restore the height array" << std::endl;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Program::map->setHeight(i, j, height[j * w + i]);
}
}
if (change != ctAll) break;
// Revert surfaces
case ctSurface:
//std::cout << "attempting to restore the surface array" << std::endl;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Program::map->setSurface(i, j, surface[j * w + i]);
}
}
if (change != ctAll) break;
// Revert objects and resources
case ctObject:
case ctResource:
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Program::map->setObject(i, j, object[j * w + i]);
}
}
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Program::map->setResource(i, j, resource[j * w + i]);
}
}
break;
}
//std::cout << "reverted changes (we hope)" << std::endl;
}
// =============================================== // ===============================================
// class Program // class Program
// =============================================== // ===============================================
Program::Program(int w, int h){ Map *Program::map = NULL;
cellSize=6;
ofsetX= 0; Program::Program(int w, int h) {
ofsetY= 0; cellSize = 6;
map= new Map(); ofsetX = 0;
renderer.init(w, h); ofsetY = 0;
map = new Map();
renderer.init(w, h);
} }
Program::~Program(){ Program::~Program() {
delete map; delete map;
} }
void Program::changeMapHeight(int x, int y, int Height, int radius){ void Program::glestChangeMapHeight(int x, int y, int Height, int radius) {
map->changeHeight((x-ofsetX)/cellSize, (y+ofsetY)/cellSize, Height, radius); map->glestChangeHeight((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, Height, radius);
} }
void Program::changeMapSurface(int x, int y, int surface, int radius){ void Program::pirateChangeMapHeight(int x, int y, int Height, int radius) {
map->changeSurface((x-ofsetX)/cellSize, (y+ofsetY)/cellSize, surface, radius); map->pirateChangeHeight((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, Height, radius);
} }
void Program::changeMapObject(int x, int y, int object, int radius){ void Program::changeMapSurface(int x, int y, int surface, int radius) {
map->changeObject((x-ofsetX)/cellSize, (y+ofsetY)/cellSize, object, radius); map->changeSurface((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, surface, radius);
} }
void Program::changeMapResource(int x, int y, int resource, int radius){ void Program::changeMapObject(int x, int y, int object, int radius) {
map->changeResource((x-ofsetX)/cellSize, (y+ofsetY)/cellSize, resource, radius); map->changeObject((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, object, radius);
} }
void Program::changeStartLocation(int x, int y, int player){ void Program::changeMapResource(int x, int y, int resource, int radius) {
map->changeStartLocation((x-ofsetX)/cellSize, (y+ofsetY)/cellSize, player); map->changeResource((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, resource, radius);
} }
void Program::renderMap(int w, int h){ void Program::changeStartLocation(int x, int y, int player) {
renderer.renderMap(map, ofsetX, ofsetY, w, h, cellSize); map->changeStartLocation((x - ofsetX) / cellSize, (y + ofsetY) / cellSize, player);
} }
void Program::setRefAlt(int x, int y){ void Program::setUndoPoint(ChangeType change) {
map->setRefAlt((x-ofsetX)/cellSize, (y+ofsetY)/cellSize); if (change == ctLocation) return;
undoStack.push(UndoPoint());
undoStack.top().init(change);
redoStack.clear();
} }
void Program::flipX(){ bool Program::undo() {
if (undoStack.empty()) {
return false;
}
// push current state onto redo stack
redoStack.push(UndoPoint());
redoStack.top().init(undoStack.top().getChange());
undoStack.top().revert();
undoStack.pop();
return true;
}
bool Program::redo() {
if (redoStack.empty()) {
return false;
}
// push current state onto undo stack
undoStack.push(UndoPoint());
undoStack.top().init(redoStack.top().getChange());
redoStack.top().revert();
redoStack.pop();
return true;
}
void Program::renderMap(int w, int h) {
renderer.renderMap(map, ofsetX, ofsetY, w, h, cellSize);
}
void Program::setRefAlt(int x, int y) {
map->setRefAlt((x - ofsetX) / cellSize, (y + ofsetY) / cellSize);
}
void Program::flipX() {
map->flipX(); map->flipX();
} }
void Program::flipY(){ void Program::flipY() {
map->flipY(); map->flipY();
} }
void Program::randomizeMapHeights(){ void Program::randomizeMapHeights() {
map->randomizeHeights(); map->randomizeHeights();
} }
void Program::randomizeMap(){ void Program::randomizeMap() {
map->randomize(); map->randomize();
} }
void Program::switchMapSurfaces(int surf1, int surf2){ void Program::switchMapSurfaces(int surf1, int surf2) {
map->switchSurfaces(surf1, surf2); map->switchSurfaces(surf1, surf2);
} }
void Program::reset(int w, int h, int alt, int surf){ void Program::reset(int w, int h, int alt, int surf) {
map->reset(w, h, (float) alt, surf); undoStack.clear();
redoStack.clear();
map->reset(w, h, (float) alt, 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, surf); map->resize(w, h, (float) alt, surf);
} }
void Program::resetPlayers(int maxPlayers){ void Program::resetFactions(int maxFactions) {
map->resetPlayers(maxPlayers); map->resetFactions(maxFactions);
} }
void Program::setMapTitle(const string &title){ bool Program::setMapTitle(const string &title) {
map->setTitle(title); if (map->getTitle() != title) {
} map->setTitle(title);
return true;
void Program::setMapDesc(const string &desc){
map->setDesc(desc);
}
void Program::setMapAuthor(const string &author){
map->setAuthor(author);
}
void Program::setOfset(int x, int y){
ofsetX+= x;
ofsetY-= y;
}
void Program::incCellSize(int i){
int minInc= 2-cellSize;
if(i<minInc){
i= minInc;
} }
cellSize+= i; return false;
ofsetX-= (map->getW()*i)/2;
ofsetY+= (map->getH()*i)/2;
} }
void Program::resetOfset(){ bool Program::setMapDesc(const string &desc) {
ofsetX= 0; if (map->getDesc() != desc) {
ofsetY= 0; map->setDesc(desc);
cellSize= 6; return true;
}
return false;
} }
void Program::setMapAdvanced(int altFactor, int waterLevel){ bool Program::setMapAuthor(const string &author) {
map->setAdvanced(altFactor, waterLevel); if (map->getAuthor() != author) {
map->setAuthor(author);
return true;
}
return false;
} }
void Program::loadMap(const string &path){ void Program::setOfset(int x, int y) {
map->loadFromFile(path); ofsetX += x;
ofsetY -= y;
} }
void Program::saveMap(const string &path){ void Program::incCellSize(int i) {
map->saveToFile(path);
int minInc = 2 - cellSize;
if (i < minInc) {
i = minInc;
}
cellSize += i;
ofsetX -= (map->getW() * i) / 2;
ofsetY += (map->getH() * i) / 2;
} }
}}// end namespace void Program::resetOfset() {
ofsetX = 0;
ofsetY = 0;
cellSize = 6;
}
void Program::setMapAdvanced(int altFactor, int waterLevel) {
map->setAdvanced(altFactor, waterLevel);
}
void Program::loadMap(const string &path) {
undoStack.clear();
redoStack.clear();
map->loadFromFile(path);
}
void Program::saveMap(const string &path) {
map->saveToFile(path);
}
}// end namespace

View File

@@ -1,63 +1,145 @@
#ifndef _GLEST_MAPEDITOR_PROGRAM_H_ // ==============================================================
#define _GLEST_MAPEDITOR_PROGRAM_H_ // This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _MAPEDITOR_PROGRAM_H_
#define _MAPEDITOR_PROGRAM_H_
#include "map.h" #include "map.h"
#include "renderer.h" #include "renderer.h"
namespace Glest{ namespace MapEditor{ #include <stack>
using std::stack;
namespace MapEditor {
class MainWindow; class MainWindow;
enum ChangeType {
ctNone = -1,
ctHeight,
ctSurface,
ctObject,
ctResource,
ctLocation,
ctGradient,
ctAll
};
// =============================================
// class Undo Point
// A linked list class that is more of an extension / modification on
// the already existing Cell struct in map.h
// Provides the ability to only specify a certain property of the map to change
// =============================================
class UndoPoint {
private:
// Only keep a certain number of undo points in memory otherwise
// Big projects could hog a lot of memory
const static int MAX_UNDO_LIST_SIZE = 100; // TODO get feedback on this value
static int undoCount;
ChangeType change;
// Pointers to arrays of each property
int *surface;
int *object;
int *resource;
float *height;
// Map width and height
static int w;
static int h;
public:
UndoPoint();
~UndoPoint();
void init(ChangeType change);
void revert();
inline ChangeType getChange() const { return change; }
};
class ChangeStack : public std::stack<UndoPoint> {
public:
static const int maxSize = 100;
void clear() { c.clear(); }
void push(UndoPoint p) {
if (c.size() >= maxSize) {
c.pop_front();
}
stack<UndoPoint>::push(p);
}
};
// =============================================== // ===============================================
// class Program // class Program
// =============================================== // ===============================================
class Program{ class Program {
private: private:
Renderer renderer; Renderer renderer;
int ofsetX, ofsetY; int ofsetX, ofsetY;
int cellSize; int cellSize;
Map *map; static Map *map;
friend class UndoPoint;
ChangeStack undoStack, redoStack;
public: public:
Program(int w, int h); Program(int w, int h);
~Program(); ~Program();
//map cell change //map cell change
void changeMapHeight(int x, int y, int Height, int radius); void glestChangeMapHeight(int x, int y, int Height, int radius);
void changeMapSurface(int x, int y, int surface, int radius); void pirateChangeMapHeight(int x, int y, int Height, int radius);
void changeMapObject(int x, int y, int object, int radius); void changeMapSurface(int x, int y, int surface, int radius);
void changeMapResource(int x, int y, int resource, int radius); void changeMapObject(int x, int y, int object, int radius);
void changeStartLocation(int x, int y, int player); void changeMapResource(int x, int y, int resource, int radius);
void changeStartLocation(int x, int y, int player);
void setUndoPoint(ChangeType change);
bool undo();
bool redo();
//map ops //map ops
void reset(int w, int h, int alt, int surf); 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, int alt, int surf);
void resetPlayers(int maxPlayers); void resetFactions(int maxFactions);
void setRefAlt(int x, int y); void setRefAlt(int x, int y);
void flipX(); void flipX();
void flipY(); void flipY();
void randomizeMapHeights(); void randomizeMapHeights();
void randomizeMap(); void randomizeMap();
void switchMapSurfaces(int surf1, int surf2); void switchMapSurfaces(int surf1, int surf2);
void loadMap(const string &path); void loadMap(const string &path);
void saveMap(const string &path); void saveMap(const string &path);
//map misc //map misc
void setMapTitle(const string &title); bool setMapTitle(const string &title);
void setMapDesc(const string &desc); bool setMapDesc(const string &desc);
void setMapAuthor(const string &author); bool setMapAuthor(const string &author);
void setMapAdvanced(int altFactor, int waterLevel); void setMapAdvanced(int altFactor, int waterLevel);
//misc //misc
void renderMap(int w, int h); void renderMap(int w, int h);
void setOfset(int x, int y); void setOfset(int x, int y);
void incCellSize(int i); void incCellSize(int i);
void resetOfset(); void resetOfset();
const Map *getMap() {return map;} static const Map *getMap() {return map;}
}; };
}}// end namespace }// end namespace
#endif #endif

View File

@@ -1,41 +1,53 @@
// ==============================================================
// This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "renderer.h" #include "renderer.h"
#include <cassert> #include <cassert>
#include "opengl.h" #include "opengl.h"
#include "vec.h" #include "vec.h"
using namespace Shared::Graphics; using namespace Shared::Graphics;
using namespace Shared::Graphics::Gl; using namespace Shared::Graphics::Gl;
namespace Glest{ namespace MapEditor{ namespace MapEditor {
// =============================================== // ===============================================
// class Renderer // class Renderer
// =============================================== // ===============================================
void Renderer::init(int clientW, int clientH){ void Renderer::init(int clientW, int clientH) {
assertGl(); assertGl();
glFrontFace(GL_CW); glFrontFace(GL_CW);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_FRONT, GL_FILL);
glClearColor(0.5, 0.5, 0.5, 1.0); glClearColor(0.5, 0.5, 0.5, 1.0);
assertGl(); assertGl();
} }
void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int cellSize){ void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int cellSize) {
float alt; float alt;
float showWater;
assertGl(); assertGl();
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
glOrtho(0, clientW,0, clientH,1,-1); glOrtho(0, clientW, 0, clientH, 1, -1);
glViewport(0, 0, clientW, clientH); glViewport(0, 0, clientW, clientH);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glPushAttrib(GL_CURRENT_BIT); glPushAttrib(GL_CURRENT_BIT);
@@ -44,33 +56,38 @@ void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int c
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0); glColor3f(0, 0, 0);
for (int j=0; j<map->getH(); j++){ for (int j = 0; j < map->getH(); j++) {
for (int i=0; i<map->getW(); i++){ for (int i = 0; i < map->getW(); i++) {
if (i*cellSize+x>-cellSize && i*cellSize+x<clientW && clientH-cellSize-j*cellSize+y>-cellSize && clientH-cellSize-j*cellSize+y<clientH){ if (i * cellSize + x > -cellSize
&& i * cellSize + x < clientW
&& clientH - cellSize - j * cellSize + y > -cellSize
&& clientH - cellSize - j * cellSize + y < clientH) {
//surface //surface
alt= map->getHeight(i, j)/20.f; alt = map->getHeight(i, j) / 20.f;
showWater = map->getWaterLevel()/ 20.f - alt;
showWater = (showWater > 0)? showWater:0;
Vec3f surfColor; Vec3f surfColor;
switch (map->getSurface(i, j)){ switch (map->getSurface(i, j)) {
case 1: surfColor= Vec3f(0.0, 0.8f*alt, 0.f); break; case 1: surfColor = Vec3f(0.0, 0.8f * alt, 0.f + showWater); break;
case 2: surfColor= Vec3f(0.4f*alt, 0.6f*alt, 0.f); break; case 2: surfColor = Vec3f(0.4f * alt, 0.6f * alt, 0.f + showWater); break;
case 3: surfColor= Vec3f(0.6f*alt, 0.3f*alt, 0.f); break; case 3: surfColor = Vec3f(0.6f * alt, 0.3f * alt, 0.f + showWater); break;
case 4: surfColor= Vec3f(0.7f*alt, 0.7f*alt, 0.7f*alt); break; case 4: surfColor = Vec3f(0.7f * alt, 0.7f * alt, 0.7f * alt + showWater); break;
case 5: surfColor= Vec3f(1.0f*alt, 0.f, 0.f); break; case 5: surfColor = Vec3f(0.7f * alt, 0.5f * alt, 0.3f * alt + showWater); break;
} }
glColor3fv(surfColor.ptr()); glColor3fv(surfColor.ptr());
glBegin(GL_TRIANGLE_STRIP); glBegin(GL_TRIANGLE_STRIP);
glVertex2i(i*cellSize, clientH-j*cellSize-cellSize); glVertex2i(i * cellSize, clientH - j * cellSize - cellSize);
glVertex2i(i*cellSize, clientH-j*cellSize); glVertex2i(i * cellSize, clientH - j * cellSize);
glVertex2i(i*cellSize+cellSize, clientH-j*cellSize-cellSize); glVertex2i(i * cellSize + cellSize, clientH - j * cellSize - cellSize);
glVertex2i(i*cellSize+cellSize, clientH-j*cellSize); glVertex2i(i * cellSize + cellSize, clientH - j * cellSize);
glEnd(); glEnd();
//objects //objects
switch(map->getObject(i,j)){ switch (map->getObject(i, j)) {
case 0: glColor3f(0.f, 0.f, 0.f); break; case 0: glColor3f(0.f, 0.f, 0.f); break;
case 1: glColor3f(1.f, 0.f, 0.f); break; case 1: glColor3f(1.f, 0.f, 0.f); break;
case 2: glColor3f(1.f, 1.f, 1.f); break; case 2: glColor3f(1.f, 1.f, 1.f); break;
case 3: glColor3f(0.5f, 0.5f, 1.f); break; case 3: glColor3f(0.5f, 0.5f, 1.f); break;
@@ -80,54 +97,54 @@ void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int c
case 7: glColor3f(0.f, 1.f, 1.f); break; case 7: glColor3f(0.f, 1.f, 1.f); break;
case 8: glColor3f(0.7f, 0.1f, 0.3f); break; case 8: glColor3f(0.7f, 0.1f, 0.3f); break;
case 9: glColor3f(0.5f, 1.f, 0.1f); break; case 9: glColor3f(0.5f, 1.f, 0.1f); break;
case 10:glColor3f(1.f, 0.2f, 0.8f); break; case 10: glColor3f(1.f, 0.2f, 0.8f); break;
} }
if(map->getObject(i, j)!=0){ if (map->getObject(i, j) != 0) {
glPointSize(cellSize/2.f); glPointSize(cellSize / 2.f);
glBegin(GL_POINTS); glBegin(GL_POINTS);
glVertex2i(i*cellSize+cellSize/2, clientH-j*cellSize-cellSize/2); glVertex2i(i * cellSize + cellSize / 2, clientH - j * cellSize - cellSize / 2);
glEnd(); glEnd();
} }
bool found= false; // bool found = false;
//height lines //height lines
if(!found){ // if (!found) {
glColor3fv((surfColor*0.5f).ptr()); glColor3fv((surfColor*0.5f).ptr());
//left //left
if(i>0 && map->getHeight(i-1, j)>map->getHeight(i, j)){ if (i > 0 && map->getHeight(i - 1, j) > map->getHeight(i, j)) {
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2i(i*cellSize, clientH-(j+1)*cellSize); glVertex2i(i * cellSize, clientH - (j + 1) * cellSize);
glVertex2i(i*cellSize, clientH-j*cellSize); glVertex2i(i * cellSize, clientH - j * cellSize);
glEnd(); glEnd();
} }
//down //down
if(j>0 && map->getHeight(i, j-1)>map->getHeight(i, j)){ if (j > 0 && map->getHeight(i, j - 1) > map->getHeight(i, j)) {
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2i(i*cellSize, clientH-j*cellSize); glVertex2i(i * cellSize, clientH - j * cellSize);
glVertex2i((i+1)*cellSize, clientH-j*cellSize); glVertex2i((i + 1) * cellSize, clientH - j * cellSize);
glEnd(); glEnd();
} }
glColor3fv((surfColor*2.f).ptr()); glColor3fv((surfColor*2.f).ptr());
//left //left
if(i>0 && map->getHeight(i-1, j)<map->getHeight(i, j)){ if (i > 0 && map->getHeight(i - 1, j) < map->getHeight(i, j)) {
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2i(i*cellSize, clientH-(j+1)*cellSize); glVertex2i(i * cellSize, clientH - (j + 1) * cellSize);
glVertex2i(i*cellSize, clientH-j*cellSize); glVertex2i(i * cellSize, clientH - j * cellSize);
glEnd();
}
if(j>0 && map->getHeight(i, j-1)<map->getHeight(i, j)){
glBegin(GL_LINES);
glVertex2i(i*cellSize, clientH-j*cellSize);
glVertex2i((i+1)*cellSize, clientH-j*cellSize);
glEnd(); glEnd();
} }
} if (j > 0 && map->getHeight(i, j - 1) < map->getHeight(i, j)) {
glBegin(GL_LINES);
glVertex2i(i * cellSize, clientH - j * cellSize);
glVertex2i((i + 1) * cellSize, clientH - j * cellSize);
glEnd();
}
// }
//resources //resources
switch(map->getResource(i,j)){ switch (map->getResource(i, j)) {
case 1: glColor3f(1.f, 1.f, 0.f); break; case 1: glColor3f(1.f, 1.f, 0.f); break;
case 2: glColor3f(0.5f, 0.5f, 0.5f); break; case 2: glColor3f(0.5f, 0.5f, 0.5f); break;
case 3: glColor3f(1.f, 0.f, 0.f); break; case 3: glColor3f(1.f, 0.f, 0.f); break;
@@ -135,13 +152,13 @@ void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int c
case 5: glColor3f(0.5f, 0.5f, 1.f); break; case 5: glColor3f(0.5f, 0.5f, 1.f); break;
} }
if (map->getResource(i,j)!=0){ if (map->getResource(i, j) != 0) {
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2i(i*cellSize, clientH-j*cellSize-cellSize); glVertex2i(i * cellSize, clientH - j * cellSize - cellSize);
glVertex2i(i*cellSize+cellSize, clientH-j*cellSize); glVertex2i(i * cellSize + cellSize, clientH - j * cellSize);
glVertex2i(i*cellSize, clientH-j*cellSize); glVertex2i(i * cellSize, clientH - j * cellSize);
glVertex2i(i*cellSize+cellSize, clientH-j*cellSize-cellSize); glVertex2i(i * cellSize + cellSize, clientH - j * cellSize - cellSize);
glEnd(); glEnd();
} }
} }
} }
@@ -149,23 +166,23 @@ void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int c
//start locations //start locations
glLineWidth(3); glLineWidth(3);
for (int i=0; i<map->getMaxPlayers(); i++){ for (int i = 0; i < map->getMaxFactions(); i++) {
switch(i){ switch (i) {
case 0: glColor3f(1.f, 0.f, 0.f); break; case 0: glColor3f(1.f, 0.f, 0.f); break;
case 1: glColor3f(0.f, 0.f, 1.f); break; case 1: glColor3f(0.f, 0.f, 1.f); break;
case 2: glColor3f(0.f, 1.f, 0.f); break; case 2: glColor3f(0.f, 1.f, 0.f); break;
case 3: glColor3f(1.f, 1.f, 0.f); break; case 3: glColor3f(1.f, 1.f, 0.f); break;
case 4: glColor3f(1.f, 1.f, 1.f); break; case 4: glColor3f(1.f, 1.f, 1.f); break;
case 5: glColor3f(0.f, 1.f, 0.8f); break; case 5: glColor3f(0.f, 1.f, 0.8f); break;
case 6: glColor3f(1.f, 0.8f, 0.f); break; case 6: glColor3f(1.f, 0.8f, 0.f); break;
case 7: glColor3f(1.f, 0.8f, 1.f); break; case 7: glColor3f(1.f, 0.8f, 1.f); break;
} }
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2i((map->getStartLocationX(i)-1)*cellSize, clientH- (map->getStartLocationY(i)-1)*cellSize); glVertex2i((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize);
glVertex2i((map->getStartLocationX(i)+1)*cellSize+cellSize, clientH- (map->getStartLocationY(i)+1)*cellSize-cellSize); glVertex2i((map->getStartLocationX(i) + 1) * cellSize + cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - cellSize);
glVertex2i((map->getStartLocationX(i)-1)*cellSize, clientH- (map->getStartLocationY(i)+1)*cellSize-cellSize); glVertex2i((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - cellSize);
glVertex2i((map->getStartLocationX(i)+1)*cellSize+cellSize, clientH- (map->getStartLocationY(i)-1)*cellSize); glVertex2i((map->getStartLocationX(i) + 1) * cellSize + cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize);
glEnd(); glEnd();
} }
glPopMatrix(); glPopMatrix();
@@ -174,4 +191,4 @@ void Renderer::renderMap(Map *map, int x, int y, int clientW, int clientH, int c
assertGl(); assertGl();
} }
}}// end namespace }// end namespace

View File

@@ -1,20 +1,31 @@
#ifndef _GLEST_MAPEDITOR_RENDERER_H_ // ==============================================================
#define _GLEST_MAPEDITOR_RENDERER_H_ // This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _MAPEDITOR_RENDERER_H_
#define _MAPEDITOR_RENDERER_H_
#include "map.h" #include "map.h"
namespace Glest{ namespace MapEditor{ namespace MapEditor {
// =============================================== // ===============================================
// class Renderer // class Renderer
// =============================================== // ===============================================
class Renderer{ class Renderer {
public: public:
void init(int clientW, int clientH); void init(int clientW, int clientH);
void renderMap(Map *map, int x, int y, int clientW, int clientH, int cellSize); void renderMap(Map *map, int x, int y, int clientW, int clientH, int cellSize);
}; };
}}// end namespace }// end namespace
#endif #endif