Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to draw directly #142

Merged
merged 9 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
395 changes: 227 additions & 168 deletions ext/ruby2d/ruby2d.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/ruby2d.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'ruby2d/rectangle'
require 'ruby2d/square'
require 'ruby2d/triangle'
require 'ruby2d/pixel'
require 'ruby2d/image'
require 'ruby2d/sprite'
require 'ruby2d/font'
Expand Down
20 changes: 18 additions & 2 deletions lib/ruby2d/circle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize(opts = {})
@x = opts[:x] || 25
@y = opts[:y] || 25
@z = opts[:z] || 0
@radius = opts[:radius] || 25
@sectors = opts[:sectors] || 20
@radius = opts[:radius] || 50
@sectors = opts[:sectors] || 30
self.color = opts[:color] || 'white'
self.opacity = opts[:opacity] if opts[:opacity]
add
Expand All @@ -21,5 +21,21 @@ def contains?(x, y)
Math.sqrt((x - @x)**2 + (y - @y)**2) <= @radius
end

def self.draw(opts = {})
ext_draw([
opts[:x], opts[:y], opts[:radius], opts[:sectors],
opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
])
end

private

def render
self.class.ext_draw([
@x, @y, @radius, @sectors,
@color.r, @color.g, @color.b, @color.a
])
end

end
end
4 changes: 4 additions & 0 deletions lib/ruby2d/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def update(&proc)
Window.update(&proc)
end

def render(&proc)
Window.render(&proc)
end

def clear
Window.clear
end
Expand Down
25 changes: 24 additions & 1 deletion lib/ruby2d/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,30 @@ def initialize(path, opts = {})
unless ext_init(@path)
raise Error, "Image `#{@path}` cannot be created"
end
add
unless opts[:show] == false then add end
end

def draw(opts = {})
opts[:width] = opts[:width] || @width
opts[:height] = opts[:height] || @height
opts[:rotate] = opts[:rotate] || @rotate
unless opts[:color]
opts[:color] = [1.0, 1.0, 1.0, 1.0]
end

self.class.ext_draw([
self, opts[:x], opts[:y], opts[:width], opts[:height], opts[:rotate],
opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
])
end

private

def render
self.class.ext_draw([
self, @x, @y, @width, @height, @rotate,
@color.r, @color.g, @color.b, @color.a
])
end

end
Expand Down
20 changes: 20 additions & 0 deletions lib/ruby2d/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,28 @@ def contains?(x, y)
(((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) <= 0.5 * @width
end

def self.draw(opts = {})
ext_draw([
opts[:x1], opts[:y1], opts[:x2], opts[:y2], opts[:width],
opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
])
end

private

def render
self.class.ext_draw([
@x1, @y1, @x2, @y2, @width,
@c1.r, @c1.g, @c1.b, @c1.a,
@c2.r, @c2.g, @c2.b, @c2.a,
@c3.r, @c3.g, @c3.b, @c3.a,
@c4.r, @c4.g, @c4.b, @c4.a
])
end

# Calculate the distance between two points
def points_distance(x1, y1, x2, y2)
Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
Expand Down
17 changes: 17 additions & 0 deletions lib/ruby2d/pixel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ruby2D::Pixel

module Ruby2D
class Pixel

def self.draw(opts = {})
ext_draw([
opts[:x] , opts[:y],
opts[:x] + opts[:size], opts[:y],
opts[:x] + opts[:size], opts[:y] + opts[:size],
opts[:x] , opts[:y] + opts[:size],
opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
])
end

end
end
18 changes: 18 additions & 0 deletions lib/ruby2d/quad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,26 @@ def contains?(x, y)
questioned_area <= self_area
end

def self.draw(opts = {})
ext_draw([
opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
opts[:x4], opts[:y4], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
])
end

private

def render
self.class.ext_draw([
@x1, @y1, @c1.r, @c1.g, @c1.b, @c1.a,
@x2, @y2, @c2.r, @c2.g, @c2.b, @c2.a,
@x3, @y3, @c3.r, @c3.g, @c3.b, @c3.a,
@x4, @y4, @c4.r, @c4.g, @c4.b, @c4.a
])
end

def triangle_area(x1, y1, x2, y2, x3, y3)
(x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
end
Expand Down
13 changes: 11 additions & 2 deletions lib/ruby2d/rectangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@ def height=(h)
update_coords(@x, @y, @width, h)
end

def self.draw(opts = {})
ext_draw([
opts[:x] , opts[:y] , opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
opts[:x] + opts[:width], opts[:y] , opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
opts[:x] + opts[:width], opts[:y] + opts[:height], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
opts[:x] , opts[:y] + opts[:height], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
])
end

private

def update_coords(x, y, w, h)
@x1 = x
@y1 = y
@x2 = x + w
@y2 = y
@x4 = x
@y4 = y + h
@x3 = x + w
@y3 = y + h
@x4 = x
@y4 = y + h
end

end
Expand Down
38 changes: 35 additions & 3 deletions lib/ruby2d/sprite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(path, opts = {})
@last_frame = 0
@done_proc = nil

# The sprite image size set by the native extension `ext_init()`
# The sprite image size set by the native extension `ext_init`
@img_width = nil; @img_height = nil

# Initialize the sprite
Expand Down Expand Up @@ -72,7 +72,7 @@ def initialize(path, opts = {})
}

# Add the sprite to the window
add
unless opts[:show] == false then add end
end

# Set the x coordinate
Expand Down Expand Up @@ -141,6 +141,7 @@ def play(opts = {}, &done_proc)
set_frame
restart_time
end
self
end

# Stop the current animation and set to the default frame
Expand Down Expand Up @@ -221,7 +222,7 @@ def restart_time
@start_time = Time.now.to_f
end

# Update the sprite animation, called by `Sprite#ext_render`
# Update the sprite animation, called by `render`
def update
if @playing

Expand All @@ -246,5 +247,36 @@ def update
end
end

def draw(opts = {})
opts[:width] = opts[:width] || @flip_width
opts[:height] = opts[:height] || @flip_height
opts[:rotate] = opts[:rotate] || @rotate
opts[:clip_x] = opts[:clip_x] || @clip_x
opts[:clip_y] = opts[:clip_y] || @clip_y
opts[:clip_width] = opts[:clip_width] || @clip_width
opts[:clip_height] = opts[:clip_height] || @clip_height
unless opts[:color]
opts[:color] = [1.0, 1.0, 1.0, 1.0]
end

self.class.ext_draw([
self, opts[:x], opts[:y], opts[:width], opts[:height], opts[:rotate],
opts[:clip_x], opts[:clip_y], opts[:clip_width], opts[:height],
opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
])
end

private

def render
update
self.class.ext_draw([
self, @flip_x, @flip_y, @flip_width, @flip_height, @rotate,
@clip_x, @clip_y, @clip_width, @clip_height,
@color.r, @color.g, @color.b, @color.a
])
end


end
end
9 changes: 9 additions & 0 deletions lib/ruby2d/square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def size=(s)
self.width = self.height = @size = s
end

def self.draw(opts = {})
ext_draw([
opts[:x] , opts[:y] , opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
opts[:x] + opts[:size], opts[:y] , opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
opts[:x] + opts[:size], opts[:y] + opts[:size], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
opts[:x] , opts[:y] + opts[:size], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
])
end

# Make the inherited width and height attribute accessors private
private :width=, :height=

Expand Down
23 changes: 22 additions & 1 deletion lib/ruby2d/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,34 @@ def initialize(text, opts = {})
unless ext_init
raise Error, "Text `#{@text}` cannot be created"
end
add
unless opts[:show] == false then add end
end

def text=(msg)
@text = msg.to_s
ext_set(@text)
end

def draw(opts = {})
opts[:rotate] = opts[:rotate] || @rotate
unless opts[:color]
opts[:color] = [1.0, 1.0, 1.0, 1.0]
end

self.class.ext_draw([
self, opts[:x], opts[:y], opts[:rotate],
opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
])
end

private

def render
self.class.ext_draw([
self, @x, @y, @rotate,
@color.r, @color.g, @color.b, @color.a
])
end

end
end
16 changes: 16 additions & 0 deletions lib/ruby2d/triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,24 @@ def contains?(x, y)
questioned_area <= self_area
end

def self.draw(opts = {})
ext_draw([
opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3]
])
end

private

def render
self.class.ext_draw([
@x1, @y1, @c1.r, @c1.g, @c1.b, @c1.a,
@x2, @y2, @c2.r, @c2.g, @c2.b, @c2.a,
@x3, @y3, @c3.r, @c3.g, @c3.b, @c3.a
])
end

def triangle_area(x1, y1, x2, y2, x3, y3)
(x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
end
Expand Down
20 changes: 19 additions & 1 deletion lib/ruby2d/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def initialize(args = {})
# The window update block
@update_proc = Proc.new {}

# The window render block
@render_proc = Proc.new {}

# Whether diagnostic messages should be printed
@diagnostics = false

Expand Down Expand Up @@ -149,6 +152,10 @@ def update(&proc)
@@window.update(&proc)
end

def render(&proc)
@@window.render(&proc)
end

def show
@@window.show
end
Expand Down Expand Up @@ -245,12 +252,18 @@ def clear
@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
Expand Down Expand Up @@ -387,6 +400,11 @@ def update_callback

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
Expand Down