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

pygame.time.set_timer not working properly #16

Closed
Matiiss opened this issue May 24, 2022 · 5 comments
Closed

pygame.time.set_timer not working properly #16

Matiiss opened this issue May 24, 2022 · 5 comments
Labels
bug Something isn't working pygame prebuilt issue may come from pygame-wasm tempfix TESTING wip feature partially implemented

Comments

@Matiiss
Copy link
Collaborator

Matiiss commented May 24, 2022

It doesn't seem to actually do any delaying (probably because usage of threads?), it apparently posts those events almost immediately, here you can see how that looks like visually (possible epilepsy warning!!!): https://matiiss.github.io/wasm_test/ and here is the source:

import asyncio
import itertools

import pygame


WIDTH, HEIGHT = 500, 400
FPS = 60
COLOR_CHANGE_EVENT = pygame.event.custom_type()
COLOR_CYCLE = itertools.cycle(pygame.color.THECOLORS.keys())


async def main():
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()

    pygame.time.set_timer(COLOR_CHANGE_EVENT, 500, 0)
    bg_color = next(COLOR_CYCLE)

    running = True
    while running:
        clock.tick(FPS)
        screen.fill(bg_color)

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                running = False
            if event.type == COLOR_CHANGE_EVENT:
                bg_color = next(COLOR_CYCLE)

        pygame.display.flip()
        await asyncio.sleep(0)


def run():
    asyncio.run(main())


if __name__ == "__main__":
    run()
@Matiiss Matiiss added the bug Something isn't working label May 24, 2022
@pmp-p
Copy link
Member

pmp-p commented May 24, 2022

maybe it's an error in the timing calculation, iirc cpython had to change the time function for wasm to function properly. maybe sdl2 still use the wrong one. would need to see the time delta beetween firing event : is it always next frame (~ 1.0/60) or some bad timing value ?

i'll get to investigate after fixing sdl_image. if it is not fixable at C level , would a python workaround (aio can inject event in pygame queue) be ok for typical usage ?

@Matiiss
Copy link
Collaborator Author

Matiiss commented May 26, 2022

If not fixable at C level, then seemingly having at least a Python implementation would be better than nothing so I guess yes, it would be ok for typical usage.

In case it wouldn't be as efficient as manual timing (can't see how that could really happen), then that should be noted somewhere to inform users that manual timing may be preferred over pygame.time.set_timer.

@pmp-p
Copy link
Member

pmp-p commented Jun 6, 2022

@pmp-p pmp-p added the pygame prebuilt issue may come from pygame-wasm label Jun 14, 2022
pmp-p added a commit to pmp-p/python-wasm-plus that referenced this issue Jun 21, 2022
@pmp-p
Copy link
Member

pmp-p commented Jun 21, 2022

@pmp-p pmp-p added the tempfix label Jun 21, 2022
@pmp-p pmp-p added the TESTING wip feature partially implemented label Dec 3, 2022
@pmp-p
Copy link
Member

pmp-p commented Jan 29, 2023

apparently it's working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pygame prebuilt issue may come from pygame-wasm tempfix TESTING wip feature partially implemented
Projects
None yet
Development

No branches or pull requests

2 participants