FIFE  2008.0
camera.h
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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
36 #include "model/structures/location.h"
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 
45  typedef Point3D ScreenPoint;
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:
60  enum Transform {
61  NormalTransform = 0,
62  WarpedTransform = 1
63  };
64 
77  Camera(const std::string& id,
78  Layer* layer,
79  const Rect& viewport,
80  RenderBackend* renderbackend);
81 
84  virtual ~Camera();
85 
88  const std::string& getId() const { return m_id; }
89 
92  void setId(const std::string& id) { m_id = id; }
93 
98  void setTilt(double tilt);
99 
103  double getTilt() const;
104 
110  void setRotation(double rotation);
111 
115  double getRotation() const;
116 
120  void setZoom(double zoom);
121 
125  double getZoom() const;
126 
132  void setCellImageDimensions(uint32_t width, uint32_t height);
133 
139 
144 
147  double getReferenceScale() const { return m_reference_scale; }
148 
152  Point3D getZOffset(Layer* layer);
153 
157  void setLocation(const Location& location);
158 
162  Location getLocation() const;
163 
167  Point3D getOrigin() const;
168 
174  Location& getLocationRef();
175 
180  void attach(Instance *instance);
181 
184  void detach();
185 
188  Instance* getAttached() const { return m_attachedto; }
189 
194  void setViewPort(const Rect& viewport);
195 
199  const Rect& getViewPort() const;
200 
206  ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
207 
211  ScreenPoint toScreenCoordinates(const ExactModelCoordinate& map_coords);
212 
217 
218  ScreenPoint virtualScreenToScreen(const DoublePoint3D& p);
219  DoublePoint3D screenToVirtualScreen(const ScreenPoint& p);
220 
223  void setEnabled(bool enabled);
224 
227  bool isEnabled();
228 
234  void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
235 
242  void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
243 
250  void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
251 
259  void update();
260 
266  void refresh();
267 
270  void resetUpdates();
271 
274  void addRenderer(RendererBase* renderer);
275 
278  RendererBase* getRenderer(const std::string& name);
279 
282  void resetRenderers();
283 
286  void calculateZValue(ScreenPoint& screen_coords);
287 
289 
290  void onRendererEnabledChanged(RendererBase* renderer);
291 
294  void setLightingColor(float red, float green, float blue);
295 
298  void resetLightingColor();
299 
302  std::vector<float> getLightingColor();
303 
306  void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
307 
310  std::vector<uint8_t> getOverlayColor();
311 
314  void resetOverlayColor();
315 
319  void setOverlayImage(int32_t id, bool fill = false);
320 
323  int32_t getOverlayImage();
324 
327  void resetOverlayImage();
328 
332  void setOverlayAnimation(AnimationPtr anim, bool fill = false);
333 
337 
340  void resetOverlayAnimation();
341 
344  void render();
345 
346  private:
347  friend class MapObserver;
348  void addLayer(Layer* layer);
349  void removeLayer(Layer* layer);
350  void updateMap(Map* map);
351  std::string m_id;
352 
353 
360  void updateMatrices();
361 
368  void updateReferenceScale();
369 
372  void updateRenderLists();
373 
376  void cacheUpdate(Layer* layer);
377 
380  DoublePoint getLogicalCellDimensions(Layer* layer);
381 
384  Point getRealCellDimensions(Layer* layer);
385 
388  void renderOverlay();
389 
390  DoubleMatrix m_matrix;
391  DoubleMatrix m_inverse_matrix;
392 
393  DoubleMatrix m_vs_matrix;
394  DoubleMatrix m_vs_inverse_matrix;
395  DoubleMatrix m_vscreen_2_screen;
396  DoubleMatrix m_screen_2_vscreen;
397 
398  double m_tilt;
399  double m_rotation;
400  double m_zoom;
401  Location m_location;
402  ScreenPoint m_cur_origo;
403  Rect m_viewport;
404  bool m_view_updated;
405  uint32_t m_screen_cell_width;
406  uint32_t m_screen_cell_height;
407  double m_reference_scale;
408  bool m_enabled;
409  Instance* m_attachedto;
410  // caches calculated image dimensions for already queried & calculated layers
411  std::map<Layer*, Point> m_image_dimensions;
412  bool m_iswarped; // true, if the geometry had changed
413 
414  // list of renderers managed by the view
415  std::map<std::string, RendererBase*> m_renderers;
416  std::list<RendererBase*> m_pipeline;
417  bool m_updated; // false, if view has not been updated
418 
419  RenderBackend* m_renderbackend;
420 
421  // caches layer -> instances structure between renders e.g. to fast query of mouse picking order
422  t_layer_to_instances m_layer_to_instances;
423 
424  std::map<Layer*,LayerCache*> m_cache;
425  MapObserver* m_map_observer;
426  Map* m_map;
427 
428  // is lighting enable
429  bool m_lighting;
430  // caches the light color for the camera
431  std::vector<float> m_light_colors;
432 
433  // overlay stuff
434  bool m_col_overlay;
435  bool m_img_overlay;
436  bool m_ani_overlay;
437  SDL_Color m_overlay_color;
438  int32_t m_img_id;
439  AnimationPtr m_ani_ptr;
440  bool m_img_fill;
441  bool m_ani_fill;
442  uint32_t m_start_time;
443  };
444 }
445 #endif
int32_t getOverlayImage()
Definition: camera.cpp:715
void setOverlayImage(int32_t id, bool fill=false)
Definition: camera.cpp:709
void resetUpdates()
Definition: camera.cpp:604
Instance * getAttached() const
Definition: camera.h:188
void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
Definition: camera.cpp:682
DoublePoint3D toVirtualScreenCoordinates(const ExactModelCoordinate &map_coords)
Definition: camera.cpp:336
void resetLightingColor()
Definition: camera.cpp:677
void resetRenderers()
Definition: camera.cpp:641
void setTilt(double tilt)
Definition: camera.cpp:128
void resetOverlayAnimation()
Definition: camera.cpp:739
void setId(const std::string &id)
Definition: camera.h:92
void attach(Instance *instance)
Definition: camera.cpp:576
double getTilt() const
Definition: camera.cpp:137
Point3D getZOffset(Layer *layer)
Definition: camera.cpp:410
std::vector< uint8_t > getOverlayColor()
Definition: camera.cpp:690
void setRotation(double rotation)
Definition: camera.cpp:141
void resetOverlayColor()
Definition: camera.cpp:705
void onRendererEnabledChanged(RendererBase *renderer)
Definition: camera.cpp:626
Camera(const std::string &id, Layer *layer, const Rect &viewport, RenderBackend *renderbackend)
Definition: camera.cpp:77
bool isEnabled()
Definition: camera.cpp:271
RendererBase * getRenderer(const std::string &name)
Definition: camera.cpp:637
void addRenderer(RendererBase *renderer)
Definition: camera.cpp:613
const Rect & getViewPort() const
Definition: camera.cpp:263
void onRendererPipelinePositionChanged(RendererBase *renderer)
Definition: camera.cpp:622
void setLocation(const Location &location)
Definition: camera.cpp:177
double getZoom() const
Definition: camera.cpp:165
void setLightingColor(float red, float green, float blue)
Definition: camera.cpp:660
Location & getLocationRef()
Definition: camera.cpp:255
void resetOverlayImage()
Definition: camera.cpp:723
void detach()
Definition: camera.cpp:585
AnimationPtr getOverlayAnimation()
Definition: camera.cpp:735
virtual ~Camera()
Definition: camera.cpp:115
const std::string & getId() const
Definition: camera.h:88
void refresh()
Definition: camera.cpp:599
Point getCellImageDimensions()
Definition: camera.cpp:229
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Definition: camera.cpp:331
void getMatchingInstances(ScreenPoint screen_coords, Layer &layer, std::list< Instance * > &instances, uint8_t alpha=0)
Definition: camera.cpp:465
Location getLocation() const
Definition: camera.cpp:251
void calculateZValue(ScreenPoint &screen_coords)
Definition: camera.cpp:319
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true)
Definition: camera.cpp:324
double getReferenceScale() const
Definition: camera.h:147
void setOverlayAnimation(AnimationPtr anim, bool fill=false)
Definition: camera.cpp:728
Point3D getOrigin() const
Definition: camera.cpp:275
void update()
Definition: camera.cpp:589
void setCellImageDimensions(uint32_t width, uint32_t height)
Definition: camera.cpp:169
void setViewPort(const Rect &viewport)
Definition: camera.cpp:259
Definition: map.h:86
void setEnabled(bool enabled)
Definition: camera.cpp:267
void render()
Definition: camera.cpp:827
std::vector< float > getLightingColor()
Definition: camera.cpp:668
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39
double getRotation() const
Definition: camera.cpp:150
void setZoom(double zoom)
Definition: camera.cpp:154