#==============================================================================
# module CP
#==============================================================================
module CP
module Cache
WIDTH = 576
FONT_HEIGHT = 64
def self.generate_damage
allcolors = [[ColorHP, ColorHP2], [ColorMP, ColorMP2], [ColorCritical,
ColorCritical2], [ColorMissed, ColorMissed2], [ColorDamage,
ColorDamage2]]
colors = []
allcolors.each {|c| colors.push(c[0])}
characters = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a + ['-', '+', '%', ',', '!']
$sprite = Sprite.new
$sprite.x, $sprite.y = 160, 224
$sprite.bitmap = Bitmap.new(320, 32)
$sprite.bitmap.clear
$sprite.bitmap.draw_text(0, 0, 320, 32, '-> Start', 1)
damages = self.create_damage(allcolors, characters)
Graphics.update
rects = self.generate_positions(damages[colors[0]], characters)
Graphics.update
height = rects.delete(0)
### rect save
data = []
characters.each {|c| data.push("'#{c}' => Rect.new(#{rects[c].x/2}, #{rects[c].y/2}, #{rects[c].width/2}, #{rects[c].height/2})")}
file = File.open('dmg.txt', 'w')
file.write("{\n" + data.join(",\n") + "\n}\n")
file.close
### bitmap save into tga
w1 = WIDTH % 256
w2 = WIDTH / 256
h1 = height % 256
h2 = height / 256
header = "\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00" + w1.chr + w2.chr + h1.chr + h2.chr + "\x20\x08"
trail = "\x00\x00\x00\x00\x00\x00\x00\x00TRUEVISION-XFILE\x00"
colors.each {|c|
Graphics.update
bitmap = self.create_bitmap(damages[c], characters, rects, height)
Graphics.update
stream = self.create_stream(bitmap)
Graphics.update
filename = sprintf("%d-%d-%d.tga", c.red.to_i, c.green.to_i, c.blue.to_i)
file = File.open(filename, 'wb')
file.write(header + stream + trail)
file.close}
$sprite.dispose
end
def self.create_bitmap(damages, characters, rects, height)
bitmap = Bitmap.new(WIDTH, height)
characters.each {|c|
r = Rect.new(0, 0, rects[c].width, rects[c].height)
bitmap.blt(rects[c].x, rects[c].y, damages[c], r)}
return bitmap
end
def self.create_stream(bitmap)
stream = []
(0...bitmap.height).to_a.reverse.each {|y|
if y % 40 == 0
$sprite.bitmap.clear
$sprite.bitmap.draw_text(0, 0, 320, 32, 'Stream ' + ((bitmap.height - y) * 100 / bitmap.height).to_s + '%', 1)
Graphics.update
end
line = []
(0...bitmap.width).each {|x|
c = bitmap.get_pixel(x, y)
line.push(c.blue.to_i.chr + c.green.to_i.chr + c.red.to_i.chr + c.alpha.to_i.chr)}
stream.push(line.join(''))}
return stream.join('')
end
def self.generate_positions(sprites, characters)
x = y = 0
rects = {}
characters.each {|c|
if x + sprites[c].width >= WIDTH
y += sprites[c].height
x = 0
end
rects[c] = Rect.new(x, y, sprites[c].width, sprites[c].height)
x += sprites[c].width}
rects[0] = y + rects[characters[0]].height
return rects
end
def self.create_damage(colors, characters)
damages = {}
b1 = Bitmap.new(FONT_HEIGHT, FONT_HEIGHT)
b1.font.name = 'Georgia'
b1.font.size = FONT_HEIGHT
b1.font.bold = false#true
b1.font.italic = true
b2 = b1.clone
colors.each {|ary|
color1, color2 = ary
damages[color1] = {}
characters.each {|c|
self.draw_text_on(b1, c, color1)
self.draw_text_on(b2, c, color2)
rect = self.get_text_size(b1)
damages[color1][c] = self.transfer_text(b1, b2, rect)}
$sprite.bitmap.clear
$sprite.bitmap.draw_text(0, 0, 320, 32, sprintf("%d, %d, %d", color1.red.to_i, color1.green.to_i, color1.blue.to_i), 1)
Graphics.update}
return damages
end
def self.draw_text_on(bitmap, text, color)
bitmap.clear
bitmap.font.color = Color.new(0, 0, 0)
bitmap.draw_text(-5, -5, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-5, -3, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-3, -5, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-3, -3, FONT_HEIGHT, FONT_HEIGHT, text, 1)
#bitmap.font.color = Color.new(0, 0, 0, 192)
bitmap.draw_text(-6, -6, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-6, -5, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-6, -4, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-6, -3, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-6, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-5, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-4, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-3, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, -3, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, -4, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, -5, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, -6, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-3, -6, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-4, -6, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-5, -6, FONT_HEIGHT, FONT_HEIGHT, text, 1)
#bitmap.font.color = Color.new(0, 0, 0, 128)
bitmap.draw_text(-5, -1, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-4, -1, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-3, -1, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, -1, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-1, -5, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-1, -4, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-1, -3, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-1, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-1, -1, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-4, 0, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-3, 0, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-2, 0, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(-1, 0, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(0, -4, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(0, -3, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(0, -2, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.draw_text(0, -1, FONT_HEIGHT, FONT_HEIGHT, text, 1)
bitmap.font.color = color
bitmap.draw_text(-4, -4, FONT_HEIGHT, FONT_HEIGHT, text, 1)
end
def self.get_text_size(bitmap)
minx, maxx, miny, maxy = bitmap.width - 1, 0, bitmap.height - 1, 0
(0...bitmap.height).each {|y| (0...bitmap.width).each {|x|
color = bitmap.get_pixel(x, y)
if color.alpha > 0
minx = x if minx > x
maxx = x if maxx < x
miny = y if miny > y
maxy = y if maxy < y
end}}
return Rect.new(minx - 1, miny, maxx - minx + 3, maxy - miny + 1)
end
def self.transfer_text(b1, b2, rect)
h, o = rect.height, rect.height / 4
bitmap = Bitmap.new(rect.width, b1.height)
(0...h).each {|y| (0...rect.width).each {|x|
c1 = b1.get_pixel(x + rect.x, y + rect.y)
if c1.alpha > 0
c2 = b2.get_pixel(x + rect.x, y + rect.y)
if y >= (h - o) / 2 && y < (h + o) / 2
i = (h - o) / 2
else
i = ((y < h / 2) ? y : h - y - 1)
end
r = c2.red + (c1.red - c2.red) * i / ((h - o) / 2)
g = c2.green + (c1.green - c2.green) * i / ((h - o) / 2)
b = c2.blue + (c1.blue - c2.blue) * i / ((h - o) / 2)
bitmap.set_pixel(x, y + rect.y, Color.new(r, g, b, c1.alpha))
end}}
return bitmap
end
end
end