#! /usr/bin/env ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2019/04/22 20:09:39 +0900> # # Ruby/SDL birmap font (SFont) draw. # # usage: ruby sfont.rb [font_kind] # # RIGHT, LEFT : change font # ESC : exit # # * Windows10 x64 + Ruby 1.9.3 p661 mingw32 + rubysdl 2.1.1.1 # load png : FAIL , load bmp : PASS # # * Windows10 x64 + Ruby 1.8.7 p330 mswin32 + rubysdl 1.3.1 # * Ubuntu Linux 18.04 + ruby 2.5.1 p57 x86_64-linux-gnu + rubysdl 2.2.0 # load png : PASS , load bmp : PASS require "sdl" fontdir = "font/" fontfiles = [ "verabd_sfont01_32col.bmp", "verabd_sfont01_32col.png", "verabd_sfont01.png", "verabd_sfont02_width_fix.png", "sfont_mplus1m_m.png", "vlgothic_sfont01.png", ] kind = ARGV.size > 0 ? ARGV[0].to_i : 0 if kind >= fontfiles.size puts "please input 0 - #{fontfiles.size - 1}" exit end fontfile = fontfiles[kind] puts "font image : #{fontfile}" SDL.init( SDL::INIT_VIDEO ) screen = SDL::Screen.open(640, 480, 16, SDL::SWSURFACE) fontflag = SDL::BMFont::TRANSPARENT | SDL::BMFont::SFONT font = SDL::BMFont.open(fontdir + fontfile, fontflag) ang = 0 while true while event = SDL::Event2.poll case event when SDL::Event2::Quit exit when SDL::Event2::KeyDown font_reload = false case event.sym when SDL::Key::ESCAPE exit when SDL::Key::RIGHT kind = (kind + 1) % fontfiles.size font_reload = true when SDL::Key::LEFT kind = (kind - 1 + fontfiles.size) % fontfiles.size font_reload = true end if font_reload fontfile = fontfiles[kind] puts "font image : #{fontfile}" font = SDL::BMFont.open(fontdir + fontfile, fontflag) end end end screen.fill_rect(0, 0, 640, 480, [128, 128, 128]) font.textout(screen, fontfile, 0, 0) y = 64.0 * Math.sin(ang * Math::PI / 180) + 128 ang += 1 font.textout(screen, "SFont (BitMapFont) Testing..", 8, y) screen.updateRect(0,0,0,0) sleep 0.016 end