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 #include <SDL.h>
00031 
00032 // FIFE includes
00033 // These includes are split up in two parts, separated by one empty line
00034 // First block: files included from the FIFE root src directory
00035 // Second block: files included from the same folder
00036 #include "model/structures/location.h"
00037 #include "util/structures/rect.h"
00038 #include "util/math/matrix.h"
00039 #include "video/animation.h"
00040 
00041 #include "rendererbase.h"
00042 
00043 namespace FIFE {
00044 
00045     typedef Point3D ScreenPoint;
00046     class Layer;
00047     class Instance;
00048     class RenderBackend;
00049     class LayerCache;
00050     class MapObserver;
00051     typedef std::map<Layer*, RenderList > t_layer_to_instances;
00052 
00058     class Camera: public IRendererListener, public IRendererContainer {
00059     public:
00060         enum Transform {
00061             NormalTransform = 0,
00062             WarpedTransform = 1
00063         };
00064 
00077         Camera(const std::string& id,
00078             Layer* layer,
00079             const Rect& viewport,
00080             RenderBackend* renderbackend);
00081 
00084         virtual ~Camera();
00085 
00088         const std::string& getId() const { return m_id; }
00089 
00092         void setId(const std::string& id) { m_id = id; }
00093 
00098         void setTilt(double tilt);
00099 
00103         double getTilt() const;
00104 
00110         void setRotation(double rotation);
00111 
00115         double getRotation() const;
00116 
00120         void setZoom(double zoom);
00121 
00125         double getZoom() const;
00126 
00132         void setCellImageDimensions(uint32_t width, uint32_t height);
00133 
00138         Point getCellImageDimensions();
00139 
00143         Point getCellImageDimensions(Layer* layer);
00144 
00147         double getReferenceScale() const { return m_reference_scale; }
00148 
00152         Point3D getZOffset(Layer* layer);
00153 
00157         void setLocation(const Location& location);
00158 
00162         Location getLocation() const;
00163 
00167         Point3D getOrigin() const;
00168 
00174         Location& getLocationRef();
00175 
00180         void attach(Instance *instance);
00181 
00184         void detach();
00185 
00188         Instance* getAttached() const { return m_attachedto; }
00189 
00194         void setViewPort(const Rect& viewport);
00195 
00199         const Rect& getViewPort() const;
00200 
00206         ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true);
00207 
00211         ScreenPoint toScreenCoordinates(const ExactModelCoordinate& map_coords);
00212 
00216         DoublePoint3D toVirtualScreenCoordinates(const ExactModelCoordinate& map_coords);
00217 
00218         ScreenPoint virtualScreenToScreen(const DoublePoint3D& p);
00219         DoublePoint3D screenToVirtualScreen(const ScreenPoint& p);
00220 
00223         void setEnabled(bool enabled);
00224 
00227         bool isEnabled();
00228 
00234         void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
00235 
00242         void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances, uint8_t alpha = 0);
00243 
00250         void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
00251 
00259         void update();
00260 
00266         void refresh();
00267 
00270         void resetUpdates();
00271 
00274         void addRenderer(RendererBase* renderer);
00275 
00278         RendererBase* getRenderer(const std::string& name);
00279 
00282         void resetRenderers();
00283 
00286         void calculateZValue(ScreenPoint& screen_coords);
00287 
00288         void onRendererPipelinePositionChanged(RendererBase* renderer);
00289 
00290         void onRendererEnabledChanged(RendererBase* renderer);
00291 
00294         void setLightingColor(float red, float green, float blue);
00295 
00298         void resetLightingColor();
00299 
00302         std::vector<float> getLightingColor();
00303 
00306         void setOverlayColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
00307 
00310         std::vector<uint8_t> getOverlayColor();
00311 
00314         void resetOverlayColor();
00315 
00319         void setOverlayImage(int32_t id, bool fill = false);
00320 
00323         int32_t getOverlayImage();
00324 
00327         void resetOverlayImage();
00328 
00332         void setOverlayAnimation(AnimationPtr anim, bool fill = false);
00333 
00336         AnimationPtr getOverlayAnimation();
00337 
00340         void resetOverlayAnimation();
00341 
00344         void render();
00345 
00346     private:
00347         friend class MapObserver;
00348         void addLayer(Layer* layer);
00349         void removeLayer(Layer* layer);
00350         void updateMap(Map* map);
00351         std::string m_id;
00352 
00353 
00360         void updateMatrices();
00361 
00368         void updateReferenceScale();
00369 
00372         void updateRenderLists();
00373 
00376         void cacheUpdate(Layer* layer);
00377 
00380         DoublePoint getLogicalCellDimensions(Layer* layer);
00381 
00384         Point getRealCellDimensions(Layer* layer);
00385 
00388         void renderOverlay();
00389 
00390         DoubleMatrix m_matrix;
00391         DoubleMatrix m_inverse_matrix;
00392 
00393         DoubleMatrix m_vs_matrix;
00394         DoubleMatrix m_vs_inverse_matrix;
00395         DoubleMatrix m_vscreen_2_screen;
00396         DoubleMatrix m_screen_2_vscreen;
00397 
00398         double m_tilt;
00399         double m_rotation;
00400         double m_zoom;
00401         Location m_location;
00402         ScreenPoint m_cur_origo;
00403         Rect m_viewport;
00404         bool m_view_updated;
00405         uint32_t m_screen_cell_width;
00406         uint32_t m_screen_cell_height;
00407         double m_reference_scale;
00408         bool m_enabled;
00409         Instance* m_attachedto;
00410         // caches calculated image dimensions for already queried & calculated layers
00411         std::map<Layer*, Point> m_image_dimensions;
00412         bool m_iswarped; // true, if the geometry had changed
00413 
00414         // list of renderers managed by the view
00415         std::map<std::string, RendererBase*> m_renderers;
00416         std::list<RendererBase*> m_pipeline;
00417         bool m_updated; // false, if view has not been updated
00418 
00419         RenderBackend* m_renderbackend;
00420 
00421         // caches layer -> instances structure between renders e.g. to fast query of mouse picking order
00422         t_layer_to_instances m_layer_to_instances;
00423 
00424         std::map<Layer*,LayerCache*> m_cache;
00425         MapObserver* m_map_observer;
00426         Map* m_map;
00427 
00428         // is lighting enable
00429         bool m_lighting;
00430         // caches the light color for the camera
00431         std::vector<float> m_light_colors;
00432 
00433         // overlay stuff
00434         bool m_col_overlay;
00435         bool m_img_overlay;
00436         bool m_ani_overlay;
00437         SDL_Color m_overlay_color;
00438         int32_t m_img_id;
00439         AnimationPtr m_ani_ptr;
00440         bool m_img_fill;
00441         bool m_ani_fill;
00442         uint32_t m_start_time;
00443     };
00444 }
00445 #endif