summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2018-09-20 21:09:43 -0700
committerTom Black <[email protected]>2018-09-20 21:09:43 -0700
commit23fea974a4369282905abf36f051bbbfc7e36863 (patch)
treeebd90209392aa4930647f02d7450bb5c3d8523c6
parent35a1c94eeef017413d0a20e68082441d2cd13264 (diff)
downloadruby2d-23fea974a4369282905abf36f051bbbfc7e36863.tar.gz
ruby2d-23fea974a4369282905abf36f051bbbfc7e36863.zip
Add `Circle` class
-rwxr-xr-xbin/ruby2d1
-rw-r--r--ext/ruby2d/extconf.rb2
-rw-r--r--ext/ruby2d/ruby2d.c31
-rw-r--r--lib/ruby2d.rb1
-rw-r--r--lib/ruby2d/circle.rb28
-rw-r--r--test/circle_spec.rb16
-rw-r--r--test/contains.rb1
-rw-r--r--test/testcard.rb43
8 files changed, 99 insertions, 24 deletions
diff --git a/bin/ruby2d b/bin/ruby2d
index c5a94bf..d1ff995 100755
--- a/bin/ruby2d
+++ b/bin/ruby2d
@@ -21,6 +21,7 @@ end
'dsl',
'quad',
'line',
+ 'circle',
'rectangle',
'square',
'triangle',
diff --git a/ext/ruby2d/extconf.rb b/ext/ruby2d/extconf.rb
index e6ab4a5..41cba4e 100644
--- a/ext/ruby2d/extconf.rb
+++ b/ext/ruby2d/extconf.rb
@@ -1,6 +1,6 @@
require 'mkmf'
-S2D_VERSION = '0.9.0' # Simple 2D minimum version required
+S2D_VERSION = '1.0.0' # Simple 2D minimum version required
$errors = []
class String
diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c
index c723e4f..d70381d 100644
--- a/ext/ruby2d/ruby2d.c
+++ b/ext/ruby2d/ruby2d.c
@@ -284,6 +284,31 @@ static R_VAL ruby2d_line_ext_render(R_VAL self) {
/*
+ * Ruby2D::Circle#ext_render
+ */
+#if MRUBY
+static R_VAL ruby2d_circle_ext_render(mrb_state* mrb, R_VAL self) {
+#else
+static R_VAL ruby2d_circle_ext_render(R_VAL self) {
+#endif
+ R_VAL c = r_iv_get(self, "@color");
+
+ S2D_DrawCircle(
+ NUM2DBL(r_iv_get(self, "@x")),
+ NUM2DBL(r_iv_get(self, "@y")),
+ NUM2DBL(r_iv_get(self, "@radius")),
+ NUM2DBL(r_iv_get(self, "@sectors")),
+ NUM2DBL(r_iv_get(c, "@r")),
+ NUM2DBL(r_iv_get(c, "@g")),
+ NUM2DBL(r_iv_get(c, "@b")),
+ NUM2DBL(r_iv_get(c, "@a"))
+ );
+
+ return R_NIL;
+}
+
+
+/*
* Ruby2D::Image#ext_init
* Initialize image structure data
*/
@@ -1035,6 +1060,12 @@ void Init_ruby2d() {
// Ruby2D::Line#ext_render
r_define_method(ruby2d_line_class, "ext_render", ruby2d_line_ext_render, r_args_none);
+ // Ruby2D::Circle
+ R_CLASS ruby2d_circle_class = r_define_class(ruby2d_module, "Circle");
+
+ // Ruby2D::Circle#ext_render
+ r_define_method(ruby2d_circle_class, "ext_render", ruby2d_circle_ext_render, r_args_none);
+
// Ruby2D::Image
R_CLASS ruby2d_image_class = r_define_class(ruby2d_module, "Image");
diff --git a/lib/ruby2d.rb b/lib/ruby2d.rb
index 823512f..6dc72dc 100644
--- a/lib/ruby2d.rb
+++ b/lib/ruby2d.rb
@@ -7,6 +7,7 @@ require 'ruby2d/window'
require 'ruby2d/dsl'
require 'ruby2d/quad'
require 'ruby2d/line'
+require 'ruby2d/circle'
require 'ruby2d/rectangle'
require 'ruby2d/square'
require 'ruby2d/triangle'
diff --git a/lib/ruby2d/circle.rb b/lib/ruby2d/circle.rb
new file mode 100644
index 0000000..4f87828
--- /dev/null
+++ b/lib/ruby2d/circle.rb
@@ -0,0 +1,28 @@
+# circle.rb
+
+module Ruby2D
+ class Circle
+ include Renderable
+
+ attr_reader :color
+ attr_accessor :x, :y, :radius, :sectors
+
+ def initialize(opts = {})
+ @x = opts[:x] || 25
+ @y = opts[:y] || 25
+ @radius = opts[:radius] || 25
+ @sectors = opts[:sectors] || 20
+ @z = opts[:z] || 0
+ self.color = opts[:color] || 'white'
+ add
+ end
+
+ def color=(c)
+ @color = Color.from(c)
+ end
+
+ def contains?(x, y)
+ Math.sqrt((x - @x)**2 + (y - @y)**2) <= @radius
+ end
+ end
+end
diff --git a/test/circle_spec.rb b/test/circle_spec.rb
new file mode 100644
index 0000000..f100cde
--- /dev/null
+++ b/test/circle_spec.rb
@@ -0,0 +1,16 @@
+require 'ruby2d'
+
+RSpec.describe Ruby2D::Circle do
+
+ describe '#new' do
+ it "creates a white circle by default" do
+ circle = Circle.new
+ expect(circle.color).to be_a(Ruby2D::Color)
+ expect(circle.color.r).to eq(1)
+ expect(circle.color.g).to eq(1)
+ expect(circle.color.b).to eq(1)
+ expect(circle.color.a).to eq(1)
+ end
+ end
+
+end
diff --git a/test/contains.rb b/test/contains.rb
index f3b3c15..c5c6686 100644
--- a/test/contains.rb
+++ b/test/contains.rb
@@ -16,6 +16,7 @@ objects.push Rectangle.new(x: 200, y: 50, width: 100, height: 75)
objects.push Quad.new(x1: 350, y1: 50, x2: 500, y2: 75, x3: 450, y3: 150, x4: 375, y4: 125)
objects.push Triangle.new(x1: 550, y1: 50, x2: 600, y2: 125, x3: 500, y3: 150)
objects.push Line.new(x1: 225, y1: 175, x2: 375, y2: 225, width: 20)
+objects.push Circle.new(x: 225, y: 275, radius: 50)
objects.push Image.new(x: 50, y: 200, path: "#{media}/colors.png")
objects.push Text.new(x: 450, y: 200, text: "Hello", size: 50, font: font)
diff --git a/test/testcard.rb b/test/testcard.rb
index 876c5c4..c433aa4 100644
--- a/test/testcard.rb
+++ b/test/testcard.rb
@@ -35,7 +35,6 @@ Rectangle.new(x: 400, width: 50, height: 50, color: 'blue')
Rectangle.new(x: 450, width: 50, height: 50, color: 'aqua')
Rectangle.new(x: 500, width: 50, height: 50, color: 'teal')
Rectangle.new(x: 550, width: 50, height: 50, color: 'olive')
-
Rectangle.new(x: 150, y: 50, width: 50, height: 50, color: 'green')
Rectangle.new(x: 200, y: 50, width: 50, height: 50, color: 'lime')
Rectangle.new(x: 250, y: 50, width: 50, height: 50, color: 'yellow')
@@ -49,17 +48,6 @@ Rectangle.new(x: 550, y: 50, width: 50, height: 50, color: 'brown')
# Mix of named colors and numbers
Rectangle.new(
x: 600,
- width: 50,
- height: 50,
- color: [
- 'red',
- 'green',
- 'blue',
- 'yellow'
- ]
-)
-Rectangle.new(
- x: 650,
y: 0,
width: 50,
height: 50,
@@ -70,8 +58,18 @@ Rectangle.new(
'yellow'
]
)
+
+# Check opacity
+opacity_square = Square.new(
+ x: 650,
+ y: 0,
+ size: 50,
+ color: ['red', 'green', 'blue', 'yellow']
+)
+
+# Fill remaining area with random colors
Rectangle.new(x: 600, y: 50, width: 50, height: 50, color: 'random')
-Rectangle.new(x: 650, y: 50, width: 50, height: 50, color: 'random')
+Square.new(x: 650, y: 50, size: 50, color: 'random')
# White to black gradient
Rectangle.new(
@@ -199,12 +197,18 @@ Line.new(
]
);
+# Circles
+
+Circle.new(x: 525, y: 225, radius: 25, color: [1.0, 0.2, 0.2, 1.0])
+Circle.new(x: 575, y: 225, radius: 25, sectors: 8, color: [0.2, 1.0, 0.2, 1.0])
+Circle.new(x: 575, y: 225, radius: 17, sectors: 16, color: [0, 0, 0, 0.6])
+
rotate = false
# Images
-img_png = Image.new(x: 590, y: 180, path: "#{media}/image.png")
-img_jpg = Image.new(x: 590, y: 290, path: "#{media}/image.jpg")
-img_bmp = Image.new(x: 590, y: 400, path: "#{media}/image.bmp")
+img_png = Image.new(x: 600, y: 180, path: "#{media}/image.png")
+img_jpg = Image.new(x: 600, y: 290, path: "#{media}/image.jpg")
+img_bmp = Image.new(x: 600, y: 400, path: "#{media}/image.bmp")
img_r = Image.new(x: 400, y: 200, width: 50, height: 25, path: "#{media}/colors.png")
img_r.color = [1.0, 0.3, 0.3, 1.0]
img_g = Image.new(x: 400, y: 225, path: "#{media}/colors.png")
@@ -237,13 +241,6 @@ pointer = Square.new(size: 10)
pointer_outline = Square.new(size: 18, color: [0, 1, 0, 0])
flash = 0
-# Updating opacity
-opacity_square = Square.new(
- x: 500,
- y: 200,
- size: 50,
- color: ["red", "green", "blue", "yellow"]
-)
time_start = Time.now
# Text size