camera.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_VIEW_CAMERA_H
00023 #define FIFE_VIEW_CAMERA_H
00024 
00025 // Standard C++ library includes
00026 #include <string>
00027 #include <map>
00028 
00029 // 3rd party library includes
00030 
00031 // FIFE includes
00032 // These includes are split up in two parts, separated by one empty line
00033 // First block: files included from the FIFE root src directory
00034 // Second block: files included from the same folder
00035 #include "model/structures/location.h"
00036 #include "util/structures/rect.h"
00037 #include "util/math/matrix.h"
00038 
00039 #include "rendererbase.h"
00040 
00041 namespace FIFE {
00042 
00043     typedef Point3D ScreenPoint;
00044     class Layer;
00045     class Rect;
00046     class Instance;
00047     class ImagePool;
00048     class AnimationPool;
00049     class RenderBackend;
00050     typedef std::map<Layer*, std::vector<Instance*> > t_layer_to_instances;
00051 
00057     class Camera: public IRendererListener, public IRendererContainer {
00058     public:
00071         Camera(const std::string& id,
00072             Layer* layer,
00073             const Rect& viewport,
00074             const ExactModelCoordinate& emc,
00075             RenderBackend* renderbackend,
00076             ImagePool* ipool,
00077             AnimationPool* apool);
00078 
00081         virtual ~Camera();
00082 
00085         const std::string& getId() const { return m_id; }
00086 
00089         void setId(const std::string& id) { m_id = id; }
00090 
00095         void setTilt(double tilt);
00096 
00100         double getTilt() const;
00101 
00107         void setRotation(double rotation);
00108 
00112         double getRotation() const;
00113 
00117         void setZoom(double zoom);
00118 
00122         double getZoom() const;
00123 
00129         void setCellImageDimensions(unsigned int width, unsigned int height);
00130 
00135         Point getCellImageDimensions();
00136 
00140         Point getCellImageDimensions(Layer* layer);
00141 
00145         void setLocation(const Location& location);
00146 
00150         Location getLocation() const;
00151 
00157         Location& getLocationRef();
00158 
00163         void attach(Instance *instance);
00164 
00167         void detach();
00168 
00171         Instance* getAttached() const { return m_attachedto; }
00172 
00177         void setViewPort(const Rect& viewport);
00178 
00182         const Rect& getViewPort() const;
00183 
00189         ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
00190 
00194         ScreenPoint toScreenCoordinates(ExactModelCoordinate map_coords);
00195 
00198         void setEnabled(bool enabled);
00199 
00202         bool isEnabled();
00203 
00209         void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances);
00210 
00217         void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances);
00218 
00225         void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
00226 
00234         void update();
00235 
00241         void refresh();
00242 
00245         void resetUpdates();
00246 
00249         void addRenderer(RendererBase* renderer);
00250 
00253         RendererBase* getRenderer(const std::string& name);
00254 
00257         void resetRenderers();
00258 
00261         void calculateZValue(ScreenPoint& screen_coords);
00262 
00263         void onRendererPipelinePositionChanged(RendererBase* renderer);
00264         void onRendererEnabledChanged(RendererBase* renderer);
00265 
00268         void render();
00269 
00270     private:
00271         std::string m_id;
00272 
00279         void updateMatrices();
00280 
00287         void updateReferenceScale();
00288 
00291         DoublePoint getLogicalCellDimensions(Layer* layer);
00292 
00293         DoubleMatrix m_matrix;
00294         DoubleMatrix m_inverse_matrix;
00295         double m_tilt;
00296         double m_rotation;
00297         double m_zoom;
00298         Location m_location;
00299         ScreenPoint m_prev_origo;
00300         ScreenPoint m_cur_origo;
00301         Rect m_viewport;
00302         bool m_view_updated;
00303         unsigned int m_screen_cell_width;
00304         unsigned int m_screen_cell_height;
00305         double m_reference_scale;
00306         bool m_enabled;
00307         Instance* m_attachedto;
00308         // caches calculated image dimensions for already queried & calculated layers
00309         std::map<Layer*, Point> m_image_dimensions;
00310         bool m_iswarped;
00311 
00312         // list of renderers managed by the view
00313         std::map<std::string, RendererBase*> m_renderers;
00314         std::list<RendererBase*> m_pipeline;
00315         bool m_updated; // false, if view has never been updated before
00316 
00317         RenderBackend* m_renderbackend;
00318         ImagePool* m_ipool;
00319         AnimationPool* m_apool;
00320 
00321         // caches layer -> instances structure between renders e.g. to fast query of mouse picking order
00322         t_layer_to_instances m_layer_to_instances;
00323     };
00324 }
00325 #endif