-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordsModule.cpp
101 lines (87 loc) · 2.51 KB
/
CoordsModule.cpp
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
#include "CoordsModule.hpp"
#include <Graphics/Graphics.hpp>
#include <Vector/VectorGL.hpp>
namespace {
Module *create_coordsmodule(const std::string ¶ms) {
return new CoordsModule();
}
class Fred {
public:
Fred() {
register_module("coords", create_coordsmodule);
}
} fred;
}
Vector2f CoordsModule::size() {
return make_vector(2.0f, 1.0f);
}
const float Time = 0.2f;
void CoordsModule::draw(Box2f viewport, Box2f screen_viewport, float scale, unsigned int recurse) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBoxToBox(viewport, screen_viewport);
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-1.0f, -0.5f);
glVertex2f( 1.0f, -0.5f);
glVertex2f( 1.0f, 0.5f);
glVertex2f(-1.0f, 0.5f);
glEnd();
unsigned int frame = 0;
if (!coords().empty()) {
float t = fmodf(time(), Time * coords().size());
frame = (unsigned int)(t / Time);
}
glBegin(GL_LINES);
for (unsigned int p = 0; p < coords().size(); ++p) {
if (p == frame) {
glColor3f(1.0f, 0.5f, 0.5f);
} else {
glColor3f(1.0f, 1.0f, 1.0f);
}
Vector2f a = make_vector(cosf(coords()[p].z), sinf(coords()[p].z)) * 0.05f;
Vector2f b = perpendicular(a);
glVertex(coords()[p].xy + a * 1.5f);
glVertex(coords()[p].xy - a);
glVertex(coords()[p].xy + b);
glVertex(coords()[p].xy - b);
}
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(-1.0f, -0.5f);
glVertex2f( 1.0f, -0.5f);
glVertex2f( 1.0f, 0.5f);
glVertex2f(-1.0f, 0.5f);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("Coords::draw");
}
void CoordsModule::update(float elapsed_time) {
if (coords().empty()) return;
float t = fmodf(time(), Time * coords().size());
unsigned int frame = (unsigned int)(t / Time);
if (frame < coords().size()) {
current() = coords()[frame];
if (frame + 1 < coords().size()) {
float i = (t - float(frame) * Time) / Time;
current() += (coords()[frame+1] - current()) * i;
}
}
}
bool CoordsModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
coords().push_back(make_vector(local_mouse, 0.0f));
return true;
}
if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(SDL_BUTTON_LEFT)) && !coords().empty()) {
coords().back().z = atan2(local_mouse.y - coords().back().y, local_mouse.x - coords().back().x);
return true;
}
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_BACKSPACE) {
coords().clear();
return true;
}
return false;
}