Window#needs_redraw?. Actually, I don't use the Window#draw method but a Thread in Window#initialize and I need to "freeze" the drawing of the screen. My first idea was to Gosu#record the screen but since I'm using Gosu#clip_to, that's not possible.def needs_redraw?
false
enddef needs_redraw?
if Graphics.needs_redraw?
Graphics.needs_redraw = false
true
else
false
end
end/lib/gosu/patches.rb:152:in `tick': SWIG director type mismatch in output value of type 'bool' (RuntimeError)bool and I didn't find any understandable explanation on Google.Thread a good idea or should I use something else ?needs_redraw? is currently broken. Oops! I've opened a ticket here: https://github.com/gosu/gosu/issues/376Thread is usually a bad idea with Ruby/Gosu, unless you use it purely for game logic. The needs_redraw? method doesn't guarantee that draw will not be called (even though it does that in recent versions of Gosu), it's just an optimisation hint.
while and a method to update the graphic part inside this while. Since I'm just porting an existing project to Gosu, the Thread was the trick I found to use the previous code.
class Game < Gosu::Window
def initialize
super
@main = Thread.new do
require_relative 'some_file.rb'
while true
# ... Do something ; all the game will be here
Graphics.update
break if # ...
end
end
end
end
draw only when Graphics.update is called. So, Graphics.update is coded like that:
def update
unless @frozen
# Do some stuff
@needs_redraw = true
end
sleep 1.0/Graphics.fps
end
record, but not without it. You cannot draw outside of Window#draw, and you can only create Gosu resources (Image etc.) on the main thread currently.Window#tickclass Game < Gosu::Window
def draw
# drawing needs to happen here, but maybe you can call into another function...
end
end
game = Game.new(...)
while true
# ... Do something
game.tick # this will call Window#update and Window#draw
break if # ...
end
#tick, the game crashes. So. How can I freeze the game ? And so how can I close the window ?break unless window.tick, but if I'm freezing the game, it's not working anymore.
tick is used like this from within Window#show:def show # pseudo-implementation
while tick
sleep 0.016
end
# stops when tick returns false
endtick returns false the game should end, because either Window#close was called, or the user clicked the [x] in the window corner.tick, the window will re-appear.# ... Do something :)
tick (to do some expensive calculation and use of MiniMagick -- file conversion, etc. -- and to setup all my objects -- images).#render_to_image method to hide changes on the screen. Ashton allows a render to texture, I think ; but I can't get it work -- Ruby OpenGL is not compiling.ashton/shader.rb:109:in enable': undefined method z' for Gosu::Window. And then... I don't want to use an old version of Ruby just for Ashton.
#render_to_image would only help you if you could use it on a background thread, which I don't think Ashton allows.Gosu#clip_to to use Gosu#record.
Gosu::Window running in parallel to your thread, and only interlock when you need to run something on the main thread (create images, read a value from button_down? etc.), and when you want to redraw the screen.needs_redraw?, which mysteriously works on my computer but I'm using a dev version of Gosu, so maybe it'll be broken for you until 0.11.1 is out. But does that seem like a good workaround?Window#tick. :)sleep calls, it runs at 30 FPS, and it'd run at 60 FPS with more careful scheduling of the draw/update blocks, but of course that depends on the game's actual code.record {} is that it doesn't work with clip_to, can you maybe record several macros and remember with which clip rect the macros need to be rendered?
class Graphics
@views = [] # An array containing all my views
@recorded_views = [] # An array containing all my views recorded
def draw_frozen
@recorded_views.each do |v|
v[3].draw v.x, v.y, v.z
end
end
def draw
if @frozen
draw_frozen
return
end
@views.each do |v|
# Draw here
end
end
def freeze
@frozen = true
@recorded_views = []
@views.each do |v|
@recorded_views << [v.x, v.y, v.z, Gosu.record(v.width,v.height){ # Draw here }]
end
end
def transition(duration)
duration.times do
# I need to find a way to create a transition.
sleep 1
end
@frozen = false
end
def update
sleep 0.016
end
endclass GameWindow < Gosu::Window
def initialize
# Init ...
main = Thread.new do
img = Gosu::Image.new 'img.png'
while
# Updating
Graphics.update
if #...
Graphics.freeze
# Change something
Graphics.transition(5) # Transition during 5 seconds
end
end
end
end
def draw
Graphics.draw
end
end
record and Image.new on a background thread, which is not supported by Gosu (yet?). Specifically, Window#draw and record cannot run in parallel right now, they use the same internal data structure.
needs_redraw? method: it's not actually broken, the error message is just extremely misleading.needs_redraw? (e.g. typo in a method name), then this exception will be swallowed by a safety mechanism inside Gosu, and SWIG will print the wrong error message.
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill