ray 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -312,3 +312,35 @@ void say_matrix_look_at(say_matrix *matrix,
312
312
 
313
313
  say_matrix_free(other);
314
314
  }
315
+
316
+ void say_matrix_set_transformation(say_matrix *matrix,
317
+ say_vector2 origin,
318
+ say_vector2 pos, float z,
319
+ say_vector2 scale,
320
+ float angle) {
321
+ angle = -angle * SAY_PI / 180;
322
+
323
+ float c = cosf(angle);
324
+ float s = sinf(angle);
325
+
326
+ float sx_cos = scale.x * c;
327
+ float sy_cos = scale.y * c;
328
+ float sx_sin = scale.x * s;
329
+ float sy_sin = scale.y * s;
330
+
331
+ float o_x = -origin.x * sx_cos - origin.y * sy_sin;
332
+ float o_y = +origin.x * sx_sin - origin.y * sy_cos;
333
+
334
+ float t_x = o_x + pos.x;
335
+ float t_y = o_y + pos.y;
336
+ float t_z = z;
337
+
338
+ float content[16] = {
339
+ +sx_cos, +sy_sin, 0, t_x,
340
+ -sx_sin, +sy_cos, 0, t_y,
341
+ 0, 0, 1, t_z,
342
+ 0, 0, 0, 1
343
+ };
344
+
345
+ say_matrix_set_content(matrix, content);
346
+ }
@@ -52,4 +52,10 @@ void say_matrix_look_at(say_matrix *matrix,
52
52
  float center_x, float center_y, float center_z,
53
53
  float up_x, float up_y, float up_z);
54
54
 
55
+ void say_matrix_set_transformation(say_matrix *matrix,
56
+ say_vector2 origin,
57
+ say_vector2 pos, float z,
58
+ say_vector2 scale,
59
+ float angle);
60
+
55
61
  #endif
@@ -79,11 +79,13 @@ static void *say_music_playback_thread(say_music *music) {
79
79
  }
80
80
  }
81
81
 
82
- /* sleep for 0.25s */
82
+ /* sleep for 0.1s */
83
+ static const int sleep_time = 100;
84
+
83
85
  #ifdef SAY_WIN
84
- Sleep(250);
86
+ Sleep(sleep_time);
85
87
  #else
86
- usleep(250 * 1000);
88
+ usleep(sleep_time * 1000);
87
89
  #endif
88
90
  }
89
91
 
@@ -1,36 +1,31 @@
1
1
  #include "say.h"
2
2
 
3
- static say_context *say_pack_pbo_last_context = NULL;
4
- static GLuint say_pack_pbo = 0;
5
-
6
3
  static void say_pack_pbo_make_current(GLuint pbo) {
7
4
  say_context *context = say_context_current();
8
- if (say_pack_pbo_last_context != context ||
9
- pbo != say_pack_pbo) {
10
- say_pack_pbo = pbo;
11
- say_pack_pbo_last_context = context;
12
5
 
6
+ if (context->pack_pbo != pbo) {
13
7
  glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
8
+ context->pack_pbo = pbo;
14
9
  }
15
10
  }
16
11
 
17
- static say_context *say_unpack_pbo_last_context = NULL;
18
- static GLuint say_unpack_pbo = 0;
19
-
20
12
  static void say_unpack_pbo_make_current(GLuint pbo) {
21
13
  say_context *context = say_context_current();
22
- if (say_unpack_pbo_last_context != context ||
23
- pbo != say_unpack_pbo) {
24
- say_unpack_pbo = pbo;
25
- say_unpack_pbo_last_context = context;
26
14
 
15
+ if (context->unpack_pbo != pbo) {
27
16
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
17
+ context->unpack_pbo = pbo;
28
18
  }
29
19
  }
30
20
 
31
21
  static void say_pbo_will_delete(GLuint pbo) {
32
- if (say_pack_pbo == pbo) say_pack_pbo = 0;
33
- if (say_unpack_pbo == pbo) say_unpack_pbo = 0;
22
+ mo_array *contexts = say_context_get_all();
23
+ for (size_t i = 0; i < contexts->size; i++) {
24
+ say_context *context = mo_array_get_as(contexts, i, say_context*);
25
+
26
+ if (context->pack_pbo == pbo) context->pack_pbo = 0;
27
+ if (context->unpack_pbo == pbo) context->unpack_pbo = 0;
28
+ }
34
29
  }
35
30
 
36
31
  bool say_pixel_bus_is_available() {
@@ -134,7 +129,9 @@ void say_pixel_bus_resize(say_pixel_bus *bus, size_t new_size) {
134
129
  say_pixel_bus_bind_pack(bus);
135
130
 
136
131
  void *buffer = malloc(sizeof(say_color) * new_size);
137
- glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, new_size, buffer);
132
+ size_t copied_size = new_size > bus->size ? bus->size : new_size;
133
+ glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, copied_size * sizeof(say_color),
134
+ buffer);
138
135
 
139
136
  glBufferData(GL_PIXEL_PACK_BUFFER, new_size * sizeof(say_color),
140
137
  buffer, bus->mode);
@@ -1,17 +1,20 @@
1
1
  #include "say.h"
2
2
 
3
- static GLuint say_current_program = 0;
4
- static say_context *say_shader_last_context = NULL;
5
-
6
3
  static void say_shader_make_current(GLuint program) {
7
4
  say_context *context = say_context_current();
8
5
 
9
- if (context != say_shader_last_context ||
10
- program != say_current_program) {
11
- say_current_program = program;
12
- say_shader_last_context = context;
13
-
6
+ if (context->program != program) {
14
7
  glUseProgram(program);
8
+ context->program = program;
9
+ }
10
+ }
11
+
12
+ static void say_shader_will_delete(GLuint program) {
13
+ mo_array *contexts = say_context_get_all();
14
+ for (size_t i = 0; i < contexts->size; i++) {
15
+ say_context *context = mo_array_get_as(contexts, i, say_context*);
16
+ if (context->program == program)
17
+ context->program = 0;
15
18
  }
16
19
  }
17
20
 
@@ -52,22 +55,6 @@ static void say_shader_find_locations(say_shader *shader) {
52
55
  glGetUniformLocation(shader->program, SAY_TEXTURE_ENABLED_ATTR);
53
56
  }
54
57
 
55
- static const char *say_default_frag_shader =
56
- "#version 110\n"
57
- "\n"
58
- "uniform sampler2D in_Texture;\n"
59
- "uniform bool in_TextureEnabled;\n"
60
- "\n"
61
- "varying vec4 var_Color;\n"
62
- "varying vec2 var_TexCoord;\n"
63
- "\n"
64
- "void main() {\n"
65
- " if (in_TextureEnabled)\n"
66
- " gl_FragColor = texture2D(in_Texture, var_TexCoord) * var_Color;\n"
67
- " else\n"
68
- " gl_FragColor = var_Color;\n"
69
- "}\n";
70
-
71
58
  static const char *say_default_vertex_shader =
72
59
  "#version 110\n"
73
60
  "\n"
@@ -87,22 +74,20 @@ static const char *say_default_vertex_shader =
87
74
  " var_TexCoord = in_TexCoord;\n"
88
75
  "}\n";
89
76
 
90
- static const char *say_new_default_frag_shader =
91
- "#version 130\n"
77
+ static const char *say_default_frag_shader =
78
+ "#version 110\n"
92
79
  "\n"
93
80
  "uniform sampler2D in_Texture;\n"
94
81
  "uniform bool in_TextureEnabled;\n"
95
82
  "\n"
96
- "in vec4 var_Color;\n"
97
- "in vec2 var_TexCoord;\n"
98
- "\n"
99
- "out vec4 out_FragColor;\n"
83
+ "varying vec4 var_Color;\n"
84
+ "varying vec2 var_TexCoord;\n"
100
85
  "\n"
101
86
  "void main() {\n"
102
87
  " if (in_TextureEnabled)\n"
103
- " out_FragColor = texture(in_Texture, var_TexCoord) * var_Color;\n"
88
+ " gl_FragColor = texture2D(in_Texture, var_TexCoord) * var_Color;\n"
104
89
  " else\n"
105
- " out_FragColor = var_Color;\n"
90
+ " gl_FragColor = var_Color;\n"
106
91
  "}\n";
107
92
 
108
93
  static const char *say_new_default_vertex_shader =
@@ -124,6 +109,24 @@ static const char *say_new_default_vertex_shader =
124
109
  " var_TexCoord = in_TexCoord;\n"
125
110
  "}\n";
126
111
 
112
+ static const char *say_new_default_frag_shader =
113
+ "#version 130\n"
114
+ "\n"
115
+ "uniform sampler2D in_Texture;\n"
116
+ "uniform bool in_TextureEnabled;\n"
117
+ "\n"
118
+ "in vec4 var_Color;\n"
119
+ "in vec2 var_TexCoord;\n"
120
+ "\n"
121
+ "out vec4 out_FragColor;\n"
122
+ "\n"
123
+ "void main() {\n"
124
+ " if (in_TextureEnabled)\n"
125
+ " out_FragColor = texture(in_Texture, var_TexCoord) * var_Color;\n"
126
+ " else\n"
127
+ " out_FragColor = var_Color;\n"
128
+ "}\n";
129
+
127
130
  static uint8_t say_shader_use_new = 0;
128
131
  static uint8_t say_shader_use_old_force = 0;
129
132
 
@@ -198,6 +201,7 @@ void say_shader_free(say_shader *shader) {
198
201
 
199
202
  say_shader_detach_geometry(shader);
200
203
 
204
+ say_shader_will_delete(shader->program);
201
205
  glDeleteProgram(shader->program);
202
206
 
203
207
  free(shader);
@@ -1,8 +1,5 @@
1
1
  #include "say.h"
2
2
 
3
- static say_target *say_current_target = NULL;
4
- static say_context *say_target_last_context = NULL;
5
-
6
3
  static void say_target_update_states(say_target *target) {
7
4
  if (target->up_to_date) {
8
5
  target->up_to_date = 0;
@@ -106,9 +103,8 @@ int say_target_make_current(say_target *target) {
106
103
  if (context) {
107
104
  say_context *current = say_context_current();
108
105
 
109
- if (current == say_target_last_context &&
110
- target == say_current_target &&
111
- current == context) {
106
+ if (current == context &&
107
+ current->target == target) {
112
108
  return 1;
113
109
  }
114
110
 
@@ -118,8 +114,7 @@ int say_target_make_current(say_target *target) {
118
114
  if (target->bind_hook)
119
115
  target->bind_hook(target->data);
120
116
 
121
- say_current_target = target;
122
- say_target_last_context = context;
117
+ current->target = target;
123
118
 
124
119
  return 1;
125
120
  }
@@ -7,13 +7,15 @@ unsigned long say_x11_event_mask =
7
7
  LeaveWindowMask;
8
8
 
9
9
  static void say_x11_window_find_config(say_x11_window *win) {
10
- static int visual_attribs[] = {
11
- GLX_DOUBLEBUFFER, GL_TRUE,
12
- GLX_RED_SIZE, 8,
13
- GLX_GREEN_SIZE, 8,
14
- GLX_BLUE_SIZE, 8,
15
- GLX_ALPHA_SIZE, 8,
16
- GLX_DEPTH_SIZE, 24,
10
+ say_context_config *say_conf = say_context_get_config();
11
+ int visual_attribs[] = {
12
+ GLX_RED_SIZE, 8,
13
+ GLX_GREEN_SIZE, 8,
14
+ GLX_BLUE_SIZE, 8,
15
+ GLX_ALPHA_SIZE, 8,
16
+ GLX_DEPTH_SIZE, say_conf->depth_size,
17
+ GLX_STENCIL_SIZE, say_conf->stencil_size,
18
+ GLX_DOUBLEBUFFER, True,
17
19
  None
18
20
  };
19
21
 
@@ -20,6 +20,7 @@ VALUE ray_sprite_alloc(VALUE self) {
20
20
 
21
21
  say_drawable_set_shader_proc(sprite->drawable, ray_drawable_shader_proc);
22
22
  say_drawable_set_other_data(sprite->drawable, (void*)rb);
23
+ rb_iv_set(rb, "@shader_attributes", Qnil);
23
24
 
24
25
  return rb;
25
26
  }
data/ext/text.c CHANGED
@@ -21,6 +21,8 @@ VALUE ray_text_alloc(VALUE self) {
21
21
 
22
22
  say_drawable_set_shader_proc(text->drawable, ray_drawable_shader_proc);
23
23
  say_drawable_set_other_data(text->drawable, (void*)rb);
24
+ rb_iv_set(rb, "@shader_attributes", Qnil);
25
+ rb_iv_set(rb, "@font", Qnil);
24
26
 
25
27
  return rb;
26
28
  }
@@ -37,17 +37,23 @@ module Ray
37
37
  new.look_at(eye, center, up)
38
38
  end
39
39
 
40
- # @param [Array<Float>] args (see #orthogonal)
40
+ # @param (see #orthogonal)
41
41
  # @return [Ray::Matrix] An orthogonal projection matrix
42
42
  def orthogonal(*args)
43
43
  new.orthogonal(*args)
44
44
  end
45
45
 
46
- # @param [Array<Float>] args (see #perspective)
46
+ # @param (see #perspective)
47
47
  # @return [Ray::Matrix] A perspective projection matrix
48
48
  def perspective(*args)
49
49
  new.perspective(*args)
50
50
  end
51
+
52
+ # @param (see #set_transformation)
53
+ # @return [Ray::Matrix] A 2D transformation matrix
54
+ def transformation(*args)
55
+ new.set_transformation(*args)
56
+ end
51
57
  end
52
58
 
53
59
  # @param [Array<Float>, nil] content Either nothing or the content of each
@@ -1,5 +1,5 @@
1
1
  require 'enumerator'
2
- require 'iconv'
2
+ require 'iconv' if RUBY_VERSION < "1.9"
3
3
 
4
4
  # Change PATH so as to find DLLs on windows.
5
5
  if RUBY_PLATFORM =~ /mingw/
@@ -103,10 +103,9 @@ module Ray
103
103
  @scene_always_block = nil
104
104
  @scene_render_block = nil
105
105
  @scene_clean_block = nil
106
-
107
106
  @scene_loops_per_second = 60
108
-
109
107
  @scene_animations = Ray::AnimationList.new
108
+ @scene_arguments = []
110
109
  end
111
110
 
112
111
  def register_events
@@ -14,7 +14,7 @@ class Ray::Turtle
14
14
 
15
15
  4.times do
16
16
  pen_down
17
- forward 2 * r * sin(PI / 4)
17
+ forward 2 * r * sin(Math::PI / 4)
18
18
 
19
19
  pen_up
20
20
  byzantium(r / 2, n - 1)
@@ -18,7 +18,7 @@ Ray::Game.new("Turtle") do
18
18
  center = target.clip.center
19
19
 
20
20
  poly = []
21
- theta = 360 * (PI / 180) / n
21
+ theta = 360 * (Math::PI / 180) / n
22
22
 
23
23
  n.times do |k|
24
24
  angle = k * theta
@@ -71,6 +71,12 @@ context "a drawable" do
71
71
  context "with several transformations" do
72
72
  attr = {:foo => 3}
73
73
 
74
+ transformation_matrix = Ray::Matrix.transformation([30, 40],
75
+ [10, 20],
76
+ 9,
77
+ [2, 0.5],
78
+ 90)
79
+
74
80
  hookup do
75
81
  topic.origin = [30, 40]
76
82
  topic.pos = [10, 20]
@@ -81,6 +87,9 @@ context "a drawable" do
81
87
  topic.shader_attributes = attr
82
88
  end
83
89
 
90
+ asserts(:default_matrix).equals transformation_matrix
91
+ asserts(:matrix).equals transformation_matrix
92
+
84
93
  # (10, 30) => (-20, -10) (origin)
85
94
  # (-20, -10) => (-40, -5) (scale)
86
95
  # (-40, -5) => (5, -40) (rotate)
@@ -113,6 +122,8 @@ context "a drawable" do
113
122
  end
114
123
 
115
124
  asserts(:matrix).equals Ray::Matrix.new
125
+ asserts(:default_matrix).equals transformation_matrix
126
+
116
127
  asserts(:transform, [10, 30]).equals Ray::Vector3[10, 30, 0]
117
128
 
118
129
  context "that has been disabled" do
@@ -136,6 +147,7 @@ context "a drawable" do
136
147
  end
137
148
 
138
149
  asserts(:matrix).equals Ray::Matrix.new
150
+ asserts(:default_matrix).equals transformation_matrix
139
151
  asserts(:matrix_proc).equals matrix_proc
140
152
  asserts(:matrix_proc).received(:call) { topic }
141
153
 
@@ -27,12 +27,15 @@ class AlmostEqualMacro < Riot::AssertionMacro
27
27
  register :almost_equals
28
28
 
29
29
  def test(actual, expected, epsilon)
30
- diff = (actual - expected)
31
- case diff
30
+ case actual
32
31
  when Numeric
33
- diff.abs <= epsilon
32
+ (actual - expected).abs <= epsilon
34
33
  when Ray::Vector2, Ray::Vector3
35
- diff.to_a.all? { |n| n.abs <= epsilon }
34
+ (actual - expected).to_a.all? { |n| n.abs <= epsilon }
35
+ when Ray::Matrix
36
+ actual.content.zip(expected.content).all? do |a, b|
37
+ (a - b).abs <= epsilon
38
+ end
36
39
  end
37
40
  end
38
41
 
@@ -25,6 +25,10 @@ context "an image loaded from a file" do
25
25
  asserts(:[]=, 128, 100, Ray::Color.green).raises_kind_of RangeError
26
26
  asserts(:[]=, 100, 192, Ray::Color.green).raises_kind_of RangeError
27
27
 
28
+ asserts(:bind_to, -1).raises_kind_of RangeError
29
+ asserts(:bind_to, 32).raises_kind_of RangeError
30
+ denies(:bind_to, 31).raises_kind_of RangeError
31
+
28
32
  context "after changing a pixel" do
29
33
  hookup { topic[0, 10] = Ray::Color.green }
30
34
  asserts(:[], 0, 10).equals Ray::Color.green