gridrenderer.cpp
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, int 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 m_renderbackend->disableLighting();
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 int cvx2 = cv.x+cv.w;
00148 int cvy2 = cv.y+cv.h;
00149 RenderList::const_iterator instance_it = instances.begin();
00150 for (;instance_it != instances.end(); ++instance_it) {
00151 Instance* instance = (*instance_it)->instance;
00152 std::vector<ExactModelCoordinate> vertices;
00153 cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
00154 std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00155 ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00156 Point pt1(firstpt.x, firstpt.y);
00157 Point pt2;
00158 ++it;
00159 for (; it != vertices.end(); it++) {
00160 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00161 pt2.x = pts.x;
00162 pt2.y = pts.y;
00163 Point cpt1 = pt1;
00164 Point cpt2 = pt2;
00165
00166 if (cpt1.x < cv.x) cpt1.x = cv.x;
00167 if (cpt2.x < cv.x) cpt2.x = cv.x;
00168 if (cpt1.y < cv.y) cpt1.y = cv.y;
00169 if (cpt2.y < cv.y) cpt2.y = cv.y;
00170 if (cpt1.x > cvx2) cpt1.x = cvx2;
00171 if (cpt2.x > cvx2) cpt2.x = cvx2;
00172 if (cpt1.y > cvy2) cpt1.y = cvy2;
00173 if (cpt2.y > cvy2) cpt2.y = cvy2;
00174
00175 m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
00176 pt1 = pt2;
00177 }
00178 if ((pt2.x >= cv.x) && (pt2.x <= cvx2) && (pt2.y >= cv.y) && (pt2.y <= cvy2)) {
00179 if ((firstpt.x >= cv.x) && (firstpt.x <= cvx2) && (firstpt.y >= cv.y) && (firstpt.y <= cvy2)) {
00180 m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
00181 }
00182 }
00183 }
00184 m_renderbackend->enableLighting();
00185 }
00186
00187 void GridRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
00188 m_color.r = r;
00189 m_color.g = g;
00190 m_color.b = b;
00191 }
00192 }