Mercurial > fife-parpg
comparison engine/core/view/renderers/camerazonerenderer.cpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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 modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * | |
20 ***************************************************************************/ | |
21 | |
22 // Standard C++ library includes | |
23 | |
24 // 3rd party library includes | |
25 | |
26 // FIFE includes | |
27 // These includes are split up in two parts, separated by one empty line | |
28 // First block: files included from the FIFE root src directory | |
29 // Second block: files included from the same folder | |
30 #include "video/renderbackend.h" | |
31 #include "video/image.h" | |
32 #include "util/math/fife_math.h" | |
33 #include "util/log/logger.h" | |
34 #include "model/metamodel/grids/cellgrid.h" | |
35 #include "model/structures/instance.h" | |
36 #include "model/structures/layer.h" | |
37 #include "model/structures/location.h" | |
38 | |
39 #include "view/camera.h" | |
40 #include "camerazonerenderer.h" | |
41 | |
42 | |
43 | |
44 namespace FIFE { | |
45 static Logger _log(LM_VIEWVIEW); | |
46 | |
47 CameraZoneRenderer::CameraZoneRenderer(RenderBackend* renderbackend, int position, ImagePool* imagepool): | |
48 RendererBase(renderbackend, position), | |
49 m_imagepool(imagepool), | |
50 m_zone_image(NULL) { | |
51 setEnabled(false); | |
52 } | |
53 | |
54 CameraZoneRenderer::CameraZoneRenderer(const CameraZoneRenderer& old): | |
55 RendererBase(old), | |
56 m_imagepool(old.m_imagepool), | |
57 m_zone_image(NULL) { | |
58 setEnabled(false); | |
59 } | |
60 | |
61 RendererBase* CameraZoneRenderer::clone() { | |
62 return new CameraZoneRenderer(*this); | |
63 } | |
64 | |
65 CameraZoneRenderer::~CameraZoneRenderer() { | |
66 delete m_zone_image; | |
67 } | |
68 | |
69 | |
70 | |
71 void CameraZoneRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) { | |
72 CellGrid* cg = layer->getCellGrid(); | |
73 if (!cg) { | |
74 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw camera zones"); | |
75 return; | |
76 } | |
77 // draw this layer's grid as the camera sees it | |
78 Rect rect = cam->getViewPort(); | |
79 if (!m_zone_image) { | |
80 // build zone image | |
81 int dataSize = rect.w * rect.h; | |
82 | |
83 // initally all pixels are transparent | |
84 uint32_t* data = new uint32_t[dataSize]; | |
85 uint32_t* end = data + dataSize; | |
86 | |
87 #if SDL_BYTEORDER == SDL_BIG_ENDIAN | |
88 uint32_t empty_pixel = 0;//0xff << 24; | |
89 uint32_t edge_pixel = 0xff << 16 | 0xff << 24; | |
90 #else | |
91 uint32_t empty_pixel = 0;//0xff; | |
92 uint32_t edge_pixel = 0xff << 8 | 0xff; | |
93 #endif | |
94 | |
95 std::fill(data, end, empty_pixel); | |
96 | |
97 // go from screen coords to grid coords (layer coords) | |
98 // Search through pixel space, drawing pixels at boundaries | |
99 ModelCoordinate prevLayerCoord; | |
100 for (int y = 0; y < rect.h; y++) { | |
101 for (int x = 0; x < rect.w; x++) { | |
102 ExactModelCoordinate mapCoord = cam->toMapCoordinates(ScreenPoint(x, y)); | |
103 ModelCoordinate layerCoord = cg->toLayerCoordinates(mapCoord); | |
104 | |
105 if (prevLayerCoord != layerCoord) { | |
106 data[x + rect.w * y] = edge_pixel; | |
107 } | |
108 prevLayerCoord = layerCoord; | |
109 } | |
110 } | |
111 | |
112 | |
113 for (int x = 0; x < rect.w; x++) { | |
114 for (int y = 0; y < rect.h; y++) { | |
115 ExactModelCoordinate mapCoord = cam->toMapCoordinates(ScreenPoint(x, y)); | |
116 ModelCoordinate layerCoord = cg->toLayerCoordinates(mapCoord); | |
117 | |
118 if (prevLayerCoord != layerCoord) { | |
119 data[x + rect.w * y] = edge_pixel; | |
120 } | |
121 prevLayerCoord = layerCoord; | |
122 } | |
123 } | |
124 | |
125 m_zone_image = m_renderbackend->createImage((uint8_t*) data, rect.w, rect.h); | |
126 delete data; | |
127 } | |
128 m_zone_image->render(rect); | |
129 } | |
130 | |
131 void CameraZoneRenderer::setEnabled(bool enabled) { | |
132 if (!enabled) { | |
133 delete m_zone_image; | |
134 m_zone_image = NULL; | |
135 } | |
136 } | |
137 } |