Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing module sdl2.ext.time #168

Open
Trainraider opened this issue Aug 24, 2020 · 4 comments · May be fixed by #172
Open

Missing module sdl2.ext.time #168

Trainraider opened this issue Aug 24, 2020 · 4 comments · May be fixed by #172

Comments

@Trainraider
Copy link

Where in PySDL2 is sdl2.ext.time? This module is documented but missing from the pypi package and the github repo. Contains basic useful stuff for regulating frame rate or using delta time.

@a-hurst
Copy link
Member

a-hurst commented Aug 24, 2020

Looks like that module was added in an unofficial fork of this project that seems to now be abandoned. You can find the full source for the module here: https://github.com/LukeMS/py-sdl2/blob/master/sdl2/ext/time.py

Luckily, it looks like that module is largely self-contained and should be easily added to any mainline pysdl2 project: all you should need to do is change the from ..timer import bit near the top to from sdl2.timer import and you should be able to include it with your python project.

As for adding the module officially to pysdl2: I'd have to check, but there might be problems with the licensing since the person who forked it based it off source from a different project.

Hope that helps!

@knghtbrd
Copy link

This module looks to be rather similar to the PyGame time module. Would a PR to add this to PySDL2 be welcome?

@a-hurst
Copy link
Member

a-hurst commented Oct 26, 2020

@iKarith Yes, definitely!

knghtbrd added a commit to knghtbrd/py-sdl2 that referenced this issue Nov 11, 2020
LukeMS has this added to his personal sdl2 repo on GitHub, but hasn't
offered the patch upstream. Enough people seem to want it that I've
decided ti offer it as a PR. I didn't take time to write unit tests for
it, sorry. Closes py-sdl#168.
@knghtbrd knghtbrd linked a pull request Nov 11, 2020 that will close this issue
@knghtbrd
Copy link

I see one minor problem with my own PR depending on how pedantic y'all are feeling about licensing. Zlib isn't public domain—you've gotta maintain a Copyright notice and you'd need to add a blurb about it somewhere.

I think Luke put it under Zlib license because he admittedly used PyGame's source to do it. Well … if you've done this kind of stuff before (and I have), there's really "only one right way to do it" given PyGame's API found here:

  • time.get_ticks: wraps SDL_GetTicks() trivially
  • time.wait(how_long): before = SDL_GetTicks(); SDL_Delay(howlong); return SDL_GetTicks() - before
  • time.delay(how_long): the busy loop version of wait()
  • time.set_timer(event_id, how_long, once=False): SDL_AddTimer with a pythonic wrapper, causes an event_id event to be triggered every how_long msec.
  • time.Clock.tick(framerate=0): updates a frame counter from last frame, delays execution to framerate fps if needed, using SDL_Delay()
  • time.Clock.tick_busy_loop(framerate=0): The busy loop version of Clock.tick()
  • time.Clock.get_time: elapsed ticks since last frame, after framerate delay
  • time.Clock.get_rawtime: elapsed ticks since last frame, before framerate delay
  • time.Clock.get_fps: keeps count either of the fps over either the last, what, 10-16 frames probably? Either by keeping track of the time taken to perform the last num frames every num frames, or by keeping track of the elapsed tick count for the last num frames, adding them up, and returning the average per second.

Dunno how accurate you're gonna get without Cython for the busy loop version. Doing without PyGame's code is pretty simple:

elapsed_ticks = 0
then = 0

# rest of your init

while running:
    now = SDL_GetTicks()
    elapsed_ticks = now - then

   # Rest of your loop

    then = now

# Shutdown code

Creating a Clock() object is just encapsulating that, adding the ability to delay until so that 1000//framerate >= elapsed_time, and deciding whether to try an implement a busy loop or not. shrug

Clock is a convenient reduction in boilerplate or a set of training wheels for those starting out, but if you'd prefer a more "clean room" implementation (not in the purest legal sense since obviously I submitted Luke's port as a PR) to keep PySDL2 purely public domain, I just all but wrote it from scratch in this comment. I can do it for real and you can freely CC0/public domain it. This kind of elapsed time look, either in msec or double precision fractions of a second are the basis of just about every game programming guide, tutorial, or book you'll find. I don't want to see its exclusion ever be the basis for someone to stick with SDL 1.2 for access to PyGame over SDL 2.0. (PyGame2 is not available on Ubuntu focal, I note.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants