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

PKG Add pygame-ce #4602

Merged
merged 32 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9596a36
Add pygame-ce
ryanking13 May 4, 2023
98a7af3
Fix readability
ryanking13 May 4, 2023
75ff0e1
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 May 6, 2023
af6d119
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 May 7, 2023
c66cb4c
Update build script
ryanking13 May 7, 2023
c12a435
Merge branch 'pyodide:main' into pygame-ce
ryanking13 Jul 29, 2023
402d56e
Sync upstream
ryanking13 Jul 29, 2023
ddd37e4
Merge branch 'pyodide:main' into pygame-ce
ryanking13 Nov 27, 2023
00c6e72
Bump pygame version
ryanking13 Nov 27, 2023
393865b
typo
ryanking13 Nov 28, 2023
55a8045
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 Feb 11, 2024
91ce858
Build latest versions of libsdl2
ryanking13 Feb 11, 2024
b2444fc
Add test
ryanking13 Feb 15, 2024
9ab4c1b
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 Feb 16, 2024
f0fefa7
wip
ryanking13 Feb 17, 2024
8055e67
Remove unused files
ryanking13 Feb 17, 2024
99d8dcc
Add font related patches
ryanking13 Feb 20, 2024
b876d05
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 Feb 21, 2024
13317a2
Support zero dely sleep
ryanking13 Feb 21, 2024
307a112
Remove typehint
ryanking13 Feb 21, 2024
b1630aa
Revert "Remove typehint"
ryanking13 Mar 8, 2024
acd9ff2
Revert "Support zero dely sleep"
ryanking13 Mar 8, 2024
0e7a564
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 Mar 8, 2024
e281ea8
Skip test in ci
ryanking13 Mar 8, 2024
2dd154c
Remove debug statements
ryanking13 Mar 8, 2024
cafc000
Bump version to 2.4.1
ryanking13 Mar 8, 2024
cca8664
Lint
ryanking13 Mar 8, 2024
96893dc
Merge remote-tracking branch 'upstream/main' into pygame-ce
ryanking13 Mar 16, 2024
2632d44
Apply patch from upstream
ryanking13 Mar 16, 2024
ed79ffe
Update docs
ryanking13 Mar 16, 2024
4cb7d17
changelog
ryanking13 Mar 16, 2024
a149897
Merge branch 'main' into pygame-ce
ryanking13 Mar 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.envs
Expand Up @@ -149,6 +149,7 @@ export MAIN_MODULE_LDFLAGS= $(LDFLAGS_BASE) \
-legl.js \
-lwebgl.js \
-lhtml5_webgl.js \
-lsdl.js \
hoodmane marked this conversation as resolved.
Show resolved Hide resolved
-sGL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0


Expand Down
3 changes: 2 additions & 1 deletion docs/project/changelog.md
Expand Up @@ -71,7 +71,8 @@ myst:
- New Packages: `cysignals`, `ppl`, `pplpy` {pr}`4407`, `flint`, `python-flint` {pr}`4410`,
`memory_allocator` {pr}`4393`, `primesieve`, `primecount`, `primecountpy` {pr}`4477`,
`pyxirr` {pr}`4513`, `ipython`, `asttokens`, `executing`, `prompt_toolkit`,
`pure_eval`, `stack_data`, `traitlets`, `wcwidth` {pr}`4452`, `altair` {pr}`4580`
`pure_eval`, `stack_data`, `traitlets`, `wcwidth` {pr}`4452`, `altair` {pr}`4580`,
`pygame-ce` {pr}`4602`

- Upgraded `scikit-learn` to 1.4.1.post1 {pr}`4409` and {pr}`4534`

Expand Down
31 changes: 31 additions & 0 deletions docs/usage/sdl.md
Expand Up @@ -34,6 +34,37 @@ pyodide.canvas.setCanvas2D(sdl2Canvas);

See also: {ref}`js-api-pyodide-canvas`

## Working with infinite loop

It is common to use an infinite loop to draw animations or game scenes with SDL-based package.

For instance, a common code pattern in `pygame` (a SDL-based Python game library) is:

```python
clock = pygame.time.Clock()
fps = 60
def run_game():
while True:
do_something()
draw_canvas()
clock.tick(fps)
```

However, in Pyodide, this will not work as expected, because the loop will block the main thread and prevent the browser from updating the canvas.
To work around this, you need to use async functions and yield control to the browser.

```python
import asyncio

async def run_game():
while True:
do_something()
draw_canvas()
await asyncio.sleep(1 / fps)
```

Using `asyncio.sleep` will yield control to the browser and allow the canvas to be updated.

(sdl-known-issues)=

## Known issues
Expand Down
93 changes: 93 additions & 0 deletions packages/pygame-ce/meta.yaml
@@ -0,0 +1,93 @@
package:
name: pygame-ce
version: 2.4.1
top-level:
- pygame
source:
url: https://files.pythonhosted.org/packages/dc/b1/64fffc2c8664497ae82b2afb4f5efe0130d38b39f2d25af0288c4261df3e/pygame-ce-2.4.1.tar.gz
sha256: 70a84aa1417c633a0fd6754ffa5dc92ee1b9aeb70baaa52c8c8c94a7c6db9cf0
patches:
- patches/0001-Replace-OpenAudioDevice-with-OpenAudio.patch
- patches/0001-freetype-init.patch
- patches/0001-wasm_unify_pygame_web_pyodide_static_Freetype_init.patch
build:
script: |
embuilder build sdl2 sdl2_ttf sdl2_image sdl2_mixer sdl2_gfx libjpeg libpng giflib harfbuzz vorbis mpg123 libmodplug freetype --pic
export SDL_CONFIG=$(em-config CACHE)/sysroot/bin/sdl2-config
cflags: |
-sRELOCATABLE=1
-DSDL_NO_COMPAT
-DBUILD_STATIC
-ferror-limit=1
-sUSE_SDL=2
-sUSE_SDL_MIXER=2
-sSDL2_MIXER_FORMATS=mid,mod,mpg,ogg
-sUSE_SDL_TTF=2
-sUSE_SDL_IMAGE=2
-sSDL2_IMAGE_FORMATS=bmp,gif,jpg,png
-sUSE_SDL_GFX=2
-sUSE_FREETYPE=1
-sUSE_LIBJPEG=1
-sUSE_LIBPNG=1
-sUSE_GIFLIB=1
-sUSE_HARFBUZZ=1
ldflags: |
-sRELOCATABLE=1
-sUSE_SDL=2
-sUSE_SDL_MIXER=2
-sSDL2_MIXER_FORMATS=mid,mod,mpg,ogg
-sUSE_SDL_TTF=2
-sUSE_SDL_IMAGE=2
-sSDL2_IMAGE_FORMATS=bmp,gif,jpg,png
-sUSE_SDL_GFX=2
-sUSE_FREETYPE=1
-sUSE_LIBJPEG=1
-sUSE_LIBPNG=1
-sUSE_GIFLIB=1
-sUSE_HARFBUZZ=1
post: |
# Remove docs to reduce the size of a wheel
# (Perhaps also remove typeshed and examples too?)
rm -rf ./pygame/docs

# WASM version of pygame uses a single module 'pygame_static',
# and pygame_static calls PyInit_* functions from other modules.
# This is okay when all these modules are bundled in a single module: libpython.
# But as we don't do that, we need to load these modules globally, so that
# pygame_static can see the symbols.
STATIC_OBJS=$(find ${PKG_BUILD_DIR} -name "*.o")
echo "build dir: " $PKG_BUILD_DIR
echo "static objs: " $STATIC_OBJS

SHARED_LIBS=$(find ${WHEELDIR} -name "*.so" ! -name "static*.so")
echo "shared libs: " $SHARED_LIBS

emcc \
-shared \
${SIDE_MODULE_LDFLAGS} \
-fPIC \
-lSDL2 \
-lSDL2_image_bmp-gif-jpg-png \
-lSDL2_ttf \
-lSDL2_mixer_mid-mod-mpg-ogg \
-lSDL2_gfx \
-lfreetype \
-lharfbuzz \
-lpng \
-ljpeg \
-lgif \
-lvorbis \
-lmodplug \
-lmpg123 \
-logg \
${STATIC_OBJS} \
${SHARED_LIBS} \
-o pygame_static.so

# Remove duplicated static.so module
rm -f ./pygame/static*.so
about:
home: https://www.pygame.org
PyPI: https://pypi.org/project/pygame
summary: Python Game Development
license: LGPL
@@ -0,0 +1,29 @@
From 948614b9410f57c599db98c3248314238a0d317c Mon Sep 17 00:00:00 2001
From: ryanking13 <def6488@gmail.com>
Date: Sat, 17 Feb 2024 10:55:27 +0900
Subject: [PATCH 1/1] Replace OpenAudioDevice with OpenAudio

FIXME: for some reason, Mix_OpenAudioDevice is not statically linked when building the SDL module.

---
src_c/mixer.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src_c/mixer.c b/src_c/mixer.c
index 51097bba..6c79747c 100644
--- a/src_c/mixer.c
+++ b/src_c/mixer.c
@@ -441,8 +441,8 @@ _init(int freq, int size, int channels, int chunk, char *devicename,
if (SDL_InitSubSystem(SDL_INIT_AUDIO))
return RAISE(pgExc_SDLError, SDL_GetError());

- if (Mix_OpenAudioDevice(freq, fmt, channels, chunk, devicename,
- allowedchanges) == -1) {
+ // No Mix_OpenAudioCompat in emscripten
+ if (Mix_OpenAudio(freq, fmt, channels, chunk) == -1) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return RAISE(pgExc_SDLError, SDL_GetError());
}
--
2.29.2.windows.2