summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2018-12-11 23:34:37 -0800
committerTom Black <[email protected]>2021-04-12 11:30:39 -0500
commit31d4065684102b18b619c7e1f4496a97ed15e806 (patch)
treef048b54279f70ca1df20180d4b075890b25ebce4
parentbd45d89611aff095a65e22b64a3eb987bdda51b1 (diff)
downloadruby2d-31d4065684102b18b619c7e1f4496a97ed15e806.tar.gz
ruby2d-31d4065684102b18b619c7e1f4496a97ed15e806.zip
Create direct draw class methods
-rw-r--r--ext/ruby2d/ruby2d.c57
-rw-r--r--lib/ruby2d/dsl.rb4
-rw-r--r--lib/ruby2d/quad.rb7
-rw-r--r--lib/ruby2d/window.rb20
-rw-r--r--test/triangle.rb64
5 files changed, 143 insertions, 9 deletions
diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c
index 655a44a..1ab16ec 100644
--- a/ext/ruby2d/ruby2d.c
+++ b/ext/ruby2d/ruby2d.c
@@ -251,6 +251,54 @@ static R_VAL ruby2d_quad_ext_render(R_VAL self) {
}
+
+/*
+ * Ruby2D::Quad#ext_draw
+ */
+#if MRUBY
+static R_VAL ruby2d_quad_ext_draw(mrb_state* mrb, R_VAL self) {
+ mrb_value x, y, c;
+ mrb_get_args(mrb, "o", &x);
+ mrb_get_args(mrb, "o", &y);
+ mrb_get_args(mrb, "o", &c);
+#else
+static R_VAL ruby2d_quad_ext_draw(R_VAL self, R_VAL x, R_VAL y, R_VAL c) {
+#endif
+
+ S2D_DrawQuad(
+ NUM2DBL(x),
+ NUM2DBL(y),
+ NUM2DBL(c),
+ NUM2DBL(c),
+ NUM2DBL(c),
+ 1.0,
+
+ NUM2DBL(x) + 10,
+ NUM2DBL(y),
+ NUM2DBL(c),
+ NUM2DBL(c),
+ NUM2DBL(c),
+ 1.0,
+
+ NUM2DBL(x) + 10,
+ NUM2DBL(y) + 10,
+ NUM2DBL(c),
+ NUM2DBL(c),
+ NUM2DBL(c),
+ 1.0,
+
+ NUM2DBL(x),
+ NUM2DBL(y) + 10,
+ NUM2DBL(c),
+ NUM2DBL(c),
+ NUM2DBL(c),
+ 1.0
+ );
+
+ return R_NIL;
+}
+
+
/*
* Ruby2D::Line#ext_render
*/
@@ -953,6 +1001,9 @@ static void render() {
R_VAL el = r_ary_entry(objects, i);
r_funcall(el, "ext_render", 0);
}
+
+ // Call render proc, `window.render`
+ r_funcall(ruby2d_window, "render_callback", 0);
}
@@ -1137,6 +1188,12 @@ void Init_ruby2d() {
// Ruby2D::Quad#ext_render
r_define_method(ruby2d_quad_class, "ext_render", ruby2d_quad_ext_render, r_args_none);
+ // Ruby2D::Quad#self.ext_draw
+ r_define_class_method(ruby2d_quad_class, "ext_draw", ruby2d_quad_ext_draw, r_args_req(3));
+
+ // // Ruby2D::Square
+ // R_CLASS ruby2d_square_class = r_define_class(ruby2d_module, "Square");
+
// Ruby2D::Line
R_CLASS ruby2d_line_class = r_define_class(ruby2d_module, "Line");
diff --git a/lib/ruby2d/dsl.rb b/lib/ruby2d/dsl.rb
index 62352b4..89cd166 100644
--- a/lib/ruby2d/dsl.rb
+++ b/lib/ruby2d/dsl.rb
@@ -24,6 +24,10 @@ module Ruby2D::DSL
Window.update(&proc)
end
+ def render(&proc)
+ Window.render(&proc)
+ end
+
def clear
Window.clear
end
diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb
index 08cee84..a333850 100644
--- a/lib/ruby2d/quad.rb
+++ b/lib/ruby2d/quad.rb
@@ -48,6 +48,13 @@ module Ruby2D
questioned_area <= self_area
end
+
+ def self.draw(x, y, c)
+ ext_draw(x, y, c)
+ end
+
+
+
private
def triangle_area(x1, y1, x2, y2, x3, y3)
diff --git a/lib/ruby2d/window.rb b/lib/ruby2d/window.rb
index dea4f26..6c151b0 100644
--- a/lib/ruby2d/window.rb
+++ b/lib/ruby2d/window.rb
@@ -83,6 +83,9 @@ module Ruby2D
# The window update block
@update_proc = Proc.new {}
+ # The window render block
+ @render_proc = Proc.new {}
+
# Whether diagnostic messages should be printed
@diagnostics = false
@@ -149,6 +152,10 @@ module Ruby2D
@@window.update(&proc)
end
+ def render(&proc)
+ @@window.render(&proc)
+ end
+
def show
@@window.show
end
@@ -245,12 +252,18 @@ module Ruby2D
@objects.clear
end
- # Set an update callback
+ # Set the update callback
def update(&proc)
@update_proc = proc
true
end
+ # Set the render callback
+ def render(&proc)
+ @render_proc = proc
+ true
+ end
+
# Generate a new event key (ID)
def new_event_key
@event_key = @event_key.next
@@ -387,6 +400,11 @@ module Ruby2D
end
+ # Render callback method, called by the native and web extentions
+ def render_callback
+ @render_proc.call
+ end
+
# Show the window
def show
ext_show
diff --git a/test/triangle.rb b/test/triangle.rb
index df5cad0..b45e03e 100644
--- a/test/triangle.rb
+++ b/test/triangle.rb
@@ -1,12 +1,60 @@
require 'ruby2d'
-set title: "Hello Triangle"
-
-Triangle.new(
- x1: 320, y1: 50,
- x2: 540, y2: 430,
- x3: 100, y3: 430,
- color: ['red', 'green', 'blue']
-)
+# set title: "Hello Triangle"
+#
+# Triangle.new(
+# x1: 320, y1: 50,
+# x2: 540, y2: 430,
+# x3: 100, y3: 430,
+# color: ['red', 'green', 'blue']
+# )
+
+set width: 1280, height: 770
+
+# # Runs at 6 fps
+# 120.times do |i|
+# 160.times do |j|
+# s = Square.new(x: j*10, y: i*10, size: 10, color: 'random')
+# end
+# end
+
+# Runs at 11 fps
+# 128.times do |i|
+# 77.times do |j|
+# s = Square.new(x: i*10, y: j*10, size: 10, color: 'random')
+# end
+# end
+
+fps = Text.new 'fps'
+
+update do
+ fps.text = Window.fps
+end
+
+render do
+# # 120.times do |i|
+# # 160.times do |j|
+# # Quad.draw(j*10, i*10)
+# # end
+# # end
+#
+# # 19200.times do
+# # Quad.draw(0, 0)
+# # end
+#
+# 120.times do |i|
+# 20.times do |j|
+# Quad.draw(j*10, i*10 + 20, rand)
+# end
+# end
+#
+
+ 128.times do |i|
+ 77.times do |j|
+ Quad.draw(i*10, j*10 + 20, rand)
+ end
+ end
+
+end
show