Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions buildconfig/stubs/pygame/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ from .constants import (
RLEACCEL as RLEACCEL,
RLEACCELOK as RLEACCELOK,
SCALED as SCALED,
SCALEMODE_LINEAR as SCALEMODE_LINEAR,
SCALEMODE_NEAREST as SCALEMODE_NEAREST,
SCALEMODE_PIXELART as SCALEMODE_PIXELART,
SCRAP_BMP as SCRAP_BMP,
SCRAP_CLIPBOARD as SCRAP_CLIPBOARD,
SCRAP_PBM as SCRAP_PBM,
Expand Down
17 changes: 17 additions & 0 deletions buildconfig/stubs/pygame/_render.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ class Texture:
@blend_mode.setter
def blend_mode(self, value: int) -> None: ...
@property
def scale_mode(self) -> int:
"""Get or set the scale mode for texture drawing operations

Valid scale modes are:
* ``pygame.SCALEMODE_NEAREST`` (default): Nearest pixel sampling (also
known as nearest neighbor), preserving the texture's pixels when scaled.
* ``pygame.SCALEMODE_LINEAR``: Linear filtering, smoothing the texture
when scaled.
* ``pygame.SCALEMODE_PIXELART``: Nearest pixel sampling with improved
scaling for pixel art. Falls back to ``pygame.SCALEMODE_NEAREST`` if
pygame is built with SDL2.

.. versionadded:: 2.5.8
"""
@scale_mode.setter
def scale_mode(self, value: int) -> None: ...
@property
def color(self) -> Color: ...
@color.setter
def color(self, value: ColorLike) -> None: ...
Expand Down
6 changes: 6 additions & 0 deletions buildconfig/stubs/pygame/constants.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ RESIZABLE: int
RLEACCEL: int
RLEACCELOK: int
SCALED: int
SCALEMODE_LINEAR: int
SCALEMODE_NEAREST: int
SCALEMODE_PIXELART: int
SCRAP_BMP: str
SCRAP_CLIPBOARD: int
SCRAP_PBM: str
Expand Down Expand Up @@ -650,6 +653,9 @@ __all__ = [
"GL_CONTEXT_RELEASE_BEHAVIOR",
"GL_CONTEXT_RELEASE_BEHAVIOR_NONE",
"GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH",
"SCALEMODE_NEAREST",
"SCALEMODE_LINEAR",
"SCALEMODE_PIXELART",
"BLENDMODE_NONE",
"BLENDMODE_BLEND",
"BLENDMODE_ADD",
Expand Down
6 changes: 6 additions & 0 deletions buildconfig/stubs/pygame/locals.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ RLEACCEL: int
RLEACCELOK: int
Rect: type
SCALED: int
SCALEMODE_LINEAR: int
SCALEMODE_NEAREST: int
SCALEMODE_PIXELART: int
SCRAP_BMP: str
SCRAP_CLIPBOARD: int
SCRAP_PBM: str
Expand Down Expand Up @@ -652,6 +655,9 @@ __all__ = [
"GL_CONTEXT_RELEASE_BEHAVIOR",
"GL_CONTEXT_RELEASE_BEHAVIOR_NONE",
"GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH",
"SCALEMODE_NEAREST",
"SCALEMODE_LINEAR",
"SCALEMODE_PIXELART",
"BLENDMODE_NONE",
"BLENDMODE_BLEND",
"BLENDMODE_ADD",
Expand Down
10 changes: 10 additions & 0 deletions src_c/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ MODINIT_DEFINE(constants)
DEC_CONST(GL_CONTEXT_RELEASE_BEHAVIOR_NONE);
DEC_CONST(GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH);

#if SDL_VERSION_ATLEAST(3, 0, 0)
DEC_CONST(SCALEMODE_NEAREST);
DEC_CONST(SCALEMODE_LINEAR);
DEC_CONST(SCALEMODE_PIXELART);
#else
DEC_CONSTS(SCALEMODE_NEAREST, SDL_ScaleModeNearest);
DEC_CONSTS(SCALEMODE_LINEAR, SDL_ScaleModeLinear);
DEC_CONSTS(SCALEMODE_PIXELART, SDL_ScaleModeNearest);
#endif

DEC_CONST(BLENDMODE_NONE);
DEC_CONST(BLENDMODE_BLEND);
DEC_CONST(BLENDMODE_ADD);
Expand Down
25 changes: 25 additions & 0 deletions src_c/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,26 @@ texture_set_blend_mode(pgTextureObject *self, PyObject *arg, void *closure)
return 0;
}

static PyObject *
texture_get_scale_mode(pgTextureObject *self, void *closure)
{
SDL_ScaleMode scale_mode;
RENDERER_ERROR_CHECK(SDL_GetTextureScaleMode(self->texture, &scale_mode))
return PyLong_FromLong((long)scale_mode);
}

static int
texture_set_scale_mode(pgTextureObject *self, PyObject *arg, void *closure)
{
long longval = PyLong_AsLong(arg);
if (longval == -1 && PyErr_Occurred()) {
return -1;
}
RENDERER_PROPERTY_ERROR_CHECK(
SDL_SetTextureScaleMode(self->texture, (int)longval))
return 0;
}

static PyObject *
texture_get_color(pgTextureObject *self, void *closure)
{
Expand Down Expand Up @@ -1270,6 +1290,11 @@ static PyGetSetDef texture_getset[] = {
DOC_SDL2_VIDEO_TEXTURE_ALPHA, NULL},
{"blend_mode", (getter)texture_get_blend_mode,
(setter)texture_set_blend_mode, DOC_SDL2_VIDEO_TEXTURE_BLENDMODE, NULL},
{"scale_mode", (getter)texture_get_scale_mode,
(setter)texture_set_scale_mode,
"scale_mode -> int\nGet or set the scale mode for texture drawing "
"operations",
NULL},
{"color", (getter)texture_get_color, (setter)texture_set_color,
DOC_SDL2_VIDEO_TEXTURE_COLOR, NULL},
{NULL, 0, NULL, NULL, NULL}};
Expand Down
5 changes: 5 additions & 0 deletions test/render_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ def test_blend_mode(self):
self.texture.blend_mode = pygame.BLENDMODE_BLEND
self.assertEqual(pygame.BLENDMODE_BLEND, self.texture.blend_mode)

def test_scale_mode(self):
self.assertEqual(pygame.SCALEMODE_NEAREST, self.texture.scale_mode)
self.texture.scale_mode = pygame.SCALEMODE_LINEAR
self.assertEqual(pygame.SCALEMODE_LINEAR, self.texture.scale_mode)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

def test_color(self):
self.assertEqual(pygame.Color(255, 255, 255, 255), self.texture.color)
self.texture.color = pygame.Color(100, 110, 120, 130)
Expand Down
Loading