summaryrefslogtreecommitdiffstats
path: root/Renderer.cpp
blob: 98859d9b13277b5c615dd103c92dba2cf8081c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "Renderer.h"
#include "PlayerStart.h"
#include <GL/gl.h>


void Renderer::drawGrid(const Rectangle &rect, float scale) {
  float depth = log10f(scale)-0.75f;
  float depth2 = floorf(depth);
  float step = powf(0.1f, depth2);
  float f;
  int i;
  float x1 = rect.getVertex1().getX(), y1 = rect.getVertex1().getY();
  float x2 = rect.getVertex2().getX(), y2 = rect.getVertex2().getY();
  
  
  glBegin(GL_LINES);
  
  for(i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; i++) {
    f = fminf(0.4f*(depth-depth2+i), 0.5f);
    glColor3f(f, f, f);
    
    for(f = x1 - fmodf(x1, step) - step; f <= x2; f+=step) {
      glVertex2f(f, y1);
      glVertex2f(f, y2);
    }
    
    for(f = y1 - fmodf(y1, step) - step; f <= y2; f+=step) {
      glVertex2f(x1, f);
      glVertex2f(x2, f);
    }
    
    step *= 10;
  }
  
  glEnd();
}

void Renderer::fillPolygon(const Polygon &polygon) {
  std::vector<Triangle> triangles;
  
  polygon.triangulate(triangles);
  
  glBegin(GL_TRIANGLES);
  
  for(std::vector<Triangle>::iterator t = triangles.begin(); t != triangles.end(); t++) {
    glVertex2f(t->getVertexA().getX(), t->getVertexA().getY());
    glVertex2f(t->getVertexB().getX(), t->getVertexB().getY());
    glVertex2f(t->getVertexC().getX(), t->getVertexC().getY());
  }
  
  glEnd();
}

void Renderer::drawPolygon(const Polygon &polygon, bool close) {
  glBegin(close ? GL_LINE_LOOP : GL_LINE_STRIP);
  
  for(Polygon::const_iterator vertex = polygon.begin(); vertex != polygon.end(); vertex++)
    glVertex2f(vertex->getX(), vertex->getY());
  
  glEnd();
}

void Renderer::drawCircle(const Vertex &m, float r, int n) {
  glBegin(GL_LINE_LOOP);
  
  for(int i = 0; i < n; i++)
    glVertex2f(m.getX()+r*cosf(2*M_PI*i/n), m.getY()+r*sinf(2*M_PI*i/n));
  
  glEnd();
}

void Renderer::drawCross(const Vertex &m, float r) {
  glBegin(GL_LINES);
  
  glVertex2f(m.getX()-r*M_SQRT1_2, m.getY()-r*M_SQRT1_2);
  glVertex2f(m.getX()+r*M_SQRT1_2, m.getY()+r*M_SQRT1_2);
  
  glVertex2f(m.getX()+r*M_SQRT1_2, m.getY()-r*M_SQRT1_2);
  glVertex2f(m.getX()-r*M_SQRT1_2, m.getY()+r*M_SQRT1_2);
  
  glEnd();
}

void Renderer::renderObject(const LevelObject &object, bool selected, bool highlighted, float scale) {
  if(object.isOfType("Room"))
    renderRoom(*(Room*)&object, selected, highlighted, scale);
  else if(object.isOfType("PlayerStart"))
    renderPlayerStart(*(PlayerStart*)&object, selected, highlighted, scale);
}

void Renderer::renderRoom(const Room &room, bool selected, bool highlighted, float scale) {
  if(selected)
    glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
  else
    glColor4f(0.0f, 0.7f, 1.0f, 0.3f);
  
  fillPolygon(room);
  
  if(selected) {
    glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
    glLineWidth(2.0f);
  }
  else if(highlighted) {
    glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
    glLineWidth(2.0f);
  }
  else {
    glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
    glLineWidth(1.0f);
  }
  
  drawPolygon(room);
}

void Renderer::renderPlayerStart(const PlayerStart &start, bool selected, bool highlighted, float scale) {
  if(selected) {
    glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
    glLineWidth(2.0f);
  }
  else if(highlighted) {
    glColor4f(0.0f, 0.7f, 0.7f, 0.7f);
    glLineWidth(2.0f);
  }
  else {
    glColor4f(0.0f, 0.7f, 0.7f, 0.7f);
    glLineWidth(1.0f);
  }
  drawCircle(Vertex(start.getX(), start.getZ()), 0.3f, 128);
  
  glLineWidth(2.0f);
  glColor4f(1.0f, 1.0f, 1.0f, 0.7f);
  drawCross(Vertex(start.getX(), start.getZ()), 0.5f/sqrtf(scale));
}

void Renderer::render(const Level &level, const Rectangle &rect, float scale) {
  glClear(GL_COLOR_BUFFER_BIT);
  
  glLineWidth(1.0f);
  glPointSize(10.0f);
  
  drawGrid(rect, scale);
  
  for(Level::const_iterator object = level.begin(); object != level.end(); object++) {
    renderObject(**object, (&**object == editManager->getSelectedObject()),
        (&**object == editManager->getHighlightedObject()), scale);
  }
}