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 getReferenceScale() const { return m_reference_scale; }
177 
181  Point3D getZOffset(Layer* layer);
182 
186  void setLocation(const Location& location);
187 
191  Location getLocation() const;
192 
196  Point3D getOrigin() const;
197 
204 
209  void attach(Instance *instance);
210 
213  void detach();
214 
217  Instance* getAttached() const { return m_attachedto; }
218 
223  void setViewPort(const Rect& viewport);
224 
228  const Rect& getViewPort() const;
229 
233  const Rect& getMapViewPort();
234 
239  Rect getLayerViewPort(Layer* layer);
240 
246  ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
247 
252 
257 
260 
263  void setEnabled(bool enabled);
264 
267  bool isEnabled();
268 
272 
281  void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
282 
291  void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
292 
299  void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
300 
308  void update();
309 
315  void refresh();
316 
319  void resetUpdates();
320 
323  bool isUpdated() { return m_updated; }
324 
327  void addRenderer(RendererBase* renderer);
328 
331  RendererBase* getRenderer(const std::string& name);
332 
335  void resetRenderers();
336 
339  void calculateZValue(ScreenPoint& screen_coords);
340 
342 
343  void onRendererEnabledChanged(RendererBase* renderer);
344 
347  void setLightingColor(float red, float green, float blue);
348 
351  void resetLightingColor();
352 
355  std::vector<float> getLightingColor();
356 
359  void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
360 
363  std::vector<uint8_t> getOverlayColor();
364 
367  void resetOverlayColor();
368 
372  void setOverlayImage(int32_t id, bool fill = false);
373 
376  int32_t getOverlayImage();
377 
380  void resetOverlayImage();
381 
385  void setOverlayAnimation(AnimationPtr anim, bool fill = false);
386 
390 
393  void resetOverlayAnimation();
394 
397  void render();
398 
399  private:
400  friend class MapObserver;
401  void addLayer(Layer* layer);
402  void removeLayer(Layer* layer);
403  void updateMap(Map* map);
404  std::string m_id;
405 
406 
413  void updateMatrices();
414 
421  void updateReferenceScale();
422 
425  void updateRenderLists();
426 
430 
434 
437  void renderOverlay();
438 
441  void renderStaticLayer(Layer* layer, bool update);
442 
445 
450 
451  double m_tilt;
452  double m_rotation;
453  double m_zoom;
454  double m_zToY;
464  bool m_enabled;
466  // caches calculated image dimensions for already queried & calculated layers
467  std::map<Layer*, Point> m_image_dimensions;
468  // contains the geometry changes
470 
471  // list of renderers managed by the view
472  std::map<std::string, RendererBase*> m_renderers;
473  std::list<RendererBase*> m_pipeline;
474  // false, if view has not been updated
475  bool m_updated;
476 
478 
479  // caches layer -> instances structure between renders e.g. to fast query of mouse picking order
481 
482  std::map<Layer*,LayerCache*> m_cache;
485 
486  // is lighting enable
488  // caches the light color for the camera
489  std::vector<float> m_light_colors;
490 
491  // overlay stuff
495  SDL_Color m_overlay_color;
496  int32_t m_img_id;
501  };
502 }
503 #endif
bool m_ani_fill
Definition: camera.h:499
Abstract interface for all the renderbackends.
Definition: renderbackend.h:92
void setZToY(double zToY)
Sets zToY value for the camera and enables their use.
Definition: camera.cpp:187
int32_t getOverlayImage()
Returns the pool id of the overlay image.
Definition: camera.cpp:779
std::list< RendererBase * > m_pipeline
Definition: camera.h:473
DoublePoint getLogicalCellDimensions(Layer *layer)
Gets logical cell image dimensions for given layer.
Definition: camera.cpp:441
RenderBackend * m_renderbackend
Definition: camera.h:477
double m_tilt
Definition: camera.h:451
DoubleMatrix m_vs_inverse_matrix
Definition: camera.h:447
std::vector< RenderItem * > RenderList
Definition: renderitem.h:82
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:773
void updateMap(Map *map)
Definition: camera.cpp:246
uint32_t m_screen_cell_height
Definition: camera.h:462
void updateMatrices()
Updates the camera transformation matrix T with requested values.
Definition: camera.cpp:364
void resetUpdates()
Resets temporary values from last update round, like warped flag.
Definition: camera.cpp:664
DoubleMatrix m_inverse_matrix
Definition: camera.h:444
Instance * getAttached() const
Returns instance where camera is attached.
Definition: camera.h:217
void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
Sets a color as overlay.
Definition: camera.cpp:746
Rect m_viewport
Definition: camera.h:458
Transform m_transform
Definition: camera.h:469
int32_t m_img_id
Definition: camera.h:496
DoubleMatrix m_vs_matrix
Definition: camera.h:446
RenderList & getRenderListRef(Layer *layer)
Returns reference to RenderList.
Definition: camera.cpp:519
bool isZToYEnabled() const
Gets if z to y manipulation is enabled / disabled.
Definition: camera.cpp:204
DoublePoint3D toVirtualScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to virtual screen coordinates.
Definition: camera.cpp:428
void resetLightingColor()
Resets lighting color.
Definition: camera.cpp:741
void setZToYEnabled(bool enabled)
Sets z to y manipulation enabled / disabled.
Definition: camera.cpp:200
void resetRenderers()
resets active layer information on all renderers.
Definition: camera.cpp:705
void setTilt(double tilt)
Sets tilt for the camera.
Definition: camera.cpp:132
void resetOverlayAnimation()
Resets the animation overlay.
Definition: camera.cpp:803
bool m_enabledZToY
Definition: camera.h:455
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:498
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:632
double getTilt() const
Gets camera tilt.
Definition: camera.cpp:141
Point3D getZOffset(Layer *layer)
Gets a point that contain the visual z(z=1) difference, based on the given layer. ...
Definition: camera.cpp:496
DoubleMatrix m_screen_2_vscreen
Definition: camera.h:449
std::vector< uint8_t > getOverlayColor()
Returns a vector that contain the overlay color.
Definition: camera.cpp:754
void setRotation(double rotation)
Sets rotation for the camera.
Definition: camera.cpp:145
void resetOverlayColor()
Resets the color overlay.
Definition: camera.cpp:769
void onRendererEnabledChanged(RendererBase *renderer)
Renderer is enabled / disabled.
Definition: camera.cpp:690
Camera(const std::string &id, Layer *layer, const Rect &viewport, RenderBackend *renderbackend)
Constructor Camera needs to be added to the view.
Definition: camera.cpp:77
bool isEnabled()
Gets if camera is enabled / disabled.
Definition: camera.cpp:356
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:492
uint32_t m_start_time
Definition: camera.h:500
ScreenPoint m_cur_origo
Definition: camera.h:457
RendererBase * getRenderer(const std::string &name)
Gets renderer with given name.
Definition: camera.cpp:701
std::map< std::string, RendererBase * > m_renderers
Definition: camera.h:472
DoubleMatrix m_matrix
Definition: camera.h:443
bool m_enabled
Definition: camera.h:464
void addRenderer(RendererBase *renderer)
Adds new renderer on the view.
Definition: camera.cpp:677
double m_zToY
Definition: camera.h:454
unsigned char uint8_t
Definition: core.h:38
double getOriginalZToY() const
Gets original zToY transformation value.
Definition: camera.cpp:172
std::map< Layer *, LayerCache * > m_cache
Definition: camera.h:482
bool m_ani_overlay
Definition: camera.h:494
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:299
void onRendererPipelinePositionChanged(RendererBase *renderer)
Renderer&#39;s pipeline position has been changed.
Definition: camera.cpp:686
void setLocation(const Location &location)
Sets the location for camera.
Definition: camera.cpp:216
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:475
t_layer_to_instances m_layer_to_instances
Definition: camera.h:480
void removeLayer(Layer *layer)
Definition: camera.cpp:718
A basic layer on a map.
Definition: layer.h:98
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:168
void setLightingColor(float red, float green, float blue)
Sets lighting color.
Definition: camera.cpp:724
Location & getLocationRef()
Gets a reference to the camera location.
Definition: camera.cpp:291
void resetOverlayImage()
Resets the image overlay.
Definition: camera.cpp:787
std::map< Layer *, RenderList > t_layer_to_instances
Definition: camera.h:50
void detach()
Detaches the camera from an instance.
Definition: camera.cpp:641
void renderOverlay()
Renders the overlay(color, image, animation) for the camera.
Definition: camera.cpp:808
const Rect & getMapViewPort()
Gets the viewport for camera in map coordinates.
Definition: camera.cpp:303
bool m_lighting
Definition: camera.h:487
RendererListener allows reaction to changes in renderer Having this implemented via callback mechanis...
Definition: rendererbase.h:50
void addLayer(Layer *layer)
Definition: camera.cpp:712
AnimationPtr getOverlayAnimation()
Returns an AnimationPtr to the overlay animation.
Definition: camera.cpp:799
virtual ~Camera()
Destructor.
Definition: camera.cpp:119
double m_reference_scale
Definition: camera.h:463
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:659
AnimationPtr m_ani_ptr
Definition: camera.h:497
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:510
Point getCellImageDimensions()
Gets screen cell image dimensions.
Definition: camera.cpp:267
bool isUpdated()
Returns true if camera view has been updated, otherwise false.
Definition: camera.h:323
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:423
std::map< Layer *, Point > m_image_dimensions
Definition: camera.h:467
A 3D Point.
Definition: point.h:202
Instance * m_attachedto
Definition: camera.h:465
bool m_mapViewPortUpdated
Definition: camera.h:460
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:523
Location getLocation() const
Gets the location camera is rendering.
Definition: camera.cpp:287
void calculateZValue(ScreenPoint &screen_coords)
calculates z-value for given screenpoint
Definition: camera.cpp:411
Map * m_map
Definition: camera.h:484
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true)
Transforms given point from screen coordinates to map coordinates.
Definition: camera.cpp:416
double getReferenceScale() const
Gets reference scale for cell image dimensions.
Definition: camera.h:176
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:792
std::string m_id
Definition: camera.h:404
void renderStaticLayer(Layer *layer, bool update)
Renders the layer part that is on screen as one image.
Definition: camera.cpp:863
Rect m_mapViewPort
Definition: camera.h:459
DoubleMatrix m_vscreen_2_screen
Definition: camera.h:448
Point3D getOrigin() const
Gets screen point for the camera location.
Definition: camera.cpp:360
void update()
General update routine.
Definition: camera.cpp:645
std::vector< float > m_light_colors
Definition: camera.h:489
void setCellImageDimensions(uint32_t width, uint32_t height)
Sets screen cell image dimensions.
Definition: camera.cpp:208
void setViewPort(const Rect &viewport)
Sets the viewport for camera viewport is rectangle inside the view where camera renders.
Definition: camera.cpp:295
A container of Layer(s).
Definition: map.h:87
double m_zoom
Definition: camera.h:453
MapObserver * m_map_observer
Definition: camera.h:483
unsigned int uint32_t
Definition: core.h:40
DoublePoint3D screenToVirtualScreen(const ScreenPoint &p)
Definition: camera.cpp:437
Location m_location
Definition: camera.h:456
void setEnabled(bool enabled)
Sets camera enabled / disabled.
Definition: camera.cpp:352
SDL_Color m_overlay_color
Definition: camera.h:495
double getZToY() const
Gets zToY value.
Definition: camera.cpp:196
void render()
Renders camera.
Definition: camera.cpp:925
An Instance is an &quot;instantiation&quot; of an Object at a Location.
Definition: instance.h:97
void updateRenderLists()
Updates camera RenderLists.
Definition: camera.cpp:900
ScreenPoint virtualScreenToScreen(const DoublePoint3D &p)
Definition: camera.cpp:433
double m_rotation
Definition: camera.h:452
std::vector< float > getLightingColor()
Returns a vector that contain the light color.
Definition: camera.cpp:732
Point3D ScreenPoint
Definition: camera.h:45
bool m_img_overlay
Definition: camera.h:493
uint32_t m_screen_cell_width
Definition: camera.h:461
double getRotation() const
Gets camera rotation.
Definition: camera.cpp:153
Point getRealCellDimensions(Layer *layer)
Gets real cell image dimensions for given layer.
Definition: camera.cpp:475
Rect getLayerViewPort(Layer *layer)
Gets the viewport for camera in layer coordinates.
Definition: camera.cpp:336
void setZoom(double zoom)
Sets zoom for the camera.
Definition: camera.cpp:157