<   2011年 04月 ( 13 )   > この月の画像一覧

なんとなく、ruby/SDLの使い方が、落ち着いてきた。
cairoとruby/SDLの組み合わせで、
部品をオブジェクトにして、
なるべく、ruby/SDLの関数を使わない。
まだ変わるかもしれないけど、
今のところ、こんな感じで進みます。

<追記>2011/08現在、こんなスタイルになりました
[PR]
by gaziya | 2011-04-30 10:24
ubuntu11.04がでた。
これを機にruby1.9に乗り換えよう思う。
それと、rubygemsの依存をやめようと思う。

この、ブログに載せたものに関してのインストールメモ

sudo apt-get install ruby1.9
sudo apt-get install libsdl-ruby
sudo apt-get install libcairo-ruby
sudo apt-get install libffi-ruby
sudo apt-get install libtcltk-ruby
sudo apt-get install librsvg2-ruby
sudo apt-get install libgdk-pixbuf2-ruby
sudo apt-get install libgtk2-ruby
sudo apt-get install libpango1-ruby

sudo apt-get install imagemagick
sudo apt-get install r-base-core
sudo apt-get install graphviz
sudo apt-get install gnuplot-nox gnuplot-x11
sudo apt-get install mimetex
sudo apt-get install flydraw
sudo apt-get install potrace
sudo apt-get install xclip

wiiについてのインストール
sudo apt-get install libbluetooth-dev
sudo apt-get install git
sudo apt-get install automake
sudo apt-get install flex
sudo apt-get install bison
sudo apt-get install libgtk2.0-dev
sudo apt-get install python-dev
git clone https://github.com/abstrakraft/cwiid.git
cd ./cwiid
aclocal
autoconf
./configure
make
sudo make install

この時 /usr/local/libにlibcwiid.soはできる。

最後に
sudo ln -f /usr/bin/ruby1.9.1 /usr/bin/ruby
これは、お薦めはしません。
あくまでも、僕のメモです。
<追記>
上記のインストール状況で過去のプログラムを動かしたら、
エラーが、でちゃいました。
エラーを追っかけるのは面倒なので、
sudo ln -f /usr/bin/ruby1.8 /usr/bin/ruby
で、もとに戻しました。
そのうち、調べるかも?
[PR]
by gaziya | 2011-04-29 11:32
ruby/sdlには関係ないけど、
テストでやってみた事
この先必要になるかな?
なんとなく、アイテムゲットって感じ
これは、rsvg2を使わない。

cairoでsvgファイルを作るには

#!usr/bin/env ruby

require 'rubygems'
require 'cairo'

Cairo::SVGSurface.new("/tmp/image.svg",200,200) do |surface|
context = Cairo::Context.new(surface)
context.set_source_color(Cairo::Color::RED)
context.rectangle(50, 50, 100, 100)
context.fill_preserve
context.set_source_color(Cairo::Color::GRAY)
context.stroke
end
`display /tmp/image.svg`

+++++++++++++++++++++++++++++++

cairoでsvgデータを作るには

#!usr/bin/env ruby

require 'rubygems'
require 'stringio'
require 'cairo'

StringIO.open {|sio|
Cairo::SVGSurface.new(sio,200,200) {|surface|
context = Cairo::Context.new(surface)
context.set_source_color(Cairo::Color::RED)
context.rectangle(50, 50, 100, 100)
context.fill_preserve
context.set_source_color(Cairo::Color::GRAY)
context.stroke
}
@svg = sio.string
}
puts @svg
`echo '#{@svg}' | display`
[PR]
by gaziya | 2011-04-28 23:32
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
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
調子に乗って、potraceでmimetexをsvg出力してrcairoに使おうとした。
汚い画面になってしまった。
でも、何かに使えるかも?
で、載せときます。
sudo apt-get install potrace
potraceはbmpをsvgに変換します。

#!usr/bin/env ruby

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

eval = "f(x)=x^5+8x^4+18x^3+4x^2-19x-12"
@svg = `mimetex -s 7 -o -d '#{eval}' | convert - bmp:- | potrace -s`

handle = RSVG::Handle.new_from_data(@svg)
dim = handle.dimensions
@image = Cairo::ImageSurface.new(dim.width, dim.height)
context = Cairo::Context.new(@image)
context.set_source_color(Cairo::Color::WHITE)
context.rectangle(0, 0, @image.width, @image.height)
context.fill
context.render_rsvg_handle(handle)
@surface = SDL::Surface.new_from(@image.data,@image.width,@image.height,32,@image.stride,0,0,0,0)
@surface.saveBMP("/tmp/image")
#@image.write_to_png("/tmp/image")
`display /tmp/image`
[PR]
by gaziya | 2011-04-27 00:19
gnuplotは、svgで出力できるみたいだ。
svgで出力してrcairo(rsvg2)を通してみた。
見た目がキレイになった。

後日、書き直しました。こちら。

[PR]
by gaziya | 2011-04-26 22:16
日本語変換をインターネット上で実現するための、CGI サービス
http://www.google.com/intl/ja/ime/cgiapi.html
を見つけた。
で、さっそく作ってみた。
sdlでは、半角/全角のキーが使えないみたい。
とりあえず、全角のみのローマ字入力でやってみた。
記号キーは悩みそうだから、今回はパス。
簡単な仕組みだけのものにした。


#!usr/bin/env ruby

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

class EventHandle
def initialize(handle)
@handle = handle
@type = {
SDL::Event::KeyDown => :sym,
SDL::Event::KeyUp => :sym,
SDL::Event::MouseButtonDown => :button,
SDL::Event::MouseButtonUp => :button
}
end

def handling(event)
name = event.class
if @type[name]
if callback = @handle[[name,event.method(@type[name]).call]]
return callback.call(event)
end
end
if callback = @handle[name]
return callback.call(event)
end
nil
end
end

class IM
attr_reader :words,:focus_idx

def initialize
self.clear
@handle = EventHandle.new(
SDL::Event::KeyDown => proc{|e| key_down(e)}
)
@char = {
SDL::Key::K0 => '0',
SDL::Key::K1 => '1',
SDL::Key::K2 => '2',
SDL::Key::K3 => '3',
SDL::Key::K4 => '4',
SDL::Key::K5 => '5',
SDL::Key::K6 => '6',
SDL::Key::K7 => '7',
SDL::Key::K8 => '8',
SDL::Key::K9 => '9',
SDL::Key::A => "a",
SDL::Key::B => "b",
SDL::Key::C => "c",
SDL::Key::D => "d",
SDL::Key::E => "e",
SDL::Key::F => "f",
SDL::Key::G => "g",
SDL::Key::H => "h",
SDL::Key::I => "i",
SDL::Key::J => "j",
SDL::Key::K => "k",
SDL::Key::L => "l",
SDL::Key::M => "m",
SDL::Key::N => "n",
SDL::Key::O => "o",
SDL::Key::P => "p",
SDL::Key::Q => "q",
SDL::Key::R => "r",
SDL::Key::S => "s",
SDL::Key::T => "t",
SDL::Key::U => "u",
SDL::Key::V => "v",
SDL::Key::W => "w",
SDL::Key::X => "x",
SDL::Key::Y => "y",
SDL::Key::Z => "z",
SDL::Key::MINUS => "ー"
}

@map = [
["kk","っk"],["ss","っs"],["tt","っt"],["nn","ん"],["hh","っh"],
["mm","っm"],["yy","っy"],["rr","っr"],["ww","っw"],
["gg","っg"],["zz","っz"],["dd","っd"],["bb","っb"],["pp","っp"],
["xtu","っ"],["ltu","っ"],
["xa","ぁ"],["xi","ぃ"],["xu","ぅ"],["xe","ぇ"],["xo","ぉ"],
["la","ぁ"],["li","ぃ"],["lu","ぅ"],["le","ぇ"],["lo","ぉ"],
["xya","ゃ"],["xyu","ゅ"],["xyo","ょ"],
["lya","ゃ"],["lyu","ゅ"],["lyo","ょ"],
["kya","きゃ"],["kyi","きゃ"],["kyu","きゅ"],["kye","きぇ"],["kyo","きょ"],
["sya","しゃ"],["syi","しぃ"],["syu","しゅ"],["sye","しぇ"],["syo","しぇ"],
["tya","ちゃ"],["tyi","ちぃ"],["tyu","ちゅ"],["tye","ちぇ"],["tyo","ちょ"],
["nya","にゃ"],["nyi","にぃ"],["nyu","にゅ"],["nye","にぇ"],["nyo","にょ"],
["hya","ひゃ"],["hyi","ひぃ"],["hyu","ひゅ"],["hye","ひぇ"],["hyo","ひょ"],
["mya","みゃ"],["myi","みぃ"],["myu","みゅ"],["mye","みぇ"],["myo","みょ"],
["rya","りゃ"],["ryi","りぃ"],["ryu","りゅ"],["rye","りぇ"],["ryo","りょ"],
["gya","ぎゃ"],["gyi","ぎぃ"],["gyu","ぎゅ"],["gye","ぎぇ"],["gyo","ぎょ"],
["zya","じゃ"],["zyi","じぃ"],["zyu","じゅ"],["zye","じぇ"],["zyo","じょ"],
["dya","ぢゃ"],["dyi","ぢぃ"],["dyu","ぢゅ"],["dye","ぢぇ"],["dyo","ぢょ"],
["bya","びゃ"],["byi","びぃ"],["byu","びゅ"],["bye","びぇ"],["byo","びょ"],
["pya","ぴゃ"],["pyi","ぴぃ"],["pyu","ぴゅ"],["pye","ぴぇ"],["pyo","ぴょ"],
["ka","か"],["ki","き"],["ku","く"],["ke","け"],["ko","こ"],
["ca","か"],["ci","し"],["cu","く"],["ce","せ"],["co","こ"],
["qa","くぁ"],["qi","くぃ"],["qu","く"],["qe","くぇ"],["qo","くぉ"],
["sa","さ"],["si","し"],["su","す"],["se","せ"],["so","そ"],
["sha","しゃ"],["shi","し"],["shu","しゅ"],["she","しぇ"],["sho","しょ"],
["ta","た"],["ti","ち"],["tu","つ"],["te","て"],["to","と"],
["tha","てゃ"],["thi","てぃ"],["thu","てゅ"],["the","てぇ"],["tho","てょ"],
["na","な"],["ni","に"],["nu","ぬ"],["ne","ね"],["no","の"],
["ha","は"],["hi","ひ"],["hu","ふ"],["he","へ"],["ho","ほ"],
["fa","ふぁ"],["fi","ふぃ"],["fu","ふ"],["fe","ふぇ"],["fo","ふぉ"],
["ma","ま"],["mi","み"],["mu","む"],["me","め"],["mo","も"],
["ya","や"],["yi","い"],["yu","ゆ"],["ye","いぇ"],["yo","よ"],
["ra","ら"],["ri","り"],["ru","る"],["re","れ"],["ro","ろ"],
["wa","わ"],["wi","うぃ"],["wu","う"],["we","うぇ"],["wo","を"],
["ga","が"],["gi","ぎ"],["gu","ぐ"],["ge","げ"],["go","ご"],
["za","ざ"],["zi","じ"],["zu","ず"],["ze","ぜ"],["zo","ぞ"],
["ja","じょ"],["ji","じ"],["ju","じゅ"],["je","じぇ"],["jo","じょ"],
["da","だ"],["di","ぢ"],["du","づ"],["de","で"],["do","ど"],
["ba","ば"],["bi","び"],["bu","ぶ"],["be","べ"],["bo","ぼ"],
["pa","ぱ"],["pi","ぴ"],["pu","ぷ"],["pe","ぺ"],["po","ぽ"],
["a","あ"],["b","b"],["c","c"],["d","d"],["e","え"],["f","f"],
["g","g"],["h","h"],["i","い"],["j","j"],["k","k"],["l","l"],
["m","m"],["n","n"],["o","お"],["p","p"],["q","q"],["r","r"],
["s","s"],["t","t"],["u","う"],["v","v"],["w","w"],["x","x"],
["y","y"],["z","z"]
]
end

def convart_kanji
url = "http://www.google.com/transliterate?langpair=ja-Hira|ja&text=#{CGI.escape(@words.join(","))}"
json = `wget -q -O - '#{url}' --user-agent='Mozilla/5.0'`
json.gsub!(/\s/,"")
json.gsub!('"',"")
json.sub!("[","")
@words = []
@candidate = []
json.scan(/\[(.*?),\[(.*?)\]/) do |x|
cand = $2.split(",")
@candidate << cand
@candidate_idx << 0
@words << cand[0]
end
@status = :kanji
@focus_idx = 0
end

def convart_hira
text = @buf.dup
@map.each do |r,k|
text.gsub!(r,k)
end
@words[0] = text
end

def clear
@status = :hira
@buf = ""
@words = [""]
@focus_idx = 0
@candidate = []
@candidate_idx = []
end


def key_down(e)
if char = @char[e.sym]
if @status == :hira
@buf << char
convart_hira
"x"
else
ret = @words.join
self.clear
@buf << char
convart_hira
ret
end
elsif e.sym == SDL::Key::RETURN
if @buf.empty?
"e"
else
ret = @words.join
self.clear
ret
end
elsif e.sym == SDL::Key::BACKSPACE
return nil if @status == :kanji
if @buf.empty?
"b"
else
buf = @words[0].chop.chop.chop
self.clear
@buf = @words[0] = buf
"x"
end
elsif e.sym == SDL::Key::ESCAPE
buf = @buf
self.clear
@buf = buf
convart_hira
"x"
elsif e.sym == SDL::Key::SPACE
return " " if @buf.empty?
if @status == :kanji
@candidate_idx[@focus_idx] += 1
@candidate_idx[@focus_idx] = 0 if @candidate_idx[@focus_idx] == @candidate[@focus_idx].size
@words[@focus_idx] = @candidate[@focus_idx][@candidate_idx[@focus_idx]]
else
convart_kanji
end
"x"
elsif e.sym == SDL::Key::LEFT
@focus_idx -= 1
@focus_idx = @words.size - 1 if @focus_idx < 0
"x"
elsif e.sym == SDL::Key::RIGHT
@focus_idx += 1
@focus_idx = 0 if @focus_idx == @words.size
"x"
end
end

def handling(e)
@handle.handling(e)
end
end

class Canvas
def initialize(screen)
@screen = screen
@x,@y = 0,0
@im =IM.new
@text = ""
self.draw
end

def handling(e)
if text = @im.handling(e)
if text == "e"
@text = ""
elsif text == "b"
@text.chop.chop.chop if !@text.empty?
elsif text != "x"
@text << text
end
self.draw
end
end

def draw
image = Cairo::ImageSurface.new(600,45)
context = Cairo::Context.new(image)
context.set_source_rgb(1, 1, 1)
context.rectangle(0, 0, 600, 45)
context.fill
context.set_source_color(Cairo::Color::BLACK)
context.font_size = 20
context.move_to(10, 30)
context.show_text(@text)
@im.words.size.times do |i|
if @im.focus_idx == i
context.set_source_color(Cairo::Color::RED)
else
context.set_source_color(Cairo::Color::BLUE)
end
context.show_text(@im.words[i])
end
@surface = SDL::Surface.new_from(image.data,image.width,image.height,32,image.stride,0,0,0,0)
end

def update
@screen.put(@surface,10,25)
end
end

class Phase
def initialize(screen)
@screen = screen
@canvas = Canvas.new(@screen)
@handle = EventHandle.new(
SDL::Event::Quit => proc{exit}
)
end

def update
@screen.draw_rect(0,0,@screen.w,@screen.h,[150,150,150],true)
@canvas.update
end

def handling(e)
@handle.handling(e)
@canvas.handling(e)
end

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

width,height = 620,100
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-22 22:40
rcairoにmimeTexとflydrawとconvertをくっつけた。
これで、いろんな事ができるだろう。
ちょっと、
mimetex -s 7 -d '#{eval}' | convert - png:-

convert: tRNS chunk has out-of-range samples for bit_depth `-' @ warning/png.c/PNGWarningHandler/1457.
とメッセージを出すけど、動くから一時保留です。

irbにプログラム全部のコピペで簡単に試せます。

#!usr/bin/env ruby

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

@image = Cairo::ImageSurface.new(450,400)
context = Cairo::Context.new(@image)
context.set_source_color(Cairo::Color::WHITE)
context.rectangle(0, 0, @image.width, @image.height)
context.fill
context.set_source_color(Cairo::Color::PINK)
context.arc(150, 100, 100, 0, 2 * Math::PI)
context.fill

eval = <<EOF
new 400,400
range -5,5,-20,20
linewidth 2
plot black,pow(x,3)-12*pow(x,1)
EOF

IO.popen("echo '#{eval}' | flydraw | convert -transparent white - png:-") do |io|
@image1 = Cairo::ImageSurface.from_png(io)
end

command = "-rotate '45'"
IO.popen("convert #{command} - -","r+") do |io|
@image1.write_to_png(io)
io.close_write
@image1 = Cairo::ImageSurface.from_png(io)
end
context.set_source(@image1, -100, -100)
context.paint

eval = "f(x)=x^5+8x^4+18x^3+4x^2-19x-12"
IO.popen("mimetex -s 7 -d '#{eval}'| convert - png:-") do |io|
@image1 = Cairo::ImageSurface.from_png(io)
end
context.set_source(@image1, 0, 120)
context.paint

context.set_source_color(Cairo::Color::RED)
context.font_size = 25
context.move_to(100, 50)
context.show_text('日本語')

@surface = SDL::Surface.new_from(@image.data,@image.width,@image.height,32,@image.stride,0,0,0,0)
@surface.saveBMP("/tmp/image.bmp")
`display /tmp/image.bmp`
[PR]
by gaziya | 2011-04-16 11:47
mimeTexも試しました。
sudo apt-get install mimetex
しばらく、mimeTexとflydrawとrcairoの組み合わせを考えてみます。


#!usr/bin/env ruby

require 'rubygems'
require 'sdl'
require 'stringio'

eval = "f(x)=x^5+8x^4+18x^3+4x^2-19x-12"
@image = `mimetex -s 7 -d '#{eval}'`
StringIO.open(@image){|s| @surface = SDL::Surface.loadFromIO(s)}
@surface.saveBMP("/tmp/image.bmp")
`display /tmp/image.bmp`
[PR]
by gaziya | 2011-04-14 14:13