Skip to content

Commit 571eafd

Browse files
committed
Add compose_blend_mode and blend mode enums.
Refactor TextureAccess enums to not need library lookup.
1 parent 5657e3b commit 571eafd

File tree

1 file changed

+107
-3
lines changed

1 file changed

+107
-3
lines changed

tcod/sdl/render.py

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
class TextureAccess(enum.IntEnum):
1919
"""Determines how a texture is expected to be used."""
2020

21-
STATIC = lib.SDL_TEXTUREACCESS_STATIC or 0
21+
STATIC = 0
2222
"""Texture rarely changes."""
23-
STREAMING = lib.SDL_TEXTUREACCESS_STREAMING or 0
23+
STREAMING = 1
2424
"""Texture frequently changes."""
25-
TARGET = lib.SDL_TEXTUREACCESS_TARGET or 0
25+
TARGET = 2
2626
"""Texture will be used as a render target."""
2727

2828

@@ -37,6 +37,110 @@ class RendererFlip(enum.IntFlag):
3737
"""Flip the image vertically."""
3838

3939

40+
class BlendFactor(enum.IntEnum):
41+
"""SDL blend factors.
42+
43+
.. seealso::
44+
:any:`compose_blend_mode`
45+
https://wiki.libsdl.org/SDL_BlendFactor
46+
47+
.. versionadded:: unreleased
48+
"""
49+
50+
ZERO = 0x1
51+
""""""
52+
ONE = 0x2
53+
""""""
54+
SRC_COLOR = 0x3
55+
""""""
56+
ONE_MINUS_SRC_COLOR = 0x4
57+
""""""
58+
SRC_ALPHA = 0x5
59+
""""""
60+
ONE_MINUS_SRC_ALPHA = 0x6
61+
""""""
62+
DST_COLOR = 0x7
63+
""""""
64+
ONE_MINUS_DST_COLOR = 0x8
65+
""""""
66+
DST_ALPHA = 0x9
67+
""""""
68+
ONE_MINUS_DST_ALPHA = 0xA
69+
""""""
70+
71+
72+
class BlendOperation(enum.IntEnum):
73+
"""SDL blend operations.
74+
75+
.. seealso::
76+
:any:`compose_blend_mode`
77+
https://wiki.libsdl.org/SDL_BlendOperation
78+
79+
.. versionadded:: unreleased
80+
"""
81+
82+
ADD = 0x1
83+
"""dest + source"""
84+
SUBTRACT = 0x2
85+
"""dest - source"""
86+
REV_SUBTRACT = 0x3
87+
"""source - dest"""
88+
MINIMUM = 0x4
89+
"""min(dest, source)"""
90+
MAXIMUM = 0x5
91+
"""max(dest, source)"""
92+
93+
94+
class BlendMode(enum.IntEnum):
95+
"""SDL blend modes.
96+
97+
.. seealso::
98+
:any:`Texture.blend_mode`
99+
:any:`Renderer.draw_blend_mode`
100+
:any:`compose_blend_mode`
101+
102+
.. versionadded:: unreleased
103+
"""
104+
105+
NONE = 0x00000000
106+
""""""
107+
BLEND = 0x00000001
108+
""""""
109+
ADD = 0x00000002
110+
""""""
111+
MOD = 0x00000004
112+
""""""
113+
INVALID = 0x7FFFFFFF
114+
""""""
115+
116+
117+
def compose_blend_mode(
118+
source_color_factor: BlendFactor,
119+
dest_color_factor: BlendFactor,
120+
color_operation: BlendOperation,
121+
source_alpha_factor: BlendFactor,
122+
dest_alpha_factor: BlendFactor,
123+
alpha_operation: BlendOperation,
124+
) -> BlendMode:
125+
"""Return a custom blend mode composed of the given factors and operations.
126+
127+
.. seealso::
128+
https://wiki.libsdl.org/SDL_ComposeCustomBlendMode
129+
130+
.. versionadded:: unreleased
131+
"""
132+
return BlendMode(
133+
lib.SDL_ComposeCustomBlendMode(
134+
source_color_factor,
135+
dest_color_factor,
136+
color_operation,
137+
source_alpha_factor,
138+
dest_alpha_factor,
139+
alpha_operation,
140+
)
141+
)
142+
143+
40144
class Texture:
41145
"""SDL hardware textures."""
42146

0 commit comments

Comments
 (0)