Ubuntu に Ruby/SDL を入れる

Ruby/SDL
GitHub
 

インストール

まずはライブラリを入れる。

$ sudo apt-get install libsdl2-2.0 libsdl-sge-dev

 
Bundler で Gem 'rubysdl' を入れる。

何か Gem の場所がわからない。

$ bundle exec gem which sdl
/home/tomoki/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/rubysdl-2.2.0/lib/sdl.rb

 
サンプルを実行してみる。

$ cd ~/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/rubysdl-2.2.0/sample
$ ruby testsprite.rb
$ ruby testgl.rb
$ ruby movesp.rb

OK ですね!

Ruby/SDL はもうメンテされていないようだけれども、ちゃんと動くではないか。
 

SGE(SDL Graphics Extension)

$ sudo apt-get install libsdl-sge-dev

これをやってから rubysdl を入れないと、使えないメソッドがいろいろある。

赤い円を描いてみる。

require 'sdl'

SDL.init(SDL::INIT_VIDEO)
screen = SDL::Screen.open(300, 300, 16, SDL::SWSURFACE)
SDL::WM::set_caption("SDL", "")

red   = screen.format.map_rgb(255,   0,   0)
white = screen.format.map_rgb(255, 255, 255)

screen.draw_rect(0, 0, 300, 300, white, true)
screen.draw_circle(150, 150, 150, red, true, true)

screen.flip

loop do
  while event = SDL::Event.poll
    case event
    when SDL::Event::KeyDown, SDL::Event::Quit
      exit
    end
  end
  sleep 0.2
end

 
線が移動していくアニメーション。

require 'sdl'

def draw(&blk)
  SDL.init(SDL::INIT_VIDEO)
  screen = SDL::Screen.open(300, 300, 16, SDL::SWSURFACE)
  SDL::WM::set_caption("SDL", "")
  
  class << screen
    def color(r, g, b)
      format.map_rgb(r, g, b)
    end
  end
  
  Thread.new {screen.instance_eval(&blk)}
  
  loop do
    while event = SDL::Event.poll
      case event
      when SDL::Event::KeyDown, SDL::Event::Quit
        exit
      end
    end
    sleep 0.2
  end
end

draw do
  green = color(  0, 255,   0)
  white = color(255, 255, 255)
  
  draw_rect(0, 0, 300, 300, white, true)
  
  300.times do |x|
    draw_line(x, 0, 299 - x, 299, green, true)
    flip
    sleep(0.05)
  end
end

 

SDL::TTF

フォント描画。
20180825123844
 
Linux Mint 18.3 の TakaoExMincho.ttf を使ってみる。
sdl_sample5.rb

require_relative 'sdl_draw'

draw(300, 150) do
  SDL::TTF.init
  font = SDL::TTF.open("/usr/share/fonts/truetype/takao-mincho/TakaoExMincho.ttf", 40)
  font.draw_solid_utf8(self, "Ruby", 10, 10, 0, 255, 255)
  font.draw_blended_utf8(self, "Ruby", 10, 50, 0, 255, 0)
  font.draw_blended_utf8(self, "岐阜", 10, 100, 255, 0, 0)
  flip
  font.close
end

ちゃんと漢字も描画可能だ。draw_solid_utf8() よりも draw_blended_utf8() の方がアンチエイリアスが効いていたりして、きれいである。

sdl_draw.rb は描画の定型を簡単にライブラリ化したもので、ここを参照。
 
20180825151227

require_relative 'sdl_draw'

draw(300, 100) do
  fill_rect(0, 0, 300, 100, color(255, 255, 255))
  
  SDL::TTF.init
  font = SDL::TTF.open("/usr/local/share/fonts/CHEESE__.TTF", 40)
  font.draw_blended_utf8(self, "Ruby/SDL", 47, 30, 0, 0, 0)
  flip
  font.close
end

#Font: http://www.cfont.jp/bitmap/cheese.html

 

SDL::Kanji

bdfフォントで漢字を使ってみる。
20180825112505
 
まず bdfフォント(いまでは一般的でない)を入手しなければならない。例えばここが参考になります。自分は東雲 ビットマップフォントファミリーを使わせて頂きました。ダウンロードはここから。これを解凍して、適当な場所に置きます。

コード。
sdl_sample4.rb

require_relative 'sdl_draw'

Dir.chdir("/home/tomoki/Documents/shinonome-0.9.11/bdf/")

draw(300, 300) do
  ["shnmk16", "shnmk16b", "shnmk16min", "shnmk16minb"].each_with_index do |font, i|
    kanji = SDL::Kanji.open(font + ".bdf", 16)
    ["Ruby", "ルビイ", "浅野"].each_with_index do |text, j|
      kanji.put(self, text, 10, i * 70 + j * 20, 255, 255, 0)
      flip
      sleep(0.5)
    end
    kanji.close
  end
end

SDL::TTF できれいに漢字が描画できるので、わざわざ SDL::Kanji を使う必要はあまりないだろう。懐かしさを体験してみるとか。
 
20180825134446

require_relative 'sdl_draw'

Dir.chdir("/home/tomoki/Documents/shinonome-0.9.11/bdf/")

draw(300, 100) do
  ["shnmk12min", "shnmk12maru", "shnmk12p"].each_with_index do |font, i|
    kanji = SDL::Kanji.open(font + ".bdf", 12)
    kanji.put(self, "魔法の杖を手に入れた!", 10, i * 20 + 15, 0, 255, 0)
    flip
    sleep(0.5)
    kanji.close
  end
end

 

SDL::BMFont

ビットマップフォントを使ってみる。
20180825145155
 
フォントは SFont 形式を使った。ダウンロード、解凍して適当な場所に置く。
sdl_sample6.rb

require_relative 'sdl_draw'

Dir.chdir("/home/tomoki/Documents/SFont-2.03")

draw(300, 150) do
  fill_rect(0, 0, 300, 150, color(0xe7, 0xfc, 0xb5))
  
  flag = SDL::BMFont::TRANSPARENT | SDL::BMFont::SFONT
  
  font = SDL::BMFont.open("24P_Arial_NeonYellow.png", flag)
  font.textout(self, "Ruby", 10, 10)
  font.close
  
  font = SDL::BMFont.open("24P_Copperplate_Blue.png", flag)
  font.textout(self, "SDL", 10, 50)
  font.close
  
  flip
end

SFont だと色も使えないし大きさも選べない上に、ASCIIコードしか表示できない。あまり使い道はなさそう。