red_bird 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +23 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +47 -0
  7. data/Rakefile +10 -0
  8. data/bin/setup +8 -0
  9. data/ext/red_bird/bird.c +15 -0
  10. data/ext/red_bird/bird.h +10 -0
  11. data/ext/red_bird/color.c +95 -0
  12. data/ext/red_bird/color.h +27 -0
  13. data/ext/red_bird/dynamic_sprite.c +163 -0
  14. data/ext/red_bird/dynamic_sprite.h +30 -0
  15. data/ext/red_bird/engine.c +354 -0
  16. data/ext/red_bird/engine.h +40 -0
  17. data/ext/red_bird/extconf.rb +9 -0
  18. data/ext/red_bird/font.c +94 -0
  19. data/ext/red_bird/font.h +26 -0
  20. data/ext/red_bird/input_device.c +100 -0
  21. data/ext/red_bird/input_device.h +15 -0
  22. data/ext/red_bird/keycode.c +42 -0
  23. data/ext/red_bird/keycode.h +12 -0
  24. data/ext/red_bird/loader.c +154 -0
  25. data/ext/red_bird/loader.h +54 -0
  26. data/ext/red_bird/main.c +38 -0
  27. data/ext/red_bird/main.h +12 -0
  28. data/ext/red_bird/palette.c +132 -0
  29. data/ext/red_bird/palette.h +23 -0
  30. data/ext/red_bird/rect.c +257 -0
  31. data/ext/red_bird/rect.h +20 -0
  32. data/ext/red_bird/render.c +130 -0
  33. data/ext/red_bird/render.h +25 -0
  34. data/ext/red_bird/sprite.c +130 -0
  35. data/ext/red_bird/sprite.h +27 -0
  36. data/ext/red_bird/text.c +212 -0
  37. data/ext/red_bird/text.h +31 -0
  38. data/ext/red_bird/texture.c +157 -0
  39. data/ext/red_bird/texture.h +33 -0
  40. data/ext/red_bird/texture_imp.cpp +49 -0
  41. data/ext/red_bird/texture_imp.hpp +29 -0
  42. data/ext/red_bird/timer.c +134 -0
  43. data/ext/red_bird/timer.h +25 -0
  44. data/lib/red_bird.rb +15 -0
  45. data/lib/red_bird/animation.rb +133 -0
  46. data/lib/red_bird/camera.rb +61 -0
  47. data/lib/red_bird/controller.rb +44 -0
  48. data/lib/red_bird/dynamic_sprite.rb +38 -0
  49. data/lib/red_bird/engine.rb +81 -0
  50. data/lib/red_bird/entity.rb +74 -0
  51. data/lib/red_bird/entity_collision.rb +31 -0
  52. data/lib/red_bird/input_device.rb +86 -0
  53. data/lib/red_bird/palette.rb +23 -0
  54. data/lib/red_bird/relative_entity.rb +95 -0
  55. data/lib/red_bird/sprite.rb +40 -0
  56. data/lib/red_bird/stage.rb +60 -0
  57. data/lib/red_bird/tile_map.rb +118 -0
  58. data/lib/red_bird/tile_set.rb +56 -0
  59. data/lib/red_bird/uibox.rb +143 -0
  60. data/lib/red_bird/version.rb +3 -0
  61. data/lib/red_bird/vertical_menu.rb +110 -0
  62. data/red_bird.gemspec +37 -0
  63. metadata +149 -0
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_FONT_H
3
+ #define RED_BIRD_FONT_H 1
4
+
5
+ #include "main.h"
6
+
7
+ extern VALUE bird_cFont;
8
+
9
+ struct bird_font_data
10
+ {
11
+ TTF_Font *data;
12
+ };
13
+
14
+ VALUE
15
+ bird_alloc_font(VALUE klass);
16
+
17
+ VALUE
18
+ bird_cFont_initialize(VALUE self, VALUE file_path, VALUE size);
19
+
20
+ struct bird_font_data*
21
+ bird_cFont_get_data(VALUE self);
22
+
23
+ void
24
+ Init_red_bird_font(void);
25
+
26
+ #endif /* RED_BIRD_FONT_H */
@@ -0,0 +1,100 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include "input_device.h"
3
+
4
+ #include "engine.h"
5
+
6
+ VALUE bird_cInputDevice;
7
+
8
+ static VALUE sym_key_down;
9
+ static VALUE sym_key_up;
10
+ static VALUE sym_mouse_motion;
11
+ static VALUE sym_mouse_button_down;
12
+ static VALUE sym_mouse_button_up;
13
+ static VALUE sym_quit_game;
14
+ static VALUE sym_x;
15
+ static VALUE sym_y;
16
+
17
+ VALUE
18
+ bird_cInputDevice_get_events(VALUE self)
19
+ {
20
+ SDL_Event event;
21
+ int mouse_x;
22
+ int mouse_y;
23
+
24
+ VALUE type;
25
+ VALUE data;
26
+ VALUE x;
27
+ VALUE y;
28
+
29
+ rb_need_block();
30
+
31
+ // Get input
32
+ while(SDL_PollEvent(&event) != 0)
33
+ {
34
+ switch(event.type)
35
+ {
36
+ case SDL_KEYDOWN:
37
+ type = sym_key_down;
38
+ data = INT2NUM(event.key.keysym.sym);
39
+ break;
40
+ case SDL_KEYUP:
41
+ type = sym_key_up;
42
+ data = INT2NUM(event.key.keysym.sym);
43
+ break;
44
+ case SDL_MOUSEMOTION:
45
+ type = sym_mouse_motion;
46
+ SDL_GetMouseState(&mouse_x, &mouse_y);
47
+ x = INT2NUM(mouse_x);
48
+ y = INT2NUM(mouse_y);
49
+ data = rb_hash_new();
50
+ rb_hash_aset(data, sym_x, x);
51
+ rb_hash_aset(data, sym_y, y);
52
+ break;
53
+ case SDL_MOUSEBUTTONDOWN:
54
+ type = sym_mouse_button_down;
55
+ SDL_GetMouseState(&mouse_x, &mouse_y);
56
+ x = INT2NUM(mouse_x);
57
+ y = INT2NUM(mouse_y);
58
+ data = rb_hash_new();
59
+ rb_hash_aset(data, sym_x, x);
60
+ rb_hash_aset(data, sym_y, y);
61
+ break;
62
+ case SDL_MOUSEBUTTONUP:
63
+ type = sym_mouse_button_up;
64
+ SDL_GetMouseState(&mouse_x, &mouse_y);
65
+ x = INT2NUM(mouse_x);
66
+ y = INT2NUM(mouse_y);
67
+ data = rb_hash_new();
68
+ rb_hash_aset(data, sym_x, x);
69
+ rb_hash_aset(data, sym_y, y);
70
+ break;
71
+ case SDL_QUIT:
72
+ type = sym_quit_game;
73
+ data = Qnil;
74
+ break;
75
+ default:
76
+ continue;
77
+ }
78
+
79
+ rb_yield_values(2, type, data);
80
+ }
81
+
82
+ return self;
83
+ }
84
+
85
+ void
86
+ Init_red_bird_input_device(void)
87
+ {
88
+ sym_key_down = ID2SYM(rb_intern("key_down"));
89
+ sym_key_up = ID2SYM(rb_intern("key_up"));
90
+ sym_mouse_motion = ID2SYM(rb_intern("mouse_motion"));
91
+ sym_mouse_button_down = ID2SYM(rb_intern("mouse_button_down"));
92
+ sym_mouse_button_up = ID2SYM(rb_intern("mouse_button_up"));
93
+ sym_quit_game = ID2SYM(rb_intern("quit_game"));
94
+ sym_x = ID2SYM(rb_intern("x"));
95
+ sym_y = ID2SYM(rb_intern("y"));
96
+
97
+ bird_cInputDevice = rb_define_class_under(bird_m, "InputDevice", rb_cObject);
98
+ rb_define_protected_method(bird_cInputDevice, "get_events",
99
+ bird_cInputDevice_get_events, 0);
100
+ }
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_INPUT_DEVICE_H
3
+ #define RED_BIRD_INPUT_DEVICE_H 1
4
+
5
+ #include "main.h"
6
+
7
+ extern VALUE bird_cInputDevice;
8
+
9
+ VALUE
10
+ bird_cInputDevice_get_events(VALUE self);
11
+
12
+ void
13
+ Init_red_bird_input_device(void);
14
+
15
+ #endif /* RED_BIRD_INPUT_DEVICE_H */
@@ -0,0 +1,42 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include "keycode.h"
3
+
4
+ VALUE bird_mKeycode;
5
+
6
+ void
7
+ Init_red_bird_keycode(void)
8
+ {
9
+ bird_mKeycode = rb_define_module_under(bird_m, "Keycode");
10
+
11
+ rb_define_const(bird_mKeycode, "A", INT2NUM(SDLK_a));
12
+ rb_define_const(bird_mKeycode, "B", INT2NUM(SDLK_b));
13
+ rb_define_const(bird_mKeycode, "C", INT2NUM(SDLK_c));
14
+ rb_define_const(bird_mKeycode, "D", INT2NUM(SDLK_d));
15
+ rb_define_const(bird_mKeycode, "E", INT2NUM(SDLK_e));
16
+ rb_define_const(bird_mKeycode, "F", INT2NUM(SDLK_f));
17
+ rb_define_const(bird_mKeycode, "G", INT2NUM(SDLK_g));
18
+ rb_define_const(bird_mKeycode, "H", INT2NUM(SDLK_h));
19
+ rb_define_const(bird_mKeycode, "I", INT2NUM(SDLK_i));
20
+ rb_define_const(bird_mKeycode, "J", INT2NUM(SDLK_j));
21
+ rb_define_const(bird_mKeycode, "K", INT2NUM(SDLK_k));
22
+ rb_define_const(bird_mKeycode, "L", INT2NUM(SDLK_l));
23
+ rb_define_const(bird_mKeycode, "M", INT2NUM(SDLK_m));
24
+ rb_define_const(bird_mKeycode, "N", INT2NUM(SDLK_n));
25
+ rb_define_const(bird_mKeycode, "O", INT2NUM(SDLK_o));
26
+ rb_define_const(bird_mKeycode, "P", INT2NUM(SDLK_p));
27
+ rb_define_const(bird_mKeycode, "Q", INT2NUM(SDLK_q));
28
+ rb_define_const(bird_mKeycode, "R", INT2NUM(SDLK_r));
29
+ rb_define_const(bird_mKeycode, "S", INT2NUM(SDLK_s));
30
+ rb_define_const(bird_mKeycode, "T", INT2NUM(SDLK_t));
31
+ rb_define_const(bird_mKeycode, "U", INT2NUM(SDLK_u));
32
+ rb_define_const(bird_mKeycode, "V", INT2NUM(SDLK_v));
33
+ rb_define_const(bird_mKeycode, "W", INT2NUM(SDLK_w));
34
+ rb_define_const(bird_mKeycode, "X", INT2NUM(SDLK_x));
35
+ rb_define_const(bird_mKeycode, "Y", INT2NUM(SDLK_y));
36
+ rb_define_const(bird_mKeycode, "Z", INT2NUM(SDLK_z));
37
+
38
+ rb_define_const(bird_mKeycode, "KEY_UP", INT2NUM(SDLK_UP));
39
+ rb_define_const(bird_mKeycode, "KEY_DOWN", INT2NUM(SDLK_DOWN));
40
+ rb_define_const(bird_mKeycode, "KEY_LEFT", INT2NUM(SDLK_LEFT));
41
+ rb_define_const(bird_mKeycode, "KEY_RIGHT", INT2NUM(SDLK_RIGHT));
42
+ }
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_KEYCODE_H
3
+ #define RED_BIRD_KEYCODE_H 1
4
+
5
+ #include "main.h"
6
+
7
+ extern VALUE bird_mKeycode;
8
+
9
+ void
10
+ Init_red_bird_keycode(void);
11
+
12
+ #endif /* RED_BIRD_KEYCODE_H */
@@ -0,0 +1,154 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include <stdlib.h>
3
+
4
+ #include "loader.h"
5
+
6
+ static SDL_bool
7
+ LoaderStack_step_load(LoaderStack *self, int32_t step)
8
+ {
9
+ SDL_bool error = SDL_FALSE;
10
+
11
+ // Already loaded, nothing to do.
12
+ if(self->last_loaded >= step) return SDL_TRUE;
13
+
14
+ for(;self->last_loaded < step; self->last_loaded++)
15
+ {
16
+ error = !self->loaders[self->last_loaded].load(self->obj, self);
17
+ if(error) break;
18
+ }
19
+
20
+ // Self number will be one after the last loeaded after the loading loop is
21
+ // over.
22
+ self->last_loaded--;
23
+
24
+ if(error)
25
+ {
26
+ // Backward interaction to unload what was loaded.
27
+ for(;self->last_loaded >= 0; self->last_loaded--)
28
+ self->loaders[self->last_loaded].unload(self->obj, self);
29
+ self->last_loaded++;
30
+ }
31
+
32
+ return !error;
33
+ }
34
+
35
+ static void
36
+ LoaderStack_step_unload(LoaderStack *self, int32_t step)
37
+ {
38
+ // Already unloaded, nothing to do.
39
+ if(self->last_loaded <= step) return;
40
+
41
+ for(; self->last_loaded >= 0; self->last_loaded--)
42
+ self->loaders[self->last_loaded].unload(self->obj, self);
43
+
44
+ // Self number will be one before the last loeaded after the unloading loop
45
+ // is over.
46
+ self->last_loaded++;
47
+
48
+ self->is_loaded = SDL_FALSE;
49
+ }
50
+
51
+ void
52
+ LoaderStack_constructor(LoaderStack *self, void *obj)
53
+ {
54
+ self->is_loaded = SDL_FALSE;
55
+ self->last_loaded = 0;
56
+
57
+ self->loader_capacity = 10; // Totally arbitrary value.
58
+ self->loader_size = 0;
59
+
60
+ self->obj = obj;
61
+
62
+ self->error_message = 0;
63
+
64
+ self->loaders = malloc(sizeof(Loader) * self->loader_capacity);
65
+ }
66
+
67
+ void
68
+ LoaderStack_destructor(LoaderStack *self)
69
+ {
70
+ if(self->error_message != 0)
71
+ free(self->error_message);
72
+
73
+ if(self->is_loaded) LoaderStack_unload(self);
74
+ free(self->loaders);
75
+ }
76
+
77
+ void
78
+ LoaderStack_add(LoaderStack *self,
79
+ SDL_bool (*load)(void *obj, LoaderStack *ls),
80
+ void (*unload)(void *obj, LoaderStack *ls))
81
+ {
82
+ Loader *l;
83
+ uint32_t next_loader = self->loader_size;
84
+
85
+ // Expand if is full.
86
+ if(self->loader_size == self->loader_capacity)
87
+ {
88
+ self->loader_capacity += 5; // Totally arbitrary value.
89
+ self->loaders = realloc(self->loaders,
90
+ sizeof(Loader) * self->loader_capacity);
91
+ }
92
+
93
+ l = &(self->loaders[next_loader]);
94
+ l->load = load;
95
+ l->unload = unload;
96
+
97
+ self->loader_size++;
98
+ }
99
+
100
+ void
101
+ LoaderStack_set_error(LoaderStack *self, const char* base_error,
102
+ const char* current_error)
103
+ {
104
+ if(self->error_message != 0)
105
+ free(self->error_message);
106
+
107
+ if(current_error != NULL)
108
+ {
109
+ self->error_message =
110
+ malloc(strlen(base_error) + strlen(current_error) + 1);
111
+
112
+ strcpy(self->error_message, base_error);
113
+ strcat(self->error_message, current_error);
114
+ }
115
+ else
116
+ {
117
+ self->error_message = malloc(strlen(base_error));
118
+
119
+ strcpy(self->error_message, base_error);
120
+ }
121
+ }
122
+
123
+ SDL_bool
124
+ LoaderStack_load(LoaderStack *self)
125
+ {
126
+ if(self->is_loaded) return SDL_TRUE;
127
+
128
+ if(LoaderStack_step_load(self, self->loader_size))
129
+ {
130
+ self->is_loaded = SDL_TRUE;
131
+ return SDL_TRUE;
132
+ }
133
+ else
134
+ {
135
+ return SDL_FALSE;
136
+ }
137
+ }
138
+
139
+ SDL_bool
140
+ LoaderStack_reload(LoaderStack *self, int32_t step)
141
+ {
142
+ if(!self->is_loaded) return SDL_FALSE;
143
+
144
+ LoaderStack_step_unload(self, self->loader_size);
145
+ return LoaderStack_step_load(self, step);
146
+ }
147
+
148
+ void
149
+ LoaderStack_unload(LoaderStack *self)
150
+ {
151
+ if(!self->is_loaded) return;
152
+
153
+ LoaderStack_step_unload(self, 0);
154
+ }
@@ -0,0 +1,54 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_LOADER_H
3
+ #define RED_BIRD_LOADER_H 1
4
+
5
+ #include "main.h"
6
+
7
+ struct LoaderStack_s;
8
+
9
+ typedef struct
10
+ {
11
+ SDL_bool (*load)(void *obj, struct LoaderStack_s *ls);
12
+ void (*unload)(void *obj, struct LoaderStack_s *ls);
13
+ } Loader;
14
+
15
+ struct LoaderStack_s
16
+ {
17
+ void *obj;
18
+ SDL_bool is_loaded;
19
+ int32_t last_loaded;
20
+
21
+ uint32_t loader_capacity;
22
+ uint32_t loader_size;
23
+ Loader *loaders;
24
+
25
+ char* error_message;
26
+ };
27
+
28
+ typedef struct LoaderStack_s LoaderStack;
29
+
30
+ void
31
+ LoaderStack_constructor(LoaderStack *self, void *obj);
32
+
33
+ void
34
+ LoaderStack_destructor(LoaderStack *self);
35
+
36
+ void
37
+ LoaderStack_add(LoaderStack *self,
38
+ SDL_bool (*load)(void *obj, LoaderStack *ls),
39
+ void (*unload)(void *obj, LoaderStack *ls));
40
+
41
+ void
42
+ LoaderStack_set_error(LoaderStack *self, const char* base_error,
43
+ const char* current_error);
44
+
45
+ SDL_bool
46
+ LoaderStack_load(LoaderStack *self);
47
+
48
+ SDL_bool
49
+ LoaderStack_reload(LoaderStack *self, int32_t step);
50
+
51
+ void
52
+ LoaderStack_unload(LoaderStack *self);
53
+
54
+ #endif /* RED_BIRD_LOADER_H */
@@ -0,0 +1,38 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include "main.h"
3
+
4
+ #include "bird.h"
5
+ #include "color.h"
6
+ #include "dynamic_sprite.h"
7
+ #include "engine.h"
8
+ #include "font.h"
9
+ #include "keycode.h"
10
+ #include "input_device.h"
11
+ #include "palette.h"
12
+ #include "rect.h"
13
+ #include "render.h"
14
+ #include "sprite.h"
15
+ #include "texture.h"
16
+ #include "text.h"
17
+ #include "timer.h"
18
+
19
+ VALUE bird_m;
20
+
21
+ void
22
+ Init_red_bird(void)
23
+ {
24
+ Init_red_bird_module();
25
+ Init_red_bird_engine();
26
+ Init_red_bird_color();
27
+ Init_red_bird_dynamic_sprite();
28
+ Init_red_bird_font();
29
+ Init_red_bird_keycode();
30
+ Init_red_bird_input_device();
31
+ Init_red_bird_palette();
32
+ Init_red_bird_rect();
33
+ Init_red_bird_render();
34
+ Init_red_bird_sprite();
35
+ Init_red_bird_text();
36
+ Init_red_bird_texture();
37
+ Init_red_bird_timer();
38
+ }