diff --git a/assets/pf-font.png b/assets/pf-font.png index 28818a9..f50df7c 100644 Binary files a/assets/pf-font.png and b/assets/pf-font.png differ diff --git a/examples/3d.cr b/examples/3d.cr index cd8ebf1..d7adb60 100644 --- a/examples/3d.cr +++ b/examples/3d.cr @@ -2,7 +2,6 @@ require "../src/game" require "../src/controller" require "../src/sprite" require "../src/pixel" -require "../src/pixel_text" require "../src/vector" require "../src/3d/*" @@ -12,7 +11,6 @@ class ThreeDee < PF::Game @camera : PF::Camera @paused = false @speed = 5.0 - @text = PF::PixelText.new("./assets/pf-font.png") @controller : PF::Controller(PF::Keys) def initialize(*args, **kwargs) @@ -96,7 +94,7 @@ class ThreeDee < PF::Game def draw clear(25, 50, 25) tris = @projector.project(@model.tris) - @text.draw_to(screen, "Triangles: #{tris.size}") + draw_string("Triangles: #{tris.size}", 3, 3) tris.each do |tri| # Rasterize all triangles diff --git a/examples/animated_sprite.cr b/examples/animated_sprite.cr index 54e9a40..ba04803 100644 --- a/examples/animated_sprite.cr +++ b/examples/animated_sprite.cr @@ -1,12 +1,9 @@ require "../src/game" require "../src/sprite" -require "../src/pixel_text" require "../src/animation" module PF class SpriteExample < Game - @text : PixelText = PixelText.new("assets/pf-font.png") - def initialize(*args, **kwargs) super @person = Animation.new("assets/walking.png", 32, 64, 10) @@ -20,7 +17,7 @@ module PF def draw clear(60, 120, 200) - @text.draw_to(screen, "Frame: #{@person.frame}", 5, 5) + draw_string("Frame: #{@person.frame}", 5, 5) fill_rect(0, 65, width - 1, height - 1, Pixel.new(100, 100, 100)) @person.draw_to(screen, (viewport // 2) - @person.size // 2) @cat.draw_to(screen, 30, 56) diff --git a/examples/balls.cr b/examples/balls.cr index 2474765..d15b0b8 100644 --- a/examples/balls.cr +++ b/examples/balls.cr @@ -2,7 +2,6 @@ require "../src/game" require "../src/shape" require "../src/entity" require "../src/entity/circle_collision" -require "../src/pixel_text" module PF class Ball < Entity @@ -22,11 +21,9 @@ module PF ADD_BALL = 2.0 @balls : Array(Ball) = [] of Ball @ball_clock = ADD_BALL - @text = PF::PixelText.new("assets/pf-font.png") def initialize(*args, **kwargs) super - @text.color(PF::Pixel.new(255, 255, 255)) add_ball end @@ -75,7 +72,7 @@ module PF fill_shape(Shape.translate(ball.frame, translation: ball.position).map(&.to_i32), ball.color) # draw_circle(ball.position.to_i32, ball.radius.to_i32, Pixel.green) end - @text.draw_to(screen, "#{@balls.size}", 5, 5) + draw_string("Balls: #{@balls.size}", 5, 5, Pixel.white) end end end diff --git a/examples/procedural.cr b/examples/procedural.cr index 2f965fb..5d9cc1d 100644 --- a/examples/procedural.cr +++ b/examples/procedural.cr @@ -1,12 +1,10 @@ require "../src/game" require "../src/lehmer32" -require "../src/pixel_text" module PF class Proceedural < Game @buffer_size : Int32 @buffer : Pointer(UInt32) - @text = PixelText.new("assets/pf-font.png") @pan : Vector2(Float64) = PF::Vector[0.0, 0.0] @seed : UInt32 @@ -16,7 +14,6 @@ module PF @buffer = screen.pixel_pointer(0, 0) @random = Lehmer32.new @redraw = true - @text.color(Pixel.new(255, 255, 255)) @controller = Controller(Keys).new({ Keys::LEFT => "left", @@ -62,7 +59,7 @@ module PF end end time = elapsed_time - start - @text.draw_to(@screen, "frame: #{time.round(2)}ms", 5, 5, bg: Pixel.black) + draw_string("frame: #{time.round(2)}ms", 5, 5, Pixel.white, bg: Pixel.black) end end end diff --git a/examples/snow.cr b/examples/snow.cr index 09cf2f6..0872574 100644 --- a/examples/snow.cr +++ b/examples/snow.cr @@ -3,7 +3,6 @@ require "../src/controller" require "../src/sprite" require "../src/pixel" require "../src/vector" -require "../src/pixel_text" class Wind property width : Int32 diff --git a/examples/text.cr b/examples/text.cr index ec0b949..3a788b0 100644 --- a/examples/text.cr +++ b/examples/text.cr @@ -1,18 +1,15 @@ require "../src/game" -require "../src/pixel_text" class TextGame < PF::Game - @text : PF::PixelText - def initialize(*args, **kwargs) super - @text = PF::PixelText.new("assets/pf-font.png") - @text.color(PF::Pixel.new(255, 255, 255)) @x = 0.0 @y = 0.0 @dx = 50.0 @dy = 50.0 @msg = "Hello, World!" + # @msg = "HI" + @color = PF::Pixel.random end def update(dt, event) @@ -22,32 +19,32 @@ class TextGame < PF::Game if @x < 0 @x = 0 @dx = -@dx - @text.color(PF::Pixel.random) + @color = PF::Pixel.random end - if @x > @width - (@msg.size * @text.char_width) - @x = @width - (@msg.size * @text.char_width) + if @x > @width - (@msg.size * PF::Sprite::CHAR_WIDTH) + @x = @width - (@msg.size * PF::Sprite::CHAR_WIDTH) @dx = -@dx - @text.color(PF::Pixel.random) + @color = PF::Pixel.random end if @y < 0 @y = 0 @dy = -@dy - @text.color(PF::Pixel.random) + @color = PF::Pixel.random end - if @y > @height - (@text.char_height) - @y = @height - (@text.char_height) + if @y > @height - (PF::Sprite::CHAR_HEIGHT) + @y = @height - (PF::Sprite::CHAR_HEIGHT) @dy = -@dy - @text.color(PF::Pixel.random) + @color = PF::Pixel.random end end def draw clear(0, 0, 50) - @text.draw_to(@screen, @msg, @x.to_i, @y.to_i) + draw_string(@msg, @x.to_i, @y.to_i, @color) end end -engine = TextGame.new(160, 100, 4, flags: SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC).run! +engine = TextGame.new(160, 100, 4).run! diff --git a/src/pixel_text.cr b/src/pixel_text.cr deleted file mode 100644 index 6253509..0000000 --- a/src/pixel_text.cr +++ /dev/null @@ -1,67 +0,0 @@ -require "sdl/image" -require "./sprite" -require "./pixel" - -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 : Int = 0, y : Int = 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 - - def draw_to(sprite : Sprite, text : String, x : Int32 = 0, y : Int32 = 0, bg : Pixel? = nil) - if background = bg - sprite.fill_rect(x - 1, y - 1, x + (char_width * text.size) - 1, y + char_height - 1, background) - end - draw_to(sprite.surface, text, x, y) - end - - def draw_to(sprite : Sprite, text : String, pos : Vector2(Int)) - draw_to(sprite.surface, text, pos.x, pos.y) - end - end -end