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
|
#include "mruby-raylib/textures.h"
#include "mruby-raylib/types.h"
#include <raylib.h>
#include <mruby/class.h>
/* Create a new texture.
* @overload initialize(path:)
* @param path [String] File path to the texture to be loaded
* @return [Texture]
*/
static mrb_value
mrb_Texture_initialize(mrb_state* mrb, mrb_value self) {
char* path;
uint32_t kw_num = 1;
const mrb_sym kw_names[] = {
mrb_intern_lit(mrb, "path"),
};
mrb_value kw_values[kw_num];
const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL };
mrb_get_args(mrb, "|z:", &path, &kwargs);
if(!mrb_undef_p(kw_values[0])) {
path = mrb_str_to_cstr(mrb, kw_values[0]);
}
Texture *texture;
WRAPSTRUCT(Texture, Texture_type, self, texture);
*texture = LoadTexture(path);
mrb_data_init(self, texture, &Texture_type);
return self;
}
/*
* Get the width of a texture.
* @overload width
* @overload w
* @return [Integer]
*/
static mrb_value
mrb_Texture_get_width(mrb_state* mrb, mrb_value self) {
Texture *texture;
UNWRAPSTRUCT(Texture, Texture_type, self, texture);
return mrb_fixnum_value(texture->width);
}
/*
* Set the width of a texture.
* @overload width=(width)
* @param width [Integer] The new width
* @overload w=(width)
* @param w [Integer] The new width
* @return [Integer]
*/
static mrb_value
mrb_Texture_set_width(mrb_state* mrb, mrb_value self) {
Texture *texture;
UNWRAPSTRUCT(Texture, Texture_type, self, texture);
mrb_int width;
mrb_get_args(mrb, "i", &width);
texture->width = width;
return mrb_fixnum_value(texture->width);
}
/*
* Get the height of a texture.
* @overload height
* @overload h
* @return [Integer]
*/
static mrb_value
mrb_Texture_get_height(mrb_state* mrb, mrb_value self) {
Texture *texture;
UNWRAPSTRUCT(Texture, Texture_type, self, texture);
return mrb_fixnum_value(texture->height);
}
/*
* Set the height of a texture.
* @overload height=(height)
* @param height [Integer] The new height
* @overload h=(height)
* @param h [Integer] The new height
* @return [Integer]
*/
static mrb_value
mrb_Texture_set_height(mrb_state* mrb, mrb_value self) {
Texture *texture;
UNWRAPSTRUCT(Texture, Texture_type, self, texture);
mrb_int height;
mrb_get_args(mrb, "i", &height);
texture->height = height;
return mrb_fixnum_value(texture->height);
}
/*
* Get the id of a texture.
* @overload id
* @return [Integer]
*/
static mrb_value
mrb_Texture_get_id(mrb_state* mrb, mrb_value self) {
Texture *texture = DATA_GET_PTR(mrb, self, &Texture_type, Texture);
return mrb_fixnum_value(texture->id);
}
/*
* Get the mipmaps of a texture.
* @overload mipmaps
* @return [Integer]
*/
/*
static mrb_value
mrb_Texture_get_mipmaps(mrb_state* mrb, mrb_value self) {
Texture *texture = DATA_GET_PTR(mrb, self, &Texture_type, Texture);
return mrb_fixnum_value(texture->mipmaps);
}
*/
/*
* Get the format of a texture.
* @overload format
* @return [Integer]
*/
/*
static mrb_value
mrb_Texture_get_format(mrb_state* mrb, mrb_value self) {
Texture *texture = DATA_GET_PTR(mrb, self, &Texture_type, Texture);
return mrb_fixnum_value(texture->format);
}
*/
/*
* Draw the texture
* @overload draw(source: Rl::Rectangle.new(0,0,texture.width,texture.height), dest: Rl::Rectangle.new(0,0,texture.width,texture.height), origin: Rl::Vector2.default, rotation: 0, tint: Rl::Color.white)
* @param tint [Color] The color the drawn texture is tinted(white is 'no tint').
* @param source [Rectangle] Which section of the texture is to be drawn, the default is the entire texture.
* @param origin [Vector2] Offset of the drawn texture. Default is no offset.
* @param rotation [Float] How much the texture is rotated when drawn(In radians).
* @param dest [Rectangle] Where the texture is to be drawn on the screen(This can scale the texture). Default is the size of the texture.
* @return [Nil]
*/
static mrb_value
mrb_Texture_draw_texture(mrb_state* mrb, mrb_value self) {
struct RClass *raylib;// = mrb_module_get(mrb, "Raylib");
Rectangle source = {0};
Rectangle dest = {0};
float rotation = 0.0;
Texture *texture_data;
UNWRAPSTRUCT(Texture, Texture_type, self, texture_data);
//mrb_value tint_obj = mrb_funcall(mrb, mrb_obj_value(color), "white", 0);
//Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint
uint32_t kw_num = 7;
const mrb_sym kw_names[] = {
mrb_intern_lit(mrb, "source"),
mrb_intern_lit(mrb, "dest"),
mrb_intern_lit(mrb, "origin"),
mrb_intern_lit(mrb, "rotation"),
mrb_intern_lit(mrb, "tint"),
};
mrb_value kw_values[kw_num];
const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL };
mrb_get_args(mrb, "|:", &kwargs);
// if source defined
if (!(mrb_undef_p(kw_values[0]))) {
Rectangle *temp_rec;
UNWRAPSTRUCT(Rectangle, Rectangle_type, kw_values[0], temp_rec);
source = *temp_rec;
} else {
source = (struct Rectangle){ 0, 0, texture_data->width, texture_data->height };
}
// if dest defined
if (!(mrb_undef_p(kw_values[1]))) {
Rectangle *temp_rec;
UNWRAPSTRUCT(Rectangle, Rectangle_type, kw_values[1], temp_rec);
dest = *temp_rec;
} else {
dest = (struct Rectangle){ 0, 0, texture_data->width, texture_data->height };
}
// if origin undefined
if ((mrb_undef_p(kw_values[2]))) {
raylib = mrb_module_get(mrb, "Raylib");
struct RClass *vector2 = mrb_class_get_under(mrb, raylib, Vector2_type.struct_name);
kw_values[2] = mrb_funcall(mrb, mrb_obj_value(vector2), "default", 0);
}
// if rotation defined
if (!(mrb_undef_p(kw_values[3]))) {
rotation = mrb_as_float(mrb, kw_values[3]) / 0.017453;
}
// if color undefined
if ((mrb_undef_p(kw_values[4]))) {
raylib = mrb_module_get(mrb, "Raylib"); // needs to be called again or else segfault
struct RClass *color = mrb_class_get_under(mrb, raylib, Color_type.struct_name);
kw_values[4] = mrb_funcall(mrb, mrb_obj_value(color), "white", 0);
}
Color *tint_data;
UNWRAPSTRUCT(Color, Color_type, kw_values[4], tint_data);
Vector2 *vector2_data;
UNWRAPSTRUCT(Vector2, Vector2_type, kw_values[2], vector2_data);
//Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint
DrawTexturePro(*texture_data, source, dest, *vector2_data, rotation, *tint_data);
return mrb_nil_value();
}
void
mrb_init_raylib_textures(mrb_state* mrb) {
struct RClass *raylib = mrb_define_module(mrb, "Raylib");
struct RClass *texture_class = mrb_define_class_under(mrb, raylib, "Texture", mrb->object_class);
MRB_SET_INSTANCE_TT(texture_class, MRB_TT_DATA);
mrb_define_method(mrb, texture_class, "initialize", mrb_Texture_initialize, MRB_ARGS_REQ(1));
mrb_define_method(mrb, texture_class, "width", mrb_Texture_get_width, MRB_ARGS_NONE());
mrb_define_method(mrb, texture_class, "width=", mrb_Texture_set_width, MRB_ARGS_REQ(1));
//mrb_define_method(mrb, texture_class, "w", mrb_Texture_get_width, MRB_ARGS_NONE());
//mrb_define_method(mrb, texture_class, "w=", mrb_Texture_set_width, MRB_ARGS_REQ(1));
mrb_define_method(mrb, texture_class, "height", mrb_Texture_get_height, MRB_ARGS_NONE());
mrb_define_method(mrb, texture_class, "height=", mrb_Texture_set_height, MRB_ARGS_REQ(1));
//mrb_define_method(mrb, texture_class, "h", mrb_Texture_get_height, MRB_ARGS_NONE());
//mrb_define_method(mrb, texture_class, "h=", mrb_Texture_set_height, MRB_ARGS_REQ(1));
mrb_define_method(mrb, texture_class, "id", mrb_Texture_get_id, MRB_ARGS_NONE());
//mrb_define_method(mrb, texture_class, "mipmaps", mrb_Texture_get_mipmaps, MRB_ARGS_NONE());
//mrb_define_method(mrb, texture_class, "format", mrb_Texture_get_format, MRB_ARGS_NONE());
mrb_define_method(mrb, texture_class, "draw", mrb_Texture_draw_texture, MRB_ARGS_OPT(3));
}
|