Fix variable reference in sprite

This commit is contained in:
Alex Clink 2022-01-02 20:41:17 -05:00
parent ce0f055bf5
commit 56b5cc198b
5 changed files with 27 additions and 25 deletions

View file

@ -109,5 +109,5 @@ class Snow < PF::Game
end
end
engine = Snow.new(1200, 800, 1)
engine = Snow.new(1200, 800, 1, window_flags: SDL::Window::Flags::RESIZABLE | SDL::Window::Flags::SHOWN)
engine.run!

View file

@ -4,7 +4,7 @@ require "../src/pixel_text"
class TextGame < PF::Game
@text : PF::PixelText
def initialize(*args)
def initialize(*args, **kwargs)
super
@text = PF::PixelText.new("assets/pf-font.png")
@text.color(PF::Pixel.new(255, 255, 255))
@ -22,29 +22,32 @@ class TextGame < PF::Game
if @x < 0
@x = 0
@dx = -@dx
@text.color(PF::Pixel.random)
end
if @x > @width - (@msg.size * @text.width)
@x = @width - (@msg.size * @text.width)
if @x > @width - (@msg.size * @text.char_width)
@x = @width - (@msg.size * @text.char_width)
@dx = -@dx
@text.color(PF::Pixel.random)
end
if @y < 0
@y = 0
@dy = -@dy
@text.color(PF::Pixel.random)
end
if @y > @height - (@text.height)
@y = @height - (@text.height)
if @y > @height - (@text.char_height)
@y = @height - (@text.char_height)
@dy = -@dy
@text.color(PF::Pixel.random)
end
end
def draw
clear(0, 0, 50)
@text.color(PF::Pixel.random)
@text.draw(@screen, @msg, @x.to_i, @y.to_i)
end
end
engine = TextGame.new(160, 100, 4).run!
engine = TextGame.new(160, 100, 4, flags: SDL::Renderer::Flags::ACCELERATED | SDL::Renderer::Flags::PRESENTVSYNC).run!

View file

@ -24,9 +24,11 @@ module PF
@fps_frames : UInt32 = 0 # frames passed since the last recorded fps.
@last_time : Float64 = Time.monotonic.total_milliseconds
def initialize(@width, @height, @scale = 1, @title = self.class.name, flags = SDL::Renderer::Flags::ACCELERATED)
def initialize(@width, @height, @scale = 1, @title = self.class.name,
flags = SDL::Renderer::Flags::ACCELERATED,
window_flags : SDL::Window::Flags = SDL::Window::Flags::SHOWN)
SDL.init(SDL::Init::VIDEO)
@window = SDL::Window.new(@title, @width * @scale, @height * @scale)
@window = SDL::Window.new(@title, @width * @scale, @height * @scale, flags: window_flags)
@renderer = SDL::Renderer.new(@window, flags: flags)
@renderer.scale = {@scale, @scale}
@screen = SDL::Surface.new(LibSDL.create_rgb_surface(

View file

@ -3,11 +3,11 @@ require "./sprite"
module PF
class PixelText < Sprite
getter width : Int32
getter height : Int32
getter char_width : Int32
getter char_height : Int32
@chars : String
def initialize(path : String, @width : Int32 = 7, @height : Int32 = 8, mapping : String? = nil)
def initialize(path : String, @char_width : Int32 = 7, @char_height : Int32 = 8, mapping : String? = nil)
super(path)
@chars = mapping || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?().,/\\[]{}$#+-“”‘’'\"@"
end
@ -16,14 +16,8 @@ module PF
color_val = pixel.format(@surface.format)
alpha_mask = @surface.format.a_mask
0.upto(@surface.height - 1) do |y|
0.upto(@surface.width - 1) do |x|
loc = pixel_pointer(x, y)
if loc.value & alpha_mask != 0
loc.value = color_val
end
end
pixels.map! do |p|
p & alpha_mask != 0_u32 ? color_val : 0_u32
end
end
@ -39,11 +33,14 @@ module PF
if index = @chars.index(char)
char_y, char_x = index.divmod(26)
char_y *= @height
char_x *= @width
char_y *= @char_height
char_x *= @char_width
unless char == ' '
@surface.blit(surface, SDL::Rect.new(char_x - 1, char_y, @width, @height), SDL::Rect.new(x + ix * @width, y + iy * @height, @width, @height))
@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

View file

@ -27,7 +27,7 @@ module PF
# Raw access to the pixels as a Slice
def pixels
Slice.new(@screen.pixels.as(Pointer(UInt32)), width * height)
Slice.new(@surface.pixels.as(Pointer(UInt32)), width * height)
end
# Get the pointer to a pixel