Fix segfault by moving "register" to the update call, rather than the draw one

This commit is contained in:
Mewrry the Kitty 2025-11-30 15:16:30 -06:00
parent d9bcf32e17
commit 70f888196f

27
main.rb
View file

@ -217,10 +217,12 @@ class Hex
@hexid = SecureRandom.uuid @hexid = SecureRandom.uuid
@dead = false @dead = false
end end
def update
def draw
amidead? amidead?
$hex_bgs.register(@hexid, @text_props.bg_color) $hex_bgs.register(@hexid, @text_props.bg_color)
end
def draw
amidead?
$hex_bgs[@text_props.bg_color].draw( $hex_bgs[@text_props.bg_color].draw(
@x, @y, 0, 1, 1, Gosu::Color::WHITE @x, @y, 0, 1, 1, Gosu::Color::WHITE
) # layer 0: hex-bg ) # layer 0: hex-bg
@ -284,7 +286,7 @@ class HexagonTerminalWindow < Gosu::Window
@old_text = [0,'',0] @old_text = [0,'',0]
# Hexes # Hexes
@xexes = ((width-$hex_x_offset) / $hex_width).floor # xexes = 'x's of hexes @xexes = ((width-$hex_x_offset) / $hex_width).floor # xexes = 'x's of hexes
@yexes = ((height-$hex_tip) / $hex_y_offset ).floor # yexes = 'y's of hexes @yexes = ((height-$hex_tip) / $hex_y_offset ).floor # yexes = 'y's of hexes
@next_row_is_shifted = false @next_row_is_shifted = false
@hexes = [] @hexes = []
@ -301,7 +303,7 @@ class HexagonTerminalWindow < Gosu::Window
# Clear the screen # Clear the screen
@hexes.each do |rex| @hexes.each do |rex|
rex.each do |hex| rex.each do |hex|
hex.char = ' ' hex.char = ' '
end end
end end
@ -323,7 +325,7 @@ class HexagonTerminalWindow < Gosu::Window
end end
new_text = [ new_text = [
self.text_input.selection_start, self.text_input.selection_start,
self.text_input.text, self.text_input.text,
self.text_input.caret_pos self.text_input.caret_pos
] ]
if @old_text != new_text if @old_text != new_text
@ -344,6 +346,13 @@ class HexagonTerminalWindow < Gosu::Window
end end
print "\x1b[G\x1b[2K#{out}\x1b[#{new_text[2]+1}G" print "\x1b[G\x1b[2K#{out}\x1b[#{new_text[2]+1}G"
end end
# End of update should call hex updates
@hexes.each do |rex|
rex.each do |hex|
hex.update
end
end
end end
def draw def draw
@ -354,7 +363,7 @@ class HexagonTerminalWindow < Gosu::Window
) )
# Draw the hexes # Draw the hexes
@hexes.each do |rex| # rex=row of hexes @hexes.each do |rex| # rex=row of hexes
rex.each do |hex| rex.each do |hex|
hex.draw hex.draw
end end
end end
@ -404,7 +413,7 @@ class HexagonTerminalWindow < Gosu::Window
Gosu::Color::WHITE Gosu::Color::WHITE
] ]
@hexes.each do |rex| @hexes.each do |rex|
rex.each do |hex| rex.each do |hex|
hex.y -= $hex_y_offset hex.y -= $hex_y_offset
end end
end end
@ -417,9 +426,9 @@ class HexagonTerminalWindow < Gosu::Window
for i in 0..@xexes-1 for i in 0..@xexes-1
@active_styling.bg_color = Gosu::Color::BLACK @active_styling.bg_color = Gosu::Color::BLACK
@hexes[-1] << Hex.new( @hexes[-1] << Hex.new(
(i*$hex_width) + (@next_row_is_shifted? $hex_x_offset : 0), (i*$hex_width) + (@next_row_is_shifted? $hex_x_offset : 0),
(@yexes-1)*$hex_y_offset, (@yexes-1)*$hex_y_offset,
" ", TextProps.new(@font, @active_styling)) " ", TextProps.new(@font, @active_styling))
end end
@next_row_is_shifted = !@next_row_is_shifted @next_row_is_shifted = !@next_row_is_shifted
end end