oakes / play-clj

A Clojure game library
The Unlicense
939 stars 73 forks source link

Bug on timer resume ? #46

Closed DjebbZ closed 9 years ago

DjebbZ commented 10 years ago

Hi,

I noticed something strange while developping on desktop. In a game where I have a timer, when the game loses focus the :on-pause function gets called (for instance when I go back to my editor). Then I click in the game window and the :on-resume function gets called. Let's call the delta of time between losing focus and regaining focus D. When the :on-resume is called, the timer is not resumed instantly. Instead it's started after the delta D. Weird, huh ?

By the way, the generated docs of play-clj mentions that :on-pause and :on-resume are called only on mobile but they're called on my desktop environment during dev.

oakes commented 10 years ago

What OS are you on? I made a timer and the timer resumed as expected. Regarding the docs, it looks like you are right, the libGDX docs lied!

DjebbZ commented 10 years ago

I experienced this behavior on both OSX Mountain Lion and Linux Arch. On both, it's been developed with IntelliJ with the CursiveClojure plugin, with the tiled window manager i3 (not sure it's related, just saying in case of). Maybe it's related to setting (or not) the screen on GL thread with (on-gl (set-screen! ...)). The code where this behavior is here, maybe you can look at it or even test it. https://github.com/DjebbZ/whack-a-lion

(A code review will be much much appreciated by the way, we intend to use play-clj this week-end for Ludum Dare).

oakes commented 10 years ago

I tried the project, and it looks like it consistently takes 4 seconds (the interval time you set) for the timer to fire after bringing the window back in focus. I don't see any indication that it is based on how long the screen was paused.

It is great to hear you are looking to use it for Ludum Dare! I hope it works well for you, as I have been hoping to see it used in competitions like that. I'll be busy teaching Clojure games to a classroom this weekend so unfortunately I probably won't have time to participate myself.

I don't see any major issues with your code. I've never used memoize to load an asset, but that should work. One thing I haven't yet added to the tutorial is that you can set a global asset manager by doing (set-asset-manager! (asset-manager)) at the top level. It prevents assets like the lion image from being loaded multiple times, no matter how many times you call (texture "lion.png").

DjebbZ commented 10 years ago

Is this timer interval behavior normal ?

Happy to hear that the code looks ok to you :) I will report on reddit after the event, surely next week.

oakes commented 10 years ago

I think it is normal, though I don't see it documented anywhere. How would you prefer it work? If you want it to fire right when it resumes, you could always manually call it in the :on-resume function like this: (run! main-screen :on-timer :id :spawn-lion)

oakes commented 10 years ago

I'll close this for now since it seems to be how libGDX designed it, but if you notice any irregularities then please let me know.

DjebbZ commented 10 years ago

The bug happened again. During this week-end we had to implement a custom timer function in the render based on :total-time. We've just switched back to the normal timer functions and we noticed that the (2) timers stop as soon as I press some key.

oakes commented 10 years ago

What OS are you on? I noticed a problem on Windows and I believe it is a libGDX bug.

DjebbZ commented 10 years ago

OSX. We narrowed the bug, it's not on key press, it happens when the game window gains focus. It stops all timers.

We read the code of add-timer!, it basically creates a libGDX Timer and schedule it. So we came to the same conclusion than you, it must be a libGDX bug.

On a side note, why aren't you simply doing (Timer.) when creating a timer here ?

oakes commented 10 years ago

Yep, this is the same bug I ran into with Nightmod. I eventually solved it by bringing the Timer.java file into my project and commenting out two lines. The issue was reported to libGDX, but it was closed because there wasn't a reproduce-able test case. I will have to try creating one in Java so we can get this resolved.

The reason I am using reflection to create the timer is that the class contains static initializers that prevent it from being directly imported into Clojure. This is because Clojure currently initializes classes right when you import them, causing those static initializers to run at compile time. If this patch is merged, it will be fixed in Clojure 1.7.

oakes commented 10 years ago

Did you experience this with your LD game, or something else? I have only experienced this with Nightmod, so it would be useful to see another example to help me write a Java test case.

oakes commented 10 years ago

I have successfully created a test case and reported it to the libGDX project.

DjebbZ commented 10 years ago

The bug happened in the LD game. Nice to hear that you were able to reproduce it, it's making progress.

Thanks for the explanation about Clojure, I learned something.

oakes commented 9 years ago

My fix was just merged into libGDX so this bug should be fixed in the next version.

edesdan commented 9 years ago

Hi, I'm having the same issue regarding call to :pause() on Desktops. It was supposed to be called just on Android, right? It seems to be called every time the window lost focus. ????

oakes commented 9 years ago

Right, :on-pause is called on desktops as well. I was incorrect about it only being on mobile.

edesdan commented 9 years ago

So, what does it means? It's a libGDX bug? Documentation issue?

Thanks, Daniele.

oakes commented 9 years ago

Calling :on-pause when the window is unfocused is intended behavior, I believe. If the libGDX docs say it's mobile only, then I think that's a doc issue.

edesdan commented 9 years ago

Ah ok , do you think I can open an issue on libgdx project for that?

oakes commented 9 years ago

Yeah definitely, if there is a mistake in their JavaDoc. On Oct 29, 2014 9:31 AM, "edesdan" notifications@github.com wrote:

Ah ok , do you think I can open an issue on libgdx project for that?

Reply to this email directly or view it on GitHub https://github.com/oakes/play-clj/issues/46#issuecomment-60923591.

edesdan commented 9 years ago

https://github.com/libgdx/libgdx/issues/2518#issuecomment-60926674