FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
camera.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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
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 
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 
60  public:
62  NoneTransform = 0x00,
63  TiltTransform = 0x01,
65  ZoomTransform = 0x04,
67  ZTransform = 0x10
68  };
70 
80  Camera(const std::string& id,
81  Layer* layer,
82  const Rect& viewport,
83  RenderBackend* renderbackend);
84 
87  virtual ~Camera();
88 
91  const std::string& getId() const { return m_id; }
92 
95  void setId(const std::string& id) { m_id = id; }
96 
101  void setTilt(double tilt);
102 
106  double getTilt() const;
107 
113  void setRotation(double rotation);
114 
118  double getRotation() const;
119 
123  void setZoom(double zoom);
124 
128  double getZoom() const;
129 
133  double getOriginalZToY() const;
134 
140  void setZToY(double zToY);
141 
145  double getZToY() const;
146 
150  void setZToYEnabled(bool enabled);
151 
155  bool isZToYEnabled() const;
156 
162  void setCellImageDimensions(uint32_t width, uint32_t height);
163 
169 
174 
177  double getReferenceScaleX() const { return m_referenceScaleX; }
178 
181  double getReferenceScaleY() const { return m_referenceScaleY; }
182 
186  Point3D getZOffset(Layer* layer);
187 
191  void setLocation(const Location& location);
192 
196  Location getLocation() const;
197 
201  Point3D getOrigin() const;
202 
209 
214  void attach(Instance *instance);
215 
218  void detach();
219 
222  Instance* getAttached() const { return m_attachedto; }
223 
228  void setViewPort(const Rect& viewport);
229 
233  const Rect& getViewPort() const;
234 
238  const Rect& getMapViewPort();
239 
244  Rect getLayerViewPort(Layer* layer);
245 
251  ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
252 
256  ScreenPoint toScreenCoordinates(const ExactModelCoordinate& map_coords);
257 
262 
266  ScreenPoint virtualScreenToScreen(const DoublePoint3D& p);
267 
271  DoublePoint3D screenToVirtualScreen(const ScreenPoint& p);
272 
275  void setEnabled(bool enabled);
276 
279  bool isEnabled();
280 
284 
293  void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
294 
303  void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
304 
311  void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
312 
320  void update();
321 
327  void refresh();
328 
331  void resetUpdates();
332 
335  bool isUpdated() { return m_updated; }
336 
339  void addRenderer(RendererBase* renderer);
340 
343  RendererBase* getRenderer(const std::string& name);
344 
347  void resetRenderers();
348 
351  void calculateZValue(DoublePoint3D& screen_coords);
352 
354 
355  void onRendererEnabledChanged(RendererBase* renderer);
356 
359  void setLightingColor(float red, float green, float blue);
360 
363  void resetLightingColor();
364 
367  std::vector<float> getLightingColor();
368 
371  void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
372 
375  std::vector<uint8_t> getOverlayColor();
376 
379  void resetOverlayColor();
380 
384  void setOverlayImage(int32_t id, bool fill = false);
385 
388  int32_t getOverlayImage();
389 
392  void resetOverlayImage();
393 
397  void setOverlayAnimation(AnimationPtr anim, bool fill = false);
398 
402 
405  void resetOverlayAnimation();
406 
409  void render();
410 
411  private:
412  friend class MapObserver;
413  void addLayer(Layer* layer);
414  void removeLayer(Layer* layer);
415  void updateMap(Map* map);
416  std::string m_id;
417 
418 
425  void updateMatrices();
426 
433  void updateReferenceScale();
434 
437  void updateRenderLists();
438 
442 
446 
449  void renderOverlay();
450 
453  void renderStaticLayer(Layer* layer, bool update);
454 
457 
462 
463  double m_tilt;
464  double m_rotation;
465  double m_zoom;
466  double m_zToY;
469  ScreenPoint m_cur_origo;
477  bool m_enabled;
479  // caches calculated image dimensions for already queried & calculated layers
480  std::map<Layer*, Point> m_image_dimensions;
481  // contains the geometry changes
482  Transform m_transform;
483 
484  // list of renderers managed by the view
485  std::map<std::string, RendererBase*> m_renderers;
486  std::list<RendererBase*> m_pipeline;
487  // false, if view has not been updated
488  bool m_updated;
489 
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;
498 
499  // is lighting enable
501  // caches the light color for the camera
502  std::vector<float> m_light_colors;
503 
504  // overlay stuff
508  SDL_Color m_overlay_color;
509  int32_t m_img_id;
514  };
515 }
516 #endif
bool m_ani_fill
Definition: camera.h:512
Abstract interface for all the renderbackends.
void setZToY(double zToY)
Sets zToY value for the camera and enables their use.
Definition: camera.cpp:191
int32_t getOverlayImage()
Returns the pool id of the overlay image.
Definition: camera.cpp:824
std::list< RendererBase * > m_pipeline
Definition: camera.h:486
DoublePoint getLogicalCellDimensions(Layer *layer)
Gets logical cell image dimensions for given layer.
Definition: camera.cpp:446
RenderBackend * m_renderbackend
Definition: camera.h:490
double m_tilt
Definition: camera.h:463
DoubleMatrix m_vs_inverse_matrix
Definition: camera.h:459
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
void setOverlayImage(int32_t id, bool fill=false)
Sets a image as overlay, if fill is true the image gets the viewport size.
Definition: camera.cpp:818
void updateMap(Map *map)
Definition: camera.cpp:250
void calculateZValue(DoublePoint3D &screen_coords)
calculates z-value for given screenpoint
Definition: camera.cpp:415
uint32_t m_screen_cell_height
Definition: camera.h:474
void updateMatrices()
Updates the camera transformation matrix T with requested values.
Definition: camera.cpp:369
void resetUpdates()
Resets temporary values from last update round, like warped flag.
Definition: camera.cpp:707
DoubleMatrix m_inverse_matrix
Definition: camera.h:456
Instance * getAttached() const
Returns instance where camera is attached.
Definition: camera.h:222
void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
Sets a color as overlay.
Definition: camera.cpp:791
Rect m_viewport
Definition: camera.h:470
Transform m_transform
Definition: camera.h:482
int32_t m_img_id
Definition: camera.h:509
DoubleMatrix m_vs_matrix
Definition: camera.h:458
RenderList & getRenderListRef(Layer *layer)
Returns reference to RenderList.
Definition: camera.cpp:528
bool isZToYEnabled() const
Gets if z to y manipulation is enabled / disabled.
Definition: camera.cpp:208
DoublePoint3D toVirtualScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to virtual screen coordinates.
Definition: camera.cpp:433
void resetLightingColor()
Resets lighting color.
Definition: camera.cpp:786
void setZToYEnabled(bool enabled)
Sets z to y manipulation enabled / disabled.
Definition: camera.cpp:204
void resetRenderers()
resets active layer information on all renderers.
Definition: camera.cpp:748
void setTilt(double tilt)
Sets tilt for the camera.
Definition: camera.cpp:136
double m_referenceScaleY
Definition: camera.h:476
void resetOverlayAnimation()
Resets the animation overlay.
Definition: camera.cpp:848
bool m_enabledZToY
Definition: camera.h:467
Interface to class owning the renderers Used to get correct subclass of renderer in scripting side (v...
Definition: rendererbase.h:66
bool m_img_fill
Definition: camera.h:511
void setId(const std::string &id)
Sets the identifier for this camera.
Definition: camera.h:95
void attach(Instance *instance)
Attaches the camera to an instance.
Definition: camera.cpp:674
double getTilt() const
Gets camera tilt.
Definition: camera.cpp:145
Point3D getZOffset(Layer *layer)
Gets a point that contain the visual z(z=1) difference, based on the given layer. ...
Definition: camera.cpp:501
DoubleMatrix m_screen_2_vscreen
Definition: camera.h:461
std::vector< uint8_t > getOverlayColor()
Returns a vector that contain the overlay color.
Definition: camera.cpp:799
void setRotation(double rotation)
Sets rotation for the camera.
Definition: camera.cpp:149
void resetOverlayColor()
Resets the color overlay.
Definition: camera.cpp:814
void onRendererEnabledChanged(RendererBase *renderer)
Renderer is enabled / disabled.
Definition: camera.cpp:733
t_layer_to_instances m_layerToInstances
Definition: camera.h:493
Camera(const std::string &id, Layer *layer, const Rect &viewport, RenderBackend *renderbackend)
Constructor Camera needs to be added to the view.
Definition: camera.cpp:80
bool isEnabled()
Gets if camera is enabled / disabled.
Definition: camera.cpp:361
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:59
bool m_col_overlay
Definition: camera.h:505
uint32_t m_start_time
Definition: camera.h:513
ScreenPoint m_cur_origo
Definition: camera.h:469
RendererBase * getRenderer(const std::string &name)
Gets renderer with given name.
Definition: camera.cpp:744
std::map< std::string, RendererBase * > m_renderers
Definition: camera.h:485
DoubleMatrix m_matrix
Definition: camera.h:455
bool m_enabled
Definition: camera.h:477
void addRenderer(RendererBase *renderer)
Adds new renderer on the view.
Definition: camera.cpp:720
double m_zToY
Definition: camera.h:466
unsigned char uint8_t
Definition: core.h:38
double getOriginalZToY() const
Gets original zToY transformation value.
Definition: camera.cpp:176
std::map< Layer *, LayerCache * > m_cache
Definition: camera.h:495
bool m_ani_overlay
Definition: camera.h:507
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:304
void onRendererPipelinePositionChanged(RendererBase *renderer)
Renderer&#39;s pipeline position has been changed.
Definition: camera.cpp:729
void setLocation(const Location &location)
Sets the location for camera.
Definition: camera.cpp:220
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
bool m_updated
Definition: camera.h:488
void removeLayer(Layer *layer)
Definition: camera.cpp:762
A basic layer on a map.
Definition: layer.h:99
double getReferenceScaleY() const
Gets y reference scale for cell image dimensions.
Definition: camera.h:181
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:172
void setLightingColor(float red, float green, float blue)
Sets lighting color.
Definition: camera.cpp:769
Location & getLocationRef()
Gets a reference to the camera location.
Definition: camera.cpp:295
void resetOverlayImage()
Resets the image overlay.
Definition: camera.cpp:832
std::map< Layer *, RenderList > t_layer_to_instances
Definition: camera.h:51
void detach()
Detaches the camera from an instance.
Definition: camera.cpp:683
void renderOverlay()
Renders the overlay(color, image, animation) for the camera.
Definition: camera.cpp:853
const Rect & getMapViewPort()
Gets the viewport for camera in map coordinates.
Definition: camera.cpp:308
bool m_lighting
Definition: camera.h:500
RendererListener allows reaction to changes in renderer Having this implemented via callback mechanis...
Definition: rendererbase.h:50
void addLayer(Layer *layer)
Definition: camera.cpp:755
AnimationPtr getOverlayAnimation()
Returns an AnimationPtr to the overlay animation.
Definition: camera.cpp:844
virtual ~Camera()
Destructor.
Definition: camera.cpp:123
const std::string & getId() const
Gets the identifier for this camera.
Definition: camera.h:91
void refresh()
Refreshes camera view in case e.g.
Definition: camera.cpp:701
AnimationPtr m_ani_ptr
Definition: camera.h:510
void updateReferenceScale()
Updates camera reference scale Reference scale is in a sense an internal zooming factor, which adjusts cell dimensions in logical space to ones shown on screen.
Definition: camera.cpp:515
Point getCellImageDimensions()
Gets screen cell image dimensions.
Definition: camera.cpp:271
bool isUpdated()
Returns true if camera view has been updated, otherwise false.
Definition: camera.h:335
uint32_t Transform
Definition: camera.h:69
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to screen coordinates.
Definition: camera.cpp:428
std::map< Layer *, Point > m_image_dimensions
Definition: camera.h:480
A 3D Point.
Definition: point.h:205
Instance * m_attachedto
Definition: camera.h:478
bool m_mapViewPortUpdated
Definition: camera.h:472
void getMatchingInstances(ScreenPoint screen_coords, Layer &layer, std::list< Instance * > &instances, uint8_t alpha=0)
Returns instances that match given screen coordinate.
Definition: camera.cpp:532
Location getLocation() const
Gets the location camera is rendering.
Definition: camera.cpp:291
Map * m_map
Definition: camera.h:497
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true)
Transforms given point from screen coordinates to map coordinates.
Definition: camera.cpp:420
double m_referenceScaleX
Definition: camera.h:475
void setOverlayAnimation(AnimationPtr anim, bool fill=false)
Sets a animation as overlay, if fill is true the animation gets the viewport size.
Definition: camera.cpp:837
std::string m_id
Definition: camera.h:416
void renderStaticLayer(Layer *layer, bool update)
Renders the layer part that is on screen as one image.
Definition: camera.cpp:908
Rect m_mapViewPort
Definition: camera.h:471
DoubleMatrix m_vscreen_2_screen
Definition: camera.h:460
Point3D getOrigin() const
Gets screen point for the camera location.
Definition: camera.cpp:365
void update()
General update routine.
Definition: camera.cpp:687
std::vector< float > m_light_colors
Definition: camera.h:502
void setCellImageDimensions(uint32_t width, uint32_t height)
Sets screen cell image dimensions.
Definition: camera.cpp:212
void setViewPort(const Rect &viewport)
Sets the viewport for camera viewport is rectangle inside the view where camera renders.
Definition: camera.cpp:299
A container of Layer(s).
Definition: map.h:88
double m_zoom
Definition: camera.h:465
MapObserver * m_map_observer
Definition: camera.h:496
unsigned int uint32_t
Definition: core.h:40
DoublePoint3D screenToVirtualScreen(const ScreenPoint &p)
Transforms given point from screen coordinates to virtual screen coordinates.
Definition: camera.cpp:442
Location m_location
Definition: camera.h:468
double getReferenceScaleX() const
Gets x reference scale for cell image dimensions.
Definition: camera.h:177
void setEnabled(bool enabled)
Sets camera enabled / disabled.
Definition: camera.cpp:357
SDL_Color m_overlay_color
Definition: camera.h:508
double getZToY() const
Gets zToY value.
Definition: camera.cpp:200
void render()
Renders camera.
Definition: camera.cpp:987
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:101
void updateRenderLists()
Updates camera RenderLists.
Definition: camera.cpp:962
ScreenPoint virtualScreenToScreen(const DoublePoint3D &p)
Transforms given point from virtual screen coordinates to screen coordinates.
Definition: camera.cpp:438
double m_rotation
Definition: camera.h:464
std::vector< float > getLightingColor()
Returns a vector that contain the light color.
Definition: camera.cpp:777
Point3D ScreenPoint
Definition: camera.h:46
bool m_img_overlay
Definition: camera.h:506
uint32_t m_screen_cell_width
Definition: camera.h:473
double getRotation() const
Gets camera rotation.
Definition: camera.cpp:157
Point getRealCellDimensions(Layer *layer)
Gets real cell image dimensions for given layer.
Definition: camera.cpp:480
Rect getLayerViewPort(Layer *layer)
Gets the viewport for camera in layer coordinates.
Definition: camera.cpp:341
void setZoom(double zoom)
Sets zoom for the camera.
Definition: camera.cpp:161