Skip to content

Commit 5be8b6e

Browse files
committed
WASM compatibility, courtesy of pmp-p
Co-authored-by: pmp-p <mail.peny@free.fr>
1 parent b7dc160 commit 5be8b6e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src_c/base.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@
2222
#define NO_PYGAME_C_API
2323
#define PYGAMEAPI_BASE_INTERNAL
2424

25+
#if defined(BUILD_STATIC)
26+
#define CONTROLLER_NOPYX
27+
#define PYGAMEAPI_RECT_INTERNAL
28+
#define PYGAMEAPI_EVENT_INTERNAL
29+
#define PYGAMEAPI_JOYSTICK_INTERNAL
30+
#define PYGAMEAPI_BASE_INTERNAL
31+
#define PYGAMEAPI_SURFACE_INTERNAL
32+
#define PYGAMEAPI_BUFFERPROXY_INTERNAL
33+
#define PYGAMEAPI_WINDOW_INTERNAL
34+
#define PYGAMEAPI_RENDER_INTERNAL
35+
#endif
36+
2537
#include "base.h"
2638

2739
PG_PixelFormatEnum pg_default_convert_format = 0;
2840

2941
/* Custom exceptions */
3042
PyObject *pgExc_BufferError = NULL;
3143

44+
PyObject *pgExc_SDLError;
45+
3246
/* Only one instance of the state per process. */
3347
static PyObject *pg_quit_functions = NULL;
3448
static int pg_is_init = 0;
@@ -2231,3 +2245,7 @@ MODINIT_DEFINE(base)
22312245
Py_XDECREF(module);
22322246
return NULL;
22332247
}
2248+
2249+
#if defined(__EMSCRIPTEN__) || defined(__wasi__)
2250+
#include "static.c"
2251+
#endif

src_c/include/_pygame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ typedef struct {
208208

209209
#define pgRect_AsRect(x) (((pgRectObject *)x)->r)
210210
#define pgFRect_AsRect(x) (((pgFRectObject *)x)->r)
211+
211212
#ifndef PYGAMEAPI_RECT_INTERNAL
212213
#define pgRect_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(rect, 0))
213214

src_c/static.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#define PYGAMEAPI_WINDOW_INTERNAL
1212
#define PYGAMEAPI_RENDER_INTERNAL
1313

14+
#if 0
1415
#define pgSurface_New(surface) (pgSurfaceObject *)pgSurface_New2((surface), 1)
1516
#define pgSurface_NewNoOwn(surface) \
1617
(pgSurfaceObject *)pgSurface_New2((surface), 0)
18+
#endif
1719

1820
#include "pygame.h"
1921
#include "Python.h"
@@ -49,6 +51,10 @@ import_pygame_surface(void)
4951
{
5052
}
5153

54+
#ifdef import_pygame_window
55+
#undef import_pygame_window
56+
#endif
57+
5258
void
5359
import_pygame_window(void)
5460
{
@@ -89,6 +95,10 @@ import_pygame_event(void)
8995
{
9096
}
9197

98+
#ifdef import_pygame_joystick
99+
#undef import_pygame_joystick
100+
#endif
101+
92102
void
93103
import_pygame_joystick(void)
94104
{
@@ -113,6 +123,8 @@ import_pygame_pixelarray(void)
113123
{
114124
}
115125

126+
PyMODINIT_FUNC
127+
PyInit_base(void);
116128
PyMODINIT_FUNC
117129
PyInit_color(void);
118130
PyMODINIT_FUNC
@@ -322,6 +334,10 @@ PyInit_pygame_static()
322334
// for correct input in wasm worker
323335
SDL_SetHint("SDL_EMSCRIPTEN_KEYBOARD_ELEMENT", "1");
324336

337+
// base module is including current file
338+
// all globals are accessible from here.
339+
load_submodule("pygame", PyInit_base(), "base");
340+
325341
load_submodule("pygame", PyInit_constants(), "constants");
326342
//
327343
load_submodule("pygame", PyInit_pg_math(), "math");
@@ -391,7 +407,7 @@ PyInit_pygame_static()
391407

392408
#endif // defined(BUILD_STATIC)
393409

394-
#include "base.h"
410+
// #include "base.h"
395411

396412
#include "rect.c"
397413
#include "pgcompat_rect.c"

0 commit comments

Comments
 (0)