-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.h
1421 lines (1191 loc) · 34.7 KB
/
io.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// io v0.1 - c++ rapid game prototyping framework using Allegro 5
// Copyright 2017 Pär Arvidsson. All rights reserved.
// Licensed under the MIT license (https://github.com/arvidsson/io/blob/master/LICENSE).
#ifndef IO_HEADER
#define IO_HEADER
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_native_dialog.h>
#include <random>
#include <vector>
#include <array>
#include <unordered_map>
#include <string>
#include <sstream>
#include <memory>
#include <cmath>
#include <ctime>
#include <cstdarg>
namespace io
{
#define io() io::GameServices::instance()
#define IO(T) \
int main(int argc, char** argv) try\
{\
io::init();\
auto game = std::make_unique<T>();\
game->run();\
}\
catch(const std::exception& e)\
{\
al_show_native_message_box(al_get_current_display(), "Error", "Exception!", e.what(), 0, ALLEGRO_MESSAGEBOX_ERROR);\
}
#define IO_THROW(...) (io::throw_exception(__FILE__, __LINE__, __VA_ARGS__))
#define IO_CHECK(cond, msg) if (!cond) { IO_THROW(msg); }
void init();
void throw_exception(const char *file, int line, const char *format, ...);
template <typename T>
class Math
{
public:
static const T almost_zero;
static const T pi;
static const T two_pi;
static const T pi_over_two;
static const T pi_over_four;
static const T deg_to_rad;
static const T rad_to_deg;
static const T e;
static T to_radians(const T degrees)
{
return degrees * deg_to_rad;
}
static T to_degrees(const T radians)
{
return radians * rad_to_deg;
}
static T round(const T value)
{
return value > 0.0 ? std::floor(value + static_cast<T>(0.5)) : std::ceil(value - static_cast<T>(0.5));
}
static bool is_zero(const T value, const T epsilon = almost_zero)
{
return std::abs(value) <= epsilon;
}
static bool is_positive(const T value, const T epsilon = almost_zero)
{
return value > epsilon;
}
static bool is_negative(const T value, const T epsilon = almost_zero)
{
return value < -epsilon;
}
static bool is_equal(const T x1, const T x2, const T epsilon = almost_zero)
{
return std::abs(x1 - x2) < epsilon;
}
static bool is_greater_than(const T x1, const T x2, const T epsilon = almost_zero)
{
return x1 > (x2 + epsilon);
}
static bool is_less_than(const T x1, const T x2, const T epsilon = almost_zero)
{
return x1 < (x2 - epsilon);
}
static bool is_greater_than_or_equal(const T x1, const T x2, const T epsilon = almost_zero)
{
return !is_less_than(x1, x2, epsilon);
}
static bool is_less_than_or_equal(const T x1, const T x2, const T epsilon = almost_zero)
{
return !is_greater_than(x1, x2, epsilon);
}
};
template <typename T> const T Math<T>::almost_zero = static_cast<T>(0.001);
template <typename T> const T Math<T>::pi = static_cast<T>(3.141592653589793);
template <typename T> const T Math<T>::two_pi = static_cast<T>(2.0) * pi;
template <typename T> const T Math<T>::pi_over_two = pi / static_cast<T>(2.0);
template <typename T> const T Math<T>::pi_over_four = pi / static_cast<T>(4.0);
template <typename T> const T Math<T>::deg_to_rad = pi / static_cast<T>(180.0);
template <typename T> const T Math<T>::rad_to_deg = static_cast<T>(180.0) / pi;
template <typename T> const T Math<T>::e = static_cast<T>(2.718281828459045);
template <class Derived, typename T, int N>
class VectorBase;
template <class Derived, typename T>
class VectorBase<Derived, T, 2>
{
protected:
T values[2];
VectorBase(T v0, T v1) : values{v0, v1} {}
bool operator==(const Derived& v) const
{
return values[0] == v.values[0] && values[1] == v.values[1];
}
bool operator!=(const Derived& v) const
{
return !(operator==(v));
}
bool is_equal(const Derived& v, T epsilon = 0.001) const
{
return Math<T>::is_equal(values[0], v.values[0], epsilon) && Math<T>::is_equal(values[1], v.values[1], epsilon);
}
friend Derived operator+(const Derived& v1, const Derived& v2)
{
return Derived{v1.values[0] + v2.values[0], v1.values[1] + v2.values[1]};
}
Derived& operator+=(const Derived& v)
{
values[0] += v.values[0];
values[1] += v.values[1];
return static_cast<Derived&>(*this);
}
friend Derived operator-(const Derived& v1, const Derived& v2)
{
return Derived{v1.values[0] - v2.values[0], v1.values[1] - v2.values[1]};
}
Derived operator-() const
{
return Derived{-values[0], -values[1]};
}
Derived& operator-=(const Derived& v)
{
values[0] -= v.values[0];
values[1] -= v.values[1];
return static_cast<Derived&>(*this);
}
Derived operator*(T f) const
{
return Derived{values[0] * f, values[1] * f};
}
Derived& operator*=(T f)
{
values[0] *= f;
values[1] *= f;
return static_cast<Derived&>(*this);
}
Derived operator/(T f) const
{
return Derived{values[0] / f, values[1] / f};
}
Derived& operator/=(T f)
{
values[0] /= f;
values[1] /= f;
return static_cast<Derived&>(*this);
}
};
template <class Derived, typename T>
class VectorBase<Derived, T, 3>
{
protected:
T values[3];
VectorBase(T v0, T v1, T v2) : values{v0, v1, v2} {}
bool operator==(const Derived& v) const
{
return values[0] == v.values[0] && values[1] == v.values[1] && values[2] == v.values[2];
}
bool operator!=(const Derived& v) const
{
return !(operator==(v));
}
bool is_equal(const Derived& v, T epsilon = 0.001) const
{
return Math<T>::is_equal(values[0], v.values[0], epsilon) && Math<T>::is_equal(values[1], v.values[1], epsilon && Math<T>::is_equal(values[2], v.values[2], epsilon));
}
friend Derived operator+(const Derived& v1, const Derived& v2)
{
return Derived{v1.values[0] + v2.values[0], v1.values[1] + v2.values[1], v1.values[2] + v2.values[2]};
}
Derived& operator+=(const Derived& v)
{
values[0] += v.values[0];
values[1] += v.values[1];
values[2] += v.values[2];
return static_cast<Derived&>(*this);
}
friend Derived operator-(const Derived& v1, const Derived& v2)
{
return Derived{v1.values[0] - v2.values[0], v1.values[1] - v2.values[1], v1.values[2] - v2.values[2]};
}
Derived operator-() const
{
return Derived{-values[0], -values[1], -values[2]};
}
Derived& operator-=(const Derived& v)
{
values[0] -= v.values[0];
values[1] -= v.values[1];
values[2] -= v.values[2];
return static_cast<Derived&>(*this);
}
Derived operator*(T f) const
{
return Derived{values[0] * f, values[1] * f, values[2] * f};
}
Derived& operator*=(T f)
{
values[0] *= f;
values[1] *= f;
values[2] *= f;
return static_cast<Derived&>(*this);
}
Derived operator/(T f) const
{
return Derived{values[0] / f, values[1] / f, values[2] / f};
}
Derived& operator/=(T f)
{
values[0] /= f;
values[1] /= f;
values[2] /= f;
return static_cast<Derived&>(*this);
}
};
template <class Derived, typename T>
class VectorBase<Derived, T, 4>
{
protected:
T values[4];
VectorBase(T v0, T v1, T v2, T v3) : values{v0, v1, v2, v3} {}
bool operator==(const Derived& v) const
{
return values[0] == v.values[0] && values[1] == v.values[1] && values[2] == v.values[2] && values[3] == v.values[3];
}
bool operator!=(const Derived& v) const
{
return !(operator==(v));
}
bool is_equal(const Derived& v, T epsilon = 0.001) const
{
return Math<T>::is_equal(values[0], v.values[0], epsilon) && Math<T>::is_equal(values[1], v.values[1], epsilon && Math<T>::is_equal(values[2], v.values[2], epsilon) && Math<T>::is_equal(values[3], v.values[3], epsilon));
}
friend Derived operator+(const Derived& v1, const Derived& v2)
{
return Derived{v1.values[0] + v2.values[0], v1.values[1] + v2.values[1], v1.values[2] + v2.values[2], v1.values[3] + v2.values[3]};
}
Derived& operator+=(const Derived& v)
{
values[0] += v.values[0];
values[1] += v.values[1];
values[2] += v.values[2];
values[3] += v.values[3];
return static_cast<Derived&>(*this);
}
friend Derived operator-(const Derived& v1, const Derived& v2)
{
return Derived{v1.values[0] - v2.values[0], v1.values[1] - v2.values[1], v1.values[2] - v2.values[2], v1.values[3] - v2.values[3]};
}
Derived operator-() const
{
return Derived{-values[0], -values[1], -values[2], -values[3]};
}
Derived& operator-=(const Derived& v)
{
values[0] -= v.values[0];
values[1] -= v.values[1];
values[2] -= v.values[2];
values[3] -= v.values[3];
return static_cast<Derived&>(*this);
}
Derived operator*(T f) const
{
return Derived{values[0] * f, values[1] * f, values[2] * f, values[3] * f};
}
Derived& operator*=(T f)
{
values[0] *= f;
values[1] *= f;
values[2] *= f;
values[3] *= f;
return static_cast<Derived&>(*this);
}
Derived operator/(T f) const
{
return Derived{values[0] / f, values[1] / f, values[2] / f, values[3] / f};
}
Derived& operator/=(T f)
{
values[0] /= f;
values[1] /= f;
values[2] /= f;
values[3] /= f;
return static_cast<Derived&>(*this);
}
};
template <typename T>
class Vector : public VectorBase<Vector<T>, T, 2>
{
using Base = VectorBase<Vector<T>, T, 2>;
public:
T& x;
T& y;
Vector(T x = T(), T y = T()) : Base(x, y), x(Base::values[0]), y(Base::values[1]) {}
Vector& operator=(const Vector& v) { x = v.x; y = v.y; return *this; }
using Base::operator==;
using Base::operator!=;
using Base::is_equal;
using Base::operator+=;
using Base::operator-;
using Base::operator-=;
using Base::operator*;
using Base::operator*=;
using Base::operator/;
using Base::operator/=;
T dot_product(const Vector& v) const
{
return x * v.x + y * v.y;
}
T cross_product(const Vector& v) const
{
return x * v.y - y * v.x;
}
T length() const
{
return std::sqrt(x * x + y * y);
}
T length_squared() const
{
return x * x + y * y;
}
void normalize()
{
T l = length();
if (l) {
x /= l;
y /= l;
}
}
Vector normalized() const
{
T l = length();
return l ? Vector{x / l, y / l} : Vector();
}
Vector polar(T angle, T length) const
{
return Vector{length * std::cos(angle), length * std::sin(angle)};
}
T magnitude() const
{
return length();
}
T direction() const
{
return std::atan2(y, x);
}
T angle() const
{
return direction();
}
T distance_to(const Vector& v)
{
T dx = v.x - x;
T dy = v.y - y;
return std::sqrt(dx * dx + dy * dy);
}
std::string to_string() const
{
std::stringstream ss;
ss << "(" << x << "," << y << ")";
return ss.str();
}
};
using Vector2i = Vector<int>;
using Vector2f = Vector<float>;
template <typename T>
class Size : public VectorBase<Size<T>, T, 2>
{
using Base = VectorBase<Size<T>, T, 2>;
public:
T& w;
T& h;
Size(T w = T(), T h = T()) : Base(w, h), w(Base::values[0]), h(Base::values[1]) {}
Size& operator=(const Size& s) { w = s.w; h = s.h; return *this; }
using Base::operator==;
using Base::operator!=;
using Base::is_equal;
using Base::operator+=;
using Base::operator-;
using Base::operator-=;
using Base::operator*;
using Base::operator*=;
using Base::operator/;
using Base::operator/=;
std::string to_string() const
{
std::stringstream ss;
ss << "(" << w << "x" << h << ")";
return ss.str();
}
};
using Size2i = Size<int>;
using Size2f = Size<float>;
template <typename T>
class Rect
{
public:
Vector<T> pos;
Size<T> size;
Rect(T x, T y, T w, T h) : pos(x, y), size(w, h) {}
Rect(Vector<T> pos, Size<T> size) : pos(pos), size(size) {}
T x() const { return pos.x; }
T y() const { return pos.y; }
T w() const { return size.w; }
T h() const { return size.h; }
T left() const { return x(); }
T right() const { return x() + w(); }
T top() const { return y(); }
T bottom() const { return y() + h(); }
Vector<T> top_left() const { return Vector<T>(left(), top()); }
Vector<T> top_right() const { return Vector<T>(right(), top()); }
Vector<T> bottom_left() const { return Vector<T>(left(), bottom()); }
Vector<T> bottom_right() const { return Vector<T>(right(), bottom()); }
T center_x() const { return x() + w() / 2; }
T center_y() const { return y() + h() / 2; }
Vector<T> center() const { return Vector<T>(center_x(), center_y()); }
bool contains(T x, T y) const
{
return !(x < left() || x > right() || y < top() || y > bottom());
}
bool contains(const Vector<T>& v) const
{
return contains(v.x, v.y);
}
bool contains(const Rect<T>& r) const
{
return contains(r.left(), r.top()) && contains(r.right(), r.bottom());
}
bool intersects(T x0, T y0, T x1, T y1) const
{
return !(x0 > right() || x1 < left() || y0 > bottom() || y1 < top());
}
bool intersects(const Rect<T>& r) const
{
return intersects(r.left(), r.top(), r.right(), r.bottom());
}
bool intersects(const Vector<T>& v, T radius) const
{
return false;
}
Rect<T> intersection(const Rect<T>& r) const
{
auto l = std::max(left(), r.left());
auto r = std::min(right(), r.right());
auto t = std::max(top(), r.top());
auto b = std::min(bottom(), r.bottom());
return Rect<T>(l, t, r - l, t - b);
}
std::string to_string() const
{
std::stringstream ss;
ss << "((" << left() << "," + top() << "),(" << right() << "," << bottom() << "))";
return ss.str();
}
};
using Rect2f = Rect<float>;
using Rect2i = Rect<int>;
class Transform
{
public:
Transform();
Transform(const ALLEGRO_TRANSFORM &transform);
Transform& operator=(const ALLEGRO_TRANSFORM &transform);
ALLEGRO_TRANSFORM to_allegro_transform() const;
void identity();
void invert();
Transform inverted() const;
void use();
void translate(Vector2f position);
void scale(Vector2f factor);
void rotate(float angle);
Vector2f transform_coordinates(Vector2f coordinates);
private:
ALLEGRO_TRANSFORM t;
};
class Random
{
public:
Random(unsigned int seed = (unsigned int)time(0));
int get_next_int(int min, int max);
float get_next_float(float min, float max);
bool one_in(int chance);
int roll_dice(int number, int sides);
private:
std::mt19937 rng;
};
class Input
{
public:
bool is_key_down(int key) const;
bool is_key_pressed(int key) const;
bool is_key_released(int key) const;
bool is_mouse_button_down(int button) const;
bool is_mouse_button_pressed(int button) const;
bool is_mouse_button_released(int button) const;
Vector2i get_mouse_pos() const;
static int wait_for_keypress();
static void wait_for_any();
private:
struct Pressable
{
bool down = false;
bool pressed = false;
bool released = false;
};
struct Keyboard
{
std::array<Pressable, ALLEGRO_KEY_MAX> keys;
} keyboard;
struct Mouse
{
std::array<Pressable, 10> buttons;
Vector2i pos;
Vector2i old_pos;
int wheel, old_wheel;
} mouse;
friend class Game;
};
using Bitmap = std::shared_ptr<ALLEGRO_BITMAP>;
using Font = std::shared_ptr<ALLEGRO_FONT>;
using Sound = std::shared_ptr<ALLEGRO_SAMPLE>;
using Music = std::shared_ptr<ALLEGRO_AUDIO_STREAM>;
Bitmap make_bitmap(int width, int height);
Bitmap load_bitmap(std::string filename);
Bitmap cut_bitmap(Bitmap parent, Vector2i pos, Size2i size);
Font load_font(std::string filename, int size);
Sound load_sound(std::string filename);
Music load_music(std::string filename);
class Resources
{
public:
Bitmap get_bitmap(std::string filename);
std::vector<Bitmap> get_sub_bitmaps(std::string filename, int width, int height);
Font get_font(std::string filename, int size);
Sound get_sound(std::string filename);
Music get_music(std::string filename);
private:
std::unordered_map<std::string, Bitmap> bitmaps;
std::unordered_map<std::string, std::vector<Bitmap>> sub_bitmaps;
std::unordered_map<std::string, Font> fonts;
std::unordered_map<std::string, Sound> sounds;
std::unordered_map<std::string, Music> music;
};
class Color : public VectorBase<Color, float, 4>
{
using Base = VectorBase<Color, float, 4>;
public:
float& r;
float& g;
float& b;
float& a;
Color(float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 0.0f);
Color(int r, int g, int b, int a);
Color& operator=(const Color& c);
Color(const ALLEGRO_COLOR& color);
Color& operator=(const ALLEGRO_COLOR& color);
using Base::operator==;
using Base::operator!=;
using Base::is_equal;
using Base::operator+=;
using Base::operator-;
using Base::operator-=;
using Base::operator*;
using Base::operator*=;
using Base::operator/;
using Base::operator/=;
ALLEGRO_COLOR to_allegro_color() const;
std::string to_string() const;
};
class Camera2D
{
public:
void use();
void reset();
void move_to(Vector2f new_pos);
void move_by(Vector2f scroll_factor);
void zoom_to(float new_zoom);
void zoom_by(float zoom_factor);
void rotate_to(float new_rotation);
void rotate_by(float rotation_factor);
float x() const;
float y() const;
float zoom() const;
float rotation() const;
Vector2f to_screen(Vector2f coords) const;
Vector2f to_world(Vector2f coords) const;
private:
Transform get_transform() const;
Vector2f pos;
float z = 1.0;
float r = 0.0f;
};
class Config
{
public:
Config(std::string filename);
~Config();
bool has_value(std::string key, std::string section = "");
bool get_bool(std::string key, std::string section = "");
int get_int(std::string key, std::string section = "");
float get_float(std::string key, std::string section = "");
std::string get_string(std::string key, std::string section = "");
private:
ALLEGRO_CONFIG* cfg;
};
class Game
{
public:
Game(std::string title, Size2i window_size, bool fullscreen);
Game(std::string title, std::string config_path);
virtual ~Game();
void run();
void quit();
virtual void update() = 0;
virtual void render() = 0;
virtual void process_event(ALLEGRO_EVENT event) {}
protected:
void initialize(std::string title, Size2i window_size, bool fullscreen);
bool done = false;
bool paused = false;
ALLEGRO_EVENT_QUEUE* queue;
ALLEGRO_TIMER* timer;
ALLEGRO_DISPLAY* display;
};
class GameServices
{
public:
static GameServices& instance() { static GameServices gs; return gs; }
Size2i window_size;
Input input;
Random random;
};
} // namespace io
#ifdef IO_IMPLEMENTATION
namespace io
{
void init()
{
IO_CHECK(al_init(), "Failed to initialize allegro");
IO_CHECK(al_install_keyboard(), "Failed to install keyboard");
IO_CHECK(al_install_mouse(), "Failed to install mouse");
ALLEGRO_PATH* resource_path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_change_directory(al_path_cstr(resource_path, ALLEGRO_NATIVE_PATH_SEP));
al_destroy_path(resource_path);
IO_CHECK(al_init_primitives_addon(), "Failed to initialize primitives addon");
IO_CHECK(al_init_image_addon(), "Failed to initialize image addon");
IO_CHECK(al_init_font_addon(), "Failed to initialize font addon");
IO_CHECK(al_init_ttf_addon(), "Failed to initialize ttf addon");
IO_CHECK(al_install_audio(), "Failed to install audio addon");
IO_CHECK(al_init_acodec_addon(), "Failed to initialize acodec addon");
IO_CHECK(al_reserve_samples(32), "Failed to reserve samples");
}
void throw_exception(const char *file, int line, const char *format, ...)
{
char buffer[4096];
va_list ap;
va_start(ap, format);
vsprintf_s(buffer, format, ap);
va_end(ap);
std::stringstream ss;
ss << buffer << " (" << file << ":" << line << ")";
throw std::runtime_error(ss.str());
}
Transform::Transform()
{
identity();
}
Transform::Transform(const ALLEGRO_TRANSFORM &transform)
{
al_copy_transform(&t, &transform);
}
Transform& Transform::operator=(const ALLEGRO_TRANSFORM &transform)
{
al_copy_transform(&t, &transform);
return *this;
}
ALLEGRO_TRANSFORM Transform::to_allegro_transform() const
{
return t;
}
void Transform::identity()
{
al_identity_transform(&t);
}
void Transform::invert()
{
al_invert_transform(&t);
}
Transform Transform::inverted() const
{
Transform t = *this;
t.invert();
return t;
}
void Transform::use()
{
al_use_transform(&t);
}
void Transform::translate(Vector2f position)
{
al_translate_transform(&t, position.x, position.y);
}
void Transform::scale(Vector2f factor)
{
al_scale_transform(&t, factor.x, factor.y);
}
void Transform::rotate(float angle)
{
al_rotate_transform(&t, angle);
}
Vector2f Transform::transform_coordinates(Vector2f coordinates)
{
float x, y;
al_transform_coordinates(&t, &x, &y);
return Vector2f{x, y};
}
Random::Random(unsigned int seed)
{
rng.seed(seed);
}
int Random::get_next_int(int min, int max)
{
std::uniform_int_distribution<int> dist(min, max);
return dist(rng);
}
float Random::get_next_float(float min, float max)
{
std::uniform_real_distribution<float> dist(min, max);
return dist(rng);
}
bool Random::one_in(int chance)
{
if (get_next_int(0, chance - 1) == 0)
return true;
return false;
}
int Random::roll_dice(int number, int sides)
{
int result = 0;
for (int i = 0; i < number; i++)
result += get_next_int(1, sides);
return result;
}
bool Input::is_key_down(int key) const
{
return keyboard.keys[key].down;
}
bool Input::is_key_pressed(int key) const
{
return keyboard.keys[key].pressed;
}
bool Input::is_key_released(int key) const
{
return keyboard.keys[key].released;
}
bool Input::is_mouse_button_down(int button) const
{
return mouse.buttons[button].down;
}
bool Input::is_mouse_button_pressed(int button) const
{
return mouse.buttons[button].pressed;
}
bool Input::is_mouse_button_released(int button) const
{
return mouse.buttons[button].released;
}
Vector2i Input::get_mouse_pos() const
{
return mouse.pos;
}
int Input::wait_for_keypress()
{
auto queue = std::shared_ptr<ALLEGRO_EVENT_QUEUE>(al_create_event_queue(), al_destroy_event_queue);
al_register_event_source(queue.get(), al_get_keyboard_event_source());
ALLEGRO_EVENT event;
do {
al_wait_for_event(queue.get(), &event);
} while (event.type != ALLEGRO_EVENT_KEY_DOWN);
return event.keyboard.keycode;
}
void Input::wait_for_any()
{
auto queue = std::shared_ptr<ALLEGRO_EVENT_QUEUE>(al_create_event_queue(), al_destroy_event_queue);
al_register_event_source(queue.get(), al_get_keyboard_event_source());
ALLEGRO_EVENT event;
do {
al_wait_for_event(queue.get(), &event);
} while (event.type != ALLEGRO_EVENT_KEY_DOWN || event.type != ALLEGRO_EVENT_MOUSE_BUTTON_DOWN);
}
Bitmap make_bitmap(int width, int height)
{
return Bitmap(al_create_bitmap(width, height), al_destroy_bitmap);
}
Bitmap load_bitmap(std::string filename)
{
return Bitmap(al_load_bitmap(filename.c_str()), al_destroy_bitmap);
}
Bitmap cut_bitmap(Bitmap parent, Vector2i pos, Size2i size)
{
return Bitmap(al_create_sub_bitmap(parent.get(), pos.x, pos.y, size.w, size.h), al_destroy_bitmap);
}
Font load_font(std::string filename, int size)
{
return Font(al_load_font(filename.c_str(), size, 0), al_destroy_font);
}
Sound load_sound(std::string filename)
{
return Sound(al_load_sample(filename.c_str()), al_destroy_sample);
}
Music load_music(std::string filename)
{
return Music(al_load_audio_stream(filename.c_str(), 4, 2048), al_destroy_audio_stream);
}
Bitmap Resources::get_bitmap(std::string filename)
{
if (bitmaps.find(filename) != bitmaps.end())
{
return bitmaps[filename];
}