ray 0.0.1 → 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. data/.gemtest +0 -0
  2. data/.yardopts +4 -0
  3. data/README.md +17 -21
  4. data/Rakefile +18 -139
  5. data/VERSION +1 -1
  6. data/ext/audio.cpp +723 -0
  7. data/ext/{color.c → color.cpp} +25 -13
  8. data/ext/drawable.cpp +91 -0
  9. data/ext/event.cpp +460 -0
  10. data/ext/extconf.rb +5 -104
  11. data/ext/font.cpp +190 -0
  12. data/ext/image.cpp +733 -0
  13. data/ext/input.cpp +74 -0
  14. data/ext/ray.cpp +168 -0
  15. data/ext/ray.hpp +356 -0
  16. data/ext/{rect.c → rect.cpp} +51 -37
  17. data/ext/shader.cpp +169 -0
  18. data/ext/shape.cpp +409 -0
  19. data/ext/sprite.cpp +306 -0
  20. data/ext/text.cpp +181 -0
  21. data/ext/vector.cpp +215 -0
  22. data/guide.md +619 -0
  23. data/lib/ray/audio.rb +0 -41
  24. data/lib/ray/color.rb +32 -10
  25. data/lib/ray/drawable.rb +16 -0
  26. data/lib/ray/dsl/event_listener.rb +25 -2
  27. data/lib/ray/dsl/event_runner.rb +33 -5
  28. data/lib/ray/dsl/event_translator.rb +66 -30
  29. data/lib/ray/dsl/handler.rb +3 -2
  30. data/lib/ray/dsl/matcher.rb +58 -14
  31. data/lib/ray/font.rb +38 -96
  32. data/lib/ray/font_set.rb +8 -8
  33. data/lib/ray/game.rb +87 -66
  34. data/lib/ray/helper.rb +105 -10
  35. data/lib/ray/image.rb +150 -24
  36. data/lib/ray/image_set.rb +3 -1
  37. data/lib/ray/input.rb +10 -0
  38. data/lib/ray/music_set.rb +5 -3
  39. data/lib/ray/ray.rb +21 -9
  40. data/lib/ray/rect.rb +48 -7
  41. data/lib/ray/rmagick.rb +41 -0
  42. data/lib/ray/scene.rb +99 -43
  43. data/lib/ray/scene_list.rb +67 -0
  44. data/lib/ray/shape.rb +132 -0
  45. data/lib/ray/sound_set.rb +4 -2
  46. data/lib/ray/sprite.rb +49 -111
  47. data/lib/ray/text.rb +101 -0
  48. data/lib/ray/text_helper.rb +37 -0
  49. data/lib/ray/turtle.rb +215 -0
  50. data/lib/ray/vector.rb +226 -0
  51. data/samples/audio/spacial.rb +44 -0
  52. data/samples/hello_world/hello.rb +9 -13
  53. data/samples/hello_world/hello_dsl.rb +8 -12
  54. data/samples/hello_world/text.rb +15 -0
  55. data/samples/opengl/binding.rb +38 -0
  56. data/samples/opengl/image.rb +32 -0
  57. data/samples/opengl/opengl.rb +34 -0
  58. data/samples/opengl/shader.rb +42 -0
  59. data/samples/pong/pong.rb +14 -10
  60. data/samples/run_scene.rb +53 -0
  61. data/samples/shaders/scene.rb +40 -0
  62. data/samples/shaders/shaders.rb +42 -0
  63. data/samples/shaders/shape.rb +34 -0
  64. data/samples/sokoban/sokoban.rb +18 -18
  65. data/samples/test/actual_scene.rb +41 -0
  66. data/samples/test/scene_riot.rb +39 -0
  67. data/samples/test/scene_spec.rb +32 -0
  68. data/samples/test/scene_test_unit.rb +25 -0
  69. data/samples/turtle/byzantium.rb +45 -0
  70. data/samples/turtle/hilbert.rb +48 -0
  71. data/samples/turtle/koch.rb +55 -0
  72. data/samples/turtle/mandala.rb +61 -0
  73. data/samples/turtle/tree.rb +57 -0
  74. data/test/audio_test.rb +69 -0
  75. data/test/color_test.rb +77 -0
  76. data/test/drawable_test.rb +19 -0
  77. data/test/dsl_test.rb +93 -0
  78. data/test/font_test.rb +57 -0
  79. data/test/helpers.rb +94 -0
  80. data/test/image_test.rb +82 -0
  81. data/test/ray_test.rb +25 -0
  82. data/test/rect_test.rb +121 -0
  83. data/{spec → test}/res/VeraMono.ttf +0 -0
  84. data/{spec → test}/res/aqua.bmp +0 -0
  85. data/{spec → test}/res/aqua.png +0 -0
  86. data/{spec → test}/res/aqua2.bmp +0 -0
  87. data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
  88. data/{spec → test}/res/pop.wav +0 -0
  89. data/test/resource_set_test.rb +99 -0
  90. data/test/run_all.rb +7 -0
  91. data/test/shape_test.rb +101 -0
  92. data/test/sprite_test.rb +89 -0
  93. data/test/text_test.rb +78 -0
  94. data/test/turtle_test.rb +176 -0
  95. data/test/vector_test.rb +111 -0
  96. data/yard_ext.rb +0 -28
  97. metadata +95 -139
  98. data/.gitignore +0 -23
  99. data/.gitmodules +0 -3
  100. data/.rspec +0 -3
  101. data/ext/audio.c +0 -473
  102. data/ext/event.c +0 -557
  103. data/ext/font.c +0 -287
  104. data/ext/image.c +0 -933
  105. data/ext/joystick.c +0 -145
  106. data/ext/ray.c +0 -489
  107. data/ext/ray.h +0 -245
  108. data/ext/ray_osx.m +0 -161
  109. data/lib/ray/joystick.rb +0 -30
  110. data/psp/SDL_psp_main.c +0 -84
  111. data/psp/bigdecimal/README +0 -60
  112. data/psp/bigdecimal/bigdecimal.c +0 -4697
  113. data/psp/bigdecimal/bigdecimal.h +0 -216
  114. data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
  115. data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
  116. data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
  117. data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
  118. data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
  119. data/psp/digest/bubblebabble/bubblebabble.c +0 -142
  120. data/psp/digest/defs.h +0 -20
  121. data/psp/digest/digest.c +0 -643
  122. data/psp/digest/digest.h +0 -32
  123. data/psp/digest/lib/digest.rb +0 -50
  124. data/psp/digest/lib/md5.rb +0 -27
  125. data/psp/digest/lib/sha1.rb +0 -27
  126. data/psp/digest/md5/md5.c +0 -420
  127. data/psp/digest/md5/md5.h +0 -80
  128. data/psp/digest/md5/md5init.c +0 -40
  129. data/psp/digest/rmd160/rmd160.c +0 -457
  130. data/psp/digest/rmd160/rmd160.h +0 -56
  131. data/psp/digest/rmd160/rmd160init.c +0 -40
  132. data/psp/digest/sha1/sha1.c +0 -269
  133. data/psp/digest/sha1/sha1.h +0 -39
  134. data/psp/digest/sha1/sha1init.c +0 -40
  135. data/psp/digest/sha2/lib/sha2.rb +0 -73
  136. data/psp/digest/sha2/sha2.c +0 -919
  137. data/psp/digest/sha2/sha2.h +0 -109
  138. data/psp/digest/sha2/sha2init.c +0 -52
  139. data/psp/enumerator/enumerator.c +0 -298
  140. data/psp/etc/etc.c +0 -559
  141. data/psp/ext.c +0 -289
  142. data/psp/fcntl/fcntl.c +0 -187
  143. data/psp/lib/rbconfig.rb +0 -178
  144. data/psp/nkf/lib/kconv.rb +0 -367
  145. data/psp/nkf/nkf-utf8/config.h +0 -88
  146. data/psp/nkf/nkf-utf8/nkf.c +0 -6040
  147. data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
  148. data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
  149. data/psp/nkf/nkf.c +0 -654
  150. data/psp/socket/addrinfo.h +0 -173
  151. data/psp/socket/getaddrinfo.c +0 -676
  152. data/psp/socket/getnameinfo.c +0 -270
  153. data/psp/socket/pspsocket.c +0 -71
  154. data/psp/socket/pspsocket.h +0 -28
  155. data/psp/socket/socket.c +0 -4662
  156. data/psp/socket/sockport.h +0 -76
  157. data/psp/stringio/stringio.c +0 -1306
  158. data/psp/strscan/strscan.c +0 -1320
  159. data/psp/syck/bytecode.c +0 -1166
  160. data/psp/syck/emitter.c +0 -1242
  161. data/psp/syck/gram.c +0 -1894
  162. data/psp/syck/gram.h +0 -79
  163. data/psp/syck/handler.c +0 -174
  164. data/psp/syck/implicit.c +0 -2990
  165. data/psp/syck/node.c +0 -408
  166. data/psp/syck/rubyext.c +0 -2367
  167. data/psp/syck/syck.c +0 -504
  168. data/psp/syck/syck.h +0 -456
  169. data/psp/syck/token.c +0 -2725
  170. data/psp/syck/yaml2byte.c +0 -257
  171. data/psp/syck/yamlbyte.h +0 -170
  172. data/psp/thread/thread.c +0 -1175
  173. data/psp/zlib/zlib.c +0 -3547
  174. data/script.rb +0 -10
  175. data/spec/ray/audio_spec.rb +0 -146
  176. data/spec/ray/color_spec.rb +0 -57
  177. data/spec/ray/event_spec.rb +0 -80
  178. data/spec/ray/font_spec.rb +0 -93
  179. data/spec/ray/image_set_spec.rb +0 -48
  180. data/spec/ray/image_spec.rb +0 -162
  181. data/spec/ray/joystick_spec.rb +0 -21
  182. data/spec/ray/matcher_spec.rb +0 -50
  183. data/spec/ray/ray_spec.rb +0 -88
  184. data/spec/ray/rect_spec.rb +0 -154
  185. data/spec/ray/resource_set_spec.rb +0 -105
  186. data/spec/ray/sprite_spec.rb +0 -163
  187. data/spec/spec.opts +0 -4
  188. data/spec/spec_helper.rb +0 -8
@@ -1,14 +1,13 @@
1
- #include "ray.h"
1
+ #include "ray.hpp"
2
2
 
3
3
  VALUE ray_cRect = Qnil;
4
4
 
5
5
  void ray_free_rect(ray_rect *ptr) {
6
- free(ptr);
6
+ delete ptr;
7
7
  }
8
8
 
9
- VALUE ray_rect2rb(ray_rect rect) {
10
- ray_rect *ptr = malloc(sizeof(ray_rect));
11
- memcpy(ptr, &rect, sizeof(ray_rect));
9
+ VALUE ray_rect2rb(const ray_rect &rect) {
10
+ ray_rect *ptr = new ray_rect(rect);
12
11
 
13
12
  VALUE obj = Data_Wrap_Struct(ray_cRect, NULL, ray_free_rect, ptr);
14
13
  return obj;
@@ -29,20 +28,14 @@ ray_rect ray_rb2rect(VALUE obj) {
29
28
  ray_rect ray_convert_to_rect(VALUE obj) {
30
29
  if (RAY_IS_A(obj, ray_cRect))
31
30
  return ray_rb2rect(obj);
32
- else if (RAY_IS_A(obj, rb_cArray))
33
- return ray_rb2rect(rb_apply(ray_cRect, RAY_METH("new"), obj));
34
31
  else {
35
- rb_raise(rb_eTypeError, "Can't convert %s into Ray::Rect",
36
- RAY_OBJ_CLASSNAME(obj));
32
+ obj = rb_funcall(obj, RAY_METH("to_rect"), 0);
33
+ return ray_rb2rect(obj);
37
34
  }
38
-
39
- /* Should never happen */
40
- ray_rect empty_rect = {0, 0, 0, 0};
41
- return empty_rect;
42
35
  }
43
36
 
44
37
  VALUE ray_alloc_rect(VALUE self) {
45
- ray_rect *rect = malloc(sizeof(ray_rect));
38
+ ray_rect *rect = new ray_rect;
46
39
  VALUE ret = Data_Wrap_Struct(self, 0, ray_free_rect, rect);
47
40
 
48
41
  return ret;
@@ -58,10 +51,13 @@ VALUE ray_alloc_rect(VALUE self) {
58
51
  @overload initialize(hash)
59
52
  Creates a new rect according to the keys specified in hash.
60
53
 
61
- @option hash [Integer] :x
62
- @option hash [Integer] :y
63
- @option hash [Integer, optional] :width
64
- @option hash [Integer, optional] :height required if width is set
54
+ @option hash [Float] :x
55
+ @option hash [Float] :y
56
+ @option hash [Float, optional] :width
57
+ @option hash [Float, optional] :height required if width is set
58
+
59
+ @option hash [Float, optional] :w alias for :width
60
+ @option hash [Float, optional] :h alias for :height
65
61
  */
66
62
  VALUE ray_init_rect(int argc, VALUE *argv, VALUE self) {
67
63
  VALUE x_or_hash, y, w, h;
@@ -106,51 +102,60 @@ VALUE ray_init_rect(int argc, VALUE *argv, VALUE self) {
106
102
  ray_rect *rect;
107
103
  Data_Get_Struct(self, ray_rect, rect);
108
104
 
109
- rect->x = NUM2INT(x_or_hash);
110
- rect->y = NUM2INT(y);
105
+ rect->Left = NUM2DBL(x_or_hash);
106
+ rect->Top = NUM2DBL(y);
111
107
 
112
108
  if (!NIL_P(w)) {
113
- rect->w = NUM2INT(w);
114
- rect->h = NUM2INT(h);
109
+ rect->Width = NUM2DBL(w);
110
+ rect->Height = NUM2DBL(h);
115
111
  }
116
112
  else {
117
- rect->w = 0;
118
- rect->h = 0;
113
+ rect->Width = 0;
114
+ rect->Height = 0;
119
115
  }
120
116
 
121
117
  return Qnil;
122
118
  }
123
119
 
124
- /* @return [Integer] position of the rect */
120
+ VALUE ray_init_rect_copy(VALUE self, VALUE other) {
121
+ ray_rect *rect = NULL, *source = NULL;
122
+ Data_Get_Struct(self, ray_rect, rect);
123
+ Data_Get_Struct(other, ray_rect, source);
124
+
125
+ *rect = *source;
126
+ return self;
127
+ }
128
+
129
+ /* @return [Float] position of the rect */
125
130
  VALUE ray_rect_x(VALUE self) {
126
131
  ray_rect *rect;
127
132
  Data_Get_Struct(self, ray_rect, rect);
128
133
 
129
- return INT2FIX(rect->x);
134
+ return rb_float_new(rect->Left);
130
135
  }
131
136
 
132
- /* @return [Integer] position of the rect */
137
+ /* @return [Float] position of the rect */
133
138
  VALUE ray_rect_y(VALUE self) {
134
139
  ray_rect *rect;
135
140
  Data_Get_Struct(self, ray_rect, rect);
136
141
 
137
- return INT2FIX(rect->y);
142
+ return rb_float_new(rect->Top);
138
143
  }
139
144
 
140
- /* @return [Integer] size of the rect */
145
+ /* @return [Float] size of the rect */
141
146
  VALUE ray_rect_w(VALUE self) {
142
147
  ray_rect *rect;
143
148
  Data_Get_Struct(self, ray_rect, rect);
144
149
 
145
- return INT2FIX(rect->w);
150
+ return rb_float_new(rect->Width);
146
151
  }
147
152
 
148
- /* @return [Integer] size of the rect */
153
+ /* @return [Float] size of the rect */
149
154
  VALUE ray_rect_h(VALUE self) {
150
155
  ray_rect *rect;
151
156
  Data_Get_Struct(self, ray_rect, rect);
152
157
 
153
- return INT2FIX(rect->h);
158
+ return rb_float_new(rect->Height);
154
159
  }
155
160
 
156
161
  /*
@@ -158,10 +163,12 @@ VALUE ray_rect_h(VALUE self) {
158
163
  Sets the position of the rect
159
164
  */
160
165
  VALUE ray_rect_set_x(VALUE self, VALUE val) {
166
+ rb_check_frozen(self);
167
+
161
168
  ray_rect *rect;
162
169
  Data_Get_Struct(self, ray_rect, rect);
163
170
 
164
- rect->x = NUM2INT(val);
171
+ rect->Left = NUM2DBL(val);
165
172
 
166
173
  return val;
167
174
  }
@@ -171,10 +178,12 @@ VALUE ray_rect_set_x(VALUE self, VALUE val) {
171
178
  Sets the position of the rect
172
179
  */
173
180
  VALUE ray_rect_set_y(VALUE self, VALUE val) {
181
+ rb_check_frozen(self);
182
+
174
183
  ray_rect *rect;
175
184
  Data_Get_Struct(self, ray_rect, rect);
176
185
 
177
- rect->y = NUM2INT(val);
186
+ rect->Top = NUM2DBL(val);
178
187
 
179
188
  return val;
180
189
  }
@@ -184,10 +193,12 @@ VALUE ray_rect_set_y(VALUE self, VALUE val) {
184
193
  Sets the size of the rect
185
194
  */
186
195
  VALUE ray_rect_set_w(VALUE self, VALUE val) {
196
+ rb_check_frozen(self);
197
+
187
198
  ray_rect *rect;
188
199
  Data_Get_Struct(self, ray_rect, rect);
189
200
 
190
- rect->w = NUM2INT(val);
201
+ rect->Width = NUM2DBL(val);
191
202
 
192
203
  return val;
193
204
  }
@@ -197,10 +208,12 @@ VALUE ray_rect_set_w(VALUE self, VALUE val) {
197
208
  Sets the size of the rect
198
209
  */
199
210
  VALUE ray_rect_set_h(VALUE self, VALUE val) {
211
+ rb_check_frozen(self);
212
+
200
213
  ray_rect *rect;
201
214
  Data_Get_Struct(self, ray_rect, rect);
202
215
 
203
- rect->h = NUM2INT(val);
216
+ rect->Height = NUM2DBL(val);
204
217
 
205
218
  return val;
206
219
  }
@@ -208,7 +221,7 @@ VALUE ray_rect_set_h(VALUE self, VALUE val) {
208
221
  /*
209
222
  Document-class: Ray::Rect
210
223
 
211
- Rects are used to represent a part of a surface, using
224
+ Rects are used to represent a part of an image, using
212
225
  two attributes to represent its position, and two
213
226
  others to represent its size.
214
227
  */
@@ -218,6 +231,7 @@ void Init_ray_rect() {
218
231
 
219
232
  rb_define_alloc_func(ray_cRect, ray_alloc_rect);
220
233
  rb_define_method(ray_cRect, "initialize", ray_init_rect, -1);
234
+ rb_define_method(ray_cRect, "initialize_copy", ray_init_rect_copy, 1);
221
235
 
222
236
  rb_define_method(ray_cRect, "x", ray_rect_x, 0);
223
237
  rb_define_method(ray_cRect, "y", ray_rect_y, 0);
@@ -0,0 +1,169 @@
1
+ #include "ray.hpp"
2
+
3
+ VALUE ray_cShader = Qnil;
4
+
5
+ void ray_free_shader(sf::Shader *shader) {
6
+ delete shader;
7
+ }
8
+
9
+ VALUE ray_alloc_shader(VALUE self) {
10
+ sf::Shader *shader = new sf::Shader;
11
+ return Data_Wrap_Struct(self, 0, ray_free_shader, shader);
12
+ }
13
+
14
+ sf::Shader *ray_rb2shader(VALUE self) {
15
+ if (!RAY_IS_A(self, ray_cShader)) {
16
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Shader",
17
+ RAY_OBJ_CLASSNAME(self));
18
+ }
19
+
20
+ sf::Shader *shader = NULL;
21
+ Data_Get_Struct(self, sf::Shader, shader);
22
+
23
+ return shader;
24
+ }
25
+
26
+ /*
27
+ @return [true, false] True if it is possible to use shaders.
28
+ */
29
+ VALUE ray_shader_available(VALUE self) {
30
+ return sf::Shader::IsAvailable() ? Qtrue : Qfalse;
31
+ }
32
+
33
+ /*
34
+ @overload initialize(filename_or_io)
35
+ Creates a new Shader with the source code found
36
+ in a file or in an IO object.
37
+
38
+ @example
39
+ shader = Ray::Shader.new StringIO.new(<<-SHADER)
40
+ // Code of the shader
41
+ SHADER
42
+ */
43
+ VALUE ray_init_shader(VALUE self, VALUE argument) {
44
+ sf::Shader *shader = ray_rb2shader(self);
45
+
46
+ if (RAY_IS_A(argument, rb_cString)) {
47
+ if (!shader->LoadFromFile(StringValuePtr(argument)))
48
+ rb_raise(rb_eRuntimeError, "Couldn't load the shader");
49
+ }
50
+ else if (rb_respond_to(argument, RAY_METH("read"))) {
51
+ VALUE string = rb_funcall2(argument, RAY_METH("read"), 0, NULL);
52
+ char *c_string = StringValuePtr(string);
53
+
54
+ if (!shader->LoadFromMemory(c_string))
55
+ rb_raise(rb_eRuntimeError, "Couldn't load the shader");
56
+ }
57
+ else {
58
+ rb_raise(rb_eArgError, "Can't convert %s into String",
59
+ RAY_OBJ_CLASSNAME(argument));
60
+ }
61
+
62
+ return self;
63
+ }
64
+
65
+ /*
66
+ @overload []=(name, argument)
67
+ Sets one of the variables of the shader.
68
+ argument can be:
69
+ - a float
70
+ - an array of 1, 2, 3, or 4 floats (for vectors)
71
+ - a Vector2 or Vector3
72
+ - an image, or :current which is a magic value to set a
73
+ texture to the current one.
74
+ */
75
+ VALUE ray_shader_set(VALUE self, VALUE param, VALUE arg) {
76
+ rb_check_frozen(self);
77
+
78
+ sf::Shader *shader = ray_rb2shader(self);
79
+ char *name = StringValuePtr(param);
80
+
81
+ if (RAY_IS_A(arg, rb_cArray)) {
82
+ switch (RARRAY_LEN(arg)) {
83
+ case 1:
84
+ shader->SetParameter(name, NUM2DBL(rb_ary_entry(arg, 0)));
85
+ break;
86
+ case 2:
87
+ shader->SetParameter(name, NUM2DBL(rb_ary_entry(arg, 0)),
88
+ NUM2DBL(rb_ary_entry(arg, 1)));
89
+ break;
90
+ case 3:
91
+ shader->SetParameter(name, NUM2DBL(rb_ary_entry(arg, 0)),
92
+ NUM2DBL(rb_ary_entry(arg, 1)),
93
+ NUM2DBL(rb_ary_entry(arg, 2)));
94
+ break;
95
+ case 4:
96
+ shader->SetParameter(name, NUM2DBL(rb_ary_entry(arg, 0)),
97
+ NUM2DBL(rb_ary_entry(arg, 1)),
98
+ NUM2DBL(rb_ary_entry(arg, 2)),
99
+ NUM2DBL(rb_ary_entry(arg, 3)));
100
+ break;
101
+ default:
102
+ rb_raise(rb_eArgError, "Can't set vectors of %ld components",
103
+ RARRAY_LEN(arg));
104
+ break;
105
+ }
106
+ }
107
+ else {
108
+ if (RAY_IS_A(arg, ray_cImage)) {
109
+ ray_image *img = ray_rb2image(arg);
110
+ if (img->is_image())
111
+ shader->SetTexture(name, img->image());
112
+ else
113
+ rb_raise(rb_eArgError, "Can't set the screen as a texture");
114
+ }
115
+ else if (arg == RAY_SYM("current"))
116
+ shader->SetTexture(name, sf::Shader::CurrentTexture);
117
+ else if (RAY_IS_A(arg, ray_cVector2))
118
+ shader->SetParameter(name, ray_convert_to_vector2(arg));
119
+ else if (RAY_IS_A(arg, ray_cVector3))
120
+ shader->SetParameter(name, ray_convert_to_vector3(arg));
121
+ else
122
+ shader->SetParameter(name, NUM2DBL(arg));
123
+ }
124
+
125
+ return arg;
126
+ }
127
+
128
+ VALUE ray_shader_ensure_unbind(VALUE self) {
129
+ ray_rb2shader(self)->Unbind();
130
+ return self;
131
+ }
132
+
133
+ /*
134
+ Binds the shader (for custom OpenGL drawing).
135
+ If a block is given, it is executed. The shader is then unbound.
136
+ */
137
+ VALUE ray_shader_bind(VALUE self) {
138
+ ray_rb2shader(self)->Bind();
139
+
140
+ if (rb_block_given_p())
141
+ rb_ensure(rb_yield, Qnil, ray_shader_ensure_unbind, self);
142
+
143
+ return self;
144
+ }
145
+
146
+ /* Unbinds the shader (useful when doing custom OpenGL drawing) */
147
+ VALUE ray_shader_unbind(VALUE self) {
148
+ return ray_shader_ensure_unbind(self);
149
+ }
150
+
151
+ /*
152
+ Document-class: Ray::Shader
153
+
154
+ Class used to load OpenGL pixel shaders, which are written
155
+ in the GLSL language (a C-like language).
156
+ */
157
+
158
+ void Init_ray_shader() {
159
+ ray_cShader = rb_define_class_under(ray_mRay, "Shader", rb_cObject);
160
+ rb_define_alloc_func(ray_cShader, ray_alloc_shader);
161
+ rb_define_method(ray_cShader, "initialize", ray_init_shader, 1);
162
+
163
+ rb_define_singleton_method(ray_cShader, "available?", ray_shader_available, 0);
164
+
165
+ rb_define_method(ray_cShader, "[]=", ray_shader_set, 2);
166
+
167
+ rb_define_method(ray_cShader, "bind", ray_shader_bind, 0);
168
+ rb_define_method(ray_cShader, "unbind", ray_shader_unbind, 0);
169
+ }
@@ -0,0 +1,409 @@
1
+ #include "ray.hpp"
2
+
3
+ VALUE ray_cShape = Qnil;
4
+
5
+ void ray_free_shape(sf::Shape *shape) {
6
+ delete shape;
7
+ }
8
+
9
+ VALUE ray_alloc_shape(VALUE self) {
10
+ sf::Shape *shape = new sf::Shape;
11
+ return Data_Wrap_Struct(self, 0, ray_free_shape, shape);
12
+ }
13
+
14
+ sf::Shape *ray_rb2shape(VALUE self) {
15
+ if (!RAY_IS_A(self, ray_cShape)) {
16
+ rb_raise(rb_eTypeError, "Can't convert %s into Ray::Shape",
17
+ RAY_OBJ_CLASSNAME(self));
18
+ }
19
+
20
+ sf::Shape *shape = NULL;
21
+ Data_Get_Struct(self, sf::Shape, shape);
22
+
23
+ return shape;
24
+ }
25
+
26
+ VALUE ray_shape2rb(const sf::Shape &shape) {
27
+ sf::Shape *copy = new sf::Shape(shape);
28
+ return Data_Wrap_Struct(ray_cShape, 0, ray_free_shape, copy);
29
+ }
30
+
31
+ VALUE ray_init_shape_copy(VALUE self, VALUE other) {
32
+ *ray_rb2shape(self) = *ray_rb2shape(other);
33
+ return self;
34
+ }
35
+
36
+ /*
37
+ @overload line(from, to, size = 1.0, color = Ray::Color.white,
38
+ outline_width = 0.0, outline_color = Ray::Color.none)
39
+ @param [Ray::Vector2, #to_vector2] from First point of the line
40
+ @param [Ray::Vector2, #to_vector2] to Last point of the line
41
+
42
+ @param [Float] size Width of the line
43
+ @param [Color] color Color of the line
44
+
45
+ @param [Float] outline_width Width of the outline
46
+ @param [Color] outline_color Color of the outline
47
+
48
+ @return [Shape] a line.
49
+ */
50
+ VALUE ray_shape_line(int argc, VALUE *argv, VALUE self) {
51
+ VALUE from, to, size;
52
+ VALUE color;
53
+ VALUE outline_width, outline_color;
54
+
55
+ rb_scan_args(argc, argv, "24", &from, &to, &size, &color, &outline_width,
56
+ &outline_color);
57
+
58
+ ray_vector2 from_point = ray_convert_to_vector2(from);
59
+ ray_vector2 to_point = ray_convert_to_vector2(to);
60
+
61
+ float c_size = 1.0f;
62
+ if (!NIL_P(size)) c_size = (float)NUM2DBL(size);
63
+
64
+ float c_outline_width = 1.0f;
65
+ if (!NIL_P(outline_width)) c_outline_width = (float)NUM2DBL(outline_width);
66
+
67
+ ray_color sf_color(0, 0, 0);
68
+ if (!NIL_P(color)) sf_color = ray_rb2col(color);
69
+
70
+ ray_color sf_outline_color(0, 0, 0);
71
+ if (!NIL_P(outline_color)) sf_outline_color = ray_rb2col(outline_color);
72
+
73
+ return ray_shape2rb(sf::Shape::Line(from_point, to_point,
74
+ c_size, sf_color,
75
+ c_outline_width, sf_outline_color));
76
+ }
77
+
78
+ /*
79
+ @overload rectangle(rect, color = Ray::Color.white,
80
+ outline_width = 0.0, outline_color = Ray::Color.none)
81
+ @param [Ray::Rect, Array<Integer>] Rect used by this shape
82
+ @param [Color] color Color of the rect
83
+ @param [Float] outline_width (See Shape.line)
84
+ @param [Color] outline_color (See Shape.line)
85
+
86
+ @return [Shape] A rectangle.
87
+ */
88
+ VALUE ray_shape_rectangle(int argc, VALUE *argv, VALUE self) {
89
+ VALUE rect, color, outline_width, outline_color;
90
+ rb_scan_args(argc, argv, "13", &rect, &color, &outline_width, &outline_color);
91
+
92
+ ray_rect c_rect = ray_convert_to_rect(rect);
93
+
94
+ ray_color c_color = ray_rb2col(color);
95
+ ray_color c_outline_color(0, 0, 0);
96
+ if (!NIL_P(outline_color))
97
+ c_outline_color = ray_rb2col(outline_color);
98
+
99
+ float c_outline_width = 0;
100
+ if (!NIL_P(outline_width))
101
+ c_outline_width = (float)NUM2DBL(outline_width);
102
+
103
+ return ray_shape2rb(sf::Shape::Rectangle(c_rect.Left, c_rect.Top,
104
+ c_rect.Width, c_rect.Height,
105
+ c_color, c_outline_width,
106
+ c_outline_color));
107
+ }
108
+
109
+ /*
110
+ @overload circle(center, radius, color = Ray::Color.white,
111
+ outline_width = 0.0, outline_color = Ray::Color.none)
112
+ @param [Ray::Vector2, #to_vector2] center Center of the circle
113
+ @param [Float] radius Radius of the circle.
114
+ @param [Color] color Color of the cirlce.
115
+
116
+ @param [Float] outline_width (See Shape.line)
117
+ @param [Color] outline_color (See Shape.line)
118
+
119
+ @return [Shape] A circle.
120
+ */
121
+ VALUE ray_shape_circle(int argc, VALUE *argv, VALUE self) {
122
+ VALUE center, radius, color, outline_width, outline_color;
123
+ rb_scan_args(argc, argv, "23", &center, &radius, &color,
124
+ &outline_width, &outline_color);
125
+
126
+ ray_vector2 c_center = ray_convert_to_vector2(center);
127
+ float c_radius = (float)NUM2DBL(radius);
128
+
129
+ ray_color c_color = ray_rb2col(color);
130
+ ray_color c_outline_color(0, 0, 0);
131
+ if (!NIL_P(outline_color))
132
+ c_outline_color = ray_rb2col(outline_color);
133
+
134
+ float c_outline_width = 0;
135
+ if (!NIL_P(outline_width))
136
+ c_outline_width = (float)NUM2DBL(outline_width);
137
+
138
+ return ray_shape2rb(sf::Shape::Circle(c_center, c_radius,
139
+ c_color, c_outline_width,
140
+ c_outline_color));
141
+ }
142
+
143
+ /* @return [Ray::Vector2] Position of the shape */
144
+ VALUE ray_shape_pos(VALUE self) {
145
+ return ray_vector2_to_rb(ray_rb2shape(self)->GetPosition());
146
+ }
147
+
148
+ /* Sets the position of the shape */
149
+ VALUE ray_shape_set_pos(VALUE self, VALUE pos) {
150
+ rb_check_frozen(self);
151
+ ray_rb2shape(self)->SetPosition(ray_convert_to_vector2(pos));
152
+ return pos;
153
+ }
154
+
155
+ /*
156
+ @overload add_point(pos, color = Ray::Color.white, outline_color = Ray::Color.none)
157
+ Adds a new point to the shape.
158
+
159
+ @param [Ray::Vector2, #to_vector2] Position of the point.
160
+ @param [Color] color Color of the shape at that point.
161
+ @param [Color] outline_color Color of the outline at that point.
162
+ */
163
+ VALUE ray_shape_add_point(int argc, VALUE *argv, VALUE self) {
164
+ rb_check_frozen(self);
165
+
166
+ sf::Shape *shape = ray_rb2shape(self);
167
+
168
+ VALUE rb_pos, rb_color, rb_outline_color;
169
+
170
+ ray_vector2 pos;
171
+ ray_color color(255, 255, 255), outline_color(255, 255, 255);
172
+
173
+ rb_scan_args(argc, argv, "12", &rb_pos, &rb_color, &rb_outline_color);
174
+
175
+ pos = ray_convert_to_vector2(rb_pos);
176
+
177
+ if (!NIL_P(rb_color))
178
+ color = ray_rb2col(rb_color);
179
+
180
+ if (!NIL_P(rb_outline_color))
181
+ outline_color = ray_rb2col(rb_outline_color);
182
+
183
+ shape->AddPoint(pos, color, outline_color);
184
+ return self;
185
+ }
186
+
187
+ /*
188
+ @return [Integer] the amount of points in this shape.
189
+ */
190
+ VALUE ray_shape_point_count(VALUE self) {
191
+ sf::Shape *shape = ray_rb2shape(self);
192
+ return INT2FIX(shape->GetPointsCount());
193
+ }
194
+
195
+ /*
196
+ Sets whether the shape should be filled. Defaults to true.
197
+ */
198
+ VALUE ray_shape_set_filled(VALUE self, VALUE val) {
199
+ rb_check_frozen(self);
200
+
201
+ sf::Shape *shape = ray_rb2shape(self);
202
+ shape->EnableFill(RTEST(val));
203
+
204
+ return val;
205
+ }
206
+
207
+ /*
208
+ Sets whether the outline of the shape should be drawn. Defaults to true.
209
+ */
210
+ VALUE ray_shape_set_outlined(VALUE self, VALUE val) {
211
+ rb_check_frozen(self);
212
+
213
+ sf::Shape *shape = ray_rb2shape(self);
214
+ shape->EnableOutline(RTEST(val));
215
+
216
+ return val;
217
+ }
218
+
219
+ /*
220
+ @overload set_position_of(id, pos)
221
+ Changes the position of a point.
222
+
223
+ @param [Integer] id Id of the point to modify.
224
+ @param [Ray::Vector2, #to_vector2] pos New position of that point.
225
+ */
226
+ VALUE ray_shape_set_position_of(VALUE self, VALUE id, VALUE pos) {
227
+ rb_check_frozen(self);
228
+
229
+ sf::Shape *shape = ray_rb2shape(self);
230
+
231
+ unsigned int c_id = NUM2UINT(id);
232
+ if (c_id >= shape->GetPointsCount())
233
+ rb_raise(rb_eArgError, "%d out of bounds", c_id);
234
+
235
+ shape->SetPointPosition(c_id, ray_convert_to_vector2(pos));
236
+
237
+ return pos;
238
+ }
239
+
240
+ /*
241
+ @overload set_color_of(id, col)
242
+ Changes the color of a point.
243
+
244
+ @param [Integer] id Id of the point to modify.
245
+ @param [Color] col New color of that point.
246
+ */
247
+ VALUE ray_shape_set_color_of(VALUE self, VALUE id, VALUE col) {
248
+ rb_check_frozen(self);
249
+
250
+ sf::Shape *shape = ray_rb2shape(self);
251
+
252
+ unsigned int c_id = NUM2UINT(id);
253
+ if (c_id >= shape->GetPointsCount())
254
+ rb_raise(rb_eArgError, "%d out of bounds", c_id);
255
+
256
+ shape->SetPointColor(c_id, ray_rb2col(col));
257
+ return col;
258
+ }
259
+
260
+ /*
261
+ @overload set_outline_of(id, col)
262
+ Changes the outline color of a point.
263
+
264
+ @param [Integer] id Id of the point to modify.
265
+ @param [Color] col New outline color of that point.
266
+ */
267
+ VALUE ray_shape_set_outline_of(VALUE self, VALUE id, VALUE col) {
268
+ rb_check_frozen(self);
269
+
270
+ sf::Shape *shape = ray_rb2shape(self);
271
+
272
+ unsigned int c_id = NUM2UINT(id);
273
+ if (c_id >= shape->GetPointsCount())
274
+ rb_raise(rb_eArgError, "%d out of bounds", c_id);
275
+
276
+ shape->SetPointOutlineColor(c_id, ray_rb2col(col));
277
+ return col;
278
+ }
279
+
280
+ /*
281
+ Sets the width of the outline of this shape.
282
+ */
283
+ VALUE ray_shape_set_outline_width(VALUE self, VALUE width) {
284
+ rb_check_frozen(self);
285
+
286
+ sf::Shape *shape = ray_rb2shape(self);
287
+ shape->SetOutlineThickness(NUM2INT(width));
288
+
289
+ return width;
290
+ }
291
+
292
+ /*
293
+ @overload position_of(id)
294
+ @return [Ray::Vector2] Position of that point.
295
+ */
296
+ VALUE ray_shape_position_of(VALUE self, VALUE id) {
297
+ sf::Shape *shape = ray_rb2shape(self);
298
+
299
+ unsigned int c_id = NUM2UINT(id);
300
+ if (c_id >= shape->GetPointsCount())
301
+ return Qnil;
302
+
303
+ sf::Vector2f point = shape->GetPointPosition(c_id);
304
+ return ray_vector2_to_rb(point);
305
+ }
306
+
307
+ /*
308
+ @overload color_of(id)
309
+ @return [Color] Color of that point.
310
+ */
311
+ VALUE ray_shape_color_of(VALUE self, VALUE id) {
312
+ sf::Shape *shape = ray_rb2shape(self);
313
+
314
+ unsigned int c_id = NUM2UINT(id);
315
+ if (c_id >= shape->GetPointsCount())
316
+ return Qnil;
317
+
318
+ return ray_col2rb(shape->GetPointColor(c_id));
319
+ }
320
+
321
+ /*
322
+ @overload outline_of(id)
323
+ @return [Color] Outline of that point.
324
+ */
325
+ VALUE ray_shape_outline_of(VALUE self, VALUE id) {
326
+ sf::Shape *shape = ray_rb2shape(self);
327
+
328
+ unsigned int c_id = NUM2UINT(id);
329
+ if (c_id >= shape->GetPointsCount())
330
+ return Qnil;
331
+
332
+ return ray_col2rb(shape->GetPointOutlineColor(c_id));
333
+ }
334
+
335
+ /* @return [Float] Outline width of this shape */
336
+ VALUE ray_shape_outline_width(VALUE self) {
337
+ sf::Shape *shape = ray_rb2shape(self);
338
+ return INT2FIX((int)(shape->GetOutlineThickness()));
339
+ }
340
+
341
+ /* @return [Float] Rotation applied to this shape when it is drawn (in degrees) */
342
+ VALUE ray_shape_angle(VALUE self) {
343
+ sf::Shape *shape = ray_rb2shape(self);
344
+ return rb_float_new(shape->GetRotation());
345
+ }
346
+
347
+ /* Sets the rotation applied to this object when it is drawn (in degrees) */
348
+ VALUE ray_shape_set_angle(VALUE self, VALUE angle) {
349
+ rb_check_frozen(self);
350
+
351
+ sf::Shape *shape = ray_rb2shape(self);
352
+ shape->SetRotation((float)NUM2DBL(angle));
353
+
354
+ return angle;
355
+ }
356
+
357
+ /* @return [Ray::Vector2] scale for width and height. */
358
+ VALUE ray_shape_scale(VALUE self) {
359
+ sf::Shape *shape = ray_rb2shape(self);
360
+ sf::Vector2f scale(shape->GetScale());
361
+
362
+ return ray_vector2_to_rb(scale);
363
+ }
364
+
365
+ /* Sets scale for width and height, using a vector. */
366
+ VALUE ray_shape_set_scale(VALUE self, VALUE val) {
367
+ rb_check_frozen(self);
368
+
369
+ sf::Shape *shape = ray_rb2shape(self);
370
+ shape->SetScale(ray_convert_to_vector2(val));
371
+
372
+ return val;
373
+ }
374
+
375
+ void Init_ray_shape() {
376
+ ray_cShape = rb_define_class_under(ray_mRay, "Shape", rb_cObject);
377
+ rb_define_alloc_func(ray_cShape, ray_alloc_shape);
378
+ rb_define_method(ray_cShape, "initialize_copy", ray_init_shape_copy, 1);
379
+
380
+ rb_define_singleton_method(ray_cShape, "line", ray_shape_line, -1);
381
+ rb_define_singleton_method(ray_cShape, "rectangle", ray_shape_rectangle, -1);
382
+ rb_define_singleton_method(ray_cShape, "circle", ray_shape_circle, -1);
383
+
384
+ rb_define_method(ray_cShape, "pos", ray_shape_pos, 0);
385
+ rb_define_method(ray_cShape, "pos=", ray_shape_set_pos, 1);
386
+
387
+ rb_define_method(ray_cShape, "add_point", ray_shape_add_point, -1);
388
+ rb_define_method(ray_cShape, "point_count", ray_shape_point_count, 0);
389
+
390
+ rb_define_method(ray_cShape, "filled=", ray_shape_set_filled, 1);
391
+ rb_define_method(ray_cShape, "outlined=", ray_shape_set_outlined, 1);
392
+
393
+ rb_define_method(ray_cShape, "outline_width=", ray_shape_set_outline_width, 1);
394
+ rb_define_method(ray_cShape, "outline_width", ray_shape_outline_width, 0);
395
+
396
+ rb_define_method(ray_cShape, "set_position_of", ray_shape_set_position_of, 2);
397
+ rb_define_method(ray_cShape, "set_color_of", ray_shape_set_color_of, 2);
398
+ rb_define_method(ray_cShape, "set_outline_of", ray_shape_set_outline_of, 2);
399
+
400
+ rb_define_method(ray_cShape, "position_of", ray_shape_position_of, 1);
401
+ rb_define_method(ray_cShape, "color_of", ray_shape_color_of, 1);
402
+ rb_define_method(ray_cShape, "outline_of", ray_shape_outline_of, 1);
403
+
404
+ rb_define_method(ray_cShape, "angle", ray_shape_angle, 0);
405
+ rb_define_method(ray_cShape, "angle=", ray_shape_set_angle, 1);
406
+
407
+ rb_define_method(ray_cShape, "scale", ray_shape_scale, 0);
408
+ rb_define_method(ray_cShape, "scale=", ray_shape_set_scale, 1);
409
+ }