FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
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 
31 // FIFE includes
32 // These includes are split up in two parts, separated by one empty line
33 // First block: files included from the FIFE root src directory
34 // Second block: files included from the same folder
35 #include "model/structures/location.h"
36 #include "util/structures/rect.h"
37 #include "util/math/matrix.h"
38 
39 #include "rendererbase.h"
40 
41 namespace FIFE {
42 
43  typedef Point3D ScreenPoint;
44  class Layer;
45  class Instance;
46  class ImagePool;
47  class AnimationPool;
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  ImagePool* ipool,
82  AnimationPool* apool);
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 
134  void setCellImageDimensions(unsigned int width, unsigned int height);
135 
141 
146 
150  void setLocation(const Location& location);
151 
155  Location getLocation() const;
156 
157  Point3D getOrigin() const;
158 
164  Location& getLocationRef();
165 
170  void attach(Instance *instance);
171 
174  void detach();
175 
178  Instance* getAttached() const { return m_attachedto; }
179 
184  void setViewPort(const Rect& viewport);
185 
189  const Rect& getViewPort() const;
190 
196  ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
197 
202 
207 
208  ScreenPoint virtualScreenToScreen(const DoublePoint3D& p);
209  DoublePoint3D screenToVirtualScreen(const ScreenPoint& p);
210 
213  void setEnabled(bool enabled);
214 
217  bool isEnabled();
218 
224  void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances);
225 
232  void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances);
233 
240  void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
241 
249  void update();
250 
256  void refresh();
257 
260  void resetUpdates();
261 
264  void addRenderer(RendererBase* renderer);
265 
268  RendererBase* getRenderer(const std::string& name);
269 
272  void resetRenderers();
273 
276  void calculateZValue(ScreenPoint& screen_coords);
277 
279  void onRendererEnabledChanged(RendererBase* renderer);
280 
284  bool testRenderedViewPort();
285 
286  void setLightingColor(float red, float green, float blue, float alpha);
287  void resetLightingColor();
288  std::vector<float> getLightingColor();
289 
292  void render();
293 
294  private:
295  friend class MapObserver;
296  void addLayer(Layer* layer);
297  void removeLayer(Layer* layer);
298  void updateMap(Map* map);
299  std::string m_id;
300 
301 
308  void updateMatrices();
309 
316  void updateReferenceScale();
317 
320  DoublePoint getLogicalCellDimensions(Layer* layer);
321 
322  DoubleMatrix m_matrix;
323  DoubleMatrix m_inverse_matrix;
324 
325  DoubleMatrix m_vs_matrix;
326  DoubleMatrix m_vs_inverse_matrix;
327  DoubleMatrix m_vscreen_2_screen;
328  DoubleMatrix m_screen_2_vscreen;
329 
330  double m_tilt;
331  double m_rotation;
332  double m_zoom;
333  Location m_location;
334  ScreenPoint m_prev_origo;
335  ScreenPoint m_cur_origo;
336  Rect m_viewport;
337  bool m_view_updated;
338  unsigned int m_screen_cell_width;
339  unsigned int m_screen_cell_height;
340  double m_reference_scale;
341  bool m_enabled;
342  Instance* m_attachedto;
343  bool m_backendSDL;
344  // caches calculated image dimensions for already queried & calculated layers
345  std::map<Layer*, Point> m_image_dimensions;
346  bool m_iswarped;
347 
348  // list of renderers managed by the view
349  std::map<std::string, RendererBase*> m_renderers;
350  std::list<RendererBase*> m_pipeline;
351  bool m_updated; // false, if view has never been updated before
352 
353  RenderBackend* m_renderbackend;
354  ImagePool* m_ipool;
355  AnimationPool* m_apool;
356 
357  // caches layer -> instances structure between renders e.g. to fast query of mouse picking order
358  t_layer_to_instances m_layer_to_instances;
359 
360  std::map<Layer*,LayerCache*> m_cache;
361  MapObserver* m_map_observer;
362  Map* m_map;
363 
364  // is lighting enable
365  bool m_lighting;
366  // caches the light color for the camera
367  std::vector<float> m_light_colors;
368  };
369 }
370 #endif