00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "video/renderbackend.h"
00031 #include "util/math/fife_math.h"
00032 #include "util/log/logger.h"
00033 #include "model/metamodel/grids/cellgrid.h"
00034 #include "model/structures/instance.h"
00035 #include "model/structures/layer.h"
00036 #include "model/structures/location.h"
00037
00038 #include "view/camera.h"
00039 #include "gridrenderer.h"
00040
00041
00042 namespace FIFE {
00043 static Logger _log(LM_VIEWVIEW);
00044
00045 GridRenderer::GridRenderer(RenderBackend* renderbackend, int32_t position):
00046 RendererBase(renderbackend, position) {
00047 setEnabled(false);
00048 m_color.r = 0;
00049 m_color.g = 255;
00050 m_color.b = 0;
00051 }
00052
00053 GridRenderer::GridRenderer(const GridRenderer& old):
00054 RendererBase(old),
00055 m_color(old.m_color) {
00056 setEnabled(false);
00057 }
00058
00059 RendererBase* GridRenderer::clone() {
00060 return new GridRenderer(*this);
00061 }
00062
00063 GridRenderer::~GridRenderer() {
00064 }
00065
00066 GridRenderer* GridRenderer::getInstance(IRendererContainer* cnt) {
00067 return dynamic_cast<GridRenderer*>(cnt->getRenderer("GridRenderer"));
00068 }
00069
00070 void GridRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00071 CellGrid* cg = layer->getCellGrid();
00072 if (!cg) {
00073 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
00074 return;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 Rect cv = cam->getViewPort();
00147 int32_t cvx2 = round((cv.x+cv.w) * 1.25);
00148 int32_t cvy2 = round((cv.y+cv.h) * 1.25);
00149 cv.x -= round((cv.x+cv.w) * 0.125);
00150 cv.y -= round((cv.y+cv.h) * 0.125);
00151 RenderList::const_iterator instance_it = instances.begin();
00152 for (;instance_it != instances.end(); ++instance_it) {
00153 Instance* instance = (*instance_it)->instance;
00154 std::vector<ExactModelCoordinate> vertices;
00155 cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
00156 std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00157 ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00158 Point pt1(firstpt.x, firstpt.y);
00159 Point pt2;
00160 ++it;
00161 for (; it != vertices.end(); it++) {
00162 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00163 pt2.x = pts.x;
00164 pt2.y = pts.y;
00165 Point cpt1 = pt1;
00166 Point cpt2 = pt2;
00167
00168 if (cpt1.x < cv.x) cpt1.x = cv.x;
00169 if (cpt2.x < cv.x) cpt2.x = cv.x;
00170 if (cpt1.y < cv.y) cpt1.y = cv.y;
00171 if (cpt2.y < cv.y) cpt2.y = cv.y;
00172 if (cpt1.x > cvx2) cpt1.x = cvx2;
00173 if (cpt2.x > cvx2) cpt2.x = cvx2;
00174 if (cpt1.y > cvy2) cpt1.y = cvy2;
00175 if (cpt2.y > cvy2) cpt2.y = cvy2;
00176
00177 m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
00178 pt1 = pt2;
00179 }
00180 if ((pt2.x >= cv.x) && (pt2.x <= cvx2) && (pt2.y >= cv.y) && (pt2.y <= cvy2)) {
00181 if ((firstpt.x >= cv.x) && (firstpt.x <= cvx2) && (firstpt.y >= cv.y) && (firstpt.y <= cvy2)) {
00182 m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
00183 }
00184 }
00185 }
00186 }
00187
00188 void GridRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
00189 m_color.r = r;
00190 m_color.g = g;
00191 m_color.b = b;
00192 }
00193 }