forked from janneku/blackbeltsorvihero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
186 lines (155 loc) · 2.92 KB
/
common.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#if !defined(_COMMON_H)
#define _COMMON_H
#define NAME "Black Belt Sorvi Hero"
#define DIR_NAME "BlackBeltSorviHero"
#include <SDL.h>
#if !defined(M_PI)
#define M_PI 3.14159265358979323846
#endif
#include <string>
#include "utils.h"
#include "gl.h"
class Shape;
struct Level {
const Shape *shapes;
const char *descr;
const char *music;
int time_limit;
int min_score;
};
struct Particle {
bool smoke;
color c;
float time;
vec3 pos;
vec3 vel;
};
class Timer {
public:
Timer();
float get_dt();
private:
Uint32 m_last_ticks;
};
#if !defined(CONFIG_GLES)
const int DIGIT_WIDTH = 40;
const int DIGIT_HEIGHT = 64;
#else
const int DIGIT_WIDTH = 20;
const int DIGIT_HEIGHT = 32;
#endif
template<class T>
T zero()
{
return 0;
}
template<>
inline vec3 zero<vec3>()
{
return vec3(0, 0, 0);
}
template<>
inline vec2 zero<vec2>()
{
return vec2(0, 0);
}
class Once {
public:
Once() :
done(false)
{}
operator bool()
{
if (done)
return false;
done = true;
return true;
}
private:
bool done;
};
template<class T>
class Animator {
public:
Animator(const T &val = zero<T>()) :
m_value(val), m_vel(zero<T>()), m_target(val)
{}
operator T() const { return m_value; }
T value() const { return m_value; }
void set_value(const T &val)
{
m_value = val;
m_vel = zero<T>();
m_target = val;
}
void set_target(const T &val)
{
m_target = val;
}
void update(float dt, float force)
{
const float MAX_STEP = 0.02;
float friction = sqrt(force) * 2;
while (dt > MAX_STEP) {
T accel = (m_target - m_value) * force - m_vel * friction;
m_value = m_value + m_vel * MAX_STEP;
m_vel = m_vel + accel * MAX_STEP;
dt -= MAX_STEP;
}
T accel = (m_target - m_value) * force - m_vel * friction;
m_value = m_value + m_vel * dt;
m_vel = m_vel + accel * dt;
}
private:
T m_value;
T m_vel;
T m_target;
};
class PackFile {
public:
PackFile() {}
PackFile(const char *fname);
size_t offset() const { return m_offset; }
size_t size() const { return m_size; }
void seek(size_t offset);
size_t read(void *buf, size_t len);
private:
size_t m_begin;
size_t m_offset;
size_t m_size;
};
struct Setting {
bool *ptr;
const char *name;
const char *text;
};
class Model;
extern int scr_width;
extern int scr_height;
extern bool fullscreen;
extern TTF_Font *font;
extern int score;
extern Model sorvi;
extern Model room;
extern Model akseli;
extern Model support;
extern const Level *level;
extern int level_num;
extern const Setting setting_list[];
float randf(float min, float max);
void exit();
void hud_matrix();
void open_window();
bool get_event(SDL_Event *e);
void set_particles_light(bool bloom);
void update_particles(float dt);
void draw_particles();
void draw_smoke(const vec3 &camera);
void add_particle(Particle p);
void kill_particles();
void draw_number(int v, int len);
void open_pack(const char *fname);
void load_settings();
void save_settings();
void set_setting(const Setting *setting, bool state);
#endif