mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2024-11-17 07:48:20 +01:00
Add fill_circle drawing method
This commit is contained in:
parent
cc1757ab4f
commit
38dbaea4af
4 changed files with 68 additions and 2 deletions
|
@ -17,8 +17,8 @@ module PF
|
|||
|
||||
@viewport : Vector2(Int32)? = nil
|
||||
|
||||
delegate :draw_point, :draw_line, :draw_circle, :draw_triangle, :draw_rect, :draw_shape,
|
||||
:fill_triangle, :fill_rect, :fill_shape, to: @screen
|
||||
delegate :draw_point, :draw_line, :scan_line, :draw_circle, :draw_triangle, :draw_rect, :draw_shape,
|
||||
:fill_triangle, :fill_rect, :fill_circle, :fill_shape, to: @screen
|
||||
|
||||
@fps_lasttime : Float64 = Time.monotonic.total_milliseconds # the last recorded time.
|
||||
@fps_current : UInt32 = 0 # the current FPS.
|
||||
|
|
|
@ -4,6 +4,26 @@ require "./sprite/*"
|
|||
|
||||
module PF
|
||||
class Sprite
|
||||
def self.load_tiles(path, tile_width, tile_height)
|
||||
sheet = Sprite.new(path)
|
||||
sprites = [] of Sprite
|
||||
|
||||
tiles_x = sheet.width // tile_width
|
||||
tiles_y = sheet.height // tile_height
|
||||
|
||||
0.upto(tiles_x - 1) do |tx|
|
||||
0.upto(tiles_y - 1) do |ty|
|
||||
sx = tx * tile_width
|
||||
sy = ty * tile_height
|
||||
sprite = Sprite.new(tile_width, tile_height)
|
||||
sheet.draw_to(sprite, Vector[sx, sy], Vector[tile_width, tile_height], Vector[0, 0])
|
||||
sprites << sprite
|
||||
end
|
||||
end
|
||||
|
||||
sprites
|
||||
end
|
||||
|
||||
property surface : SDL::Surface
|
||||
|
||||
delegate :fill, :lock, :format, to: @surface
|
||||
|
@ -59,6 +79,11 @@ module PF
|
|||
draw_to(dest, at.x, at.y)
|
||||
end
|
||||
|
||||
# Draw this sprite to another given a source rect and destination
|
||||
def draw_to(sprite : Sprite, source : Vector2(Int), size : Vector2(Int), dest : Vector2(Int))
|
||||
@surface.blit(sprite.surface, SDL::Rect.new(source.x, source.y, size.x, size.y), SDL::Rect.new(dest.x, dest.y, size.x, size.y))
|
||||
end
|
||||
|
||||
# Raw access to the pixels as a Slice
|
||||
def pixels
|
||||
Slice.new(@surface.pixels.as(Pointer(UInt32)), width * height)
|
||||
|
|
|
@ -54,5 +54,12 @@ module PF
|
|||
def draw_line(line : Line, pixel : Pixel = Pixel.new)
|
||||
draw_line(line.p1.to_i32, line.p2.to_i32, pixel)
|
||||
end
|
||||
|
||||
# Draw a horizontal line to a certain *width*
|
||||
def scan_line(x : Int, y : Int, width : Int, pixel : Pixel = Pixel.new)
|
||||
0.upto(width) do |n|
|
||||
draw_point(x + n, y, pixel)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
34
src/sprite/fill_circle.cr
Normal file
34
src/sprite/fill_circle.cr
Normal file
|
@ -0,0 +1,34 @@
|
|||
module PF
|
||||
class Sprite
|
||||
# Fill a circle using Bresenham’s Algorithm
|
||||
def fill_circle(cx : Int, cy : Int, r : Int, pixel : Pixel = Pixel.new)
|
||||
x, y = 0, r
|
||||
balance = 0 - r
|
||||
|
||||
while x <= y
|
||||
p0 = cx - x
|
||||
p1 = cx - y
|
||||
|
||||
w0 = x + x
|
||||
w1 = y + y
|
||||
|
||||
scan_line(p0, cy + y, w0, pixel)
|
||||
scan_line(p0, cy - y, w0, pixel)
|
||||
scan_line(p1, cy + x, w1, pixel)
|
||||
scan_line(p1, cy - x, w1, pixel)
|
||||
|
||||
x += 1
|
||||
balance += x + x
|
||||
|
||||
if balance >= 0
|
||||
y -= 1
|
||||
balance -= (y + y)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def fill_circle(c : Vector2(Int), r : Int, pixel : Pixel = Pixel.new)
|
||||
fill_circle(c.x, c.y, r, pixel)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue