Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / How to best use Window#record method
- - By Dschinghis Date 2012-01-27 23:41 Edited 2012-01-28 03:29
I'm making a tile based game engine using the gosu ruby gem, things are going great so far. Gosu is really excellent!

I want to use the Window#record method to batch any updates to the visible tile objects, but I'm unsure the best place to put the record operation. I expect the flow should go something like:

def update
    # some game logic which flags the grid as dirty

    if grid.dirty?
        @grid_image = record(w,h){ tiles.each{|tile| tile.draw} }
    end
end

def draw
    @grid_image.draw( 0, 0, 0 )
end


I've read over and over that you shouldn't call draw from within the update method. However, since the record method just batches a bunch of GL calls and doesn't actually draw to the window, it seems like it should be okay to call from within the update loop. So where's the proper place to perform the record operation? Should I put that in the window's draw method guarded by the same grid.dirty? flag, something like this:

def draw
    if grid_dirty?
        @grid_image = record(w,h){ tiles.each{|tile| tile.draw} }
    end

    @grid_image.draw( 0, 0, 0 )
end


Any information I can get about the best usage of the Window#record method would be appreciated. If fact, if people want to use this thread as a knowledge dump for Window#record best practices, tips, tricks, etc, I'd be happy to do a quick wiki page about it, since the record method is really slick and deserves some documentation. Julian posted a bit ago that "there are countless ways to use and abuse" the record feature, it always made me curious what kinds of ways people were using it. :)

Cheers!
Parent - - By jlnr (dev) Date 2012-01-28 01:48
1. You should be able to call record anywhere. This should be documented, I guess - and if it doesn't work, please let me know or file a bug on GitHub :)

2. I like to just set @grid_image to nil when it is not valid. Then I can do:

@grid_image ||= record { ... }
@grid_image.draw 0, 0, 0


I like to do this in draw because it means that the image will be created right when it is needed. It is impossible for it to be invalid or missing when I want to draw it.
Parent - By Dschinghis Date 2012-01-28 03:44
1. Excellent. I hadn't had any problems using it in the update method, but it's good to know that's intended and not just a happy (and potentially unpredictable) coincidence.

2. I was trying to stay away from doing anything mutable in draw(), since the docs suggested that I'd generally be safer if I treated draw() as a read-only method, but that's a nice, concise way to handle it.

Thanks for the reply!
Parent - - By erisdiscord Date 2012-01-28 04:20
Somewhat related, because a terrible idea has stuck me: is record callable from a thread? Is it meant to be? C: I know that most of Gosu isn't.

It might be cool to have a sort of provider/consumer model for map chunks where one thread generates and managed recorded chunks according to the camera position and the other draws whatever's available. Only for large maps, of course. :D
Parent - - By jlnr (dev) Date 2012-01-28 08:10
No, for that I would have to move some stuff into thread-local storage. :(
Parent - - By erisdiscord Date 2012-01-28 16:48
Oh, well, that's fine. I guess it's the same problem Gosu has in general, right?
Parent - By jlnr (dev) Date 2012-01-29 13:20
Yes in that it is not thread-safe. But for each part, I'd have to choose between locking and making it thread-local. This is one of the cases where it should be thread-local.
Up Topic Gosu / Gosu Exchange / How to best use Window#record method

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill