Skip to content

Commit f144b5f

Browse files
committed
convert() and convert_alpha() now support Window
1 parent b560863 commit f144b5f

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

src_c/_pygame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ typedef enum {
485485
#define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
486486
#define PYGAMEAPI_COLOR_NUMSLOTS 5
487487
#define PYGAMEAPI_MATH_NUMSLOTS 2
488-
#define PYGAMEAPI_BASE_NUMSLOTS 27
488+
#define PYGAMEAPI_BASE_NUMSLOTS 29
489489
#define PYGAMEAPI_EVENT_NUMSLOTS 8
490490
#define PYGAMEAPI_WINDOW_NUMSLOTS 1
491491
#define PYGAMEAPI_GEOMETRY_NUMSLOTS 1

src_c/base.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,23 @@ pg_SetDefaultWindowSurface(pgSurfaceObject *screen)
20572057
pg_default_screen = screen;
20582058
}
20592059

2060+
SDL_PixelFormat *pg_default_convert_format = NULL;
2061+
2062+
static SDL_PixelFormat *
2063+
pg_GetDefaultConvertFormat(void)
2064+
{
2065+
if (pg_default_screen) {
2066+
return pg_default_screen->surf->format;
2067+
}
2068+
return pg_default_convert_format;
2069+
}
2070+
2071+
static void
2072+
pg_SetDefaultConvertFormat(SDL_PixelFormat *fmt)
2073+
{
2074+
pg_default_convert_format = fmt;
2075+
}
2076+
20602077
static char *
20612078
pg_EnvShouldBlendAlphaSDL2(void)
20622079
{
@@ -2273,8 +2290,10 @@ MODINIT_DEFINE(base)
22732290
c_api[24] = pg_DoubleFromObj;
22742291
c_api[25] = pg_TwoDoublesFromObj;
22752292
c_api[26] = pg_TwoDoublesFromFastcallArgs;
2293+
c_api[27] = pg_GetDefaultConvertFormat;
2294+
c_api[28] = pg_SetDefaultConvertFormat;
22762295

2277-
#define FILLED_SLOTS 27
2296+
#define FILLED_SLOTS 29
22782297

22792298
#if PYGAMEAPI_BASE_NUMSLOTS != FILLED_SLOTS
22802299
#error export slot count mismatch

src_c/include/_pygame.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ typedef struct pg_bufferinfo_s {
173173
#define pg_EnvShouldBlendAlphaSDL2 \
174174
(*(char *(*)(void))PYGAMEAPI_GET_SLOT(base, 23))
175175

176+
#define pg_GetDefaultConvertFormat \
177+
(*(SDL_PixelFormat * (*)(void)) PYGAMEAPI_GET_SLOT(base, 27))
178+
179+
#define pg_SetDefaultConvertFormat \
180+
(*(void (*)(SDL_PixelFormat *))PYGAMEAPI_GET_SLOT(base, 28))
181+
176182
#define import_pygame_base() IMPORT_PYGAME_MODULE(base)
177183
#endif /* ~PYGAMEAPI_BASE_INTERNAL */
178184

src_c/surface.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,32 +1590,34 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
15901590
static SDL_Surface *
15911591
pg_DisplayFormat(SDL_Surface *surface)
15921592
{
1593-
SDL_Surface *displaysurf;
1594-
if (!pg_GetDefaultWindowSurface()) {
1595-
SDL_SetError("No video mode has been set");
1593+
if (!pg_GetDefaultConvertFormat()) {
1594+
SDL_SetError("No convert format has been set, try display.set_mode()"
1595+
// " or Window.get_surface()"
1596+
// (uncomment when Window API is published)
1597+
);
15961598
return NULL;
15971599
}
1598-
displaysurf = pgSurface_AsSurface(pg_GetDefaultWindowSurface());
1599-
return PG_ConvertSurface(surface, displaysurf->format);
1600+
return PG_ConvertSurface(surface, pg_GetDefaultConvertFormat());
16001601
}
16011602

16021603
static SDL_Surface *
16031604
pg_DisplayFormatAlpha(SDL_Surface *surface)
16041605
{
1605-
SDL_Surface *displaysurf;
16061606
SDL_PixelFormat *dformat;
16071607
Uint32 pfe;
16081608
Uint32 amask = 0xff000000;
16091609
Uint32 rmask = 0x00ff0000;
16101610
Uint32 gmask = 0x0000ff00;
16111611
Uint32 bmask = 0x000000ff;
16121612

1613-
if (!pg_GetDefaultWindowSurface()) {
1614-
SDL_SetError("No video mode has been set");
1613+
if (!pg_GetDefaultConvertFormat()) {
1614+
SDL_SetError("No convert format has been set, try display.set_mode()"
1615+
// " or Window.get_surface()"
1616+
// (uncomment when Window API is published)
1617+
);
16151618
return NULL;
16161619
}
1617-
displaysurf = pgSurface_AsSurface(pg_GetDefaultWindowSurface());
1618-
dformat = displaysurf->format;
1620+
dformat = pg_GetDefaultConvertFormat();
16191621

16201622
switch (dformat->BytesPerPixel) {
16211623
case 2:

src_c/window.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ window_get_surface(pgWindowObject *self)
163163
return NULL;
164164
}
165165
self->surf->surf = _surf;
166+
167+
if (pg_GetDefaultConvertFormat() == NULL)
168+
pg_SetDefaultConvertFormat(_surf->format);
169+
166170
Py_INCREF(self->surf);
167171
return (PyObject *)self->surf;
168172
}

0 commit comments

Comments
 (0)