2021-12-12 00:10:52 -05:00
|
|
|
require "sdl/image"
|
2022-01-02 19:39:17 -05:00
|
|
|
require "./sprite"
|
2021-12-12 00:10:52 -05:00
|
|
|
|
|
|
|
module PF
|
2022-01-02 19:39:17 -05:00
|
|
|
class PixelText < Sprite
|
2022-01-02 20:41:17 -05:00
|
|
|
getter char_width : Int32
|
|
|
|
getter char_height : Int32
|
2021-12-12 00:10:52 -05:00
|
|
|
@chars : String
|
|
|
|
|
2022-01-02 20:41:17 -05:00
|
|
|
def initialize(path : String, @char_width : Int32 = 7, @char_height : Int32 = 8, mapping : String? = nil)
|
2022-01-02 19:39:17 -05:00
|
|
|
super(path)
|
2021-12-12 00:10:52 -05:00
|
|
|
@chars = mapping || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?().,/\\[]{}$#+-“”‘’'\"@"
|
|
|
|
end
|
|
|
|
|
|
|
|
def color(pixel : Pixel)
|
2022-01-02 19:39:17 -05:00
|
|
|
color_val = pixel.format(@surface.format)
|
|
|
|
alpha_mask = @surface.format.a_mask
|
2021-12-12 00:10:52 -05:00
|
|
|
|
2022-01-02 20:41:17 -05:00
|
|
|
pixels.map! do |p|
|
|
|
|
p & alpha_mask != 0_u32 ? color_val : 0_u32
|
2021-12-12 00:10:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-06 20:25:39 -05:00
|
|
|
def draw_to(surface : SDL::Surface, text : String, x : Int32 = 0, y : Int32 = 0)
|
2021-12-12 00:10:52 -05:00
|
|
|
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)
|
2022-01-02 20:41:17 -05:00
|
|
|
char_y *= @char_height
|
|
|
|
char_x *= @char_width
|
2021-12-12 00:10:52 -05:00
|
|
|
|
|
|
|
unless char == ' '
|
2022-01-02 20:41:17 -05:00
|
|
|
@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)
|
|
|
|
)
|
2021-12-12 00:10:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ix += 1
|
|
|
|
end
|
|
|
|
end
|
2022-01-06 20:25:39 -05:00
|
|
|
|
|
|
|
def draw_to(sprite : Sprite, text : String, x : Int32 = 0, y : Int32 = 0)
|
|
|
|
draw_to(sprite.surface, text, x, y)
|
|
|
|
end
|
2021-12-12 00:10:52 -05:00
|
|
|
end
|
|
|
|
end
|