#!ruby -Ku # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2017/04/18 21:28:17 +0900> # # tinypixelartgrad + gosu require 'gosu' require 'rmagick' require_relative 'tinypixelartgrad' class MyWindow < Gosu::Window WDW_W = 640 WDW_H = 480 def initialize super WDW_W, WDW_H, false self.caption = "TinyPixelartGrad + RMagick + gosu" @w = 64 @h = @w @seed = 0 @count = (WDW_W / (@w + 4)) * (WDW_H / (@h + 4)) @imgs = generate_arts(@w, @h, @count, @seed) @seed += @count @update_arts = false end def generate_arts(w, h, count, seed) srand(seed) imgs = [] count.times do |i| art = TinyPixelartGrad.new(w, h, seed + i) img = convert_image(w, h, art.surface) imgs.push(img) end return imgs end # convert cairo surface to Gosu::Image def convert_image(w, h, surface) mimg = Magick::Image.new(w, h) mimg.import_pixels(0, 0, w, h, "BGRA", surface.data) img = Gosu::Image.new(mimg, :tileable => true, :retro => true) return img end def update if @update_arts @imgs = generate_arts(@w, @h, @count, @seed) @seed += @count @update_arts = false end end def draw z = 0 x, y = 0, 0 @imgs.each do |img| img.draw(x, y, z) x += img.width + 4 if x + img.width >= WDW_W y += img.height + 4 x = 0 end end end def button_up(id) if id == Gosu::KB_SPACE @update_arts = true end end def button_down(id) if id == Gosu::KB_ESCAPE close end end end w = MyWindow.new w.show