instancerenderer.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_INSTANCERENDERER_H
00023 #define FIFE_INSTANCERENDERER_H
00024 
00025 // Standard C++ library includes
00026 #include <string>
00027 #include <list>
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 "view/rendererbase.h"
00036 #include "util/time/timer.h"
00037 
00038 namespace FIFE {
00039     class Location;
00040     class RenderBackend;
00041     class InstanceDeleteListener;
00042 
00043     class InstanceRenderer: public RendererBase {
00044     public:
00050         InstanceRenderer(RenderBackend* renderbackend, int32_t position);
00051 
00052         InstanceRenderer(const InstanceRenderer& old);
00053 
00054         RendererBase* clone();
00055 
00058         virtual ~InstanceRenderer();
00059         void render(Camera* cam, Layer* layer, RenderList& instances);
00060         std::string getName() { return "InstanceRenderer"; }
00061 
00064         void addOutlined(Instance* instance, int32_t r, int32_t g, int32_t b, int32_t width, int32_t threshold = 1);
00065 
00068         void addColored(Instance* instance, int32_t r, int32_t g, int32_t b);
00069 
00072         void addTransparentArea(Instance* instance, const std::list<std::string> &groups, uint32_t w, uint32_t h, uint8_t trans, bool front = true);
00073 
00076         void removeOutlined(Instance* instance);
00077 
00080         void removeColored(Instance* instance);
00081 
00084         void removeTransparentArea(Instance* instance);
00085 
00088         void removeAllOutlines();
00089 
00092         void removeAllColored();
00093 
00096         void removeAllTransparentAreas();
00097 
00101         void addIgnoreLight(const std::list<std::string> &groups);
00102 
00105         void removeIgnoreLight(const std::list<std::string> &groups);
00106 
00109         void removeAllIgnoreLight();
00110 
00113         static InstanceRenderer* getInstance(IRendererContainer* cnt);
00114 
00117         RenderBackend* getRenderBackend() const {return m_renderbackend;}
00118 
00121         void reset();
00122 
00125         void setRemoveInterval(uint32_t interval);
00126         
00129         uint32_t getRemoveInterval() const;
00130         
00134         void addToCheck(const ImagePtr& image);
00135 
00138         void check();
00139 
00143         void removeInstance(Instance* instance);
00144 
00147         bool needColorBinding() { return m_need_bind_coloring; }
00148 
00149     private:
00150         bool m_area_layer;
00151         uint32_t m_interval;
00152         bool m_timer_enabled;
00153         std::list<std::string> m_unlit_groups;
00154         bool m_need_sorting;
00155         bool m_need_bind_coloring;
00156 
00157         enum InstanceRendererEffect {
00158             NOTHING = 0x00,
00159             OUTLINE = 0x01,
00160             COLOR = 0x02,
00161             AREA = 0x04
00162         };
00163         typedef uint8_t Effect;
00164 
00165         // contains per-instance information for outline drawing
00166         class OutlineInfo {
00167         public:
00168             uint8_t r;
00169             uint8_t g;
00170             uint8_t b;
00171             int32_t width;
00172             int32_t threshold;
00173             bool dirty;
00174             ImagePtr outline;
00175             Image* curimg;
00176             InstanceRenderer* renderer;
00177             OutlineInfo(InstanceRenderer* r);
00178             ~OutlineInfo();
00179         };
00180         // contains per-instance information for overlay drawing
00181         class ColoringInfo {
00182         public:
00183             uint8_t r;
00184             uint8_t g;
00185             uint8_t b;
00186             bool dirty;
00187             ImagePtr overlay;
00188             Image* curimg;
00189             InstanceRenderer* renderer;
00190             ColoringInfo(InstanceRenderer* r);
00191             ~ColoringInfo();
00192         };
00193         class AreaInfo {
00194         public:
00195             Instance* instance;
00196             std::list<std::string> groups;
00197             uint32_t w;
00198             uint32_t h;
00199             uint8_t trans;
00200             bool front;
00201             double z;
00202             AreaInfo();
00203             ~AreaInfo();
00204         };
00205         typedef std::map<Instance*, OutlineInfo> InstanceToOutlines_t;
00206         typedef std::map<Instance*, ColoringInfo> InstanceToColoring_t;
00207         typedef std::map<Instance*, AreaInfo> InstanceToAreas_t;
00208 
00209         InstanceToOutlines_t m_instance_outlines;
00210         InstanceToColoring_t m_instance_colorings;
00211         InstanceToAreas_t m_instance_areas;
00212 
00213         // struct to hold the ImagePtr with a timestamp
00214         typedef struct {
00215             ImagePtr image;
00216             uint32_t timestamp;
00217         } s_image_entry;
00218         typedef std::list<s_image_entry> ImagesToCheck_t;
00219         // old effect images
00220         ImagesToCheck_t m_check_images;
00221         // timer
00222         Timer m_timer;
00223         
00224         // InstanceDeleteListener to automatically remove Instance effect (outline, coloring, ...)
00225         InstanceDeleteListener* m_delete_listener;
00226         typedef std::map<Instance*, Effect> InstanceToEffects_t;
00227         InstanceToEffects_t m_assigned_instances;
00228 
00231         Image* bindOutline(OutlineInfo& info, RenderItem& vc, Camera* cam);
00232         Image* bindColoring(ColoringInfo& info, RenderItem& vc, Camera* cam);
00233 
00234         void renderUnsorted(Camera* cam, Layer* layer, RenderList& instances);
00235         void renderAlreadySorted(Camera* cam, Layer* layer, RenderList& instances);
00236 
00237         void removeFromCheck(const ImagePtr& image);
00238         bool isValidImage(const ImagePtr& image);
00239     };
00240 }
00241 
00242 #endif