23 * "real" units. Scaling is needed because physics simulation uses |
36 * "real" units. Scaling is needed because physics simulation uses |
24 * "real" units. The idea is that this class is used with "real" units |
37 * "real" units. The idea is that this class is used with "real" units |
25 * and it uses pixelcoordinates internally. |
38 * and it uses pixelcoordinates internally. |
26 */ |
39 */ |
27 class Terrain { |
40 class Terrain { |
28 public: // XXX: until we fix Network's access to this |
|
29 std::vector<std::vector<TerrainType> > terrain; |
|
30 |
|
31 /** |
|
32 * Generates pixelbuffer from terrain. Should be used only on |
|
33 * constructors because this is expected to be slow. |
|
34 */ |
|
35 void generatePixelBuffer(); |
|
36 |
|
37 protected: |
41 protected: |
38 // terrain dimensions |
42 /** The terrain data is stored as a linear array in row-major order, with width*height elements */ |
39 PixelDimension map_width, map_height; |
43 TerrainPixel *terrain_buf; |
40 |
44 |
41 // We can pre-render the terrain as a pixel buffer and then just modify this |
45 /** Terrain dimensions */ |
|
46 PixelDimension width, height; |
|
47 |
|
48 /** We pre-render the textured terrain data for display */ |
42 CL_PixelBuffer pixbuf; |
49 CL_PixelBuffer pixbuf; |
43 |
50 |
44 // terrain texture |
51 // XXX: terrain texture |
45 std::vector<std::vector<int> > texture; |
52 std::vector<std::vector<int> > texture; |
46 |
53 |
47 /** |
54 /** |
48 * Default constructor. The width/height are set to zero and the terrain is invalid until it gets updated |
55 * Default constructor. The width/height are set to zero and the terrain is invalid until it gets updated |
49 * |
|
50 * @param scale The "real" width and height of the terrain. |
|
51 */ |
56 */ |
52 Terrain (void); |
57 Terrain (void); |
53 |
58 |
54 /** |
59 /** |
55 * Constructor. |
60 * Constructor. |
56 * |
61 * |
57 * @param scale The "real" width and height of the terrain. |
62 * @param width terrain width |
58 * @param seed Random number generator seed used to create the |
63 * @param height terrain height |
59 * terrain. |
64 * @param seed andom number generator seed used to generate the random terrain. |
60 */ |
65 */ |
61 Terrain (PixelDimension map_width, PixelDimension map_height, int seed); |
66 Terrain (PixelDimension width, PixelDimension height, int seed); |
62 |
|
63 /** |
|
64 * Copy constructor. |
|
65 * |
|
66 * @param t Terrain to be copied. |
|
67 */ |
|
68 Terrain (const Terrain &t); |
|
69 |
67 |
70 /** |
68 /** |
71 * Destructor |
69 * Destructor |
72 */ |
70 */ |
73 ~Terrain (void) {} |
71 ~Terrain (void); |
|
72 |
|
73 private: |
|
74 /* No copying */ |
|
75 Terrain (const Terrain ©); |
|
76 Terrain &operator= (const Terrain ©); |
74 |
77 |
75 protected: |
78 protected: |
76 void generate_texture(); |
79 /** |
77 void noisifyPixel(CL_Color& col, PixelCoordinate pc); |
80 * Set induvidual pixel value, updates both terrain_buf and pixbuf |
78 |
81 */ |
|
82 inline void setType (PixelDimension x, PixelDimension y, TerrainType t) { |
|
83 terrain_buf[y * width + x] = (TerrainPixel) t; |
|
84 |
|
85 // XXX: locking? |
|
86 pixbuf.draw_pixel(x, y, getTexturePixel(x, y)); |
|
87 } |
|
88 |
|
89 /** |
|
90 * Generate random terrain using given seed |
|
91 * |
|
92 * @param seed seed for the random number generator |
|
93 */ |
|
94 void generateTerrain (int seed); |
|
95 |
|
96 /** |
|
97 * Regenerates our pixbuf from the current terrain. This is a slow process, but using the pixbuffer directly |
|
98 * greatly speeds up drawing. |
|
99 */ |
|
100 void generatePixelBuffer (void); |
|
101 |
|
102 /** |
|
103 * Generates an area of random texture using a fractal-based algorithm, this can then be applied to the terrain |
|
104 * pixels using noisifyPixel |
|
105 */ |
|
106 void generateTexture(); |
|
107 |
|
108 /** |
|
109 * Applies noise generated using generateTexture to generate a slightly varying color for the terrain pixel |
|
110 * at \a pc. |
|
111 */ |
|
112 CL_Color getTexturePixel (PixelDimension x, PixelDimension y); |
79 |
113 |
80 /** |
114 /** |
81 * Scale parameter to "pixels" |
115 * Scale parameter to "pixels" |
82 * |
116 * |
83 * @param x Scaled value |
117 * @param x Scaled value |
84 * @return Corresponding value in pixels |
118 * @return Corresponding value in pixels |
85 */ |
119 */ |
86 PixelDimension scale (float x) const; |
120 inline PixelDimension scale (float x) const { |
87 |
121 return (int) x; |
88 /** |
122 } |
89 * Sets to color the correct color of pixel in (x,y) |
|
90 */ |
|
91 void loadPixelColor (CL_Color& color, PixelCoordinate pc); |
|
92 |
123 |
93 public: |
124 public: |
94 /** |
125 /** |
95 * Get pixel location of a point that is in "real" units. |
126 * Get pixel location of a point that is in "real" units. |
96 * |
127 * |
97 * @param point Point in "real" units |
128 * @param point Point in "real" units |
98 * @return Int vector |
129 * @return Int vector |
99 */ |
130 */ |
100 PixelCoordinate getPixelCoordinate (Vector point) const; |
131 inline PixelCoordinate getPixelCoordinate (Vector point) const { |
|
132 return PixelCoordinate(scale(point.x), scale(point.y)); |
|
133 } |
101 |
134 |
102 /** |
135 /** |
103 * Return the terrain dimensions à la a PixelCoordinate |
136 * Return the terrain dimensions à la a PixelCoordinate |
104 */ |
137 */ |
105 PixelCoordinate getDimensions (void) const; |
138 inline PixelCoordinate getDimensions (void) const { |
106 |
139 return PixelCoordinate(width, height); |
107 /** |
140 } |
108 * Return the type of terrain at given position. Returns ROCK if |
141 |
109 * given point is not inside terrain area. |
142 /** |
110 * |
143 * Return the type of terrain at given position. Returns TERRAIN_ROCK if given point is not inside terrain area. |
111 * @param x X coordinate |
144 * |
112 * @param y Y coordinate |
145 * @param x terrain x coordinate |
113 * @return Terrain type |
146 * @param y terrain x coordinate |
114 */ |
147 * @return terrain pixel type |
115 TerrainType getType (PixelDimension px, PixelDimension py) const; |
148 */ |
116 TerrainType getType (PixelCoordinate pc) const; |
149 TerrainType getType (PixelDimension x, PixelDimension y) const { |
117 TerrainType getType (Vector point) const; |
150 // XXX: optimize access by removing error checking? |
118 |
151 if (x < 0 || y < 0 || x >= width || y >= height) |
119 /** |
152 return TERRAIN_ROCK; |
120 * Check if given point has some terrain. |
153 |
121 * |
154 return (TerrainType) terrain_buf[y * width + x]; |
122 * @param point Point that is in scaled units. |
155 } |
|
156 |
|
157 inline TerrainType getType (PixelCoordinate pc) const { |
|
158 return getType(pc.x, pc.y); |
|
159 } |
|
160 |
|
161 |
|
162 inline TerrainType getType (Vector point) const { |
|
163 return getType(getPixelCoordinate(point)); |
|
164 } |
|
165 |
|
166 /** |
|
167 * Get raw read-only terrain data buffer |
|
168 * |
|
169 * @see terrain_buf |
|
170 */ |
|
171 const TerrainPixel* getTerrainBuffer (void) const; |
|
172 |
|
173 /** |
|
174 * Load new terrain data from given buffer. Must be the right size |
|
175 * |
|
176 * @see terrain_buf |
|
177 */ |
|
178 void loadFromBuffer (const TerrainPixel *buf); |
|
179 |
|
180 /** |
|
181 * Check if the given point is terrain |
123 */ |
182 */ |
124 bool collides (const Vector &point) const; |
183 bool collides (const Vector &point) const; |
125 /** |
184 |
126 * Check if given line collides with terrain. |
185 /** |
127 * |
186 * Check if the given line collides with terrain |
128 * @param begin Line begin point in scaled units. |
187 * |
129 * @param end Line end point in scaled units. |
188 * @param begin line starting point |
|
189 * @param end line ending point |
130 */ |
190 */ |
131 bool collides (const Vector &begin, const Vector &end) const; |
191 bool collides (const Vector &begin, const Vector &end) const; |
132 |
192 |
133 /** |
193 /** |
134 * Remove a circular area from terrain. |
194 * Remove a circular area of terrain |
135 * |
195 * |
136 * @param pos Circle center |
196 * @param pos circle center |
137 * @param r Circle radius |
197 * @param r circle radius |
138 */ |
198 */ |
139 void removeGround (const Vector &pos, float r); |
199 void removeGround (const Vector &pos, float r); |
140 |
200 |
141 /** |
201 /** |
142 * Return normal for the given point. |
202 * Returns direction normal vector for the given \a point. |
143 * |
203 * |
144 * @param point Point for which the normal is calculated. |
204 * \a point should be ground, and \a prevPoint air. |
145 * @return Normal vector ((0,0) if there's no terrain) |
205 * |
|
206 * @param point the ground that we collided into |
|
207 * @param prevPoint position in air where we were before the collision |
|
208 * @return direction ormal vector, or (0,0) if there's no terrain |
146 */ |
209 */ |
147 Vector getNormal (Vector point, Vector prevPoint) const; |
210 Vector getNormal (Vector point, Vector prevPoint) const; |
148 |
211 |
149 /** |
212 /** |
150 * Generate random terrain. |
213 * Draw the terrain onto the given graphics context |
151 * |
214 * |
152 * @param seed Seed for the randomnumber generator. |
215 * @param gc Graphics to draw on |
153 */ |
216 * @param camera view position |
154 void generateTerrain (int seed); |
|
155 |
|
156 /** |
|
157 * Draw the terrain for given graphicscontext. |
|
158 * |
|
159 * @param gc CL_GraphicContext |
|
160 * @param camera - upper left corner of screen |
|
161 */ |
217 */ |
162 virtual void draw (Graphics *g, PixelCoordinate camera = PixelCoordinate(0, 0)); |
218 virtual void draw (Graphics *g, PixelCoordinate camera = PixelCoordinate(0, 0)); |
163 |
|
164 /** |
|
165 * Draw part of the terrain for given graphiscontext. |
|
166 * |
|
167 * @param gc CL_GraphicContext |
|
168 * @param center Center of the rectangle drawn. |
|
169 * @param dimension Dimensions of the rectangle. |
|
170 */ |
|
171 //void draw(CL_GraphicContext &gc, Vector center, Vector dimensions); |
|
172 |
|
173 /** |
|
174 * Set terrain. |
|
175 * |
|
176 * @param terrain Terrain. |
|
177 */ |
|
178 void setTerrain (const std::vector<std::vector<TerrainType> > &terrain); |
|
179 |
|
180 /** |
|
181 * Get terrain. |
|
182 * |
|
183 * @return Terrain. |
|
184 */ |
|
185 std::vector<std::vector<TerrainType> > getTerrain() const; |
|
186 }; |
219 }; |
187 |
220 |
188 #endif |
221 #endif |