Skip to content

Commit

Permalink
Merge pull request #2481 from Starbuck5/sdl3-surface
Browse files Browse the repository at this point in the history
Add PG_CreateSurface(From) for SDL2/3 compat
  • Loading branch information
ankith26 authored Oct 1, 2023
2 parents 6e51743 + 55074bd commit dfdf8b8
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 65 deletions.
15 changes: 6 additions & 9 deletions src_c/_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ surf_colorspace(PyObject *self, PyObject *arg)
surf = pgSurface_AsSurface(surfobj);

if (!surfobj2) {
newsurf = SDL_CreateRGBSurfaceWithFormat(0, surf->w, surf->h,
surf->format->BitsPerPixel,
surf->format->format);
newsurf = PG_CreateSurface(surf->w, surf->h, surf->format->format);
if (!newsurf) {
return NULL;
}
Expand Down Expand Up @@ -376,11 +374,11 @@ camera_get_image(pgCameraObject *self, PyObject *arg)

if (!surfobj) {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
surf = SDL_CreateRGBSurfaceWithFormat(0, self->width, self->height, 24,
SDL_PIXELFORMAT_RGB24);
surf =
PG_CreateSurface(self->width, self->height, SDL_PIXELFORMAT_RGB24);
#else
surf = SDL_CreateRGBSurfaceWithFormat(0, self->width, self->height, 24,
SDL_PIXELFORMAT_BGR24);
surf =
PG_CreateSurface(self->width, self->height, SDL_PIXELFORMAT_BGR24);
#endif
}
else {
Expand Down Expand Up @@ -427,8 +425,7 @@ camera_get_image(pgCameraObject *self, PyObject *arg)
return NULL;

if (!surfobj) {
surf = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32,
PG_PIXELFORMAT_XRGB8888);
surf = PG_CreateSurface(width, height, PG_PIXELFORMAT_XRGB8888);
}
else {
surf = pgSurface_AsSurface(surfobj);
Expand Down
8 changes: 8 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@

#define PG_JOYBALLMOTION 0

#define PG_CreateSurface SDL_CreateSurface
#define PG_CreateSurfaceFrom SDL_CreateSurfaceFrom

#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/

#define PG_INIT_NOPARACHUTE SDL_INIT_NOPARACHUTE
Expand All @@ -81,6 +84,11 @@

#define PG_JOYBALLMOTION SDL_JOYBALLMOTION

#define PG_CreateSurface(width, height, format) \
SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format)
#define PG_CreateSurfaceFrom(pixels, width, height, pitch, format) \
SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, 0, pitch, format)

#endif

/* DictProxy is useful for event posting with an arbitrary dict. Maintains
Expand Down
10 changes: 4 additions & 6 deletions src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ pg_ResizeEventWatch(void *userdata, SDL_Event *event)
int h = event->window.data2;
pgSurfaceObject *display_surface =
pg_GetDefaultWindowSurface();
SDL_Surface *surf = SDL_CreateRGBSurfaceWithFormat(
0, w, h, 32, PG_PIXELFORMAT_XRGB8888);
SDL_Surface *surf =
PG_CreateSurface(w, h, PG_PIXELFORMAT_XRGB8888);

SDL_FreeSurface(display_surface->surf);
display_surface->surf = surf;
Expand Down Expand Up @@ -1169,8 +1169,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
So we make a fake surface.
*/
surf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32,
PG_PIXELFORMAT_XRGB8888);
surf = PG_CreateSurface(w, h, PG_PIXELFORMAT_XRGB8888);
newownedsurf = surf;
}
else {
Expand Down Expand Up @@ -1271,8 +1270,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
pg_renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, w, h);
}
surf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32,
PG_PIXELFORMAT_XRGB8888);
surf = PG_CreateSurface(w, h, PG_PIXELFORMAT_XRGB8888);
newownedsurf = surf;
}
else {
Expand Down
3 changes: 1 addition & 2 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,7 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)

if (strlen(astring) == 0) { /* special 0 string case */
int height = TTF_FontHeight(font);
surf = SDL_CreateRGBSurfaceWithFormat(0, 0, height, 32,
PG_PIXELFORMAT_XRGB8888);
surf = PG_CreateSurface(0, height, PG_PIXELFORMAT_XRGB8888);
}
else { /* normal case */
if (antialias && bg_rgba_obj == Py_None) {
Expand Down
2 changes: 1 addition & 1 deletion src_c/freetype/ft_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj,
else {
pixelformat = SDL_PIXELFORMAT_RGBA32;
}
surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, pixelformat);
surface = PG_CreateSurface(width, height, pixelformat);
if (!surface) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return 0;
Expand Down
38 changes: 14 additions & 24 deletions src_c/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1070,8 +1070,7 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds)
PyExc_ValueError,
"Bytes length does not equal format and resolution size");

surf =
SDL_CreateRGBSurfaceWithFormat(0, w, h, 8, SDL_PIXELFORMAT_INDEX8);
surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_INDEX8);
if (!surf)
return RAISE(pgExc_SDLError, SDL_GetError());
SDL_LockSurface(surf);
Expand All @@ -1096,11 +1095,9 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds)
"Bytes length does not equal format and resolution size");

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
surf =
SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, SDL_PIXELFORMAT_BGR24);
surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_BGR24);
#else
surf =
SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, SDL_PIXELFORMAT_RGB24);
surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_RGB24);
#endif
if (!surf)
return RAISE(pgExc_SDLError, SDL_GetError());
Expand Down Expand Up @@ -1141,8 +1138,8 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Bytes length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormat(
0, w, h, 32,
surf = PG_CreateSurface(
w, h,
(alphamult ? SDL_PIXELFORMAT_RGBA32 : PG_PIXELFORMAT_RGBX32));
if (!surf)
return RAISE(pgExc_SDLError, SDL_GetError());
Expand All @@ -1169,8 +1166,7 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Bytes length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32,
SDL_PIXELFORMAT_BGRA32);
surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_BGRA32);
if (!surf)
return RAISE(pgExc_SDLError, SDL_GetError());
SDL_LockSurface(surf);
Expand All @@ -1196,8 +1192,7 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Bytes length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32,
SDL_PIXELFORMAT_ARGB32);
surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB32);
if (!surf)
return RAISE(pgExc_SDLError, SDL_GetError());
SDL_LockSurface(surf);
Expand Down Expand Up @@ -1310,8 +1305,7 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
PyExc_ValueError,
"Buffer length does not equal format and resolution size");

surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 8, pitch,
SDL_PIXELFORMAT_INDEX8);
surf = PG_CreateSurfaceFrom(data, w, h, pitch, SDL_PIXELFORMAT_INDEX8);
}
else if (!strcmp(format, "RGB")) {
if (pitch == -1) {
Expand All @@ -1327,8 +1321,7 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 24, pitch,
SDL_PIXELFORMAT_RGB24);
surf = PG_CreateSurfaceFrom(data, w, h, pitch, SDL_PIXELFORMAT_RGB24);
}
else if (!strcmp(format, "BGR")) {
if (pitch == -1) {
Expand All @@ -1344,8 +1337,7 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 24, pitch,
SDL_PIXELFORMAT_BGR24);
surf = PG_CreateSurfaceFrom(data, w, h, pitch, SDL_PIXELFORMAT_BGR24);
}
else if (!strcmp(format, "BGRA")) {
if (pitch == -1) {
Expand All @@ -1361,8 +1353,7 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 32, pitch,
SDL_PIXELFORMAT_BGRA32);
surf = PG_CreateSurfaceFrom(data, w, h, pitch, SDL_PIXELFORMAT_BGRA32);
}
else if (!strcmp(format, "RGBA") || !strcmp(format, "RGBX")) {
if (pitch == -1) {
Expand All @@ -1379,8 +1370,8 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormatFrom(
data, w, h, 32, pitch,
surf = PG_CreateSurfaceFrom(
data, w, h, pitch,
(alphamult ? SDL_PIXELFORMAT_RGBA32 : PG_PIXELFORMAT_RGBX32));
}
else if (!strcmp(format, "ARGB")) {
Expand All @@ -1397,8 +1388,7 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 32, pitch,
SDL_PIXELFORMAT_ARGB32);
surf = PG_CreateSurfaceFrom(data, w, h, pitch, SDL_PIXELFORMAT_ARGB32);
}
else
return RAISE(PyExc_ValueError, "Unrecognized type of format");
Expand Down
4 changes: 1 addition & 3 deletions src_c/pixelarray_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args)

/* Create the second surface. */

temp_surf = SDL_CreateRGBSurfaceWithFormat(0, (int)dim0, (int)dim1,
surf->format->BitsPerPixel,
surf->format->format);
temp_surf = PG_CreateSurface((int)dim0, (int)dim1, surf->format->format);
if (!temp_surf) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
Expand Down
2 changes: 1 addition & 1 deletion src_c/pixelcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ make_surface(PyObject *self, PyObject *arg)
sizex = (int)view_p->shape[0];
sizey = (int)view_p->shape[1];

surf = SDL_CreateRGBSurfaceWithFormat(0, sizex, sizey, 0, pixelformat);
surf = PG_CreateSurface(sizex, sizey, pixelformat);
if (!surf) {
pgBuffer_Release(&pg_view);
return RAISE(pgExc_SDLError, SDL_GetError());
Expand Down
9 changes: 3 additions & 6 deletions src_c/rotozoom.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
/*
* New source surface is 32bit with a defined RGBA ordering
*/
rz_src = SDL_CreateRGBSurfaceWithFormat(0, src->w, src->h, 32,
SDL_PIXELFORMAT_ABGR8888);
rz_src = PG_CreateSurface(src->w, src->h, SDL_PIXELFORMAT_ABGR8888);
SDL_BlitSurface(src, NULL, rz_src, NULL);
src_converted = 1;
}
Expand Down Expand Up @@ -583,8 +582,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
/*
* Target surface is 32bit with source RGBA/ABGR ordering
*/
rz_dst = SDL_CreateRGBSurfaceWithFormat(0, dstwidth, dstheight, 32,
rz_src->format->format);
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);

/*
* Lock source surface
Expand Down Expand Up @@ -630,8 +628,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
/*
* Target surface is 32bit with source RGBA/ABGR ordering
*/
rz_dst = SDL_CreateRGBSurfaceWithFormat(0, dstwidth, dstheight, 32,
rz_src->format->format);
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);

/*
* Lock source surface
Expand Down
8 changes: 3 additions & 5 deletions src_c/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args)
sdlrect.h = 0;
sdlrect.w = 0;

surface =
SDL_CreateRGBSurfaceWithFormat(0, 1, 1, 32, surf->format->format);
surface = PG_CreateSurface(1, 1, surf->format->format);

SDL_LowerBlit(surf, &sdlrect, surface, &sdlrect);
SDL_FreeSurface(surface);
Expand Down Expand Up @@ -2675,9 +2674,8 @@ surf_subsurface(PyObject *self, PyObject *args)
pixeloffset = rect->x * format->BytesPerPixel + rect->y * surf->pitch;
startpixel = ((char *)surf->pixels) + pixeloffset;

sub = SDL_CreateRGBSurfaceWithFormatFrom(startpixel, rect->w, rect->h,
format->BitsPerPixel, surf->pitch,
format->format);
sub = PG_CreateSurfaceFrom(startpixel, rect->w, rect->h, surf->pitch,
format->format);

pgSurface_Unlock((pgSurfaceObject *)self);

Expand Down
13 changes: 5 additions & 8 deletions src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
return (SDL_Surface *)(RAISE(
PyExc_ValueError, "unsupported Surface bit depth for transform"));

newsurf = SDL_CreateRGBSurfaceWithFormat(
0, width, height, surf->format->BitsPerPixel, surf->format->format);
newsurf = PG_CreateSurface(width, height, surf->format->format);
if (!newsurf)
return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError()));

Expand Down Expand Up @@ -445,10 +444,9 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width,
* rejects the input.
* For example, RGBA and RGBX surfaces are compatible in this way. */
if (retsurf->format->Amask != src->format->Amask) {
modsurf = SDL_CreateRGBSurfaceWithFormatFrom(
retsurf->pixels, retsurf->w, retsurf->h,
retsurf->format->BitsPerPixel, retsurf->pitch,
src->format->format);
modsurf =
PG_CreateSurfaceFrom(retsurf->pixels, retsurf->w, retsurf->h,
retsurf->pitch, src->format->format);
}
}

Expand Down Expand Up @@ -885,8 +883,7 @@ surf_rotozoom(PyObject *self, PyObject *args, PyObject *kwargs)
}
else {
Py_BEGIN_ALLOW_THREADS;
surf32 = SDL_CreateRGBSurfaceWithFormat(0, surf->w, surf->h, 32,
SDL_PIXELFORMAT_ABGR8888);
surf32 = PG_CreateSurface(surf->w, surf->h, SDL_PIXELFORMAT_ABGR8888);
SDL_BlitSurface(surf, NULL, surf32, NULL);
Py_END_ALLOW_THREADS;
}
Expand Down

0 comments on commit dfdf8b8

Please sign in to comment.