Skip to content

Commit

Permalink
Add get_surface() and update_from_surface
Browse files Browse the repository at this point in the history
  • Loading branch information
yunline committed Oct 3, 2023
1 parent 128be25 commit 21bd3ba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ typedef struct pgColorObject pgColorObject;
typedef struct {
PyObject_HEAD SDL_Window *_win;
SDL_bool _is_borrowed;
pgSurfaceObject *surf;
} pgWindowObject;

#ifndef PYGAMEAPI_WINDOW_INTERNAL
Expand Down
52 changes: 52 additions & 0 deletions src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,54 @@ window_destroy(pgWindowObject *self, PyObject *_null)
Py_RETURN_NONE;
}

static PyObject *
window_get_surface(pgWindowObject *self)

This comment has been minimized.

Copy link
@pmp-p

pmp-p May 29, 2024

Member

wrong number of arguments here, by convention the missing one should be , PyObject *_null)

{
PyObject *surf = NULL;
SDL_Surface *_surf = SDL_GetWindowSurface(self->_win);
if (!_surf) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
surf = (PyObject *)pgSurface_New2(_surf, SDL_FALSE);
if (!surf) {
return NULL;
}
Py_INCREF(surf);
Py_XDECREF(self->surf);
self->surf = (pgSurfaceObject *)surf;
Py_INCREF(surf);
return surf;
}

static PyObject *
window_update_from_surface(pgWindowObject *self, PyObject *const *args,
Py_ssize_t nargs)
{
int i;
SDL_Rect *rects = NULL, tmp, *r;
if (nargs == 0) {
if (SDL_UpdateWindowSurface(self->_win)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
else {
rects = malloc(nargs * sizeof(SDL_Rect));
for (i = 0; i < nargs; i++) {
r = pgRect_FromObject(args[i], &tmp);
if (!r) {
return RAISE(PyExc_TypeError,
"Arguments must be rect or rect-like objects.");
}
rects[i] = *r;
if (SDL_UpdateWindowSurfaceRects(self->_win, rects, (int)nargs)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
free(rects);
}
Py_RETURN_NONE;
}

static PyObject *
window_set_windowed(pgWindowObject *self, PyObject *_null)
{
Expand Down Expand Up @@ -739,6 +787,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs)
}
self->_win = _win;
self->_is_borrowed = SDL_FALSE;
self->surf = NULL;

SDL_SetWindowData(_win, "pg_window", self);

Expand Down Expand Up @@ -827,6 +876,9 @@ static PyMethodDef window_methods[] = {
DOC_SDL2_VIDEO_WINDOW_SETMODALFOR},
{"set_icon", (PyCFunction)window_set_icon, METH_O,
DOC_SDL2_VIDEO_WINDOW_SETICON},
{"update_from_surface", (PyCFunction)window_update_from_surface,
METH_FASTCALL, "docs"},
{"get_surface", (PyCFunction)window_get_surface, METH_NOARGS, "docs"},
{"from_display_module", (PyCFunction)window_from_display_module,
METH_CLASS | METH_NOARGS, DOC_SDL2_VIDEO_WINDOW_FROMDISPLAYMODULE},
{NULL, NULL, 0, NULL}};
Expand Down

0 comments on commit 21bd3ba

Please sign in to comment.