red_bird 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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,25 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_RENDER_H
3
+ #define RED_BIRD_RENDER_H 1
4
+
5
+ #include "main.h"
6
+
7
+ extern VALUE bird_mRender;
8
+
9
+ VALUE
10
+ bird_mRender_set_color(VALUE self, VALUE color);
11
+
12
+ VALUE
13
+ bird_mRender_fill_rect(VALUE self, VALUE x, VALUE y,
14
+ VALUE width, VALUE height);
15
+
16
+ VALUE
17
+ bird_mRender_clear_screen(VALUE self);
18
+
19
+ VALUE
20
+ bird_mRender_update_screen(VALUE self);
21
+
22
+ void
23
+ Init_red_bird_render(void);
24
+
25
+ #endif /* RED_BIRD_RENDER_H */
@@ -0,0 +1,130 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include "sprite.h"
3
+
4
+ #include "engine.h"
5
+ #include "texture.h"
6
+
7
+ VALUE bird_cSprite;
8
+
9
+ static ID id_at_texture;
10
+
11
+ /*
12
+ Basic functions that all Ruby classes need.
13
+ */
14
+
15
+ void
16
+ bird_free_sprite(void* obj)
17
+ {
18
+ struct bird_sprite_data *ptr = obj;
19
+
20
+ free(ptr);
21
+ }
22
+
23
+ size_t
24
+ bird_memsize_sprite(const void* obj)
25
+ {
26
+ // TODO
27
+ return 0;
28
+ }
29
+
30
+ static const rb_data_type_t
31
+ bird_sprite_type = {
32
+ "red_bird_sprite",
33
+ {0, bird_free_sprite, bird_memsize_sprite,},
34
+ 0, 0,
35
+ RUBY_TYPED_FREE_IMMEDIATELY,
36
+ };
37
+
38
+ VALUE
39
+ bird_alloc_sprite(VALUE klass)
40
+ {
41
+ VALUE obj;
42
+ struct bird_sprite_data *ptr;
43
+
44
+ obj = TypedData_Make_Struct(klass, struct bird_sprite_data,
45
+ &bird_sprite_type, ptr);
46
+
47
+ return obj;
48
+ }
49
+
50
+ /*
51
+ @param texture [RedBird::Texture] image to use as source for this sprite.
52
+ @param x [Integer] vertical position in the texture.
53
+ @param y [Integer] horizontal position in the texture.
54
+ @param width [Integer] width of the sprite.
55
+ @param height [Integer] height of the sprite.
56
+ @return [RedBird::Sprite] self
57
+ @author Frederico Linhares
58
+ */
59
+ VALUE
60
+ bird_cSprite_initialize(VALUE self, VALUE texture, VALUE x, VALUE y,
61
+ VALUE width, VALUE height)
62
+ {
63
+ struct bird_sprite_data *ptr;
64
+
65
+ if(!rb_obj_is_kind_of(texture, bird_cTexture))
66
+ rb_raise(rb_eTypeError, "%s",
67
+ "texture must be an instance of RedBird::Texture");
68
+ RB_INTEGER_TYPE_P(x);
69
+ RB_INTEGER_TYPE_P(y);
70
+ RB_INTEGER_TYPE_P(width);
71
+ RB_INTEGER_TYPE_P(height);
72
+
73
+ TypedData_Get_Struct(self, struct bird_sprite_data, &bird_sprite_type, ptr);
74
+
75
+ rb_ivar_set(self, id_at_texture, texture);
76
+ ptr->rect.x = NUM2INT(x);
77
+ ptr->rect.y = NUM2INT(y);
78
+ ptr->rect.w = NUM2INT(width);
79
+ ptr->rect.h = NUM2INT(height);
80
+
81
+ return self;
82
+ }
83
+
84
+ /*
85
+ Render this sprite to the screen.
86
+
87
+ @param x [Integer] screen horizontal position.
88
+ @param y [Integer] screen vertical position.
89
+ @return [RedBird::Sprite] self
90
+ @author Frederico Linhares
91
+ */
92
+ VALUE
93
+ bird_cSprite_render_to_screen(VALUE self, VALUE x, VALUE y)
94
+ {
95
+ SDL_Rect dst_rect;
96
+
97
+ struct bird_sprite_data *ptr;
98
+ struct bird_texture_data *texture_ptr;
99
+
100
+ VALUE texture;
101
+
102
+ RB_INTEGER_TYPE_P(x);
103
+ RB_INTEGER_TYPE_P(y);
104
+
105
+ texture = rb_ivar_get(self, id_at_texture);
106
+
107
+ TypedData_Get_Struct(self, struct bird_sprite_data, &bird_sprite_type, ptr);
108
+ texture_ptr = bird_cTexture_get_data(texture);
109
+
110
+ dst_rect.x = NUM2INT(x);
111
+ dst_rect.y = NUM2INT(y);
112
+ dst_rect.w = ptr->rect.w;
113
+ dst_rect.h = ptr->rect.h;
114
+
115
+ SDL_RenderCopy(bird_core.renderer, texture_ptr->data, &ptr->rect,
116
+ &dst_rect);
117
+
118
+ return self;
119
+ }
120
+
121
+ void
122
+ Init_red_bird_sprite(void)
123
+ {
124
+ id_at_texture = rb_intern("@texture");
125
+ bird_cSprite = rb_define_class_under(bird_m, "Sprite", rb_cData);
126
+ rb_define_alloc_func(bird_cSprite, bird_alloc_sprite);
127
+ rb_define_method(bird_cSprite, "initialize", bird_cSprite_initialize, 5);
128
+ rb_define_method(bird_cSprite, "render_to_screen",
129
+ bird_cSprite_render_to_screen, 2);
130
+ }
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_SPRITE_H
3
+ #define RED_BIRD_SPRITE_H 1
4
+
5
+ #include "main.h"
6
+
7
+ extern VALUE bird_cSprite;
8
+
9
+ struct bird_sprite_data
10
+ {
11
+ SDL_Rect rect;
12
+ };
13
+
14
+ VALUE
15
+ bird_alloc_sprite(VALUE klass);
16
+
17
+ VALUE
18
+ bird_cSprite_initialize(VALUE self, VALUE texture, VALUE x, VALUE y,
19
+ VALUE width, VALUE height);
20
+
21
+ VALUE
22
+ bird_cSprite_render_to_screen(VALUE self, VALUE x, VALUE y);
23
+
24
+ void
25
+ Init_red_bird_sprite(void);
26
+
27
+ #endif // RED_BIRD_SPRITE_H
@@ -0,0 +1,212 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include "text.h"
3
+
4
+ #include "color.h"
5
+ #include "engine.h"
6
+ #include "font.h"
7
+
8
+ /*
9
+ Document-class: RedBird::Text
10
+
11
+ A text to be printed on the screen. This class renders the text received
12
+ during the initialization as an image and store the image internally for
13
+ rendering.
14
+
15
+ @author Frederico Linhares
16
+ */
17
+ VALUE bird_cText;
18
+
19
+ /*
20
+ Basic functions all Ruby classes need.
21
+ */
22
+
23
+ static void
24
+ bird_free_text(void* obj)
25
+ {
26
+ struct bird_text_data *ptr = obj;
27
+
28
+ SDL_DestroyTexture(ptr->data);
29
+ free(ptr);
30
+ }
31
+
32
+ static size_t
33
+ bird_memsize_text(const void* obj)
34
+ {
35
+ // TODO
36
+ return 0;
37
+ }
38
+
39
+ static const rb_data_type_t
40
+ bird_text_type = {
41
+ "red_bird_text",
42
+ {0, bird_free_text, bird_memsize_text,},
43
+ 0, 0,
44
+ RUBY_TYPED_FREE_IMMEDIATELY,
45
+ };
46
+
47
+ static VALUE
48
+ bird_alloc_text(VALUE klass)
49
+ {
50
+ VALUE obj;
51
+ struct bird_text_data *ptr;
52
+
53
+ obj = TypedData_Make_Struct(klass, struct bird_text_data, &bird_text_type,
54
+ ptr);
55
+
56
+ return obj;
57
+ }
58
+
59
+ /*
60
+ @param text [String] text to rendered.
61
+ @param font [RedBird::Font] font to be used for rendering the text.
62
+ @param tx_color [RedBird::Color] text color.
63
+ @param bg_color [RedBird::Color, nil] if the attribute is a color, this color
64
+ will be used as background; if the attribute is nil, there will be no
65
+ background.
66
+ @author Frederico Linhares
67
+ */
68
+ VALUE
69
+ bird_cText_initialize(VALUE self, VALUE text, VALUE font, VALUE tx_color,
70
+ VALUE bg_color)
71
+ {
72
+ SDL_Surface *surface = NULL;
73
+
74
+ struct bird_text_data *ptr;
75
+ struct bird_font_data *font_ptr;
76
+ struct bird_color_data *tx_color_ptr;
77
+ struct bird_color_data *bg_color_ptr;
78
+
79
+ if(!engine_initialized)
80
+ rb_raise(rb_eRuntimeError, "%s",
81
+ "can not create a RedBird::Text instance before RedBird::Engine "
82
+ "is started");
83
+
84
+ SafeStringValue(text);
85
+
86
+ if(!rb_obj_is_kind_of(font, bird_cFont))
87
+ {
88
+ rb_raise(rb_eTypeError, "%s", "font must be an instance of RedBird::Font");
89
+ return self;
90
+ }
91
+
92
+ if(!rb_obj_is_kind_of(tx_color, bird_cColor))
93
+ {
94
+ rb_raise(rb_eTypeError, "%s",
95
+ "tx_color must be an instance of RedBird::Color");
96
+ return self;
97
+ }
98
+
99
+ font_ptr = bird_cFont_get_data(font);
100
+ tx_color_ptr = bird_cColor_get_data(tx_color);
101
+ if(TYPE(bg_color) == T_NIL)
102
+ {
103
+ surface = TTF_RenderUTF8_Solid(font_ptr->data, StringValueCStr(text),
104
+ tx_color_ptr->data);
105
+ }
106
+ else if(rb_obj_is_kind_of(bg_color, bird_cColor))
107
+ {
108
+ bg_color_ptr = bird_cColor_get_data(bg_color);
109
+ surface = TTF_RenderUTF8_Shaded(font_ptr->data, StringValueCStr(text),
110
+ tx_color_ptr->data, bg_color_ptr->data);
111
+ }
112
+ else
113
+ {
114
+ rb_raise(rb_eTypeError, "%s",
115
+ "bg_color must be an instance of RedBird::Color or nil");
116
+ return self;
117
+ }
118
+
119
+ if(surface == NULL)
120
+ {
121
+ rb_raise(rb_eRuntimeError, "failed to render text: %s", TTF_GetError());
122
+ return self;
123
+ }
124
+
125
+ TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);
126
+
127
+ ptr->data = NULL;
128
+ ptr->data = SDL_CreateTextureFromSurface(bird_core.renderer, surface);
129
+ SDL_FreeSurface(surface);
130
+ if(ptr->data == NULL)
131
+ {
132
+ rb_raise(rb_eRuntimeError, "failed to convert text: %s", SDL_GetError());
133
+ return self;
134
+ }
135
+
136
+ SDL_QueryTexture(ptr->data, NULL, NULL, &ptr->width, &ptr->height);
137
+ return self;
138
+ }
139
+
140
+ /*
141
+ Returns the width of the image.
142
+
143
+ @return [Integer] text width.
144
+ @author Frederico Linhares
145
+ */
146
+ VALUE
147
+ bird_cText_width(VALUE self)
148
+ {
149
+ struct bird_text_data *ptr;
150
+
151
+ TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);
152
+
153
+ return INT2FIX(ptr->width);
154
+ }
155
+
156
+ /*
157
+ Returns the height of the image.
158
+
159
+ @return [Integer] text height.
160
+ @author Frederico Linhares
161
+ */
162
+ VALUE
163
+ bird_cText_height(VALUE self)
164
+ {
165
+ struct bird_text_data *ptr;
166
+
167
+ TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);
168
+
169
+ return INT2FIX(ptr->height);
170
+ }
171
+
172
+ /*
173
+ Render this text to the screen.
174
+
175
+ @param x [Integer] screen horizontal position.
176
+ @param y [Integer] screen vertical position.
177
+ @return [RedBird::Sprite] self
178
+ @author Frederico Linhares
179
+ */
180
+ VALUE
181
+ bird_cText_render_to_screen(VALUE self, VALUE x, VALUE y)
182
+ {
183
+ SDL_Rect dst_rect;
184
+
185
+ struct bird_text_data *ptr;
186
+
187
+ RB_INTEGER_TYPE_P(x);
188
+ RB_INTEGER_TYPE_P(y);
189
+
190
+ TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);
191
+
192
+ dst_rect.x = NUM2INT(x);
193
+ dst_rect.y = NUM2INT(y);
194
+ dst_rect.w = ptr->width;
195
+ dst_rect.h = ptr->height;
196
+
197
+ SDL_RenderCopy(bird_core.renderer, ptr->data, NULL, &dst_rect);
198
+
199
+ return self;
200
+ }
201
+
202
+ void
203
+ Init_red_bird_text(void)
204
+ {
205
+ bird_cText = rb_define_class_under(bird_m, "Text", rb_cData);
206
+ rb_define_alloc_func(bird_cText, bird_alloc_text);
207
+ rb_define_method(bird_cText, "initialize", bird_cText_initialize, 4);
208
+ rb_define_method(bird_cText, "width", bird_cText_width, 0);
209
+ rb_define_method(bird_cText, "height", bird_cText_height, 0);
210
+ rb_define_method(bird_cText, "render_to_screen",
211
+ bird_cText_render_to_screen, 2);
212
+ }
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #ifndef RED_BIRD_TEXT_H
3
+ #define RED_BIRD_TEXT_H 1
4
+
5
+ #include "main.h"
6
+
7
+ extern VALUE bird_cText;
8
+
9
+ struct bird_text_data
10
+ {
11
+ SDL_Texture* data;
12
+ int width, height;
13
+ };
14
+
15
+ VALUE
16
+ bird_cText_initialize(VALUE self, VALUE text, VALUE font, VALUE tx_color,
17
+ VALUE bg_color);
18
+
19
+ VALUE
20
+ bird_cText_width(VALUE self);
21
+
22
+ VALUE
23
+ bird_cText_height(VALUE self);
24
+
25
+ VALUE
26
+ bird_cText_render_to_screen(VALUE self, VALUE x, VALUE y);
27
+
28
+ void
29
+ Init_red_bird_text(void);
30
+
31
+ #endif /* RED_BIRD_TEXT_H */
@@ -0,0 +1,157 @@
1
+ // SPDX-License-Identifier: MIT
2
+ #include "texture.h"
3
+ #include "texture_imp.hpp"
4
+
5
+ #include "engine.h"
6
+ #include "palette.h"
7
+
8
+ /*
9
+ Document-class: RedBird::Texture
10
+
11
+ An image optimized for rendering on the screen.
12
+
13
+ @author Frederico Linhares
14
+ */
15
+ VALUE bird_cTexture;
16
+
17
+ /*
18
+ Basic functions all Ruby classes need.
19
+ */
20
+
21
+ void
22
+ bird_free_texture(void* obj)
23
+ {
24
+ struct bird_texture_data *ptr = obj;
25
+
26
+ SDL_DestroyTexture(ptr->data);
27
+ free(ptr);
28
+ }
29
+
30
+ size_t
31
+ bird_memsize_texture(const void* obj)
32
+ {
33
+ // TODO
34
+ return 0;
35
+ }
36
+
37
+ static const rb_data_type_t
38
+ bird_texture_type = {
39
+ "red_bird_texture",
40
+ {0, bird_free_texture, bird_memsize_texture,},
41
+ 0, 0,
42
+ RUBY_TYPED_FREE_IMMEDIATELY,
43
+ };
44
+
45
+ VALUE
46
+ bird_alloc_texture(VALUE klass)
47
+ {
48
+ VALUE obj;
49
+ struct bird_texture_data *ptr;
50
+
51
+ obj = TypedData_Make_Struct(klass, struct bird_texture_data,
52
+ &bird_texture_type, ptr);
53
+
54
+ return obj;
55
+ }
56
+
57
+ /*
58
+ @param file_path [String] path to an image to be loaded into a texture.
59
+ @author Frederico Linhares
60
+ */
61
+ VALUE
62
+ bird_cTexture_initialize(VALUE self, VALUE file_path, VALUE palette)
63
+ {
64
+ struct bird_texture_data *ptr;
65
+ struct bird_palette_data *palette_data;
66
+ struct bird_cTexture_PGM pgm_img;
67
+ SDL_Surface *surface = NULL;
68
+
69
+ if(!engine_initialized)
70
+ rb_raise(rb_eRuntimeError, "%s",
71
+ "can not create a RedBird::Texture instance before "
72
+ "RedBird::Engine is started");
73
+
74
+ if(!rb_obj_is_kind_of(palette, bird_cPalette))
75
+ rb_raise(
76
+ rb_eArgError, "%s",
77
+ "palette must be and instance of RedBird::Palette");
78
+
79
+ SafeStringValue(file_path);
80
+ if(!bird_cTexture_PGM_load(&pgm_img, StringValueCStr(file_path)))
81
+ rb_raise(
82
+ rb_eArgError, "%s",
83
+ "failed to load image: it does not exists or is in an invalid format");
84
+
85
+ // Depth = 8 bits image.
86
+ // Pitch = 1 byte (8 bits) * image width.
87
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(
88
+ pgm_img.data, pgm_img.width, pgm_img.height, 8, pgm_img.width,
89
+ SDL_PIXELFORMAT_INDEX8);
90
+ bird_cTexture_PGM_unload(&pgm_img);
91
+ if(surface == NULL)
92
+ rb_raise(rb_eArgError, "failed to create surface: %s\n", SDL_GetError());
93
+
94
+ TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
95
+ ptr);
96
+ palette_data = bird_cPalette_get_data(palette);
97
+ SDL_SetPaletteColors(surface->format->palette, palette_data->colors, 0, 256);
98
+
99
+ ptr->data = SDL_CreateTextureFromSurface(bird_core.renderer, surface);
100
+ SDL_FreeSurface(surface);
101
+ if(ptr->data == NULL)
102
+ rb_raise(rb_eArgError, "failed to convert image: %s\n", SDL_GetError());
103
+
104
+ SDL_QueryTexture(ptr->data, NULL, NULL, &ptr->width, &ptr->height);
105
+ return self;
106
+ }
107
+
108
+ /*
109
+ @return [Integer]
110
+ @author Frederico Linhares
111
+ */
112
+ VALUE
113
+ bird_cTexture_width(VALUE self)
114
+ {
115
+ struct bird_texture_data *ptr;
116
+
117
+ TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
118
+ ptr);
119
+
120
+ return INT2FIX(ptr->width);
121
+ }
122
+
123
+ /*
124
+ @return [Integer]
125
+ @author Frederico Linhares
126
+ */
127
+ VALUE
128
+ bird_cTexture_height(VALUE self)
129
+ {
130
+ struct bird_texture_data *ptr;
131
+
132
+ TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
133
+ ptr);
134
+
135
+ return INT2FIX(ptr->height);
136
+ }
137
+
138
+ struct bird_texture_data*
139
+ bird_cTexture_get_data(VALUE self)
140
+ {
141
+ struct bird_texture_data *ptr;
142
+
143
+ TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
144
+ ptr);
145
+
146
+ return ptr;
147
+ }
148
+
149
+ void
150
+ Init_red_bird_texture(void)
151
+ {
152
+ bird_cTexture = rb_define_class_under(bird_m, "Texture", rb_cData);
153
+ rb_define_alloc_func(bird_cTexture, bird_alloc_texture);
154
+ rb_define_method(bird_cTexture, "initialize", bird_cTexture_initialize, 2);
155
+ rb_define_method(bird_cTexture, "width", bird_cTexture_width, 0);
156
+ rb_define_method(bird_cTexture, "height", bird_cTexture_height, 0);
157
+ }