FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
camera.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 
29 // 3rd party library includes
30 #include <SDL.h>
31 
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src directory
35 // Second block: files included from the same folder
37 #include "util/structures/rect.h"
38 #include "util/math/matrix.h"
39 #include "video/animation.h"
40 
41 #include "rendererbase.h"
42 
43 namespace FIFE {
44 
46  class Layer;
47  class Instance;
48  class RenderBackend;
49  class LayerCache;
50  class MapObserver;
51  typedef std::map<Layer*, RenderList > t_layer_to_instances;
52 
59  public:
61  NoneTransform = 0x00,
62  TiltTransform = 0x01,
64  ZoomTransform = 0x04,
66  ZTransform = 0x10
67  };
69 
79  Camera(const std::string& id,
80  Layer* layer,
81  const Rect& viewport,
82  RenderBackend* renderbackend);
83 
86  virtual ~Camera();
87 
90  const std::string& getId() const { return m_id; }
91 
94  void setId(const std::string& id) { m_id = id; }
95 
100  void setTilt(double tilt);
101 
105  double getTilt() const;
106 
112  void setRotation(double rotation);
113 
117  double getRotation() const;
118 
122  void setZoom(double zoom);
123 
127  double getZoom() const;
128 
132  double getOriginalZToY() const;
133 
139  void setZToY(double zToY);
140 
144  double getZToY() const;
145 
149  void setZToYEnabled(bool enabled);
150 
154  bool isZToYEnabled() const;
155 
161  void setCellImageDimensions(uint32_t width, uint32_t height);
162 
168 
173 
176  double getReferenceScaleX() const { return m_referenceScaleX; }
177 
180  double getReferenceScaleY() const { return m_referenceScaleY; }
181 
185  Point3D getZOffset(Layer* layer);
186 
190  void setLocation(const Location& location);
191 
195  Location getLocation() const;
196 
200  Point3D getOrigin() const;
201 
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 
256 
261 
266 
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(ScreenPoint& 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  void updateMap(Map* map);
415  std::string m_id;
416 
417 
424  void updateMatrices();
425 
432  void updateReferenceScale();
433 
436  void updateRenderLists();
437 
441 
445 
448  void renderOverlay();
449 
452  void renderStaticLayer(Layer* layer, bool update);
453 
456 
461 
462  double m_tilt;
463  double m_rotation;
464  double m_zoom;
465  double m_zToY;
476  bool m_enabled;
478  // caches calculated image dimensions for already queried & calculated layers
479  std::map<Layer*, Point> m_image_dimensions;
480  // contains the geometry changes
482 
483  // list of renderers managed by the view
484  std::map<std::string, RendererBase*> m_renderers;
485  std::list<RendererBase*> m_pipeline;
486  // false, if view has not been updated
487  bool m_updated;
488 
490 
491  // caches layer -> instances structure between renders e.g. to fast query of mouse picking order
493 
494  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
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:821
std::list< RendererBase * > m_pipeline
Definition: camera.h:485
DoublePoint getLogicalCellDimensions(Layer *layer)
Gets logical cell image dimensions for given layer.
Definition: camera.cpp:444
RenderBackend * m_renderbackend
Definition: camera.h:489
double m_tilt
Definition: camera.h:462
DoubleMatrix m_vs_inverse_matrix
Definition: camera.h:458
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:815
void updateMap(Map *map)
Definition: camera.cpp:250
uint32_t m_screen_cell_height
Definition: camera.h:473
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:704
DoubleMatrix m_inverse_matrix
Definition: camera.h:455
Instance * getAttached() const
Returns instance where camera is attached.
Definition: camera.h:221
void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
Sets a color as overlay.
Definition: camera.cpp:788
Rect m_viewport
Definition: camera.h:469
Transform m_transform
Definition: camera.h:481
int32_t m_img_id
Definition: camera.h:508
DoubleMatrix m_vs_matrix
Definition: camera.h:457
RenderList & getRenderListRef(Layer *layer)
Returns reference to RenderList.
Definition: camera.cpp:526
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:431
void resetLightingColor()
Resets lighting color.
Definition: camera.cpp:783
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:745
void setTilt(double tilt)
Sets tilt for the camera.
Definition: camera.cpp:136
double m_referenceScaleY
Definition: camera.h:475
void resetOverlayAnimation()
Resets the animation overlay.
Definition: camera.cpp:845
bool m_enabledZToY
Definition: camera.h:466
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:94
void attach(Instance *instance)
Attaches the camera to an instance.
Definition: camera.cpp:672
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:499
DoubleMatrix m_screen_2_vscreen
Definition: camera.h:460
std::vector< uint8_t > getOverlayColor()
Returns a vector that contain the overlay color.
Definition: camera.cpp:796
void setRotation(double rotation)
Sets rotation for the camera.
Definition: camera.cpp:149
void resetOverlayColor()
Resets the color overlay.
Definition: camera.cpp:811
void onRendererEnabledChanged(RendererBase *renderer)
Renderer is enabled / disabled.
Definition: camera.cpp:730
t_layer_to_instances m_layerToInstances
Definition: camera.h:492
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:360
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:58
bool m_col_overlay
Definition: camera.h:504
uint32_t m_start_time
Definition: camera.h:512
ScreenPoint m_cur_origo
Definition: camera.h:468
RendererBase * getRenderer(const std::string &name)
Gets renderer with given name.
Definition: camera.cpp:741
std::map< std::string, RendererBase * > m_renderers
Definition: camera.h:484
DoubleMatrix m_matrix
Definition: camera.h:454
bool m_enabled
Definition: camera.h:476
void addRenderer(RendererBase *renderer)
Adds new renderer on the view.
Definition: camera.cpp:717
double m_zToY
Definition: camera.h:465
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:494
bool m_ani_overlay
Definition: camera.h:506
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:303
void onRendererPipelinePositionChanged(RendererBase *renderer)
Renderer's pipeline position has been changed.
Definition: camera.cpp:726
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:487
void removeLayer(Layer *layer)
Definition: camera.cpp:759
A basic layer on a map.
Definition: layer.h:99
double getReferenceScaleY() const
Gets y reference scale for cell image dimensions.
Definition: camera.h:180
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:172
void setLightingColor(float red, float green, float blue)
Sets lighting color.
Definition: camera.cpp:766
Location & getLocationRef()
Gets a reference to the camera location.
Definition: camera.cpp:295
void resetOverlayImage()
Resets the image overlay.
Definition: camera.cpp:829
std::map< Layer *, RenderList > t_layer_to_instances
Definition: camera.h:50
void detach()
Detaches the camera from an instance.
Definition: camera.cpp:681
void renderOverlay()
Renders the overlay(color, image, animation) for the camera.
Definition: camera.cpp:850
const Rect & getMapViewPort()
Gets the viewport for camera in map coordinates.
Definition: camera.cpp:307
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:752
AnimationPtr getOverlayAnimation()
Returns an AnimationPtr to the overlay animation.
Definition: camera.cpp:841
virtual ~Camera()
Destructor.
Definition: camera.cpp:123
const std::string & getId() const
Gets the identifier for this camera.
Definition: camera.h:90
void refresh()
Refreshes camera view in case e.g.
Definition: camera.cpp:699
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:513
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:334
uint32_t Transform
Definition: camera.h:68
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to screen coordinates.
Definition: camera.cpp:426
std::map< Layer *, Point > m_image_dimensions
Definition: camera.h:479
A 3D Point.
Definition: point.h:202
Instance * m_attachedto
Definition: camera.h:477
bool m_mapViewPortUpdated
Definition: camera.h:471
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:530
Location getLocation() const
Gets the location camera is rendering.
Definition: camera.cpp:291
void calculateZValue(ScreenPoint &screen_coords)
calculates z-value for given screenpoint
Definition: camera.cpp:414
Map * m_map
Definition: camera.h:496
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true)
Transforms given point from screen coordinates to map coordinates.
Definition: camera.cpp:419
double m_referenceScaleX
Definition: camera.h:474
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:834
std::string m_id
Definition: camera.h:415
void renderStaticLayer(Layer *layer, bool update)
Renders the layer part that is on screen as one image.
Definition: camera.cpp:905
Rect m_mapViewPort
Definition: camera.h:470
DoubleMatrix m_vscreen_2_screen
Definition: camera.h:459
Point3D getOrigin() const
Gets screen point for the camera location.
Definition: camera.cpp:364
void update()
General update routine.
Definition: camera.cpp:685
std::vector< float > m_light_colors
Definition: camera.h:501
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:464
MapObserver * m_map_observer
Definition: camera.h:495
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:440
Location m_location
Definition: camera.h:467
double getReferenceScaleX() const
Gets x reference scale for cell image dimensions.
Definition: camera.h:176
void setEnabled(bool enabled)
Sets camera enabled / disabled.
Definition: camera.cpp:356
SDL_Color m_overlay_color
Definition: camera.h:507
double getZToY() const
Gets zToY value.
Definition: camera.cpp:200
void render()
Renders camera.
Definition: camera.cpp:984
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:100
void updateRenderLists()
Updates camera RenderLists.
Definition: camera.cpp:959
ScreenPoint virtualScreenToScreen(const DoublePoint3D &p)
Transforms given point from virtual screen coordinates to screen coordinates.
Definition: camera.cpp:436
double m_rotation
Definition: camera.h:463
std::vector< float > getLightingColor()
Returns a vector that contain the light color.
Definition: camera.cpp:774
Point3D ScreenPoint
Definition: camera.h:45
bool m_img_overlay
Definition: camera.h:505
uint32_t m_screen_cell_width
Definition: camera.h:472
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:478
Rect getLayerViewPort(Layer *layer)
Gets the viewport for camera in layer coordinates.
Definition: camera.cpp:340
void setZoom(double zoom)
Sets zoom for the camera.
Definition: camera.cpp:161