pixelfaucet/src/pixel_text.cr
2022-01-06 20:25:39 -05:00

55 lines
1.4 KiB
Crystal

require "sdl/image"
require "./sprite"
module PF
class PixelText < Sprite
getter char_width : Int32
getter char_height : Int32
@chars : String
def initialize(path : String, @char_width : Int32 = 7, @char_height : Int32 = 8, mapping : String? = nil)
super(path)
@chars = mapping || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?().,/\\[]{}$#+-“”‘’'\"@"
end
def color(pixel : Pixel)
color_val = pixel.format(@surface.format)
alpha_mask = @surface.format.a_mask
pixels.map! do |p|
p & alpha_mask != 0_u32 ? color_val : 0_u32
end
end
def draw_to(surface : SDL::Surface, text : String, x : Int32 = 0, y : Int32 = 0)
ix = 0
iy = 0
text.each_char do |char|
if char == '\n'
iy += 1
ix = 0
next
end
if index = @chars.index(char)
char_y, char_x = index.divmod(26)
char_y *= @char_height
char_x *= @char_width
unless char == ' '
@surface.blit(surface,
SDL::Rect.new(char_x - 1, char_y, @char_width, @char_height),
SDL::Rect.new(x + ix * @char_width, y + iy * @char_height, @char_width, @char_height)
)
end
end
ix += 1
end
end
def draw_to(sprite : Sprite, text : String, x : Int32 = 0, y : Int32 = 0)
draw_to(sprite.surface, text, x, y)
end
end
end