ray 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. data/README.md +9 -6
  2. data/Rakefile +1 -5
  3. data/ext/audio.c +25 -19
  4. data/ext/audio_source.c +67 -39
  5. data/ext/color.c +19 -19
  6. data/ext/drawable.c +190 -31
  7. data/ext/extconf.rb +16 -14
  8. data/ext/gl.c +310 -30
  9. data/ext/gl_buffer.c +223 -2
  10. data/ext/gl_index_buffer.c +11 -0
  11. data/ext/gl_int_array.c +24 -22
  12. data/ext/gl_vertex.c +84 -49
  13. data/ext/image.c +115 -51
  14. data/ext/image_target.c +58 -10
  15. data/ext/input.c +73 -6
  16. data/ext/mo.c +583 -0
  17. data/ext/mo.h +189 -0
  18. data/ext/music.c +9 -8
  19. data/ext/pixel_bus.c +349 -0
  20. data/ext/polygon.c +68 -45
  21. data/ext/ray.c +1 -0
  22. data/ext/ray.h +19 -1
  23. data/ext/rect.c +9 -47
  24. data/ext/say.h +1 -2
  25. data/ext/say_all.h +6 -0
  26. data/ext/say_audio.h +3 -0
  27. data/ext/say_audio_context.c +1 -4
  28. data/ext/say_basic_type.c +24 -0
  29. data/ext/say_basic_type.h +4 -0
  30. data/ext/say_buffer.c +217 -88
  31. data/ext/say_buffer.h +20 -5
  32. data/ext/say_buffer_renderer.c +10 -7
  33. data/ext/say_buffer_renderer.h +1 -1
  34. data/ext/say_buffer_slice.c +70 -76
  35. data/ext/say_context.c +109 -22
  36. data/ext/say_context.h +14 -0
  37. data/ext/say_drawable.c +113 -25
  38. data/ext/say_drawable.h +23 -2
  39. data/ext/say_error.c +7 -2
  40. data/ext/say_font.c +30 -27
  41. data/ext/say_font.h +3 -6
  42. data/ext/say_get_proc.c +35 -0
  43. data/ext/say_image.c +102 -27
  44. data/ext/say_image.h +11 -4
  45. data/ext/say_image_target.c +88 -34
  46. data/ext/say_image_target.h +3 -2
  47. data/ext/say_index_buffer.c +31 -19
  48. data/ext/say_index_buffer.h +4 -2
  49. data/ext/say_index_buffer_slice.c +78 -70
  50. data/ext/say_music.c +4 -2
  51. data/ext/say_osx.h +3 -2
  52. data/ext/say_osx_context.h +37 -4
  53. data/ext/say_osx_window.h +32 -37
  54. data/ext/say_pixel_bus.c +163 -0
  55. data/ext/say_pixel_bus.h +44 -0
  56. data/ext/say_polygon.c +2 -2
  57. data/ext/say_shader.c +66 -62
  58. data/ext/say_shader.h +2 -0
  59. data/ext/say_sprite.c +1 -2
  60. data/ext/say_target.c +14 -23
  61. data/ext/say_target.h +3 -1
  62. data/ext/say_text.c +45 -7
  63. data/ext/say_text.h +12 -3
  64. data/ext/say_thread.c +13 -6
  65. data/ext/say_thread.h +1 -1
  66. data/ext/say_thread_variable.c +5 -5
  67. data/ext/say_vertex_type.c +79 -41
  68. data/ext/say_vertex_type.h +6 -2
  69. data/ext/say_view.c +10 -31
  70. data/ext/say_view.h +1 -5
  71. data/ext/say_win.h +2 -2
  72. data/ext/say_win_context.h +49 -11
  73. data/ext/say_win_window.h +30 -27
  74. data/ext/say_window.c +3 -3
  75. data/ext/say_x11.h +3 -1
  76. data/ext/say_x11_context.h +64 -10
  77. data/ext/say_x11_window.h +22 -17
  78. data/ext/shader.c +9 -0
  79. data/ext/sprite.c +7 -1
  80. data/ext/target.c +80 -28
  81. data/ext/text.c +43 -1
  82. data/ext/view.c +53 -1
  83. data/ext/window.c +4 -0
  84. data/lib/ray/animation_list.rb +17 -2
  85. data/lib/ray/audio_source.rb +11 -0
  86. data/lib/ray/color.rb +14 -0
  87. data/lib/ray/drawable.rb +23 -0
  88. data/lib/ray/dsl/event.rb +1 -9
  89. data/lib/ray/dsl/event_runner.rb +3 -4
  90. data/lib/ray/dsl/matcher.rb +20 -1
  91. data/lib/ray/effect.rb +116 -0
  92. data/lib/ray/effect/black_and_white.rb +38 -0
  93. data/lib/ray/effect/color_inversion.rb +16 -0
  94. data/lib/ray/effect/generator.rb +145 -0
  95. data/lib/ray/effect/grayscale.rb +32 -0
  96. data/lib/ray/game.rb +25 -5
  97. data/lib/ray/gl/vertex.rb +105 -26
  98. data/lib/ray/helper.rb +5 -0
  99. data/lib/ray/image.rb +54 -13
  100. data/lib/ray/image_target.rb +7 -0
  101. data/lib/ray/matrix.rb +26 -0
  102. data/lib/ray/music.rb +4 -0
  103. data/lib/ray/pixel_bus.rb +22 -0
  104. data/lib/ray/polygon.rb +17 -0
  105. data/lib/ray/pp.rb +28 -0
  106. data/lib/ray/ray.rb +7 -1
  107. data/lib/ray/rect.rb +7 -13
  108. data/lib/ray/scene.rb +24 -5
  109. data/lib/ray/scene_list.rb +9 -0
  110. data/lib/ray/shader.rb +11 -2
  111. data/lib/ray/sound.rb +4 -0
  112. data/lib/ray/sprite.rb +23 -62
  113. data/lib/ray/target.rb +25 -0
  114. data/lib/ray/text.rb +10 -0
  115. data/lib/ray/turtle.rb +9 -3
  116. data/lib/ray/vector.rb +18 -0
  117. data/lib/ray/vertex.rb +6 -0
  118. data/lib/ray/view.rb +22 -0
  119. data/samples/animation/sprite_motion.rb +0 -60
  120. data/samples/audio/{spacial.rb → spatial.rb} +1 -1
  121. data/samples/buffer/buffer.rb +1 -0
  122. data/samples/buffer/index_buffer.rb +2 -0
  123. data/samples/cptn_ruby/cptn_ruby.rb +6 -7
  124. data/samples/effects/effect.rb +39 -0
  125. data/samples/effects/grayscale.rb +27 -0
  126. data/samples/opengl/image.rb +7 -5
  127. data/samples/opengl/instancing.rb +159 -0
  128. data/samples/opengl/instancing.rbc +3231 -0
  129. data/samples/opengl/obj_loader.rb +9 -8
  130. data/samples/opengl/shader.rb +1 -3
  131. data/samples/shaders/geometry.rb +108 -38
  132. data/samples/shaders/geometry.rbc +2074 -0
  133. data/samples/shaders/shape.rb +2 -2
  134. data/samples/starfighter/starfighter.rb +5 -5
  135. data/samples/window/get_pixel.rb +1 -1
  136. data/test/animation_list_test.rb +18 -4
  137. data/test/drawable_test.rb +70 -1
  138. data/test/effect_generator_test.rb +63 -0
  139. data/test/effect_test.rb +61 -0
  140. data/test/game_test.rb +18 -0
  141. data/test/gl_buffer_test.rb +43 -1
  142. data/test/gl_index_buffer_test.rb +5 -0
  143. data/test/gl_vertex_test.rb +28 -1
  144. data/test/image_test.rb +5 -5
  145. data/test/input_test.rb +49 -0
  146. data/test/pixel_bus_test.rb +28 -0
  147. data/test/rect_test.rb +4 -0
  148. data/{samples/_media → test/res}/Beep.wav +0 -0
  149. data/samples/_media/CptnRuby Gem.png b/data/test/res/CptnRuby → Gem.png +0 -0
  150. data/samples/_media/CptnRuby Map.txt b/data/test/res/CptnRuby → Map.txt +0 -0
  151. data/samples/_media/CptnRuby Tileset.png b/data/test/res/CptnRuby → Tileset.png +0 -0
  152. data/{samples/_media → test/res}/CptnRuby.png +0 -0
  153. data/{samples/_media → test/res}/Space.png +0 -0
  154. data/{samples/_media → test/res}/Star.png +0 -0
  155. data/{samples/_media → test/res}/Starfighter.png +0 -0
  156. data/test/res/cube.obj +28 -0
  157. data/test/res/light3d.c +2 -2
  158. data/test/res/stone.png +0 -0
  159. data/test/scene_test.rb +3 -0
  160. data/test/sprite_test.rb +10 -0
  161. data/test/text_test.rb +31 -2
  162. data/test/view_test.rb +13 -1
  163. metadata +38 -17
  164. data/ext/say_array.c +0 -124
  165. data/ext/say_array.h +0 -34
  166. data/ext/say_table.c +0 -86
  167. data/ext/say_table.h +0 -24
@@ -17,12 +17,18 @@ say_polygon *ray_rb2polygon(VALUE obj) {
17
17
  static
18
18
  VALUE ray_polygon_alloc(VALUE self) {
19
19
  say_polygon *obj = say_polygon_create(0);
20
- return Data_Wrap_Struct(self, NULL, say_polygon_free, obj);
20
+ VALUE rb = Data_Wrap_Struct(self, NULL, say_polygon_free, obj);
21
+
22
+ say_drawable_set_shader_proc(obj->drawable, ray_drawable_shader_proc);
23
+ say_drawable_set_other_data(obj->drawable, (void*)rb);
24
+
25
+ return rb;
21
26
  }
22
27
 
23
28
  static
24
29
  VALUE ray_polygon_init_copy(VALUE self, VALUE other) {
25
30
  say_polygon_copy(ray_rb2polygon(self), ray_rb2polygon(other));
31
+ ray_drawable_copy_attr(self, other);
26
32
  return self;
27
33
  }
28
34
 
@@ -41,16 +47,16 @@ void ray_polygon_make_outline(say_polygon *poly, VALUE outline_width,
41
47
  }
42
48
 
43
49
  /*
44
- @overload line(first, last, width = 1, color = Ray::Color.white)
45
- @param [Vector2, #to_vector2] first First point of the line
46
- @param [Vector2, #to_vector2] last Last point of the line
47
-
48
- @param [Float] width Width of the line
49
-
50
- @param [Ray::Color] color Color of the line
51
-
52
- @return [Ray::Polygon] A line
53
- */
50
+ * @overload line(first, last, width = 1, color = Ray::Color.white)
51
+ * @param [Vector2, #to_vector2] first First point of the line
52
+ * @param [Vector2, #to_vector2] last Last point of the line
53
+ *
54
+ * @param [Float] width Width of the line
55
+ *
56
+ * @param [Ray::Color] color Color of the line
57
+ *
58
+ * @return [Ray::Polygon] A line
59
+ */
54
60
  static
55
61
  VALUE ray_polygon_line(int argc, VALUE *argv, VALUE self) {
56
62
  VALUE first = Qnil, last = Qnil, width = Qnil, color = Qnil;
@@ -62,19 +68,24 @@ VALUE ray_polygon_line(int argc, VALUE *argv, VALUE self) {
62
68
  NIL_P(color) ?
63
69
  say_make_color(255, 255, 255, 255) :
64
70
  ray_rb2col(color));
65
- return Data_Wrap_Struct(self, NULL, say_polygon_free, line);
71
+ VALUE rb = Data_Wrap_Struct(self, NULL, say_polygon_free, line);
72
+
73
+ say_drawable_set_shader_proc(line->drawable, ray_drawable_shader_proc);
74
+ say_drawable_set_other_data(line->drawable, (void*)rb);
75
+
76
+ return rb;
66
77
  }
67
78
 
68
79
  /*
69
- @overload rectangle(rect, color = Ray::Color.white, outline_width = 0,
70
- outline_color = Ray::Color.white)
71
- @param [Ray::Rect, #to_rect] rect Rectangle occupied by the polygon
72
- @param [Ray::Color] color Color of the rectangle
73
- @param [Float] outline_width Width of the outline
74
- @param [Ray::Color] outline_color Color of the outline
75
-
76
- @return [Ray::Polygon] A rectangle
77
- */
80
+ * @overload rectangle(rect, color = Ray::Color.white, outline_width = 0,
81
+ * outline_color = Ray::Color.white)
82
+ * @param [Ray::Rect, #to_rect] rect Rectangle occupied by the polygon
83
+ * @param [Ray::Color] color Color of the rectangle
84
+ * @param [Float] outline_width Width of the outline
85
+ * @param [Ray::Color] outline_color Color of the outline
86
+ *
87
+ * @return [Ray::Polygon] A rectangle
88
+ */
78
89
  static
79
90
  VALUE ray_polygon_rectangle(int argc, VALUE *argv, VALUE self) {
80
91
  VALUE rb_rect = Qnil, rb_color = Qnil, outline_width = Qnil, outline = Qnil;
@@ -91,21 +102,25 @@ VALUE ray_polygon_rectangle(int argc, VALUE *argv, VALUE self) {
91
102
 
92
103
  ray_polygon_make_outline(poly, outline_width, outline);
93
104
 
105
+ say_drawable_set_shader_proc(poly->drawable, ray_drawable_shader_proc);
106
+ say_drawable_set_other_data(poly->drawable, (void*)ret);
107
+
94
108
  return ret;
95
109
  }
96
110
 
97
111
  /*
98
- @overload circle(center, radius, color = Ray::Color.white, outline_width = 0,
99
- outline_color = Ray::Color.white)
100
- @param [Ray::Vector2] center The center of the circle
101
- @param [Float] radius The radius of the circle
102
- @param [Ray::Color] color The color of the circle
103
-
104
- @param outline_width (see rectangle)
105
- @param outline_color (see rectangle)
106
-
107
- @return [Ray::Polygon] A circle
108
- */
112
+ * @overload circle(center, radius, color = Ray::Color.white, outline_width = 0,
113
+ * outline_color = Ray::Color.white)
114
+ *
115
+ * @param [Ray::Vector2] center The center of the circle
116
+ * @param [Float] radius The radius of the circle
117
+ * @param [Ray::Color] color The color of the circle
118
+ *
119
+ * @param outline_width (see rectangle)
120
+ * @param outline_color (see rectangle)
121
+ *
122
+ * @return [Ray::Polygon] A circle
123
+ */
109
124
  static
110
125
  VALUE ray_polygon_circle(int argc, VALUE *argv, VALUE self) {
111
126
  VALUE rb_center = Qnil, rb_radius = Qnil, rb_color = Qnil,
@@ -124,22 +139,25 @@ VALUE ray_polygon_circle(int argc, VALUE *argv, VALUE self) {
124
139
 
125
140
  ray_polygon_make_outline(poly, rb_outline_width, rb_outline);
126
141
 
142
+ say_drawable_set_shader_proc(poly->drawable, ray_drawable_shader_proc);
143
+ say_drawable_set_other_data(poly->drawable, (void*)ret);
144
+
127
145
  return ret;
128
146
  }
129
147
 
130
148
  /*
131
- @overload ellipse(center, rx, ry, color = Ray::Color.white, outline_width = 0,
132
- outline_color = Ray::Color.white)
133
- @param [Ray::Vector2] center The center of the ellipse
134
- @param [Float] rx Horizontal radius
135
- @param [Float] ry Vertical radius
136
- @param [Ray::Color] color The color of the ellipse
137
-
138
- @param outline_width (see rectangle)
139
- @param outline_color (see rectangle)
140
-
141
- @return [Ray::Polygon] An ellipse
142
- */
149
+ * @overload ellipse(center, rx, ry, color = Ray::Color.white,
150
+ * outline_width = 0, outline_color = Ray::Color.white)
151
+ *
152
+ * @param [Ray::Vector2] center The center of the ellipse
153
+ * @param [Float] rx Horizontal radius
154
+ * @param [Float] ry Vertical radius
155
+ * @param [Ray::Color] color The color of the ellipse
156
+ * @param outline_width (see rectangle)
157
+ * @param outline_color (see rectangle)
158
+ *
159
+ * @return [Ray::Polygon] An ellipse
160
+ */
143
161
  static
144
162
  VALUE ray_polygon_ellipse(int argc, VALUE *argv, VALUE self) {
145
163
  VALUE rb_center = Qnil, rb_rx = Qnil, rb_ry = Qnil, rb_color = Qnil,
@@ -157,7 +175,12 @@ VALUE ray_polygon_ellipse(int argc, VALUE *argv, VALUE self) {
157
175
 
158
176
  ray_polygon_make_outline(poly, rb_outline_width, rb_outline);
159
177
 
160
- return Data_Wrap_Struct(self, NULL, say_polygon_free, poly);
178
+ VALUE rb = Data_Wrap_Struct(self, NULL, say_polygon_free, poly);
179
+
180
+ say_drawable_set_shader_proc(poly->drawable, ray_drawable_shader_proc);
181
+ say_drawable_set_other_data(poly->drawable, (void*)rb);
182
+
183
+ return rb;
161
184
  }
162
185
 
163
186
  /*
data/ext/ray.c CHANGED
@@ -37,6 +37,7 @@ void Init_ray_ext() {
37
37
  Init_ray_int_array();
38
38
  Init_ray_gl_buffer();
39
39
  Init_ray_gl_index_buffer();
40
+ Init_ray_pixel_bus();
40
41
  Init_ray_image();
41
42
  Init_ray_font();
42
43
  Init_ray_shader();
data/ext/ray.h CHANGED
@@ -29,9 +29,11 @@ extern VALUE ray_cColor;
29
29
  extern VALUE ray_cVertex;
30
30
  extern VALUE ray_mGL;
31
31
  extern VALUE ray_cGLVertex;
32
+ extern VALUE ray_cGLInstance;
32
33
  extern VALUE ray_cIntArray;
33
34
  extern VALUE ray_cGLBuffer;
34
35
  extern VALUE ray_cGLIndexBuffer;
36
+ extern VALUE ray_cPixelBus;
35
37
  extern VALUE ray_cImage;
36
38
  extern VALUE ray_cFont;
37
39
  extern VALUE ray_cShader;
@@ -63,6 +65,18 @@ extern VALUE ray_cMusic;
63
65
 
64
66
  #define RAY_ARRAY_AT(ary, i) (rb_funcall(ary, RAY_METH("[]"), 1, INT2FIX(i)))
65
67
 
68
+ #if defined(JRUBY) && defined(_DARWIN_C_SOURCE)
69
+ # include <dispatch/dispatch.h>
70
+
71
+ # define RAY_MAIN dispatch_sync(dispatch_get_main_queue(), ^
72
+ # define RAY_MAIN_END );
73
+ # define RAY_BLOCK __block
74
+ #else
75
+ # define RAY_MAIN if (1)
76
+ # define RAY_MAIN_END
77
+ # define RAY_BLOCK
78
+ #endif
79
+
66
80
  void Init_ray_ext();
67
81
  void Init_ray_vector();
68
82
  void Init_ray_rect();
@@ -74,6 +88,7 @@ void Init_ray_gl_vertex();
74
88
  void Init_ray_int_array();
75
89
  void Init_ray_gl_buffer();
76
90
  void Init_ray_gl_index_buffer();
91
+ void Init_ray_pixel_bus();
77
92
  void Init_ray_image();
78
93
  void Init_ray_font();
79
94
  void Init_ray_shader();
@@ -128,6 +143,9 @@ say_font *ray_rb2font(VALUE obj);
128
143
  VALUE ray_shader2rb(say_shader *shader, VALUE owner);
129
144
  say_shader *ray_rb2shader(VALUE obj);
130
145
 
146
+ void ray_drawable_copy_attr(VALUE self, VALUE orig);
147
+ void ray_drawable_shader_proc(void *data, say_shader *shader);
148
+
131
149
  say_drawable *ray_rb2drawable(VALUE obj);
132
150
  say_polygon *ray_rb2polygon(VALUE obj);
133
151
  say_sprite *ray_rb2sprite(VALUE obj);
@@ -148,7 +166,7 @@ say_audio_source *ray_rb2audio_source(VALUE obj);
148
166
  say_sound *ray_rb2sound(VALUE obj);
149
167
  say_music *ray_rb2music(VALUE obj);
150
168
 
151
- say_array *ray_rb2int_array(VALUE obj);
169
+ mo_array *ray_rb2int_array(VALUE obj);
152
170
 
153
171
  say_buffer_renderer *ray_rb2buf_renderer(VALUE obj);
154
172
 
data/ext/rect.c CHANGED
@@ -39,59 +39,21 @@ VALUE ray_alloc_rect(VALUE self) {
39
39
  }
40
40
 
41
41
  /*
42
- @overload initialize(x, y)
43
- Creates a new rect with size set to 0, 0.
44
-
45
- @overload initialize(x, y, w, h)
46
- Creates a new rect with the specified size.
47
-
48
- @overload initialize(hash)
49
- Creates a new rect according to the keys specified in hash.
50
-
51
- @option hash [Float] :x
52
- @option hash [Float] :y
53
- @option hash [Float] (0) :width
54
- @option hash [Float] (0) :height required if width is set
55
-
56
- @option hash [Float] (0) :w alias for :width
57
- @option hash [Float] (0) :h alias for :height
58
- */
42
+ * @overload initialize(x, y)
43
+ * Creates a new rect with size set to (0, 0).
44
+ *
45
+ * @overload initialize(x, y, w, h)
46
+ * Creates a new rect with the specified size.
47
+ */
59
48
  static
60
49
  VALUE ray_init_rect(int argc, VALUE *argv, VALUE self) {
61
- VALUE x_or_hash, y, w, h;
62
- rb_scan_args(argc, argv, "13", &x_or_hash, &y, &w, &h);
63
-
64
- int type = TYPE(x_or_hash);
65
- if (type == T_HASH) {
66
- if (!NIL_P(y)) {
67
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)",
68
- argc);
69
- }
70
-
71
- VALUE hash = x_or_hash;
72
-
73
- x_or_hash = rb_hash_aref(hash, RAY_SYM("x"));
74
- y = rb_hash_aref(hash, RAY_SYM("y"));
75
-
76
- w = rb_hash_aref(hash, RAY_SYM("width"));
77
- h = rb_hash_aref(hash, RAY_SYM("height"));
78
-
79
- if (NIL_P(w)) w = rb_hash_aref(hash, RAY_SYM("w"));
80
- if (NIL_P(h)) h = rb_hash_aref(hash, RAY_SYM("h"));
81
-
82
- if (NIL_P(y)) {
83
- rb_raise(rb_eArgError, "missing option :y");
84
- }
85
-
86
- if (!NIL_P(w) && NIL_P(h)) {
87
- rb_raise(rb_eArgError, "missing option :height");
88
- }
89
- }
50
+ VALUE x, y, w, h;
51
+ rb_scan_args(argc, argv, "22", &x, &y, &w, &h);
90
52
 
91
53
  say_rect *rect;
92
54
  Data_Get_Struct(self, say_rect, rect);
93
55
 
94
- rect->x = NUM2DBL(x_or_hash);
56
+ rect->x = NUM2DBL(x);
95
57
  rect->y = NUM2DBL(y);
96
58
 
97
59
  if (!NIL_P(w)) {
data/ext/say.h CHANGED
@@ -2,8 +2,6 @@
2
2
  #define SAY_H_
3
3
 
4
4
  #include "say_basic_type.h"
5
- #include "say_array.h"
6
- #include "say_table.h"
7
5
  #include "say_thread.h"
8
6
  #include "say_matrix.h"
9
7
  #include "say_image.h"
@@ -14,6 +12,7 @@
14
12
  #include "say_buffer_slice.h"
15
13
  #include "say_index_buffer.h"
16
14
  #include "say_index_buffer_slice.h"
15
+ #include "say_pixel_bus.h"
17
16
  #include "say_drawable.h"
18
17
  #include "say_view.h"
19
18
  #include "say_buffer_renderer.h"
@@ -3,6 +3,9 @@
3
3
 
4
4
  /* Dependencies for all of the files */
5
5
 
6
+ /* Data structures */
7
+ #include "mo.h"
8
+
6
9
  /* Standard headers */
7
10
  #include <stdlib.h>
8
11
  #include <stdio.h>
@@ -72,6 +75,9 @@ void say_error_set(const char *message);
72
75
 
73
76
  void say_error_clean_up();
74
77
 
78
+ /* Get OpenGL proc */
79
+ void *say_get_proc(const char *name);
80
+
75
81
  #define SAY_PI 3.14159265358979323846
76
82
 
77
83
  #endif
@@ -1,6 +1,9 @@
1
1
  #ifndef SAY_AUDIO_H_
2
2
  #define SAY_AUDIO_H_
3
3
 
4
+ #include "say_basic_type.h"
5
+ #include "say_thread.h"
6
+
4
7
  typedef struct {
5
8
  ALuint buf;
6
9
 
@@ -24,8 +24,6 @@ void say_audio_context_ensure() {
24
24
  }
25
25
 
26
26
  void say_audio_context_clean_up() {
27
- /* Can't clean properly on windows... */
28
- #ifndef SAY_WIN
29
27
  alcMakeContextCurrent(NULL);
30
28
 
31
29
  if (say_audio_context)
@@ -35,6 +33,5 @@ void say_audio_context_clean_up() {
35
33
  alcCloseDevice(say_audio_device);
36
34
 
37
35
  say_audio_context = NULL;
38
- say_audio_device = NULL;
39
- #endif
36
+ say_audio_device = NULL;
40
37
  }
@@ -15,3 +15,27 @@ bool say_rect_eq(say_rect a, say_rect b) {
15
15
  bool say_color_eq(say_color a, say_color b) {
16
16
  return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
17
17
  }
18
+
19
+ say_color *say_flip_color_buffer_copy(say_color *buf, size_t width,
20
+ size_t height) {
21
+ size_t line_size = sizeof(say_color) * width;
22
+ say_color *ret = malloc(line_size * height);
23
+
24
+ for (size_t y = 0; y < height; y++)
25
+ memcpy(&ret[y * width], &buf[(height - y - 1) * width], line_size);
26
+
27
+ return ret;
28
+ }
29
+
30
+ void say_flip_color_buffer(say_color *buffer, size_t width, size_t height) {
31
+ size_t line_size = sizeof(say_color) * width;
32
+ say_color *temp_line = malloc(line_size);
33
+
34
+ for (size_t y = 0; y < height / 2.0; y++) {
35
+ memcpy(temp_line, &buffer[y * width], line_size);
36
+ memcpy(&buffer[y * width], &buffer[(height - y - 1) * width], line_size);
37
+ memcpy(&buffer[(height - y - 1) * width], temp_line, line_size);
38
+ }
39
+
40
+ free(temp_line);
41
+ }
@@ -44,4 +44,8 @@ bool say_color_eq(say_color a, say_color b);
44
44
  typedef void (*say_destructor)(void *data);
45
45
  typedef void (*say_creator)(void *data);
46
46
 
47
+ say_color *say_flip_color_buffer_copy(say_color *buffer, size_t width,
48
+ size_t height);
49
+ void say_flip_color_buffer(say_color *buffer, size_t width, size_t height);
50
+
47
51
  #endif
@@ -1,8 +1,8 @@
1
1
  #include "say.h"
2
2
 
3
3
  static bool say_has_vao() {
4
- return __GLEW_ARB_vertex_array_object ||
5
- __GLEW_APPLE_vertex_array_object;
4
+ return GLEW_ARB_vertex_array_object ||
5
+ GLEW_VERSION_3_0;
6
6
  }
7
7
 
8
8
  static GLuint say_current_vbo = 0;
@@ -16,11 +16,11 @@ static void say_vbo_make_current(GLuint vbo) {
16
16
  say_current_vbo = vbo;
17
17
  say_vbo_last_context = context;
18
18
 
19
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
19
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
20
20
  }
21
21
  }
22
22
 
23
- static GLuint say_current_vao = 0;
23
+ static GLuint say_current_vao = 0;
24
24
  static say_context *say_vao_last_context = NULL;
25
25
 
26
26
  typedef struct {
@@ -37,6 +37,8 @@ static void say_vao_make_current(GLuint vao) {
37
37
  say_vao_last_context = context;
38
38
 
39
39
  glBindVertexArray(vao);
40
+
41
+ say_index_buffer_rebind();
40
42
  }
41
43
  }
42
44
 
@@ -72,9 +74,46 @@ static void say_buffer_delete_vao_pair(say_vao_pair *pair) {
72
74
  if (say_vao_last_context == pair->context && say_current_vao == pair->vao) {
73
75
  say_current_vao = 0;
74
76
  glDeleteVertexArrays(1, &pair->vao);
77
+
78
+ say_index_buffer_rebind();
79
+ }
80
+ }
81
+
82
+ static size_t say_buffer_register_pointer(size_t attr_i, say_vertex_elem_type t,
83
+ size_t stride, size_t offset) {
84
+ switch (t) {
85
+ case SAY_FLOAT:
86
+ glVertexAttribPointer(attr_i, 1, GL_FLOAT, GL_FALSE, stride,
87
+ (void*)offset);
88
+ return offset + sizeof(GLfloat);
89
+ case SAY_INT:
90
+ glVertexAttribPointer(attr_i, 1, GL_INT, GL_FALSE, stride,
91
+ (void*)offset);
92
+ return offset + sizeof(GLint);
93
+ case SAY_UBYTE:
94
+ glVertexAttribPointer(attr_i, 1, GL_UNSIGNED_BYTE, GL_FALSE, stride,
95
+ (void*)offset);
96
+ return offset + sizeof(GLubyte);
97
+ case SAY_BOOL:
98
+ glVertexAttribPointer(attr_i, 1, GL_INT, GL_FALSE, stride,
99
+ (void*)offset);
100
+ return offset + sizeof(GLint);
101
+
102
+ case SAY_COLOR:
103
+ glVertexAttribPointer(attr_i, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
104
+ (void*)offset);
105
+ return offset + sizeof(GLubyte) * 4;
106
+ case SAY_VECTOR2:
107
+ glVertexAttribPointer(attr_i, 2, GL_FLOAT, GL_FALSE, stride,
108
+ (void*)offset);
109
+ return offset + sizeof(GLfloat) * 2;
110
+ case SAY_VECTOR3:
111
+ glVertexAttribPointer(attr_i, 3, GL_FLOAT, GL_FALSE, stride,
112
+ (void*)offset);
113
+ return offset + sizeof(GLfloat) * 3;
75
114
  }
76
115
 
77
- free(pair);
116
+ return offset;
78
117
  }
79
118
 
80
119
  static void say_buffer_setup_pointer(say_buffer *buf) {
@@ -82,64 +121,69 @@ static void say_buffer_setup_pointer(say_buffer *buf) {
82
121
 
83
122
  say_vertex_type *type = say_get_vertex_type(buf->vtype);
84
123
 
85
- size_t count = say_vertex_type_get_elem_count(type);
86
- size_t stride = say_vertex_type_get_size(type);
124
+ size_t count = say_vertex_type_get_elem_count(type);
125
+ if (count == 0) /* No attrbutes to set */
126
+ return;
87
127
 
128
+ size_t stride = say_vertex_type_get_size(type);
88
129
  size_t offset = 0;
89
130
 
131
+ size_t instance_stride = say_vertex_type_get_instance_size(type);
132
+ size_t instance_offset = 0;
133
+
134
+ /*
135
+ * This fixes a bug on OSX (with OpenGL 2.1). Nothing is drawn unless vertex
136
+ * attribute 0 is enabled.
137
+ *
138
+ * We set its data to the same as those used by the first element to ensure
139
+ * memory we do not own isn't accessed.
140
+ */
141
+ say_vertex_elem_type t = say_vertex_type_get_type(type, 0);
142
+
143
+ if (say_vertex_type_is_per_instance(type, 0)) {
144
+ say_vbo_make_current(buf->instance_vbo);
145
+ say_buffer_register_pointer(0, t, instance_stride, instance_offset);
146
+ }
147
+ else {
148
+ say_vbo_make_current(buf->vbo);
149
+ say_buffer_register_pointer(0, t, stride, offset);
150
+ }
151
+
152
+ glEnableVertexAttribArray(0);
153
+
90
154
  size_t i = 0;
91
155
  for (; i < count; i++) {
92
- say_vertex_elem_type t = say_vertex_type_get_type(type, i);
156
+ t = say_vertex_type_get_type(type, i);
93
157
 
94
- switch (t) {
95
- case SAY_FLOAT:
96
- glVertexAttribPointerARB(i, 1, GL_FLOAT, GL_FALSE, stride, (void*)offset);
97
- offset += sizeof(GLfloat);
98
- break;
99
- case SAY_INT:
100
- glVertexAttribPointerARB(i, 1, GL_INT, GL_FALSE, stride, (void*)offset);
101
- offset += sizeof(GLint);
102
- break;
103
- case SAY_UBYTE:
104
- glVertexAttribPointerARB(i, 1, GL_UNSIGNED_BYTE, GL_FALSE, stride,
105
- (void*)offset);
106
- offset += sizeof(GLubyte);
107
- break;
108
- case SAY_BOOL:
109
- glVertexAttribPointerARB(i, 1, GL_INT, GL_FALSE, stride, (void*)offset);
110
- offset += sizeof(GLint);
111
- break;
112
-
113
- case SAY_COLOR:
114
- glVertexAttribPointerARB(i, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride,
115
- (void*)offset);
116
- offset += sizeof(GLubyte) * 4;
117
- break;
118
- case SAY_VECTOR2:
119
- glVertexAttribPointerARB(i, 2, GL_FLOAT, GL_FALSE, stride,
120
- (void*)offset);
121
- offset += sizeof(GLfloat) * 2;
122
- break;
123
- case SAY_VECTOR3:
124
- glVertexAttribPointerARB(i, 3, GL_FLOAT, GL_FALSE, stride,
125
- (void*)offset);
126
- offset += sizeof(GLfloat) * 3;
127
- break;
158
+ if (say_vertex_type_is_per_instance(type, i)) {
159
+ say_vbo_make_current(buf->instance_vbo);
160
+ instance_offset = say_buffer_register_pointer(i + 1, t, instance_stride,
161
+ instance_offset);
162
+ }
163
+ else {
164
+ say_vbo_make_current(buf->vbo);
165
+ offset = say_buffer_register_pointer(i + 1, t, stride, offset);
128
166
  }
129
167
 
130
- glEnableVertexAttribArrayARB(i);
168
+ glEnableVertexAttribArray(i + 1);
169
+ if (glVertexAttribDivisor) {
170
+ if (say_vertex_type_is_per_instance(type, i))
171
+ glVertexAttribDivisor(i + 1, 1);
172
+ else
173
+ glVertexAttribDivisor(i + 1, 0);
174
+ }
131
175
  }
132
176
 
133
177
  /*
134
178
  * Say will always use all the attribs. Disable all of them until
135
179
  * finding one that is already disabled.
136
180
  */
137
- for (; i < GL_MAX_VERTEX_ATTRIBS_ARB; i++) {
181
+ for (; i < GL_MAX_VERTEX_ATTRIBS - 1; i++) {
138
182
  GLint enabled;
139
- glGetVertexAttribivARB(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB, &enabled);
183
+ glGetVertexAttribiv(i + 1, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
140
184
 
141
185
  if (enabled)
142
- glDisableVertexAttribArrayARB(i);
186
+ glDisableVertexAttribArray(i + 1);
143
187
  else
144
188
  break;
145
189
  }
@@ -147,29 +191,24 @@ static void say_buffer_setup_pointer(say_buffer *buf) {
147
191
 
148
192
  static void say_buffer_build_vao(say_buffer *buf, GLuint vao) {
149
193
  say_vao_make_current(vao);
150
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf->vbo); /* forcefully bind */
151
194
  say_buffer_setup_pointer(buf);
152
195
  }
153
196
 
154
197
  static GLuint say_buffer_get_vao(say_buffer *buf) {
155
198
  say_context *ctxt = say_context_current();
156
- uint32_t count = ctxt->count;
157
-
158
- say_vao_pair *pair = say_table_get(buf->vaos, count);
159
199
 
160
- if (!pair) {
161
- pair = malloc(sizeof(say_vao_pair));
162
- say_table_set(buf->vaos, count, pair);
200
+ if (!mo_hash_has_key(buf->vaos, &ctxt)) {
201
+ say_vao_pair pair;
202
+ pair.context = ctxt;
203
+ glGenVertexArrays(1, &pair.vao);
204
+ say_buffer_build_vao(buf, pair.vao);
163
205
 
164
- pair->context = ctxt;
206
+ mo_hash_set(buf->vaos, &ctxt, &pair);
165
207
 
166
- glGenVertexArrays(1, &pair->vao);
167
- say_buffer_build_vao(buf, pair->vao);
168
-
169
- return pair->vao;
208
+ return pair.vao;
170
209
  }
171
210
  else {
172
- return pair->vao;
211
+ return mo_hash_get_ptr(buf->vaos, &ctxt, say_vao_pair)->vao;
173
212
  }
174
213
  }
175
214
 
@@ -178,23 +217,40 @@ say_buffer *say_buffer_create(size_t vtype, GLenum type, size_t size) {
178
217
 
179
218
  say_buffer *buf = (say_buffer*)malloc(sizeof(say_buffer));
180
219
 
181
- if (say_has_vao())
182
- buf->vaos = say_table_create((say_destructor)say_buffer_delete_vao_pair);
220
+ if (say_has_vao()) {
221
+ buf->vaos = mo_hash_create(sizeof(say_context*), sizeof(say_vao_pair));
222
+ buf->vaos->release = (say_destructor)say_buffer_delete_vao_pair;
223
+ buf->vaos->hash_of = mo_hash_of_pointer;
224
+ buf->vaos->key_cmp = mo_hash_pointer_cmp;
225
+
226
+ }
183
227
  else
184
228
  buf->vaos = NULL;
185
229
 
186
230
  buf->vtype = vtype;
187
231
 
188
- glGenBuffersARB(1, &buf->vbo);
232
+ glGenBuffers(1, &buf->vbo);
189
233
  say_vbo_make_current(buf->vbo);
190
234
 
191
235
  buf->type = type;
192
236
 
193
- size_t byte_size = say_vertex_type_get_size(say_get_vertex_type(vtype));
194
- buf->buffer = say_array_create(byte_size, NULL, NULL);
195
- say_array_resize(buf->buffer, size);
237
+ say_vertex_type *vtype_ref = say_get_vertex_type(vtype);
238
+ size_t byte_size = say_vertex_type_get_size(vtype_ref);
239
+ mo_array_init(&buf->buffer, byte_size);
240
+ mo_array_resize(&buf->buffer, size);
241
+
242
+ glBufferData(GL_ARRAY_BUFFER, size * byte_size, NULL, type);
196
243
 
197
- glBufferDataARB(GL_ARRAY_BUFFER_ARB, size * byte_size, NULL, type);
244
+ buf->instance_vbo = 0;
245
+ buf->instance_buffer = NULL;
246
+
247
+ if (say_vertex_type_has_instance_data(vtype_ref)) {
248
+ glGenBuffers(1, &buf->instance_vbo);
249
+ say_vbo_make_current(buf->instance_vbo);
250
+
251
+ byte_size = say_vertex_type_get_instance_size(vtype_ref);
252
+ buf->instance_buffer = mo_array_create(byte_size);
253
+ }
198
254
 
199
255
  return buf;
200
256
  }
@@ -203,19 +259,34 @@ void say_buffer_free(say_buffer *buf) {
203
259
  say_context_ensure();
204
260
 
205
261
  if (buf->vaos)
206
- say_table_free(buf->vaos);
262
+ mo_hash_free(buf->vaos);
207
263
  else
208
264
  say_buffer_will_delete(buf);
209
265
 
210
266
  say_vbo_will_delete(buf->vbo);
211
- glDeleteBuffersARB(1, &(buf->vbo));
267
+ glDeleteBuffers(1, &buf->vbo);
268
+
269
+ if (buf->instance_vbo) {
270
+ say_vbo_will_delete(buf->instance_vbo);
271
+ glDeleteBuffers(1, &buf->instance_vbo);
272
+
273
+ mo_array_free(buf->instance_buffer);
274
+ }
212
275
 
213
- say_array_free(buf->buffer);
276
+ mo_array_release(&buf->buffer);
214
277
  free(buf);
215
278
  }
216
279
 
280
+ bool say_buffer_has_instance(say_buffer *buf) {
281
+ return buf->instance_buffer != NULL;
282
+ }
283
+
217
284
  void *say_buffer_get_vertex(say_buffer *buf, size_t id) {
218
- return say_array_get(buf->buffer, id);
285
+ return mo_array_at(&buf->buffer, id);
286
+ }
287
+
288
+ void *say_buffer_get_instance(say_buffer *buf, size_t id) {
289
+ return mo_array_at(buf->instance_buffer, id);
219
290
  }
220
291
 
221
292
  void say_buffer_bind(say_buffer *buf) {
@@ -227,51 +298,109 @@ void say_buffer_bind(say_buffer *buf) {
227
298
  say_buffer_make_current(buf);
228
299
  }
229
300
 
301
+ void say_buffer_bind_vbo(say_buffer *buf) {
302
+ say_context_ensure();
303
+ say_vbo_make_current(buf->vbo);
304
+ }
305
+
306
+ void say_buffer_bind_instance_vbo(say_buffer *buf) {
307
+ say_context_ensure();
308
+ say_vbo_make_current(buf->instance_vbo);
309
+ }
310
+
230
311
  void say_buffer_unbind() {
231
- say_vbo_make_current(0);
312
+ say_context_ensure();
232
313
 
233
314
  if (say_has_vao())
234
315
  say_vao_make_current(0);
235
316
  else { /* disable vertex attribs */
236
- for (size_t i = 0; i < GL_MAX_VERTEX_ATTRIBS_ARB; i++) {
317
+ for (size_t i = 1; i < GL_MAX_VERTEX_ATTRIBS; i++) {
237
318
  GLint enabled;
238
- glGetVertexAttribivARB(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED_ARB, &enabled);
319
+ glGetVertexAttribiv(i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
239
320
 
240
321
  if (enabled)
241
- glDisableVertexAttribArrayARB(i);
322
+ glDisableVertexAttribArray(i);
242
323
  else
243
324
  break;
244
325
  }
245
326
  }
246
327
  }
247
328
 
329
+ void say_buffer_unbind_vbo() {
330
+ say_context_ensure();
331
+ say_vbo_make_current(0);
332
+ }
333
+
248
334
  void say_buffer_update_part(say_buffer *buf, size_t id, size_t size) {
249
335
  if (size == 0) return;
250
336
 
251
337
  say_context_ensure();
252
- say_buffer_bind(buf);
253
338
 
254
- size_t byte_size = say_array_get_elem_size(buf->buffer);
255
- glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
256
- byte_size * id,
257
- byte_size * size,
258
- say_buffer_get_vertex(buf, id));
339
+ size_t byte_size = buf->buffer.el_size;
340
+ say_vbo_make_current(buf->vbo);
341
+ glBufferSubData(GL_ARRAY_BUFFER,
342
+ byte_size * id,
343
+ byte_size * size,
344
+ say_buffer_get_vertex(buf, id));
259
345
  }
260
346
 
261
347
  void say_buffer_update(say_buffer *buf) {
262
- say_buffer_update_part(buf, 0, say_array_get_size(buf->buffer));
348
+ say_buffer_update_part(buf, 0, buf->buffer.size);
263
349
  }
264
350
 
265
351
  size_t say_buffer_get_size(say_buffer *buf) {
266
- return say_array_get_size(buf->buffer);
352
+ return buf->buffer.size;
267
353
  }
268
354
 
269
355
  void say_buffer_resize(say_buffer *buf, size_t size) {
270
- say_array_resize(buf->buffer, size);
356
+ mo_array_resize(&buf->buffer, size);
357
+
358
+ say_context_ensure();
359
+ say_vbo_make_current(buf->vbo);
360
+ glBufferData(GL_ARRAY_BUFFER,
361
+ size * buf->buffer.el_size,
362
+ say_buffer_get_vertex(buf, 0),
363
+ buf->type);
364
+ }
365
+
366
+ void say_buffer_update_instance_part(say_buffer *buf, size_t id,
367
+ size_t size) {
368
+ if (size == 0) return;
369
+
370
+ say_context_ensure();
371
+
372
+ size_t byte_size = buf->instance_buffer->el_size;
373
+ say_vbo_make_current(buf->instance_vbo);
374
+ glBufferSubData(GL_ARRAY_BUFFER,
375
+ byte_size * id,
376
+ byte_size * size,
377
+ say_buffer_get_instance(buf, id));
378
+ }
379
+
380
+ void say_buffer_update_instance(say_buffer *buf) {
381
+ say_buffer_update_instance_part(buf, 0,
382
+ buf->instance_buffer->size);
383
+ }
384
+
385
+ size_t say_buffer_get_instance_size(say_buffer *buf) {
386
+ return buf->instance_buffer->size;
387
+ }
388
+
389
+ void say_buffer_resize_instance(say_buffer *buf, size_t size) {
390
+ mo_array_resize(buf->instance_buffer, size);
391
+
392
+ say_context_ensure();
393
+ say_vbo_make_current(buf->instance_vbo);
394
+ glBufferData(GL_ARRAY_BUFFER,
395
+ size * buf->instance_buffer->el_size,
396
+ say_buffer_get_instance(buf, 0),
397
+ buf->type);
398
+ }
399
+
400
+ GLuint say_buffer_get_vbo(say_buffer *buf) {
401
+ return buf->vbo;
402
+ }
271
403
 
272
- say_buffer_bind(buf);
273
- glBufferDataARB(GL_ARRAY_BUFFER_ARB,
274
- size * say_array_get_elem_size(buf->buffer),
275
- say_buffer_get_vertex(buf, 0),
276
- buf->type);
404
+ GLuint say_buffer_get_instance_vbo(say_buffer *buf) {
405
+ return buf->instance_vbo;
277
406
  }