| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_VIEW_CAMERA_H |
| 23 |
#define FIFE_VIEW_CAMERA_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <string> |
| 27 |
#include <map> |
| 28 |
#include <algorithm> |
| 29 |
|
| 30 |
// 3rd party library includes |
| 31 |
#include <SDL.h> |
| 32 |
|
| 33 |
// FIFE includes |
| 34 |
// These includes are split up in two parts, separated by one empty line |
| 35 |
// First block: files included from the FIFE root src directory |
| 36 |
// Second block: files included from the same folder |
| 37 |
#include "model/structures/location.h" |
| 38 |
#include "util/structures/rect.h" |
| 39 |
#include "util/math/matrix.h" |
| 40 |
#include "video/animation.h" |
| 41 |
|
| 42 |
#include "rendererbase.h" |
| 43 |
|
| 44 |
namespace FIFE { |
| 45 |
|
| 46 |
typedef Point3D ScreenPoint; |
| 47 |
class Layer; |
| 48 |
class Instance; |
| 49 |
class RenderBackend; |
| 50 |
class LayerCache; |
| 51 |
class MapObserver; |
| 52 |
typedef std::map<Layer*, RenderList > t_layer_to_instances; |
| 53 |
|
| 54 |
/** Camera describes properties of a view port shown in the main screen |
| 55 |
* Main screen can have multiple cameras active simultanously |
| 56 |
* Different cameras can have different properties, like location |
| 57 |
* to shoot, zoom or tilt |
| 58 |
*/ |
| 59 |
class Camera: public IRendererListener, public IRendererContainer { |
| 60 |
public: |
| 61 |
enum TransformType { |
| 62 |
NoneTransform = 0x00, |
| 63 |
TiltTransform = 0x01, |
| 64 |
RotationTransform = 0x02, |
| 65 |
ZoomTransform = 0x04, |
| 66 |
PositionTransform = 0x08, |
| 67 |
ZTransform = 0x10 |
| 68 |
}; |
| 69 |
typedef uint32_t Transform; |
| 70 |
|
| 71 |
/** Constructor |
| 72 |
* Camera needs to be added to the view. If not done so, it is not rendered. |
| 73 |
* @param id identifier for the camera |
| 74 |
* @param map map where camera is bound |
| 75 |
* @param viewport used viewport for the camera. Viewport is measured in pixels in relation to game main screen |
| 76 |
* @param renderbackend to use with rendering |
| 77 |
*/ |
| 78 |
Camera(const std::string& id, Map* map, const Rect& viewport, RenderBackend* renderbackend); |
| 79 |
|
| 80 |
/** Destructor |
| 81 |
*/ |
| 82 |
virtual ~Camera(); |
| 83 |
|
| 84 |
/** Gets the identifier for this camera. |
| 85 |
*/ |
| 86 |
const std::string& getId() const { return m_id; } |
| 87 |
|
| 88 |
/** Sets the identifier for this camera. |
| 89 |
*/ |
| 90 |
void setId(const std::string& id) { m_id = id; } |
| 91 |
|
| 92 |
/** Gets the map where camera is bound. |
| 93 |
* @return The associated map for this camera. |
| 94 |
*/ |
| 95 |
Map* getMap() { return m_map; } |
| 96 |
|
| 97 |
/** Sets tilt for the camera. |
| 98 |
* e.g. overhead camera has tilt 0, while traditional isometric camera has tilt 45 |
| 99 |
* @param tilt tilt for the camera |
| 100 |
*/ |
| 101 |
void setTilt(double tilt); |
| 102 |
|
| 103 |
/** Gets camera tilt |
| 104 |
* @return tilt of camera |
| 105 |
*/ |
| 106 |
double getTilt() const; |
| 107 |
|
| 108 |
/** Sets rotation for the camera. |
| 109 |
* Rotation can be visualized by thinking camera that rotates around an object |
| 110 |
* that it is rendering |
| 111 |
* @param rotation rotation for the camera |
| 112 |
*/ |
| 113 |
void setRotation(double rotation); |
| 114 |
|
| 115 |
/** Gets camera rotation |
| 116 |
* @return rotation of the camera |
| 117 |
*/ |
| 118 |
double getRotation() const; |
| 119 |
|
| 120 |
/** Sets zoom for the camera. |
| 121 |
* @param zoom zoom for the camera |
| 122 |
*/ |
| 123 |
void setZoom(double zoom); |
| 124 |
|
| 125 |
/** Gets camera zoom |
| 126 |
* @return zoom of the camera |
| 127 |
*/ |
| 128 |
double getZoom() const; |
| 129 |
|
| 130 |
/** Gets original zToY transformation value. |
| 131 |
* @return zToY value of the camera. |
| 132 |
*/ |
| 133 |
double getOriginalZToY() const; |
| 134 |
|
| 135 |
/** Sets zToY value for the camera and enables their use. |
| 136 |
* This means the factor which influenced the z to y transformation, |
| 137 |
* so if you set zToY=32 then 1z corresponds to 32 pixels in y direction. |
| 138 |
* @param zToY influenced the z to y transformation of the camera. |
| 139 |
*/ |
| 140 |
void setZToY(double zToY); |
| 141 |
|
| 142 |
/** Gets zToY value. |
| 143 |
* @return zToY value of the camera. |
| 144 |
*/ |
| 145 |
double getZToY() const; |
| 146 |
|
| 147 |
/** Sets z to y manipulation enabled / disabled. |
| 148 |
* @param enabled If true then the zToY value is used instead of the original matrix value. |
| 149 |
*/ |
| 150 |
void setZToYEnabled(bool enabled); |
| 151 |
|
| 152 |
/** Gets if z to y manipulation is enabled / disabled. |
| 153 |
* @return true if z to y manipulation is enabled, otherwise false. |
| 154 |
*/ |
| 155 |
bool isZToYEnabled() const; |
| 156 |
|
| 157 |
/** Sets screen cell image dimensions. |
| 158 |
* Cell image dimension is basically width and height of a bitmap, that covers |
| 159 |
* one cell in the layer where camera is bind |
| 160 |
* @return Point Point containing x=width and y=height |
| 161 |
*/ |
| 162 |
void setCellImageDimensions(uint32_t width, uint32_t height); |
| 163 |
|
| 164 |
/** Gets screen cell image dimensions. |
| 165 |
* @see setCellImageDimensions |
| 166 |
* @return Point containing x=width and y=height |
| 167 |
*/ |
| 168 |
Point getCellImageDimensions(); |
| 169 |
|
| 170 |
/** Gets screen cell image dimensions for given layer. |
| 171 |
* @return Point containing x=width and y=height |
| 172 |
*/ |
| 173 |
Point getCellImageDimensions(Layer* layer); |
| 174 |
|
| 175 |
/** Gets x reference scale for cell image dimensions |
| 176 |
*/ |
| 177 |
double getReferenceScaleX() const { return m_referenceScaleX; } |
| 178 |
|
| 179 |
/** Gets y reference scale for cell image dimensions |
| 180 |
*/ |
| 181 |
double getReferenceScaleY() const { return m_referenceScaleY; } |
| 182 |
|
| 183 |
/** Sets the location for camera |
| 184 |
* @param location location (center point) to render |
| 185 |
*/ |
| 186 |
void setLocation(const Location& location); |
| 187 |
|
| 188 |
/** Gets the location camera is rendering. If no location was set, the camera creates a location. |
| 189 |
* The top layer of the map together with current camera position are used for that. |
| 190 |
* @return camera location |
| 191 |
*/ |
| 192 |
Location getLocation(); |
| 193 |
|
| 194 |
/** Sets map point for the camera |
| 195 |
* @param position The camera position on the map |
| 196 |
*/ |
| 197 |
void setPosition(const ExactModelCoordinate& position); |
| 198 |
|
| 199 |
/** Gets map point of the camera |
| 200 |
* @return The camera position on the map |
| 201 |
*/ |
| 202 |
ExactModelCoordinate getPosition() const; |
| 203 |
|
| 204 |
/** Gets screen point of the camera |
| 205 |
* @return camera screen point |
| 206 |
*/ |
| 207 |
Point3D getOrigin() const; |
| 208 |
|
| 209 |
/** Attaches the camera to an instance. |
| 210 |
* @param instance Instance to which the camera shall be attached |
| 211 |
* @note The camera can only be attached to an instance at the same layer! |
| 212 |
*/ |
| 213 |
void attach(Instance *instance); |
| 214 |
|
| 215 |
/** Detaches the camera from an instance. |
| 216 |
*/ |
| 217 |
void detach(); |
| 218 |
|
| 219 |
/** Returns instance where camera is attached. NULL if not attached |
| 220 |
*/ |
| 221 |
Instance* getAttached() const { return m_attachedTo; } |
| 222 |
|
| 223 |
/** Sets the viewport for camera |
| 224 |
* viewport is rectangle inside the view where camera renders |
| 225 |
* @param viewport area for camera render |
| 226 |
*/ |
| 227 |
void setViewPort(const Rect& viewport); |
| 228 |
|
| 229 |
/** Gets the viewport for camera in pixel coordinates |
| 230 |
* @return camera viewport |
| 231 |
*/ |
| 232 |
const Rect& getViewPort() const; |
| 233 |
|
| 234 |
/** Gets the viewport for camera in map coordinates |
| 235 |
* @return camera viewport |
| 236 |
*/ |
| 237 |
const Rect& getMapViewPort(); |
| 238 |
|
| 239 |
/** Gets the viewport for camera in layer coordinates |
| 240 |
* @param layer A pointer to the layer whose geometry is used for the conversion |
| 241 |
* @return camera viewport |
| 242 |
*/ |
| 243 |
Rect getLayerViewPort(Layer* layer); |
| 244 |
|
| 245 |
/** Transforms given point from screen coordinates to map coordinates |
| 246 |
* @param screen_coords screen coordinates to transform |
| 247 |
* @param z_calculated if true, z-value (depth cut point) is pre-calculated. If false, camera calculates it |
| 248 |
* @return point in map coordinates |
| 249 |
*/ |
| 250 |
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true); |
| 251 |
|
| 252 |
/** Transforms given point from map coordinates to screen coordinates |
| 253 |
* @return point in screen coordinates |
| 254 |
*/ |
| 255 |
ScreenPoint toScreenCoordinates(const ExactModelCoordinate& map_coords); |
| 256 |
|
| 257 |
/** Transforms given point from map coordinates to virtual screen coordinates |
| 258 |
* @return point in virtual screen coordinates |
| 259 |
*/ |
| 260 |
DoublePoint3D toVirtualScreenCoordinates(const ExactModelCoordinate& map_coords); |
| 261 |
|
| 262 |
/** Transforms given point from virtual screen coordinates to screen coordinates |
| 263 |
* @return point in screen coordinates |
| 264 |
*/ |
| 265 |
ScreenPoint virtualScreenToScreen(const DoublePoint3D& p); |
| 266 |
|
| 267 |
/** Transforms given point from screen coordinates to virtual screen coordinates |
| 268 |
* @return point in virtual screen coordinates |
| 269 |
*/ |
| 270 |
DoublePoint3D screenToVirtualScreen(const ScreenPoint& p); |
| 271 |
|
| 272 |
/** Sets camera enabled / disabled |
| 273 |
*/ |
| 274 |
void setEnabled(bool enabled); |
| 275 |
|
| 276 |
/** Gets if camera is enabled / disabled |
| 277 |
*/ |
| 278 |
bool isEnabled(); |
| 279 |
|
| 280 |
/** Returns reference to RenderList. |
| 281 |
*/ |
| 282 |
RenderList& getRenderListRef(Layer* layer); |
| 283 |
|
| 284 |
/** Returns instances that match given screen coordinate |
| 285 |
* @param screen_coords screen coordinates to be used for hit search |
| 286 |
* @param layer layer to use for search |
| 287 |
* @param instances list of instances that is filled based on hit test results |
| 288 |
* @param alpha the alpha to use to filter the matching instances. Pixels |
| 289 |
* that have an alpha value higher than what is specified here are |
| 290 |
* considered a hit. |
| 291 |
*/ |
| 292 |
void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0); |
| 293 |
|
| 294 |
/** Returns instances that match given screen coordinate |
| 295 |
* @param screen_rect rect that contains screen coordinates to be used for search |
| 296 |
* @param layer layer to use for search |
| 297 |
* @param instances list of instances that is filled based on hit test results |
| 298 |
* @param alpha the alpha to use to filter the matching instances. Pixels |
| 299 |
* that have an alpha value higher than what is specified here are |
| 300 |
* considered a hit. |
| 301 |
*/ |
| 302 |
void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0); |
| 303 |
|
| 304 |
/** Returns instances that match given location. Instances are sorted based on camera view, so that "topmost" |
| 305 |
* instance is first in returned list |
| 306 |
* @param loc location where to fetch instances from |
| 307 |
* @param instances list of instances that is filled based on hit test results |
| 308 |
* @param use_exactcoordinates if true, comparison is done using exact coordinates. if not, cell coordinates are used |
| 309 |
*/ |
| 310 |
void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false); |
| 311 |
|
| 312 |
/** General update routine. |
| 313 |
* In this function, the camera's position gets updated when its attached |
| 314 |
* to another instance. |
| 315 |
* @note call this only once in engine update cycle, so that tracking between |
| 316 |
* current position and previous position keeps in sync. This information |
| 317 |
* is used e.g. by view to fix the pixel wobbling problem |
| 318 |
*/ |
| 319 |
void update(); |
| 320 |
|
| 321 |
/** Refreshes camera view in case e.g. location is updated directly (not via setLocation) |
| 322 |
* @note calling this function marks camera as "warped", therefore it causes all instance |
| 323 |
* positions to be recalculated. If you constantly call this, you end up with pixel wobbling |
| 324 |
* effect when camera is moved. |
| 325 |
*/ |
| 326 |
void refresh(); |
| 327 |
|
| 328 |
/** Resets temporary values from last update round, like warped flag |
| 329 |
*/ |
| 330 |
void resetUpdates(); |
| 331 |
|
| 332 |
/** Returns true if camera view has been updated, otherwise false |
| 333 |
*/ |
| 334 |
bool isUpdated() { return m_updated; } |
| 335 |
|
| 336 |
/** Adds new renderer on the view. Ownership is transferred to the camera. |
| 337 |
*/ |
| 338 |
void addRenderer(RendererBase* renderer); |
| 339 |
|
| 340 |
/** Gets renderer with given name |
| 341 |
*/ |
| 342 |
RendererBase* getRenderer(const std::string& name); |
| 343 |
|
| 344 |
/** resets active layer information on all renderers. |
| 345 |
*/ |
| 346 |
void resetRenderers(); |
| 347 |
|
| 348 |
/** calculates z-value for given screenpoint |
| 349 |
*/ |
| 350 |
void calculateZValue(DoublePoint3D& screen_coords); |
| 351 |
|
| 352 |
void onRendererPipelinePositionChanged(RendererBase* renderer); |
| 353 |
|
| 354 |
void onRendererEnabledChanged(RendererBase* renderer); |
| 355 |
|
| 356 |
/** Sets lighting color |
| 357 |
*/ |
| 358 |
void setLightingColor(float red, float green, float blue); |
| 359 |
|
| 360 |
/** Resets lighting color |
| 361 |
*/ |
| 362 |
void resetLightingColor(); |
| 363 |
|
| 364 |
/** Returns a vector that contain the light color |
| 365 |
*/ |
| 366 |
std::vector<float> getLightingColor(); |
| 367 |
|
| 368 |
/** Sets a color as overlay |
| 369 |
*/ |
| 370 |
void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); |
| 371 |
|
| 372 |
/** Returns a vector that contain the overlay color |
| 373 |
*/ |
| 374 |
std::vector<uint8_t> getOverlayColor(); |
| 375 |
|
| 376 |
/** Resets the color overlay |
| 377 |
*/ |
| 378 |
void resetOverlayColor(); |
| 379 |
|
| 380 |
/** Sets a image as overlay, |
| 381 |
* if fill is true the image gets the viewport size. |
| 382 |
*/ |
| 383 |
void setOverlayImage(int32_t id, bool fill = false); |
| 384 |
|
| 385 |
/** Returns the pool id of the overlay image |
| 386 |
*/ |
| 387 |
int32_t getOverlayImage(); |
| 388 |
|
| 389 |
/** Resets the image overlay |
| 390 |
*/ |
| 391 |
void resetOverlayImage(); |
| 392 |
|
| 393 |
/** Sets a animation as overlay, |
| 394 |
* if fill is true the animation gets the viewport size. |
| 395 |
*/ |
| 396 |
void setOverlayAnimation(AnimationPtr anim, bool fill = false); |
| 397 |
|
| 398 |
/** Returns an AnimationPtr to the overlay animation |
| 399 |
*/ |
| 400 |
AnimationPtr getOverlayAnimation(); |
| 401 |
|
| 402 |
/** Resets the animation overlay |
| 403 |
*/ |
| 404 |
void resetOverlayAnimation(); |
| 405 |
|
| 406 |
/** Renders camera |
| 407 |
*/ |
| 408 |
void render(); |
| 409 |
|
| 410 |
private: |
| 411 |
friend class MapObserver; |
| 412 |
void addLayer(Layer* layer); |
| 413 |
void removeLayer(Layer* layer); |
| 414 |
|
| 415 |
void init(); |
| 416 |
std::string m_id; |
| 417 |
Map* m_map; |
| 418 |
Rect m_viewport; |
| 419 |
RenderBackend* m_renderbackend; |
| 420 |
|
| 421 |
ExactModelCoordinate m_position; |
| 422 |
|
| 423 |
/** Updates the camera transformation matrix T with requested values. |
| 424 |
* The requests are done using these functions : |
| 425 |
* - setLocation |
| 426 |
* - setRotation |
| 427 |
* - setTilt |
| 428 |
*/ |
| 429 |
void updateMatrices(); |
| 430 |
|
| 431 |
/** Updates camera reference scale |
| 432 |
* Reference scale is in a sense an internal zooming factor, |
| 433 |
* which adjusts cell dimensions in logical space to ones shown on |
| 434 |
* screen. Calculation is based on current camera properties (e.g. rotation) |
| 435 |
*/ |
| 436 |
void updateReferenceScale(); |
| 437 |
|
| 438 |
/** Updates camera RenderLists |
| 439 |
*/ |
| 440 |
void updateRenderLists(); |
| 441 |
|
| 442 |
/** Gets logical cell image dimensions for given layer |
| 443 |
*/ |
| 444 |
DoublePoint getLogicalCellDimensions(Layer* layer); |
| 445 |
|
| 446 |
/** Gets logical cell image dimensions and ignores the layer and cellgrid |
| 447 |
*/ |
| 448 |
DoublePoint getLogicalCellDimensions(); |
| 449 |
|
| 450 |
/** Renders the overlay(color, image, animation) for the camera. |
| 451 |
*/ |
| 452 |
void renderOverlay(); |
| 453 |
|
| 454 |
/** Renders the layer part that is on screen as one image. |
| 455 |
*/ |
| 456 |
void renderStaticLayer(Layer* layer, bool update); |
| 457 |
|
| 458 |
DoubleMatrix m_matrix; |
| 459 |
DoubleMatrix m_inverse_matrix; |
| 460 |
|
| 461 |
DoubleMatrix m_vs_matrix; |
| 462 |
DoubleMatrix m_vs_inverse_matrix; |
| 463 |
DoubleMatrix m_vscreen_2_screen; |
| 464 |
DoubleMatrix m_screen_2_vscreen; |
| 465 |
|
| 466 |
double m_tilt; |
| 467 |
double m_rotation; |
| 468 |
double m_zoom; |
| 469 |
double m_zToY; |
| 470 |
bool m_enabledZToY; |
| 471 |
Location m_location; |
| 472 |
ScreenPoint m_curOrigin; |
| 473 |
|
| 474 |
Rect m_mapViewPort; |
| 475 |
bool m_mapViewPortUpdated; |
| 476 |
uint32_t m_screenCellWidth; |
| 477 |
uint32_t m_screenCellHeight; |
| 478 |
double m_referenceScaleX; |
| 479 |
double m_referenceScaleY; |
| 480 |
bool m_enabled; |
| 481 |
Instance* m_attachedTo; |
| 482 |
|
| 483 |
// contains the geometry changes |
| 484 |
Transform m_transform; |
| 485 |
|
| 486 |
// list of renderers managed by the view |
| 487 |
std::map<std::string, RendererBase*> m_renderers; |
| 488 |
std::list<RendererBase*> m_pipeline; |
| 489 |
// false, if view has not been updated |
| 490 |
bool m_updated; |
| 491 |
|
| 492 |
// caches layer -> instances structure between renders e.g. to fast query of mouse picking order |
| 493 |
t_layer_to_instances m_layerToInstances; |
| 494 |
|
| 495 |
std::map<Layer*,LayerCache*> m_cache; |
| 496 |
MapObserver* m_map_observer; |
| 497 |
|
| 498 |
// is lighting enable |
| 499 |
bool m_lighting; |
| 500 |
// caches the light color for the camera |
| 501 |
std::vector<float> m_light_colors; |
| 502 |
|
| 503 |
// overlay stuff |
| 504 |
bool m_col_overlay; |
| 505 |
bool m_img_overlay; |
| 506 |
bool m_ani_overlay; |
| 507 |
SDL_Color m_overlay_color; |
| 508 |
int32_t m_img_id; |
| 509 |
AnimationPtr m_ani_ptr; |
| 510 |
bool m_img_fill; |
| 511 |
bool m_ani_fill; |
| 512 |
uint32_t m_start_time; |
| 513 |
}; |
| 514 |
} |
| 515 |
#endif |