From adf99db2061a509e4202641b00f6592253596ac0 Mon Sep 17 00:00:00 2001 From: mewrrythekibby Date: Sat, 29 Nov 2025 23:37:18 -0600 Subject: [PATCH] Properly structure HEX_BGs class --- main.rb | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/main.rb b/main.rb index 9c3dc81..b43914d 100644 --- a/main.rb +++ b/main.rb @@ -60,38 +60,44 @@ def recalculate_hexagons puts "(#{$hex_points[3][0]},#{$hex_points[3][1]})".center(HEX_DEBUG_WIDTH) $hex_x_offset = $hex_width/2 $hex_y_offset = $hex_tip + $hex_mid - $hex_bgs.clear + $hex_bgs.clear! end class HexEditXref_BgGrphStore def initialize + clear! + end + def clear! @hex_ids = Hash.new #hexid => color - @hex_refs = Hash.new #color => [num_refs, hexids] + @hex_refs = Hash.new #color => hexids @hex_bgs = Hash.new #color => bg end def register(hexid,color) - argb=color.argb + raise TypeError, ("color must be a Gosu::Color, not a '#{color.class}'" + ) unless color.is_a? Gosu::Color + argb = color.argb + return if (@hex_ids.has_key?(hexid) && + @hex_ids[hexid] == argb) unregister hexid @hex_ids[hexid] = argb if @hex_refs.has_key? argb - @hex_refs[argb][0] += 1 - unless @hex_refs[argb][1].include? hexid - @hex_refs[argb][1].append hexid - end + @hex_refs[argb].add(hexid) + else + @hex_refs[argb] = Set.new([hexid, ]) end render argb end def unregister(hexid) - if @hex_ids.has_key? hexid - argb = @hex_ids[hexid] - @hex_ids.delete hexid - end + return unless @hex_ids.has_key? hexid + # ➣ If the key doesn't exist, assume + # the call was made mistakenly. + # Note: may cause leaks later + + argb = @hex_ids[hexid] + @hex_ids.delete hexid if @hex_refs.has_key? argb - if @hex_refs[argb][1].include? hexid - @hex_refs[argb][1].delete hexid - @hex_refs[argb][0] -= 1 - end - if @hex_refs[argb][0] < 1 + @hex_refs[argb].delete hexid + if @hex_refs[argb].size < 1 @hex_refs.delete argb end end @@ -101,6 +107,8 @@ class HexEditXref_BgGrphStore end end def [](color) + raise TypeError, ("color must be a Gosu::Color, not a '#{color.class}'" + ) unless color.is_a? Gosu::Color argb = color.argb raise RuntimeError.new( "Color #{argb} was requested from "+ @@ -109,9 +117,10 @@ class HexEditXref_BgGrphStore ) unless @hex_bgs.has_key? argb return @hex_bgs[argb] end + private def render(argb) color = Gosu::Color.new(argb) - @hex_bgs[argb] = [Gosu::record($hex_width, $hex_mid+($hex_tip*2)) { + @hex_bgs[argb] = Gosu::record($hex_width, $hex_mid+($hex_tip*2)) { # Double Quad Method (0145, 1234): Gosu::draw_quad( # 0145 $hex_points[0][0], $hex_points[0][1], color, #0 @@ -127,8 +136,7 @@ class HexEditXref_BgGrphStore $hex_points[4][0], $hex_points[4][1], color, #4 0 ) - },Time.now - ] + } end end @@ -339,7 +347,7 @@ class HexagonTerminalWindow < Gosu::Window end end -$hex_bgs = HexBgCache.new +$hex_bgs = HexEditXref_BgGrphStore.new recalculate_hexagons window = HexagonTerminalWindow.new(800, 600, false) window.show \ No newline at end of file