Skip to content

Commit efeb3ff

Browse files
authored
Add interface for _sdl2.video classes (#3317)
* Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes * Add interface for _sdl2.video classes
1 parent 227118b commit efeb3ff

File tree

8 files changed

+194
-0
lines changed

8 files changed

+194
-0
lines changed

Diff for: buildconfig/Setup.Android.SDL2.in

+1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ math src_c/math.c $(SDL) $(DEBUG)
6262
pixelcopy src_c/pixelcopy.c $(SDL) $(DEBUG)
6363
newbuffer src_c/newbuffer.c $(SDL) $(DEBUG)
6464
window src_c/window.c $(SDL) $(DEBUG)
65+
_render src_c/render.c $(SDL) $(DEBUG)
6566
geometry src_c/geometry.c $(SDL) $(DEBUG)

Diff for: buildconfig/Setup.Emscripten.SDL2.in

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ rect src_c/void.c
6464
rwobject src_c/void.c
6565
system src_c/void.c
6666
window src_c/void.c
67+
_render src_c/void.c
6768
geometry src_c/void.c
6869

6970
#_sdl2.controller src_c/_sdl2/controller.c $(SDL) $(DEBUG) -Isrc_c

Diff for: buildconfig/Setup.SDL2.in

+1
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ newbuffer src_c/newbuffer.c $(SDL) $(DEBUG)
7575
system src_c/system.c $(SDL) $(DEBUG)
7676
geometry src_c/geometry.c $(SDL) $(DEBUG)
7777
window src_c/window.c $(SDL) $(DEBUG)
78+
_render src_c/render.c $(SDL) $(DEBUG)

Diff for: src_c/_pygame.h

+1
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ typedef enum {
616616
#define PYGAMEAPI_BASE_NUMSLOTS 30
617617
#define PYGAMEAPI_EVENT_NUMSLOTS 10
618618
#define PYGAMEAPI_WINDOW_NUMSLOTS 1
619+
#define PYGAMEAPI_RENDER_NUMSLOTS 3
619620
#define PYGAMEAPI_GEOMETRY_NUMSLOTS 2
620621

621622
#endif /* _PYGAME_INTERNAL_H */

Diff for: src_c/include/_pygame.h

+46
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,50 @@ typedef struct {
520520
#define import_pygame_window() IMPORT_PYGAME_MODULE(window)
521521
#endif
522522

523+
typedef struct pgTextureObject pgTextureObject;
524+
525+
/*
526+
* Render module
527+
*/
528+
typedef struct {
529+
PyObject_HEAD SDL_Renderer *renderer;
530+
pgWindowObject *window;
531+
pgTextureObject *target;
532+
SDL_bool _is_borrowed;
533+
} pgRendererObject;
534+
535+
struct pgTextureObject {
536+
PyObject_HEAD SDL_Texture *texture;
537+
pgRendererObject *renderer;
538+
int width;
539+
int height;
540+
};
541+
542+
typedef struct {
543+
PyObject_HEAD pgTextureObject *texture;
544+
pgRectObject *srcrect;
545+
pgColorObject *color;
546+
float angle;
547+
float alpha;
548+
SDL_bool has_origin;
549+
SDL_FPoint origin;
550+
SDL_bool flip_x;
551+
SDL_bool flip_y;
552+
SDL_BlendMode blend_mode;
553+
} pgImageObject;
554+
555+
#ifndef PYGAMEAPI_RENDER_INTERNAL
556+
#define pgRenderer_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(_render, 0))
557+
#define pgTexture_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(_render, 1))
558+
#define pgImage_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(_render, 2))
559+
#define pgRenderer_Check(x) \
560+
(PyObject_IsInstance((x), (PyObject *)&pgRender_Type))
561+
#define pgTexture_Check(x) \
562+
(PyObject_IsInstance((x), (PyObject *)&pgTexture_Type))
563+
#define pgImage_Check(x) (PyObject_IsInstance((x), (PyObject *)&pgImage_Type))
564+
#define import_pygame_render() IMPORT_PYGAME_MODULE(_render)
565+
#endif
566+
523567
#define IMPORT_PYGAME_MODULE _IMPORT_PYGAME_MODULE
524568

525569
/*
@@ -539,6 +583,7 @@ PYGAMEAPI_DEFINE_SLOTS(pixelarray);
539583
PYGAMEAPI_DEFINE_SLOTS(color);
540584
PYGAMEAPI_DEFINE_SLOTS(math);
541585
PYGAMEAPI_DEFINE_SLOTS(window);
586+
PYGAMEAPI_DEFINE_SLOTS(_render);
542587
PYGAMEAPI_DEFINE_SLOTS(geometry);
543588
#else /* ~PYGAME_H */
544589
PYGAMEAPI_EXTERN_SLOTS(base);
@@ -553,6 +598,7 @@ PYGAMEAPI_EXTERN_SLOTS(pixelarray);
553598
PYGAMEAPI_EXTERN_SLOTS(color);
554599
PYGAMEAPI_EXTERN_SLOTS(math);
555600
PYGAMEAPI_EXTERN_SLOTS(window);
601+
PYGAMEAPI_EXTERN_SLOTS(_render);
556602
PYGAMEAPI_EXTERN_SLOTS(geometry);
557603

558604
#endif /* ~PYGAME_H */

Diff for: src_c/meson.build

+12
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ window = py.extension_module(
316316
subdir: pg,
317317
)
318318

319+
# TODO: support SDL3
320+
if sdl_api != 3
321+
_render = py.extension_module(
322+
'_render',
323+
'render.c',
324+
c_args: warnings_error,
325+
dependencies: pg_base_deps,
326+
install: true,
327+
subdir: pg,
328+
)
329+
endif
330+
319331
# TODO: support SDL3
320332
if sdl_api != 3
321333
gfxdraw = py.extension_module(

Diff for: src_c/render.c

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#define PYGAMEAPI_RENDER_INTERNAL
2+
3+
#include "pygame.h"
4+
5+
#include "pgcompat.h"
6+
7+
#include "doc/sdl2_video_doc.h"
8+
9+
static PyTypeObject pgRenderer_Type;
10+
11+
static PyTypeObject pgTexture_Type;
12+
13+
static PyTypeObject pgImage_Type;
14+
15+
static PyMethodDef renderer_methods[] = {{NULL, NULL, 0, NULL}};
16+
17+
static PyGetSetDef renderer_getset[] = {{NULL, 0, NULL, NULL, NULL}};
18+
19+
static PyMethodDef texture_methods[] = {{NULL, NULL, 0, NULL}};
20+
21+
static PyGetSetDef texture_getset[] = {{NULL, 0, NULL, NULL, NULL}};
22+
23+
static PyMethodDef image_methods[] = {{NULL, NULL, 0, NULL}};
24+
25+
static PyGetSetDef image_getset[] = {{NULL, 0, NULL, NULL, NULL}};
26+
27+
static PyTypeObject pgRenderer_Type = {
28+
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame._render.Renderer",
29+
.tp_basicsize = sizeof(pgRendererObject),
30+
//.tp_dealloc = (destructor)renderer_dealloc,
31+
.tp_doc = DOC_SDL2_VIDEO_RENDERER, .tp_methods = renderer_methods,
32+
//.tp_init = (initproc)renderer_init,
33+
.tp_new = PyType_GenericNew, .tp_getset = renderer_getset};
34+
35+
static PyTypeObject pgTexture_Type = {
36+
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame._render.Texture",
37+
.tp_basicsize = sizeof(pgTextureObject),
38+
//.tp_dealloc = (destructor)texture_dealloc,
39+
.tp_doc = DOC_SDL2_VIDEO_TEXTURE, .tp_methods = texture_methods,
40+
//.tp_init = (initproc)texture_init,
41+
.tp_new = PyType_GenericNew, .tp_getset = texture_getset};
42+
43+
static PyTypeObject pgImage_Type = {
44+
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame._render.Image",
45+
.tp_basicsize = sizeof(pgImageObject),
46+
//.tp_dealloc = (destructor)image_dealloc,
47+
.tp_doc = DOC_SDL2_VIDEO_IMAGE, .tp_methods = image_methods,
48+
//.tp_init = (initproc)image_init,
49+
.tp_new = PyType_GenericNew, .tp_getset = image_getset};
50+
51+
static PyMethodDef _render_methods[] = {{NULL, NULL, 0, NULL}};
52+
53+
MODINIT_DEFINE(_render)
54+
{
55+
PyObject *module, *apiobj;
56+
static void *c_api[PYGAMEAPI_RENDER_NUMSLOTS];
57+
58+
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
59+
"_render",
60+
"docs_needed",
61+
-1,
62+
_render_methods,
63+
NULL,
64+
NULL,
65+
NULL,
66+
NULL};
67+
68+
/* imported needed apis; Do this first so if there is an error
69+
the module is not loaded.
70+
*/
71+
import_pygame_base();
72+
if (PyErr_Occurred()) {
73+
return NULL;
74+
}
75+
76+
if (PyType_Ready(&pgRenderer_Type) < 0) {
77+
return NULL;
78+
}
79+
80+
if (PyType_Ready(&pgTexture_Type) < 0) {
81+
return NULL;
82+
}
83+
84+
if (PyType_Ready(&pgImage_Type) < 0) {
85+
return NULL;
86+
}
87+
88+
/* create the module */
89+
module = PyModule_Create(&_module);
90+
if (module == 0) {
91+
return NULL;
92+
}
93+
94+
Py_INCREF(&pgRenderer_Type);
95+
if (PyModule_AddObject(module, "Renderer", (PyObject *)&pgRenderer_Type)) {
96+
Py_DECREF(&pgRenderer_Type);
97+
Py_DECREF(module);
98+
return NULL;
99+
}
100+
101+
Py_INCREF(&pgTexture_Type);
102+
if (PyModule_AddObject(module, "Texture", (PyObject *)&pgTexture_Type)) {
103+
Py_DECREF(&pgTexture_Type);
104+
Py_DECREF(module);
105+
return NULL;
106+
}
107+
108+
Py_INCREF(&pgImage_Type);
109+
if (PyModule_AddObject(module, "Image", (PyObject *)&pgImage_Type)) {
110+
Py_DECREF(&pgImage_Type);
111+
Py_DECREF(module);
112+
return NULL;
113+
}
114+
115+
c_api[0] = &pgRenderer_Type;
116+
c_api[1] = &pgTexture_Type;
117+
c_api[2] = &pgImage_Type;
118+
apiobj = encapsulate_api(c_api, "_render");
119+
if (PyModule_AddObject(module, PYGAMEAPI_LOCAL_ENTRY, apiobj)) {
120+
Py_XDECREF(apiobj);
121+
Py_DECREF(module);
122+
return NULL;
123+
}
124+
125+
return module;
126+
}

Diff for: src_c/static.c

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PYGAMEAPI_BASE_INTERNAL
99
#define PYGAMEAPI_SURFACE_INTERNAL
1010
#define PYGAMEAPI_WINDOW_INTERNAL
11+
#define PYGAMEAPI_RENDER_INTERNAL
1112

1213
#define pgSurface_New(surface) (pgSurfaceObject *)pgSurface_New2((surface), 1)
1314
#define pgSurface_NewNoOwn(surface) \
@@ -187,6 +188,9 @@ PyInit_pixelarray(void);
187188
PyMODINIT_FUNC
188189
PyInit_window(void);
189190

191+
PyMODINIT_FUNC
192+
PyInit__render(void);
193+
190194
// pygame_static module
191195

192196
void
@@ -320,6 +324,7 @@ PyInit_pygame_static()
320324
load_submodule("pygame.mixer", PyInit_mixer_music(), "music");
321325

322326
load_submodule("pygame", PyInit_window(), "window");
327+
load_submodule("pygame", PyInit__render(), "_render");
323328

324329
load_submodule("pygame", PyInit_pixelarray(), "pixelarray");
325330

@@ -364,6 +369,7 @@ PyInit_pygame_static()
364369
#include "simd_blitters_sse2.c"
365370

366371
#include "window.c"
372+
#include "render.c"
367373

368374
#undef pgVidInfo_Type
369375
#undef pgVidInfo_New

0 commit comments

Comments
 (0)