-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobals.h
338 lines (296 loc) · 8 KB
/
globals.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#ifndef GLOBALS_H_
#define GLOBALS_H_
#pragma warning(disable:4010)
#include <SDL.h>
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <list>
#include <iostream>
#include <fstream>
#include <map>
#include <functional>
#include "GameState.h"
#include "timer.h"
using namespace std;
//Toggle this on to enable debug mode
extern const bool g_debug_mode_on;
//Will be used all over the place for SDL_RenderCopy
extern SDL_Window* g_window;
//Will be used all over the place for SDL_RenderCopy
extern SDL_Renderer* g_renderer;
//Event handling will be done in multiple places
extern SDL_Event g_event;
//We will need to switch the next game state in multiple places
extern GameStateType g_next_game_state;
//Game controller
//extern SDL_Joystick* gGameController;
//We may need to exit the game at any time
extern bool g_main_loop_is_running;
//The whole program will need the screen data
extern int g_screen_width;
extern int g_screen_height;
//I might need these, I might not. I probably won't.
extern const int g_base_screen_width;
extern const int g_base_screen_height;
//I will need this:
extern SDL_Rect g_viewport;
extern double g_height_modifier;
extern double g_width_modifier;
extern bool g_window_is_minimized;
extern double g_FPS;
//One timer for everything
extern Timer g_timer;
extern const double g_pi;
//Or this.
extern const double g_sqrt2over2;
//default drawing colors
extern int g_default_red;
extern int g_default_blue;
extern int g_default_green;
extern int g_default_alpha;
extern const double g_gravity;
extern const double g_terminal_velocity;
void change_screen_height(int value);
void change_screen_width(int value);
//Maybe I'll want to change this later
const static double G_TILE_WIDTH = 16;
const static double G_TILE_HEIGHT = 16;
const static int TILES_PER_SCREEN_X = 64;
const static int TILES_PER_SCREEN_Y = 36;
inline SDL_RendererFlip bool_to_SDL_RendererFlip(bool flip)
{
if (flip)
return SDL_FLIP_HORIZONTAL;
else
return SDL_FLIP_NONE;
}
//2022 comment - Why did I not put these in their own file?? /facepalm
inline int round_to_nearest(double value)
{
return static_cast<int>(floor(value + 0.5));
}
inline bool collide_two_sdl_rects(const SDL_Rect& lhs, const SDL_Rect& rhs);
bool rect_rect_collision(const Rect& lhs, const Rect& rhs);
inline double distance_between_points(const Point& lhs, const Point& rhs);
double calculate_angle_between_points_in_radians(const Point& lhs, const Point& rhs);
double calculate_angle_between_points_in_degrees(const Point& lhs, const Point& rhs);
inline bool point_circle_collision(const Circle& theCircle, const Point* thePoint);
inline bool point_point_collision(const Point* lhs, const Point* rhs);
inline bool rect_point_collision(const Rect& theRect, const Point& thePoint);
inline bool circle_circle_collision(const Circle& lhs, const Circle& rhs);
bool circle_rect_collision(const Circle& theCircle, const Rect& theRect);
bool line_line_collision(const Line& lhs, const Line& rhs);
bool line_rect_collision(const Line& the_line, const Rect& the_rect);
//Tells you how much needs to pass in one sprite's object based on its FPS\
(This assumes that its less than the global FPS, which is set to 60, and can be changed in Globals.cpp)
int time_per_frame(int FPS);
inline double center_of_screen_x() { return static_cast<double>(g_screen_width) / 2; }
inline double center_of_screen_y() { return static_cast<double>(g_screen_height) / 2; }
void modify_velocity_with_gravity(double* velocity, double* force_from_gravity);
void modify_velocity_with_gravity_and_limit(double* velocity, double* force_from_gravity);
void modify_velocity_with_force(double* velocity, double* force_from_gravity, double force);
void modify_velocity_without_gravity(double* velocity, double* force_from_gravity);
bool did_it_pass(double old_value, double new_value, const double magic_number);
bool did_it_pass_neg(double old_value, double new_value, const double magic_number);
Point turn_point_in_a_box(const Point& movingPoint, const double width, const double height, const double angle);
inline string take_ms_return_english(long value)
{
//Removed milliseconds. Figured it was too much
//long milliseconds = value % 1000;
long seconds = (value / 1000);
long minutes = seconds / 60;
long hours = minutes / 60;
string new_string;
if (hours != 0)
{
seconds -= 60 * minutes;
minutes -= 60 * hours;
new_string += to_string(hours);
new_string += ":";
if (minutes < 10)
new_string += "0";
}
if (hours == 0)
seconds -= 60 * minutes;
new_string += to_string(minutes);
new_string += ":";
if (seconds < 10)
new_string += "0";
new_string += to_string(seconds);
//new_string += ".";
//new_string += to_string(milliseconds);
return new_string;
}
inline double sin_degrees(double value)
{
return sin(value * g_pi / 180);
}
inline double cos_degrees(double value)
{
return cos(value * g_pi / 180);
}inline double tan_degrees(double value)
{
return tan(value * g_pi / 180);
}
inline void pos_x_in_world(double* pos_x, int screen_x)
{
*pos_x = *pos_x + static_cast<double>(screen_x * g_screen_width);
}
inline void pos_y_in_world(double* pos_y, int screen_y)
{
*pos_y = *pos_y + static_cast<double>(screen_y * g_screen_height);
}
inline Point get_rand_point_in_box(const Rect& box)
{
Point new_point;
new_point.x = rand() % static_cast<int>(box.w) + box.x;
new_point.y = rand() % static_cast<int>(box.h) + box.y;
return new_point;
}
//Uses the global timer to keep track of how much time has passed since initialization
//Set the time to '-1' to allow the timer to last forever.
class Countdown
{
public:
Countdown(double how_long) :
time_left_(how_long)
{}
//Call this once per loop. Returns true if time is up.
inline bool countdown_logic()
{
if (time_left_ == -1)
return false;
time_left_ -= g_timer.deltaT_in_seconds();
return time_left_ <= 0;
}
inline double time_left()
{
return time_left_;
}
private:
double time_left_;
};
class Frequency_Timer
{
public:
Frequency_Timer(double how_often) :
timer_(0),
max_time_(how_often),
recent_cycle_(false)
{}
//Call this once per loop. Returns true if time
inline bool frequency_timer_logic()
{
timer_ += g_timer.deltaT_in_seconds();
if (timer_ >= max_time_)
{
timer_ -= max_time_;
recent_cycle_ = true;
return true;
}
recent_cycle_ = false;
return false;
}
bool recent_cycle()
{
return recent_cycle_;
}
double time_spent()
{
return timer_;
}
double time_left()
{
return max_time_ - timer_;
}
void change_max_time(double value)
{
max_time_ = value;
}
private:
double timer_;
double max_time_;
bool recent_cycle_;
};
//An angle in degrees. Zero degrees points straight to the right.
class Angle
{
public:
Angle(double angle) :
angle_(angle)
{
}
void increase_by(double value)
{
angle_ += value;
int cycles = static_cast<int>(angle_) / 360;
angle_ -= cycles * 360;
if (angle_ < 0)
angle_ = 360 + angle_;
}
void decrease_by(double value)
{
angle_ -= value;
int cycles = static_cast<int>(angle_) / 360;
angle_ += cycles * 360;
if (angle_ < 0)
angle_ = 360 + angle_;
}
void set_angle(double value)
{
angle_ = value;
int cycles = static_cast<int>(angle_) / 360;
if (cycles > 0)
angle_ -= cycles * 360;
else if (cycles < 0)
angle_ += cycles * 360;
else if (angle_ < 0)
angle_ = 360 + angle_;
}
double angle()
{
return angle_;
}
private:
double angle_;
};
class Vector
{
public:
//Angle in degrees!
Vector(double magnitude, double angle) :
magnitude_(magnitude),
angle_(angle)
{}
double magnitude() { return magnitude_; }
inline void set_angle(double value)
{
angle_.set_angle(value);
}
double magnitude_x()
{
return magnitude_ * cos_degrees(angle_.angle());
}
double magnitude_y()
{
return magnitude_ * sin_degrees(angle_.angle());
}
void decrease_magnitude(double value)
{
magnitude_ -= value;
}
void decrease_magnitude_with_zero_minimum(double value)
{
magnitude_ -= value;
if (magnitude_ < 0)
magnitude_ = 0;
}
private:
double magnitude_;
Angle angle_;
};
#endif