-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinModule.cpp
157 lines (137 loc) · 4.4 KB
/
BinModule.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
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
148
149
150
151
152
153
154
155
156
157
#include "BinModule.hpp"
#include <Graphics/Graphics.hpp>
#include <Graphics/Font.hpp>
#include <Vector/VectorGL.hpp>
#include <algorithm>
namespace {
Module *create_binmodule(const std::string ¶ms) {
Game2d::init_gamestuff();
return new BinModule();
}
class Fred {
public:
Fred() {
register_module("bin", create_binmodule);
}
} fred;
}
Vector2f BinModule::size() {
return make_vector(1.5f, 1.0f);
}
void BinModule::draw(Box2f viewport, Box2f screen_viewport, float scale, unsigned int recurse) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBoxToBox(viewport, screen_viewport);
Vector2f size = this->size();
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3f(-0.5f * size.x, -0.5f * size.y, 1.0f);
glVertex3f( 0.5f * size.x, -0.5f * size.y, 1.0f);
glVertex3f( 0.5f * size.x, 0.5f * size.y, 1.0f);
glVertex3f(-0.5f * size.x, 0.5f * size.y, 1.0f);
glEnd();
static vector< float > jump_bins;
static vector< float > nojump_bins;
static bool inited = false;
if (!inited) {
inited = true;
jump_bins.clear();
nojump_bins.clear();
for (unsigned int b = 0; b < Game2d::bins.size(); ++b) {
if (Game2d::bins[b].jump) {
jump_bins.push_back(Game2d::bins[b].vel);
} else {
nojump_bins.push_back(Game2d::bins[b].vel);
}
}
std::sort(jump_bins.begin(), jump_bins.end());
std::sort(nojump_bins.begin(), nojump_bins.end());
}
const float min = -3.1f;
const float max = 3.1f;
if (control().jump && index() < Game2d::bins.size()) {
for (unsigned int b = 0; b < jump_bins.size(); ++b) {
if (jump_bins[b] == Game2d::bins[index()].vel) {
float prev = min;
float next = max;
if (b > 0) prev = 0.5f * (jump_bins[b-1] + jump_bins[b]);
if (b + 1 < jump_bins.size()) next = 0.5f * (jump_bins[b] + jump_bins[b+1]);
prev = ((prev - min) / (max - min) - 0.5f) * size.x;
next = ((next - min) / (max - min) - 0.5f) * size.x;
glBegin(GL_QUADS);
glColor3f(0.7f, 0.7f, 0.4f);
glVertex2f(prev, 0.0f);
glVertex2f(next, 0.0f);
glVertex2f(next, 0.5f * size.y);
glVertex2f(prev, 0.5f * size.y);
glEnd();
break;
}
}
}
if (!control().jump && index() < Game2d::bins.size()) {
for (unsigned int b = 0; b < nojump_bins.size(); ++b) {
if (jump_bins[b] == Game2d::bins[index()].vel) {
float prev = min;
float next = max;
if (b > 0) prev = 0.5f * (nojump_bins[b-1] + nojump_bins[b]);
if (b + 1 < nojump_bins.size()) next = 0.5f * (nojump_bins[b] + nojump_bins[b+1]);
prev = ((prev - min) / (max - min) - 0.5f) * size.x;
next = ((next - min) / (max - min) - 0.5f) * size.x;
glBegin(GL_QUADS);
glColor3f(0.7f, 0.7f, 0.4f);
glVertex2f(prev, 0.0f);
glVertex2f(next, 0.0f);
glVertex2f(next,-0.5f * size.y);
glVertex2f(prev,-0.5f * size.y);
glEnd();
break;
}
}
}
glBegin(GL_LINES);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(-size.x * 0.5f, 0.0f);
glVertex2f( size.x * 0.5f, 0.0f);
for (unsigned int b = 1; b < jump_bins.size(); ++b) {
float pos = (((jump_bins[b-1] + jump_bins[b]) * 0.5f - min) / (max - min) - 0.5f) * size.x;
glVertex2f(pos, 0.0f);
glVertex2f(pos, size.y * 0.5f);
}
for (unsigned int b = 1; b < nojump_bins.size(); ++b) {
float pos = (((nojump_bins[b-1] + nojump_bins[b]) * 0.5f - min) / (max - min) - 0.5f) * size.x;
glVertex2f(pos, 0.0f);
glVertex2f(pos, size.y * -0.5f);
}
glEnd();
glBegin(GL_LINES);
Vector2f pos;
pos.y = size.y * (control().jump ? 0.25f : -0.25f);
pos.x = ((control().vel - min) / (max - min) - 0.5f) * size.x;
glColor3f(1.0f, 0.0f, 0.0f);
glVertex(pos + make_vector( 0.1f, 0.1f));
glVertex(pos + make_vector(-0.1f,-0.1f));
glVertex(pos + make_vector( 0.1f,-0.1f));
glVertex(pos + make_vector(-0.1f, 0.1f));
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f * size.x, -0.5f * size.y, 1.0f);
glVertex3f( 0.5f * size.x, -0.5f * size.y, 1.0f);
glVertex3f( 0.5f * size.x, 0.5f * size.y, 1.0f);
glVertex3f(-0.5f * size.x, 0.5f * size.y, 1.0f);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("Bin::draw");
}
void BinModule::update(float elapsed_time) {
const float min = -3.1f;
const float max = 3.1f;
index() = Game2d::control_bin(control());
control.position.y = size().y * (control().jump ? 0.25f : -0.25f);
control.position.x = ((control().vel - min) / (max - min) - 0.5f) * size().x;
}
bool BinModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
return false;
}