#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2017/04/08 23:35:23 +0900> # # tinypixelartgen_take3.rb # # ドット絵自動生成のテスト # sinカーブで型を与えて生成、かつ、境界線を付加してみる # Scale2x , Scale3x で拡大する処理を追加 # # License : GPL # (利用してる Scale2x、Scale3x がGPLなので) require_relative 'scalenx' # Generate PixleArt class TinyPixelArtGen # @return [Array] generate pixels attr_accessor :pixels # @return [Integer] generate pixels width attr_accessor :width # @return [Integer] generate pixels height attr_accessor :height include ScaleNx # initialize # @param w [Integer] width # @param h [Integer] height # @param mirror_x [true, false] x mirror # @param mirror_y [true, false] y mirror # @param seed [Integer] random seed # @param color [Integer] color length # @param scale [Integer] scaling 1, 2, 3, 4, 6, 8 def initialize(w, h, mirror_x: true, mirror_y: false, seed: 0, color: 8, scale: 1) srand(seed) @color = color @w, @h = w, h @scale = scale @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 ang = rand(360) ang_d = rand(270.0 / @h) + (45.0 / @h) (1..(@h-2)).each do |y| rad = (ang + (ang_d * y)) * Math::PI / 180.0 rw = (((w * 4 / 10.0 - 1) * Math.sin(rad)).to_i + (w * 5 / 10.0)) * 2 rx = (w - rw) / 2 @w.times do |x| xx = rand(rw) + rx @canvas[y][xx] = rand(@color) end end # clear border left and right @h.times do |y| @canvas[y][0] = 0 @canvas[y][@w - 1] = 0 end # generate edge edge = 1 @h.times do |y| @w.times do |x| next if @canvas[y][x] <= 1 @canvas[y - 1][x] = edge if (y - 1 >= 0 and @canvas[y - 1][x] == 0) @canvas[y + 1][x] = edge if (y + 1 < @h and @canvas[y + 1][x] == 0) @canvas[y][x - 1] = edge if (x - 1 >= 0 and @canvas[y][x - 1] == 0) @canvas[y][x + 1] = edge if (x + 1 < @w and @canvas[y][x + 1] == 0) end end # generate palette @pal = Array.new(@color) rr, gg, bb = rand(128), rand(128), rand(128) @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) c = 255 - rand(48) r, g, b, a = c, c, c, 255 else r = rr + rand(128) g = gg + rand(128) b = bb + rand(128) 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 # scaling if @scale == 2 @pixels = scale2x(@pixels) elsif @scale == 3 @pixels = scale3x(@pixels) elsif @scale == 4 @pixels = scale2x(@pixels) @pixels = scale2x(@pixels) elsif @scale == 6 @pixels = scale2x(@pixels) @pixels = scale3x(@pixels) elsif @scale == 8 @pixels = scale2x(@pixels) @pixels = scale2x(@pixels) @pixels = scale2x(@pixels) end @width = @pixels[0].size @height = @pixels.size end end if $0 == __FILE__ # ---------------------------------------- # usage sample require 'dxruby' # convert pixels array to DXRuby Image 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 # generate pixelart images 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: 12, scale: 3 ) img = conv_pixels_to_image(pat.pixels) imgs.push(img) end return imgs end Window.resize(640, 480) Window.bgcolor = [32, 128, 196] Window.scale = 1 Window.minFilter = TEXF_POINT Window.magFilter = TEXF_POINT cnt = 96 seed = 0 imgs = generate_images(cnt, seed) seed += cnt # main loop Window.loop do break if Input.keyPush?(K_ESCAPE) if Input.keyPush?(K_SPACE) imgs = generate_images(cnt, seed) seed += cnt end x, y = 0, 0 imgs.each do |img| Window.draw(x, y, img) x += img.width + 4 if (x + img.width) >= Window.width x = 0 y += img.height + 4 end end end end