Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created April 3, 2010 16:40
Show Gist options
  • Save Mon-Ouie/354665 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/354665 to your computer and use it in GitHub Desktop.
require 'joyau/init'
require 'joyau/color'
require 'joyau/buffer_library'
require 'enumerator'
def draw_circle(x, y, radius, line_number,
color, border_color, line_color, line_color2 = line_color)
perimeter = (2 * Math::PI * radius).to_i
raise ArgumentError, "line_number should be an Integer" unless
line_number.is_a? Integer
raise ArgumentError, "You cannot draw #{line_number} lines in this circle" if
line_number > perimeter or line_number < 0
painter = Joyau::Painter.new(Joyau::Buffer.actual)
painter.drawFillCircle(x, y, radius, color)
painter.drawCircle(x, y, radius, border_color)
return if line_number.zero?
step = 2 * Math::PI / line_number
(0..(2 * Math::PI - step)).step(step) do |angle|
abs = Math.cos(angle) * radius
ord = Math.sin(angle) * radius
painter.drawLine(x, y, (x + abs).to_i, (y + ord).to_i,
line_color, line_color2)
end
return
end
module CircleLibrary
include Joyau::BufferLibrary
@@buffers = {}
COLOR = /rgb\((\d+), (\d+), (\d+)\)/
@@regex = /^circle:(\d+):(\d+):#{COLOR}:#{COLOR}:#{COLOR}:#{COLOR}$/
class << self
def [](res_name)
@@buffers[res_name] || create_buffer(res_name)
end
def create_buffer(res_name)
datas = res_name.match(@@regex)
unless datas
raise(Joyau::InvalidRessourceName,
"#{res_name} should match #{@@regex.inspect}")
end
radius, line_number = datas.captures
rest = datas.captures[2..-1]
radius = radius.to_i
line_number = line_number.to_i
buffer = Joyau::Buffer.new((radius + 1) * 2, (radius + 1) * 2)
colors = []
rest.each_slice(3) do |tab|
colors << Joyau::Color.new(tab[0].to_i, tab[1].to_i, tab[2].to_i)
end
Joyau.draw(:buffer => buffer) do
draw_circle(radius, radius, radius, line_number, *colors)
end
@@buffers[res_name] = buffer
return buffer
end
end
register_loader @@regex
end
Joyau.init(:gfx)
res_name = "circle:50:15:rgb(255, 0, 0):rgb(0, 255, 0):rgb(0, 0, 255)"
res_name << ":rgb(255, 255, 255)"
sprite = Joyau::Sprite.from_res_name(res_name)
sprite.setPos(50, 50)
Joyau.draw do
sprite.draw
end while Joyau.mayPlay
Joyau.stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment