cairoで拡大回転(ruby/SDL)

svgで拡大回転は、キレイと喜んでいたが、
どうも、cairoの性能だったみたい。
まあ、これも勉強やな。

#!usr/bin/env ruby

require 'rubygems'
require 'sdl'
require 'cairo'

class Text
def initialize(screen)
@screen = screen
@image = Cairo::ImageSurface.new(@screen.w,@screen.h)
@ratio = 0.1
@angle = 0.0
end

def draw
context = Cairo::Context.new(@image)
context.set_source_color(Cairo::Color::GRAY)
context.rectangle(0, 0, @image.width, @image.height)
context.fill
context.scale(@ratio,@ratio)
context.rotate(@angle)
context.set_source_color(Cairo::Color::WHITE)
context.font_size = 25
context.move_to(10, 50)
context.show_text('Cairo on Ruby/SDL')
@surface = SDL::Surface.new_from(@image.data,@image.width,@image.height,32,@image.stride,0,0,0,0)
end

def update
@ratio += 0.1
@ratio = 0.1 if @ratio > 5
@angle += 0.1
@angle = 0.0 if @angle > 1.0
draw
@screen.put(@surface,0,0)
end
end

class Phase
def initialize(screen)
@screen = screen
@text = Text.new(@screen)
end

def update
@text.update
end

def handling(e)
exit if e.kind_of?(SDL::Event::Quit) || (e.kind_of?(SDL::Event::KeyDown) && e.sym == SDL::Key::ESCAPE)
end

def run
loop do
while e=SDL::Event.poll
handling(e)
end
update
@screen.flip
SDL.delay(200)
end
end
end

width,height = 500,500
SDL.init(SDL::INIT_VIDEO)
screen = SDL::Screen.open(width,height,SDL::Screen.info.bpp,SDL::SWSURFACE)
Phase.new(screen).run
[PR]
by gaziya | 2011-04-28 22:33