src/Terrain.hh
author terom
Wed, 03 Dec 2008 19:16:32 +0000
changeset 185 25becd2cb026
child 203 3ec7ab40755f
permissions -rw-r--r--
that's not a prototype anymore... at least it shouldn't be
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
#ifndef TERRAIN_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#define TERRAIN_HH
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
#include <vector>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
#include "Vector.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
#include "Config.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
enum TerrainType {EMPTY, DIRT, ROCK};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
const Vector DIRECTIONS[] = {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    Vector(0,-1),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
    Vector(1,-1),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
    Vector(1,0),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
    Vector(1,1),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
    Vector(0,1),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
    Vector(-1,1),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
    Vector(-1,0),
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    19
    Vector(-1,-1)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
/**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
 * Terrain class. Represents game terrain and contains member
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
 * functions to manipulate terrain and get info about it.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    25
 * 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    26
 * Terrain resolution is a constant that is defined in
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
 * configuration. Terrain has a scale (i.e. the width and height in
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
 * "real" units. Scaling is needed because physics simulation uses
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
 * "real" units. The idea is that this class is used with "real" units
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    30
 * and it uses pixelcoordinates internally.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    31
 */ 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    32
class Terrain {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
private:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    34
    std::vector<std::vector<TerrainType> > terrain;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    36
    // Terrain graphic
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    37
    CL_PixelBuffer pixbuf;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    38
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    39
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    40
     * Generates pixelbuffer from terrain. Should be used only on
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    41
     * constructors because this is expected to be slow.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    42
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    43
    void generatePixelBuffer();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    44
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    46
     * Get pixel location of a point that is in "real" units.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    47
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
     * @param point Point in "real" units
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    49
     * @return Int vector
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
    Vector getPixelLocation(Vector point) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    52
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    53
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
     * Scale parameter to "pixels"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    55
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    56
     * @param x Scaled value
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    57
     * @return Corresponding value in pixels
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    58
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    59
    uint16_t scale(float x) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    60
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
 public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    63
    // TODO: This should be private.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    64
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    65
     * Return the type of terrain at given position. Returns ROCK if
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    66
     * given point is not inside terrain area.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
     * @param x X coordinate
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
     * @param y Y coordinate
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    70
     * @return Terrain type
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    72
    TerrainType getType(int32_t x, int32_t y) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    73
    //point is just rounded and redirected to that above
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
    TerrainType getType(Vector point) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    76
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
     * Constructor.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    78
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
     * @param scale The "real" width and height of the terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    80
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    81
    Terrain();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    82
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    83
     * Constructor.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    84
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    85
     * @param scale The "real" width and height of the terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
     * @param seed Random number generator seed used to create the
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    87
     * terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    88
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    89
    Terrain(const int &seed);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    90
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    91
     * Copy constructor.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    92
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    93
     * @param t Terrain to be copied.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
    Terrain(const Terrain &t);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    96
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    97
     * Destructor
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
    ~Terrain() {}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   100
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   101
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   102
     * Check if given point has some terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   103
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
     * @param point Point that is in scaled units.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   105
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
    bool collides(const Vector &point) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
     * Check if given line collides with terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
     * @param begin Line begin point in scaled units.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
     * @param end Line end point in scaled units.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   112
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
    bool collides(const Vector &begin, const Vector &end) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   114
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   115
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   116
     * Remove a circular area from terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   118
     * @param pos Circle center
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   119
     * @param r Circle radius
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   120
     */ 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   121
    void removeGround(const Vector &pos, const float &r);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   122
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   123
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   124
     * Return normal for the given point.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   125
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
     * @param point Point for which the normal is calculated.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
     * @return Normal vector ((0,0) if there's no terrain)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   128
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   129
    Vector getNormal(Vector point, Vector prevPoint) const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   130
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   131
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   132
     * Generate random terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   133
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   134
     * @param seed Seed for the randomnumber generator.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   135
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   136
    void generateTerrain(int seed);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   137
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   138
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   139
     * Draw the terrain for given graphicscontext.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   140
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   141
     * @param gc CL_GraphicContext
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   142
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   143
    virtual void draw(CL_GraphicContext *gc);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   144
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   145
     * Draw part of the terrain for given graphiscontext.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   146
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   147
     * @param gc CL_GraphicContext
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   148
     * @param center Center of the rectangle drawn.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   149
     * @param dimension Dimensions of the rectangle.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   150
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   151
    //void draw(CL_GraphicContext &gc, Vector center, Vector dimensions);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   152
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   153
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   154
     * Set terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   155
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   156
     * @param terrain Terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   157
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   158
    void setTerrain(const std::vector<std::vector<TerrainType> > &terrain);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   159
    /**
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   160
     * Get terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   161
     *
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   162
     * @return Terrain.
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   163
     */
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   164
    std::vector<std::vector<TerrainType> > getTerrain() const;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   165
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   166
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   167
Vector direction(const Vector &v);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   168
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   169
#endif