Skip to content

Commit 21bd3ba

Browse files
committed
Add get_surface() and update_from_surface
1 parent 128be25 commit 21bd3ba

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src_c/include/_pygame.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ typedef struct pgColorObject pgColorObject;
474474
typedef struct {
475475
PyObject_HEAD SDL_Window *_win;
476476
SDL_bool _is_borrowed;
477+
pgSurfaceObject *surf;
477478
} pgWindowObject;
478479

479480
#ifndef PYGAMEAPI_WINDOW_INTERNAL

src_c/window.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,54 @@ window_destroy(pgWindowObject *self, PyObject *_null)
127127
Py_RETURN_NONE;
128128
}
129129

130+
static PyObject *
131+
window_get_surface(pgWindowObject *self)
132+
{
133+
PyObject *surf = NULL;
134+
SDL_Surface *_surf = SDL_GetWindowSurface(self->_win);
135+
if (!_surf) {
136+
return RAISE(pgExc_SDLError, SDL_GetError());
137+
}
138+
surf = (PyObject *)pgSurface_New2(_surf, SDL_FALSE);
139+
if (!surf) {
140+
return NULL;
141+
}
142+
Py_INCREF(surf);
143+
Py_XDECREF(self->surf);
144+
self->surf = (pgSurfaceObject *)surf;
145+
Py_INCREF(surf);
146+
return surf;
147+
}
148+
149+
static PyObject *
150+
window_update_from_surface(pgWindowObject *self, PyObject *const *args,
151+
Py_ssize_t nargs)
152+
{
153+
int i;
154+
SDL_Rect *rects = NULL, tmp, *r;
155+
if (nargs == 0) {
156+
if (SDL_UpdateWindowSurface(self->_win)) {
157+
return RAISE(pgExc_SDLError, SDL_GetError());
158+
}
159+
}
160+
else {
161+
rects = malloc(nargs * sizeof(SDL_Rect));
162+
for (i = 0; i < nargs; i++) {
163+
r = pgRect_FromObject(args[i], &tmp);
164+
if (!r) {
165+
return RAISE(PyExc_TypeError,
166+
"Arguments must be rect or rect-like objects.");
167+
}
168+
rects[i] = *r;
169+
if (SDL_UpdateWindowSurfaceRects(self->_win, rects, (int)nargs)) {
170+
return RAISE(pgExc_SDLError, SDL_GetError());
171+
}
172+
}
173+
free(rects);
174+
}
175+
Py_RETURN_NONE;
176+
}
177+
130178
static PyObject *
131179
window_set_windowed(pgWindowObject *self, PyObject *_null)
132180
{
@@ -739,6 +787,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
739787
}
740788
self->_win = _win;
741789
self->_is_borrowed = SDL_FALSE;
790+
self->surf = NULL;
742791

743792
SDL_SetWindowData(_win, "pg_window", self);
744793

@@ -827,6 +876,9 @@ static PyMethodDef window_methods[] = {
827876
DOC_SDL2_VIDEO_WINDOW_SETMODALFOR},
828877
{"set_icon", (PyCFunction)window_set_icon, METH_O,
829878
DOC_SDL2_VIDEO_WINDOW_SETICON},
879+
{"update_from_surface", (PyCFunction)window_update_from_surface,
880+
METH_FASTCALL, "docs"},
881+
{"get_surface", (PyCFunction)window_get_surface, METH_NOARGS, "docs"},
830882
{"from_display_module", (PyCFunction)window_from_display_module,
831883
METH_CLASS | METH_NOARGS, DOC_SDL2_VIDEO_WINDOW_FROMDISPLAYMODULE},
832884
{NULL, NULL, 0, NULL}};

0 commit comments

Comments
 (0)