#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2017/04/07 21:01:15 +0900> # # tinypixelartgen.rb # ドット絵自動生成のテスト # # License : CC0 / Public Domain class TinyPixelArtGen attr_accessor :pixels def initialize(w, h, mirror_x: true, mirror_y: false, seed: 0, color: 8) srand(seed) @color = color @w, @h = w, h @mirror_x, @mirror_y = mirror_x, mirror_y @canvas = Array.new(@h).map { Array.new(@w, 0) } @pixels = Array.new(@h).map { Array.new(@w).map { Array.new(4, 0) } } ww, hh = (@w / 2.0).round, (@h / 2.0).round wwf, hhf = @w % 2, @h % 2 # generate dot pattern @h.times do |y| @w.times do |x| @canvas[y][x] = rand(@color) end end # generate palette @pal = Array.new(@color) @pal.size.times do |i| if i == 0 r, g, b, a = 0, 0, 0, 0 elsif i == 1 r, g, b, a = 0, 0, 0, 255 elsif i == @pal.size - 1 r, g, b, a = 255, 255, 255, 255 else r, g, b = rand(256), rand(256), rand(256) a = 255 end @pal[i] = [r, g, b, a] end # make pixel data @h.times do |y| @w.times do |x| sx, sy = x, y sx = (x >= ww and @mirror_x)? (ww - 1 - wwf - (x - ww)) : x sy = (y >= hh and @mirror_y)? (hh - 1 - hhf - (y - hh)) : y r, g, b, a = @pal[@canvas[sy][sx]] @pixels[y][x] = [r, g, b, a] end end end end if $0 == __FILE__ # ---------------------------------------- require 'dxruby' def conv_pixels_to_image(pixels) w, h = pixels[0].size, pixels.size img = Image.new(w, h, [0, 0, 0, 0]) h.times do |y| w.times do |x| r, g, b, a = pixels[y][x] img[x, y] = [a, r, g, b] end end return img end def generate_images(n, seed) sz = 16 imgs = [] n.times do |i| pat = TinyPixelArtGen.new( sz, sz, mirror_x: (rand > 0.5)? true : false, mirror_y: (rand > 0.5)? true : false, seed: seed + i, color: 8 ) imgs.push(conv_pixels_to_image(pat.pixels)) end return imgs end Window.resize(640, 480) Window.bgcolor = [32, 128, 196] Window.minFilter = TEXF_POINT Window.magFilter = TEXF_POINT cnt = 96 seed = 0 imgs = generate_images(cnt, seed) seed += cnt Window.loop do break if Input.keyPush?(K_ESCAPE) if Input.keyPush?(K_SPACE) imgs = generate_images(cnt, seed) seed += cnt end scale = 3 border = 4 x, y = 0, 0 imgs.each do |img| Window.drawScale(x, y, img, scale, scale, 0, 0) x += img.width * scale + border if (x + img.width * scale) >= Window.width x = 0 y += img.height * scale + border end end end end