> The real magic would be getting a fast implementation in Haskell, which I understand is also notoriously slow. ;P
> int? double? 2D only, or with a depth?
cpSpaceBBQuery
. The declaration looks like this:void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void *data);
class Viewport
def scroll_to x, y
@bb.l, @bb.t = x, y
end
def visible_objects space
shapes = space.active_shapes_hash.query_by_bb @bb
magically_get_objects_from shapes
end
def apply &draw_block
$window.translate -@bb.x, -@bb.y, &draw_block
end
end
class MyWindow < Gosu::Window
def draw
@viewport.apply do
@viewport.visible_objects(@space).each { |obj| ... }
end
end
end
class MessageBox
attr_accessor :speed
attr_reader :text, :font, :padding
def initialize text, options = {}
@x, @y = options[:pos] || [0, 0]
@w, @h = options[:size] || [$window.width, $window.height / 3]
@speed = options[:speed] || 40
self.padding = options[:padding] || 8
self.font = options[:font]
self.text = text
@last_update = Gosu.milliseconds / 1000.0
end
def text= text
@reveal = 0
@text = text
calculate_wrap
end
def font= font
if font
@font = font
else
@font = Gosu::Font.new $window, Gosu.default_font_name, 16
end
calculate_wrap
end
def padding= padding
@padding = padding
calculate_wrap
end
def finish
@reveal = @text.length
end
def update
old_time, new_time = @last_update, Gosu.milliseconds / 1000.0
dt = new_time - old_time
@last_update = new_time
@reveal += @speed * dt
end
def draw
x, y = @x + @padding, @y + @padding
@wrap.each do |range|
if range.last > @reveal
trunc_range = range.first ... @reveal.floor
@font.draw @text[trunc_range], x, y, Float::INFINITY
break
else
@font.draw @text[range], x, y, Float::INFINITY
end
y += @font.height
end
end
protected
def calculate_wrap
return unless @font and @text and @padding
box_width = @w - @padding * 2
@wrap = []
first_index = 0
last_index = test_index = @text.index(/\s|$/)
while test_index
if @font.text_width(@text[first_index ... test_index]) >= box_width
@wrap << (first_index ... last_index)
first_index = last_index + 1
end
last_index = test_index
test_index = @text.index /\s|$/, last_index + 1
end
@wrap << (first_index .. last_index)
@wrap
end
end
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill