Drafted somekind of Terrain class. Still unfinished and not uses.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/Terrain.cc Sat Nov 29 23:43:47 2008 +0000
@@ -0,0 +1,63 @@
+#include "Terrain.hh"
+
+#include <cmath>
+#include <algorithm>
+#include <ClanLib/display.h>
+
+Terrain::Terrain(const Vector &scale) : scale(scale) {}
+Terrain::Terrain(const Vector &scale, const int &seed) : scale(scale) {
+ this->generateTerrain(seed);
+}
+Terrain::Terrain(const Terrain &t) {
+ this->terrain = t.getTerrain();
+ this->generatePixelBuffer();
+}
+
+bool Terrain::collides(const Vector &point) const {
+ Vector coor = getPixelLocation(point);
+ return !(terrain[coor.x][coor.y] == EMPTY);
+}
+
+bool collides(const Vector &begin, const Vector &end) const {
+ // TODO: Implement. Bresenhams line algorithm could be usefull
+}
+
+void removeGround(const Vector &pos, const float r) {
+ // TODO: Implement. Some circle algoritmh should be usefull here.
+}
+
+Vector Terrain::getPixelLocation(Vector point) {
+ Vector scal(scale.x/MAP_WIDTH, scale.y/MAP_HEIGHT);
+ return Vector((int)round(point.x/scal.x),
+ (int)round(point.y/scal.y));
+}
+
+void Terrain::generatePixelBuffer() {
+ // These could be somewhere else
+ const CL_Color colorEmpty(86, 41, 0);
+ const CL_Color colorDirt(144, 82, 23);
+ const CL_Color colorRock(132, 136, 135);
+
+ this->pixbuf = CL_PixelBuffer(MAP_WIDTH, MAP_HEIGHT, 4*MAP_WIDTH,
+ CL_PixelFormat::rgba8888);
+
+ CL_Color color;
+ for (uint16_t i = 0; i < MAP_WIDTH; i++) {
+ for (uint16_t j = 0; j < MAP_HEIGHT; j++) {
+ switch(terrain[i][j]) {
+ case EMPTY:
+ color = colorEmpty;
+ break;
+ case DIRT:
+ color = colorDirt;
+ break;
+ case ROCK:
+ color = colorRock;
+ break;
+ default: // TODO: Shouldn't be here.
+ break;
+ }
+ this->pixbuf.draw_pixel(i, j, color);
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/Terrain.hh Sat Nov 29 23:43:47 2008 +0000
@@ -0,0 +1,157 @@
+#ifndef TERRAIN_HH
+#define TERRAIN_HH
+
+#include <vector>
+
+#include "Vector.hh"
+#include "Config.hh"
+
+enum TerrainType {EMPTY, DIRT, ROCK};
+
+/**
+ * Terrain class. Represents game terrain and contains member
+ * functions to manipulate terrain and get info about it.
+ *
+ * Terrain resolution is a constant that is defined in
+ * configuration. Terrain has a scale (i.e. the width and height in
+ * "real" units. Scaling is needed because physics simulation uses
+ * "real" units. The idea is that this class is used with "real" units
+ * and it uses pixelcoordinates internally.
+ */
+class Terrain {
+private:
+ const Vector scale;
+ std::vector<std::vector<TerrainType> > terrain;
+
+ // Terrain graphic
+ CL_PixelBuffer pixbuf;
+
+ /**
+ * Generates pixelbuffer from terrain. Should be used only on
+ * constructors because this is expected to be slow.
+ */
+ void generatePixelBuffer();
+
+ /**
+ * Get pixel location of a point that is in "real" units.
+ *
+ * @param point Point in "real" units
+ * @return Int vector
+ */
+ Vector getPixelLocation(Vector point);
+
+public:
+ /**
+ * Constructor.
+ *
+ * @param scale The "real" width and height of the terrain.
+ */
+ Terrain(const Vector &scale);
+ /**
+ * Constructor.
+ *
+ * @param scale The "real" width and height of the terrain.
+ * @param seed Random number generator seed used to create the
+ * terrain.
+ */
+ Terrain(const Vector &scale, const int &seed);
+ /**
+ * Copy constructor.
+ *
+ * @param t Terrain to be copied.
+ */
+ Terrain(const Terrain &t);
+ /**
+ * Destructor
+ */
+ ~Terrain();
+
+ /**
+ * Check if given point has some terrain.
+ *
+ * @param point Point that is in scaled units.
+ */
+ bool collides(const Vector &point) const;
+ /**
+ * Check if given line collides with terrain.
+ *
+ * @param begin Line begin point in scaled units.
+ * @param end Line end point in scaled units.
+ */
+ bool collides(const Vector &begin, const Vector &end) const;
+
+ /**
+ * Remove a circular area from terrain.
+ *
+ * @param pos Circle center
+ * @param r Circle radius
+ */
+ void removeGround(Vector pos, float r);
+
+ /**
+ * Get tangent for the given point.
+ *
+ * @param point Point where the tangent is calculated
+ * @param n Accuracy in pixels
+ */
+ Vector getTangent(Vector point, int n = 1) const;
+ /**
+ * Get tangent for the given point.
+ *
+ * @param point Point where the tangent is calculated
+ * @param a Accuracy in scaled units.
+ */
+ Vector getTangent(Vector point, float a) const;
+
+ /**
+ * Return normal for the given point.
+ *
+ * @param point Point for which the normal is calculated.
+ * @param n Accuracy in pixels
+ */
+ Vector getNormal(Vector point, int n = 1) const;
+ /**
+ * Return a normal for the given point.
+ *
+ * @param point Point for which the normal is calculated.
+ * @param a Accuracy in scaled units
+ */
+ Vector getNormal(Vector point, float a) const;
+
+ /**
+ * Generate random terrain.
+ *
+ * @param seed Seed for the randomnumber generator.
+ */
+ void generateTerrain(int seed);
+
+ /**
+ * Draw the terrain for given graphicscontext.
+ *
+ * @param gc CL_GraphicContext
+ */
+ void draw(CL_GraphicContext &gc) const;
+ /**
+ * Draw part of the terrain for given graphiscontext.
+ *
+ * @param gc CL_GraphicContext
+ * @param center Center of the rectangle drawn.
+ * @param dimension Dimensions of the rectangle.
+ */
+ void draw(CL_GraphicContext &gc, Vector center, Vector dimensions);
+
+ /**
+ * Set terrain.
+ *
+ * @param terrain Terrain.
+ */
+ void setTerrain(const std::vector<std::vector<TerrainType> > &terrain);
+ /**
+ * Get terrain.
+ *
+ * @return Terrain.
+ */
+ std::vector<std::vector<TerrainType> > getTerrain() const;
+};
+
+#endif