Crsfml sprite texture not updating on it's own

I’m trying to get the texture to update on it’s own, but currently it only does it when I press the up or down keys:

CURSOR_TEXTURE_1 = SF::Texture.from_file("graphics/Cursor.png")
Cursor_opt1.texture_rect = SF.int_rect(0, 0, 62, 65)
Cursor_opt1.position = SF.vector2(750, 610)
Cursor_opt1 = SF::Sprite.new(CURSOR_TEXTURE_1) 


module CONTROLS
    class Menucontrols
        
        
   def Menucontrols.arrowup (this)
    Cursor_opt1.position = SF.vector2(750, 610)
    
   end

   def Menucontrols.arrowdown (this)
    Cursor_opt1.position = SF.vector2(750,730)
     
end


def Menucontrols.cursorFunc(this)
  spawn do
    blinking = true
    loop do
      if blinking
        puts "0"
        Cursor_opt1.texture_rect = SF.int_rect(62, 0, 62, 65)
    
      else
        puts "1"
        Cursor_opt1.texture_rect = SF.int_rect(0, 0, 62, 65)
        
      end
      blinking = !blinking
      if this.open?
        Cursor_opt1.scale = SF.vector2(1, 1) 
      #CURSOR_TEXTURE_1.update(this)

      sleep 2.seconds 
      end
    end
  end
  Fiber.yield
end
end
end

CURSOR_TEXTURE_1.update(this) is currently commented out because this happens:


I’m pretty sure the rectangle fits, I even made it tiny and this still happened

Hi and welcome to the forum!

Do you have a link to the full code? The snippet alone is missing some details that might be crucial here.

Also you are using a separate fiber for your event loop, which I don’t think is necessary here. Event processing itself might be faster than switching the context - and you avoid some really nasty problems like race conditions (which is also a potential cause for the assertion fail).

1 Like

Here’s the github repository: GitHub - MagicSparklyGoldfish/crystal_m
it’s a bit of a mess right now, I’m planning on cleaning it up soon. I think I used a fiber because the first iteration of the script ate all my memory, forcing me to shut down my computer. I’ve worked on it a lot since then so that may no longer be the case, though. I’ve spent the past few days trying stuff to make it work :sweat_smile:

One problem might be the code at https://github.com/MagicSparklyGoldfish/crystal_m/blob/master/src/crystal_meth.cr#L63 (line 63 to 79 of src/crystal_meth.cr):

 while event = window.poll_event
    this = window
    this2 = event
    case (@@menu)
    when "main"
        Gui::Menus.drawmainmenu(window)
        break
    when "charselect"
        Gui::Menus.character_select(window)
        break
    else
        puts "something is very wrong"
    end

    window.display
    this = window
end

I’m not sure this is related to the problems you described, but this code only renders the window if you have a new event. This will likely lead to massive performance issues - and might be the reason why you encountered memory issues before.

Also there’s a distinct difference in your handlers for Up/Down and Left/Right inputs (same file as the code above), maybe that is the issue of your initial problem.

I think you should try to reduce your code to a bare working minimum (mostly an SFML event loop where you display a simple text with the FPS or so) and then add any key press handling back one step at a time - this might also help finding any issues early on, before the code becomes too confusing :slight_smile:

Yeah, that’s the first file I ever wrote in crystal so it’s a HUGE mess :sweat_smile:. I’ll just redo the whole file before it gets too unmanageable, It already gives me a headache. Thanks! :smile:

2 Likes