svg を拡大回転 (ruby/sdl)

svgを回転、拡大してruby/sdlで動かしてみた。
キレイだね。
svgを直接書いて図形なり、テキストなりを表現するのもありかな。

#!usr/bin/env ruby

require 'rubygems'
require 'sdl'
require 'rsvg2'

class Text
def initialize(screen)
@screen = screen
@data = <<EOF
<svg width="350" height="30" >
<text x="10" y="50" fill="white" font-size="30px">SVG on Ruby/SDL</text>
</svg>
EOF
@svg = RSVG::Handle.new_from_data(@data)
@image = Cairo::ImageSurface.new(500,500)
@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.render_rsvg_handle(@svg)
@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-27 23:39