Skip to main content
GameDev.net gamedev.net
🔒 Locked

OO design problem

Started by Nitage Feb 28, 2006 at 3:46 AM 1 replies 650+ views
Original Post
Nitage
Nitage
I'm creating a small terrain editor for fun, but I'm having a problem deciding on where the responsibility for action should lie. I have two classes: heightmap and editorTool. editorTool is a base class from which all the tools in the editro will derive (eg, hill tool, valley tool etc). I'm having trouble implementing the tools without giving them access to the raw heightmap data. The problem with this is that I'm currently using openGL immeadiate mode, and when I change to use vertex arrays, the internal representation of my heightmap will probably change. I could implement each tool to just call a funciton on the heighmap class (like makeHill(), makeValley()), but then I'd need to change the heightmap class every time I add a new tool. Any ideas?
Stary
Stary
Generally, the proper "nice" way to solve this is to provide accessor functions for your data in your data container class (heightmap in your case). value = getHeight(x, y) and setHeight(x, y, value), overloaded operators, or whatever you want. Done properly (inline the functions and keep them small), it should not hurt your performance in any notable way.

It does restrict how you represent your data, that's true - but there's just no way to get around that when you're creating data editing tools.
Emmanuel Deloget
Emmanuel Deloget
Depending on how large is your terrain:
A) forget about the heightmap representation
B) or rebuild the vertex array from the heightmap when it change

Another solution maybe to provide some kind of iterator that will give acces to bothe the heightmap AND the vertex array:

template <class VERTEXTYPE, class HFTYPE>class TerrainIterator{  VERTEXTYPE *mVertex;  HFTYPE     *mHeight;  // debug: what is the current position...  int mX, mY;  int mWidth:public:  void change_height(HFTYPE value)  {     mVertex->position.y += value;     *mHeight += value;   }  // go to the next vertex on the same hmap line  void operator++(int)  {    mHeight++;    mVertex++;    // this is only for debug reasons    mX++;     if (mX >= mWidth) {      mX = 0;       mY++;    }  }  void nextline()  {    mHeight += mWidth;    mVertex += mWidth;  }  // same to go to the previous vertex and line  // operator+/- and operator+=/-= to handle   // vertex skiping, and so on. Ya know, iterator stuff :)};


You have to take care about how you init your vertex array. Most of the time, the position of your vertex in your vertex array will be (y*width+x) - ie the address of the corresponding point in the heightmap.

class Terrain{public:  TerrainIterator<Vertex, int> get_terrain_iterator(int x, int y) const;};


Now, you can handle your tools using the strategy design pattern:

class Tool{public:  virtual bool apply_algorithm(TerrainIterator<Vertex, int>& first_point) = 0;};class HillTool{public:  HillTool(int center_height, int radius);  bool apply_algorithm(TerrainIterator<Vertex, int>& first_point)  {    // play with the iterators...  }};


HTH,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.