-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockModule.cpp
81 lines (69 loc) · 1.74 KB
/
ClockModule.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
#include "ClockModule.hpp"
#include <Graphics/Graphics.hpp>
#include <Graphics/Font.hpp>
#include <Vector/VectorGL.hpp>
namespace {
Module *create_clockmodule(const std::string ¶ms) {
return new ClockModule();
}
class Fred {
public:
Fred() {
register_module("clock", create_clockmodule);
}
} fred;
}
Vector2f ClockModule::size() {
return make_vector(0.5f, 0.5f);
}
void ClockModule::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(-0.25f, -0.25f);
glVertex2f( 0.25f, -0.25f);
glVertex2f( 0.25f, 0.25f);
glVertex2f(-0.25f, 0.25f);
glEnd();
glColor3f(0.7f, 0.7f, 0.7f);
glBegin(GL_LINE_LOOP);
for (unsigned int a = 0; a < 23; ++a) {
float ang = a / float(23) * float(M_PI) * 2.0f;
glVertex2f(0.22f * cosf(ang), 0.22f * sinf(ang));
}
glEnd();
glBegin(GL_LINES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.2f * cosf(M_PI * 0.5f - accum() * float(M_PI) * 2.0f), 0.2f * sinf(M_PI * 0.5f - accum() * float(M_PI) * 2.0f));
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.25f, -0.25f);
glVertex2f( 0.25f, -0.25f);
glVertex2f( 0.25f, 0.25f);
glVertex2f(-0.25f, 0.25f);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("Clock::draw");
}
void ClockModule::update(float elapsed_time) {
if (!paused) {
accum() += elapsed_time;
}
}
bool ClockModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (paused) {
paused = false;
accum() = 0.0f;
} else {
paused = true;
}
return true;
}
return false;
}