FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
camera.h
Go to the documentation of this file.
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
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 
78  Camera(const std::string& id, Map* map, const Rect& viewport, RenderBackend* renderbackend);
79 
82  virtual ~Camera();
83 
86  const std::string& getId() const { return m_id; }
87 
90  void setId(const std::string& id) { m_id = id; }
91 
95  Map* getMap() { return m_map; }
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  void setLocation(const Location& location);
187 
193 
197  void setPosition(const ExactModelCoordinate& position);
198 
203 
207  Point3D getOrigin() const;
208 
213  void attach(Instance *instance);
214 
217  void detach();
218 
221  Instance* getAttached() const { return m_attachedTo; }
222 
227  void setViewPort(const Rect& viewport);
228 
232  const Rect& getViewPort() const;
233 
237  const Rect& getMapViewPort();
238 
243  Rect getLayerViewPort(Layer* layer);
244 
250  ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
251 
255  ScreenPoint toScreenCoordinates(const ExactModelCoordinate& map_coords);
256 
261 
265  ScreenPoint virtualScreenToScreen(const DoublePoint3D& p);
266 
270  DoublePoint3D screenToVirtualScreen(const ScreenPoint& p);
271 
274  void setEnabled(bool enabled);
275 
278  bool isEnabled();
279 
283 
292  void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
293 
302  void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
303 
310  void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
311 
319  void update();
320 
326  void refresh();
327 
330  void resetUpdates();
331 
334  bool isUpdated() { return m_updated; }
335 
338  void addRenderer(RendererBase* renderer);
339 
342  RendererBase* getRenderer(const std::string& name);
343 
346  void resetRenderers();
347 
350  void calculateZValue(DoublePoint3D& screen_coords);
351 
353 
354  void onRendererEnabledChanged(RendererBase* renderer);
355 
358  void setLightingColor(float red, float green, float blue);
359 
362  void resetLightingColor();
363 
366  std::vector<float> getLightingColor();
367 
370  void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
371 
374  std::vector<uint8_t> getOverlayColor();
375 
378  void resetOverlayColor();
379 
383  void setOverlayImage(int32_t id, bool fill = false);
384 
387  int32_t getOverlayImage();
388 
391  void resetOverlayImage();
392 
396  void setOverlayAnimation(AnimationPtr anim, bool fill = false);
397 
401 
404  void resetOverlayAnimation();
405 
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;
420 
422 
429  void updateMatrices();
430 
436  void updateReferenceScale();
437 
440  void updateRenderLists();
441 
445 
449 
452  void renderOverlay();
453 
456  void renderStaticLayer(Layer* layer, bool update);
457 
460 
465 
466  double m_tilt;
467  double m_rotation;
468  double m_zoom;
469  double m_zToY;
472  ScreenPoint m_curOrigin;
473 
480  bool m_enabled;
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;
497 
498  // is lighting enable
500  // caches the light color for the camera
501  std::vector<float> m_light_colors;
502 
503  // overlay stuff
507  SDL_Color m_overlay_color;
508  int32_t m_img_id;
513  };
514 }
515 #endif
bool m_ani_fill
Definition: camera.h:511
void setPosition(const ExactModelCoordinate &position)
Sets map point for the camera.
Definition: camera.cpp:350
Abstract interface for all the renderbackends.
void setZToY(double zToY)
Sets zToY value for the camera and enables their use.
Definition: camera.cpp:207
int32_t getOverlayImage()
Returns the pool id of the overlay image.
Definition: camera.cpp:819
std::list< RendererBase * > m_pipeline
Definition: camera.h:488
RenderBackend * m_renderbackend
Definition: camera.h:419
double m_tilt
Definition: camera.h:466
DoubleMatrix m_vs_inverse_matrix
Definition: camera.h:462
const std::string & getId() const
Gets the identifier for this camera.
Definition: camera.h:86
Map * getMap()
Gets the map where camera is bound.
Definition: camera.h:95
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:813
void calculateZValue(DoublePoint3D &screen_coords)
calculates z-value for given screenpoint
Definition: camera.cpp:410
void init()
Definition: camera.cpp:137
void updateMatrices()
Updates the camera transformation matrix T with requested values.
Definition: camera.cpp:368
void resetUpdates()
Resets temporary values from last update round, like warped flag.
Definition: camera.cpp:699
DoubleMatrix m_inverse_matrix
Definition: camera.h:459
void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
Sets a color as overlay.
Definition: camera.cpp:786
Instance * m_attachedTo
Definition: camera.h:481
Rect m_viewport
Definition: camera.h:418
Transform m_transform
Definition: camera.h:484
int32_t m_img_id
Definition: camera.h:508
uint32_t m_screenCellHeight
Definition: camera.h:477
DoubleMatrix m_vs_matrix
Definition: camera.h:461
RenderList & getRenderListRef(Layer *layer)
Returns reference to RenderList.
Definition: camera.cpp:520
DoublePoint3D toVirtualScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to virtual screen coordinates.
Definition: camera.cpp:428
double getZToY() const
Gets zToY value.
Definition: camera.cpp:216
void resetLightingColor()
Resets lighting color.
Definition: camera.cpp:781
void setZToYEnabled(bool enabled)
Sets z to y manipulation enabled / disabled.
Definition: camera.cpp:220
void resetRenderers()
resets active layer information on all renderers.
Definition: camera.cpp:740
void setTilt(double tilt)
Sets tilt for the camera.
Definition: camera.cpp:151
double m_referenceScaleY
Definition: camera.h:479
void resetOverlayAnimation()
Resets the animation overlay.
Definition: camera.cpp:843
Camera(const std::string &id, Map *map, const Rect &viewport, RenderBackend *renderbackend)
Constructor Camera needs to be added to the view.
Definition: camera.cpp:80
bool m_enabledZToY
Definition: camera.h:470
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:510
void setId(const std::string &id)
Sets the identifier for this camera.
Definition: camera.h:90
void attach(Instance *instance)
Attaches the camera to an instance.
Definition: camera.cpp:669
DoubleMatrix m_screen_2_vscreen
Definition: camera.h:464
std::vector< uint8_t > getOverlayColor()
Returns a vector that contain the overlay color.
Definition: camera.cpp:794
void setRotation(double rotation)
Sets rotation for the camera.
Definition: camera.cpp:164
void resetOverlayColor()
Resets the color overlay.
Definition: camera.cpp:809
void onRendererEnabledChanged(RendererBase *renderer)
Renderer is enabled / disabled.
Definition: camera.cpp:725
t_layer_to_instances m_layerToInstances
Definition: camera.h:493
bool isEnabled()
Gets if camera is enabled / disabled.
Definition: camera.cpp:346
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:504
ScreenPoint m_curOrigin
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:524
uint32_t m_start_time
Definition: camera.h:512
RendererBase * getRenderer(const std::string &name)
Gets renderer with given name.
Definition: camera.cpp:736
uint32_t m_screenCellWidth
Definition: camera.h:476
std::map< std::string, RendererBase * > m_renderers
Definition: camera.h:487
DoubleMatrix m_matrix
Definition: camera.h:458
bool m_enabled
Definition: camera.h:480
void addRenderer(RendererBase *renderer)
Adds new renderer on the view.
Definition: camera.cpp:712
double m_zToY
Definition: camera.h:469
unsigned char uint8_t
Definition: core.h:38
std::map< Layer *, LayerCache * > m_cache
Definition: camera.h:495
double getReferenceScaleX() const
Gets x reference scale for cell image dimensions.
Definition: camera.h:177
bool m_ani_overlay
Definition: camera.h:506
void onRendererPipelinePositionChanged(RendererBase *renderer)
Renderer&#39;s pipeline position has been changed.
Definition: camera.cpp:721
void setLocation(const Location &location)
Sets the location for camera.
Definition: camera.cpp:236
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:490
void removeLayer(Layer *layer)
Definition: camera.cpp:754
A basic layer on a map.
Definition: layer.h:99
double getRotation() const
Gets camera rotation.
Definition: camera.cpp:173
void setLightingColor(float red, float green, float blue)
Sets lighting color.
Definition: camera.cpp:764
void resetOverlayImage()
Resets the image overlay.
Definition: camera.cpp:827
double getOriginalZToY() const
Gets original zToY transformation value.
Definition: camera.cpp:192
std::map< Layer *, RenderList > t_layer_to_instances
Definition: camera.h:51
void detach()
Detaches the camera from an instance.
Definition: camera.cpp:675
void renderOverlay()
Renders the overlay(color, image, animation) for the camera.
Definition: camera.cpp:848
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:188
const Rect & getMapViewPort()
Gets the viewport for camera in map coordinates.
Definition: camera.cpp:293
bool m_lighting
Definition: camera.h:499
RendererListener allows reaction to changes in renderer Having this implemented via callback mechanis...
Definition: rendererbase.h:50
void addLayer(Layer *layer)
Definition: camera.cpp:747
AnimationPtr getOverlayAnimation()
Returns an AnimationPtr to the overlay animation.
Definition: camera.cpp:839
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:289
virtual ~Camera()
Destructor.
Definition: camera.cpp:118
void refresh()
Refreshes camera view in case e.g.
Definition: camera.cpp:693
AnimationPtr m_ani_ptr
Definition: camera.h:509
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:507
Point getCellImageDimensions()
Gets screen cell image dimensions.
Definition: camera.cpp:259
bool isUpdated()
Returns true if camera view has been updated, otherwise false.
Definition: camera.h:334
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:423
A 3D Point.
Definition: point.h:205
double getTilt() const
Gets camera tilt.
Definition: camera.cpp:160
bool m_mapViewPortUpdated
Definition: camera.h:475
ExactModelCoordinate m_position
Definition: camera.h:421
Map * m_map
Definition: camera.h:417
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true)
Transforms given point from screen coordinates to map coordinates.
Definition: camera.cpp:415
double getReferenceScaleY() const
Gets y reference scale for cell image dimensions.
Definition: camera.h:181
double m_referenceScaleX
Definition: camera.h:478
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:832
Location getLocation()
Gets the location camera is rendering.
Definition: camera.cpp:271
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:903
Rect m_mapViewPort
Definition: camera.h:474
DoubleMatrix m_vscreen_2_screen
Definition: camera.h:463
Point3D getOrigin() const
Gets screen point of the camera.
Definition: camera.cpp:364
void update()
General update routine.
Definition: camera.cpp:679
Instance * getAttached() const
Returns instance where camera is attached.
Definition: camera.h:221
std::vector< float > m_light_colors
Definition: camera.h:501
DoublePoint getLogicalCellDimensions()
Gets logical cell image dimensions and ignores the layer and cellgrid.
Definition: camera.cpp:475
void setCellImageDimensions(uint32_t width, uint32_t height)
Sets screen cell image dimensions.
Definition: camera.cpp:228
void setViewPort(const Rect &viewport)
Sets the viewport for camera viewport is rectangle inside the view where camera renders.
Definition: camera.cpp:284
A container of Layer(s).
Definition: map.h:88
double m_zoom
Definition: camera.h:468
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:437
Location m_location
Definition: camera.h:471
void setEnabled(bool enabled)
Sets camera enabled / disabled.
Definition: camera.cpp:342
SDL_Color m_overlay_color
Definition: camera.h:507
ExactModelCoordinate getPosition() const
Gets map point of the camera.
Definition: camera.cpp:360
void render()
Renders camera.
Definition: camera.cpp:981
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:94
void updateRenderLists()
Updates camera RenderLists.
Definition: camera.cpp:957
ScreenPoint virtualScreenToScreen(const DoublePoint3D &p)
Transforms given point from virtual screen coordinates to screen coordinates.
Definition: camera.cpp:433
double m_rotation
Definition: camera.h:467
std::vector< float > getLightingColor()
Returns a vector that contain the light color.
Definition: camera.cpp:772
Point3D ScreenPoint
Definition: camera.h:46
bool m_img_overlay
Definition: camera.h:505
bool isZToYEnabled() const
Gets if z to y manipulation is enabled / disabled.
Definition: camera.cpp:224
Rect getLayerViewPort(Layer *layer)
Gets the viewport for camera in layer coordinates.
Definition: camera.cpp:326
void setZoom(double zoom)
Sets zoom for the camera.
Definition: camera.cpp:177