comparison engine/core/view/renderers/instancerenderer.cpp @ 95:0b6998fde366

speedup instance renderer; now instances can change their layer, old optimisation made wrong asumption... (fix by yonibear)
author spq@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 20 Jul 2008 19:40:21 +0000
parents 90005975cdbb
children 52d4a149389d
comparison
equal deleted inserted replaced
94:848d8d311407 95:0b6998fde366
43 43
44 #include "view/camera.h" 44 #include "view/camera.h"
45 #include "view/visual.h" 45 #include "view/visual.h"
46 #include "instancerenderer.h" 46 #include "instancerenderer.h"
47 47
48 namespace {
49 unsigned int scale(unsigned int val, double factor) {
50 return static_cast<unsigned int>(ceil(static_cast<double>(val) * factor));
51 }
52 }
48 53
49 namespace FIFE { 54 namespace FIFE {
50 static Logger _log(LM_VIEWVIEW); 55 static Logger _log(LM_VIEWVIEW);
51 56
52 InstanceRenderer::OutlineInfo::OutlineInfo(): 57 InstanceRenderer::OutlineInfo::OutlineInfo():
78 } 83 }
79 84
80 InstanceRenderer::InstanceRenderer(RenderBackend* renderbackend, int position, ImagePool* imagepool, AnimationPool* animpool): 85 InstanceRenderer::InstanceRenderer(RenderBackend* renderbackend, int position, ImagePool* imagepool, AnimationPool* animpool):
81 RendererBase(renderbackend, position), 86 RendererBase(renderbackend, position),
82 m_imagepool(imagepool), 87 m_imagepool(imagepool),
83 m_animationpool(animpool), 88 m_animationpool(animpool) {
84 m_layer_to_outlinemap(),
85 m_layer_to_coloringmap() {
86 setEnabled(true); 89 setEnabled(true);
87 } 90 }
88 91
89 InstanceRenderer::InstanceRenderer(const InstanceRenderer& old): 92 InstanceRenderer::InstanceRenderer(const InstanceRenderer& old):
90 RendererBase(old), 93 RendererBase(old),
91 m_imagepool(old.m_imagepool), 94 m_imagepool(old.m_imagepool),
92 m_animationpool(old.m_animationpool), 95 m_animationpool(old.m_animationpool) {
93 m_layer_to_outlinemap(),
94 m_layer_to_coloringmap() {
95 setEnabled(true); 96 setEnabled(true);
96 } 97 }
97 98
98 RendererBase* InstanceRenderer::clone() { 99 RendererBase* InstanceRenderer::clone() {
99 return new InstanceRenderer(*this); 100 return new InstanceRenderer(*this);
100 } 101 }
101 102
102 InstanceRenderer::~InstanceRenderer() { 103 InstanceRenderer::~InstanceRenderer() {
103 }
104
105 unsigned int scale(unsigned int val, double factor) {
106 return static_cast<unsigned int>(ceil(static_cast<double>(val) * factor));
107 } 104 }
108 105
109 void InstanceRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) { 106 void InstanceRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
110 FL_DBG(_log, "Iterating layer..."); 107 FL_DBG(_log, "Iterating layer...");
111 CellGrid* cg = layer->getCellGrid(); 108 CellGrid* cg = layer->getCellGrid();
112 if (!cg) { 109 if (!cg) {
113 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw instances"); 110 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw instances");
114 return; 111 return;
115 } 112 }
116 113
117 bool potential_outlining = false; 114 const bool any_effects = !(m_instance_outlines.empty() && m_instance_colorings.empty());
118 InstanceToOutlines_t outline_i2o;
119 InstanceToOutlines_t::iterator outline_end;
120
121 LayerToOutlineMap_t::iterator outline_l2i = m_layer_to_outlinemap.find(layer);
122 if (outline_l2i != m_layer_to_outlinemap.end()) {
123 potential_outlining = true;
124 outline_i2o = outline_l2i->second;
125 outline_end = outline_i2o.end();
126 }
127
128 bool potential_coloring = false;
129 InstanceToColoring_t coloring_i2o;
130 InstanceToColoring_t::iterator coloring_end;
131
132 LayerToColoringMap_t::iterator coloring_l2i = m_layer_to_coloringmap.find(layer);
133 if (coloring_l2i != m_layer_to_coloringmap.end()) {
134 potential_coloring = true;
135 coloring_i2o = coloring_l2i->second;
136 coloring_end = coloring_i2o.end();
137 }
138 115
139 std::vector<Instance*>::const_iterator instance_it = instances.begin(); 116 std::vector<Instance*>::const_iterator instance_it = instances.begin();
140 for (;instance_it != instances.end(); ++instance_it) { 117 for (;instance_it != instances.end(); ++instance_it) {
141 FL_DBG(_log, "Iterating instances..."); 118 FL_DBG(_log, "Iterating instances...");
142 Instance* instance = (*instance_it); 119 Instance* instance = (*instance_it);
143 InstanceVisual* visual = instance->getVisual<InstanceVisual>(); 120 InstanceVisual* visual = instance->getVisual<InstanceVisual>();
144 InstanceVisualCacheItem& vc = visual->getCacheItem(cam); 121 InstanceVisualCacheItem& vc = visual->getCacheItem(cam);
145 FL_DBG(_log, LMsg("Instance layer coordinates = ") << instance->getLocationRef().getLayerCoordinates()); 122 FL_DBG(_log, LMsg("Instance layer coordinates = ") << instance->getLocationRef().getLayerCoordinates());
146 123
147 if (potential_outlining) { 124 if (any_effects) {
148 InstanceToOutlines_t::iterator outline_it = outline_i2o.find(instance); 125 InstanceToOutlines_t::iterator outline_it = m_instance_outlines.find(instance);
149 if (outline_it != outline_end) { 126 if (outline_it != m_instance_outlines.end()) {
150 bindOutline(outline_it->second, vc, cam)->render(vc.dimensions); 127 bindOutline(outline_it->second, vc, cam)->render(vc.dimensions);
151 } 128 }
152 } 129
153 if (potential_coloring) { 130 InstanceToColoring_t::iterator coloring_it = m_instance_colorings.find(instance);
154 InstanceToColoring_t::iterator coloring_it = coloring_i2o.find(instance); 131 if (coloring_it != m_instance_colorings.end()) {
155 if (coloring_it != coloring_end) {
156 bindColoring(coloring_it->second, vc, cam)->render(vc.dimensions); 132 bindColoring(coloring_it->second, vc, cam)->render(vc.dimensions);
157 continue; 133 continue; // Skip normal rendering after drawing overlay
158 } 134 }
159 } 135 }
136
160 vc.image->render(vc.dimensions); 137 vc.image->render(vc.dimensions);
161 } 138 }
162 } 139 }
163 140
164 Image* InstanceRenderer::bindOutline(OutlineInfo& info, InstanceVisualCacheItem& vc, Camera* cam) { 141 Image* InstanceRenderer::bindOutline(OutlineInfo& info, InstanceVisualCacheItem& vc, Camera* cam) {
165 if (info.curimg == vc.image) { 142 if (info.curimg == vc.image) {
166 return info.outline; 143 return info.outline;
167 } 144 } else {
145 info.curimg = vc.image;
146 }
147
168 if (info.outline) { 148 if (info.outline) {
169 delete info.outline; // delete old mask 149 delete info.outline; // delete old mask
170 info.outline = NULL; 150 info.outline = NULL;
171 } 151 }
172 SDL_Surface* surface = vc.image->getSurface(); 152 SDL_Surface* surface = vc.image->getSurface();
224 } 204 }
225 205
226 Image* InstanceRenderer::bindColoring(ColoringInfo& info, InstanceVisualCacheItem& vc, Camera* cam) { 206 Image* InstanceRenderer::bindColoring(ColoringInfo& info, InstanceVisualCacheItem& vc, Camera* cam) {
227 if (info.curimg == vc.image) { 207 if (info.curimg == vc.image) {
228 return info.overlay; 208 return info.overlay;
209 } else {
210 info.curimg = vc.image;
229 } 211 }
230 if (info.overlay) { 212 if (info.overlay) {
231 delete info.overlay; // delete old mask 213 delete info.overlay; // delete old mask
232 info.overlay = NULL; 214 info.overlay = NULL;
233 } 215 }
258 OutlineInfo info; 240 OutlineInfo info;
259 info.r = r; 241 info.r = r;
260 info.g = g; 242 info.g = g;
261 info.b = b; 243 info.b = b;
262 info.width = width; 244 info.width = width;
263 InstanceToOutlines_t& i2h = m_layer_to_outlinemap[instance->getLocation().getLayer()]; 245
264 i2h[instance] = info; 246 m_instance_outlines[instance] = info;
265 } 247 }
266 248
267 void InstanceRenderer::addColored(Instance* instance, int r, int g, int b) { 249 void InstanceRenderer::addColored(Instance* instance, int r, int g, int b) {
268 ColoringInfo info; 250 ColoringInfo info;
269 info.r = r; 251 info.r = r;
270 info.g = g; 252 info.g = g;
271 info.b = b; 253 info.b = b;
272 InstanceToColoring_t& i2h = m_layer_to_coloringmap[instance->getLocation().getLayer()]; 254
273 i2h[instance] = info; 255 m_instance_colorings[instance] = info;
274 } 256 }
275 257
276 void InstanceRenderer::removeOutlined(Instance* instance) { 258 void InstanceRenderer::removeOutlined(Instance* instance) {
277 InstanceToOutlines_t& i2h = m_layer_to_outlinemap[instance->getLocation().getLayer()]; 259 m_instance_outlines.erase(instance);
278 i2h.erase(instance);
279 } 260 }
280 261
281 void InstanceRenderer::removeColored(Instance* instance) { 262 void InstanceRenderer::removeColored(Instance* instance) {
282 InstanceToColoring_t& i2h = m_layer_to_coloringmap[instance->getLocation().getLayer()]; 263 m_instance_colorings.erase(instance);
283 i2h.erase(instance);
284 } 264 }
285 265
286 void InstanceRenderer::removeAllOutlines() { 266 void InstanceRenderer::removeAllOutlines() {
287 m_layer_to_outlinemap.clear(); 267 m_instance_outlines.clear();
288 } 268 }
289 269
290 void InstanceRenderer::removeAllColored() { 270 void InstanceRenderer::removeAllColored() {
291 m_layer_to_coloringmap.clear(); 271 m_instance_colorings.clear();
292 } 272 }
293 273
294 void InstanceRenderer::reset() { 274 void InstanceRenderer::reset() {
295 removeAllOutlines(); 275 removeAllOutlines();
296 removeAllColored(); 276 removeAllColored();