Skip to content

Commit 4add12e

Browse files
committed
Clean up FastRenderSample attributes
Move names for mutable variables into the class Refactor Light into a dataclass Numpy is not optional with latest tcod
1 parent 1708d8e commit 4add12e

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

examples/samples_tcod.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sys
1414
import time
1515
import warnings
16+
from dataclasses import dataclass
1617
from pathlib import Path
1718
from typing import TYPE_CHECKING, Any
1819

@@ -1111,9 +1112,6 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
11111112
#############################################
11121113
# python fast render sample
11131114
#############################################
1114-
numpy_available = True
1115-
1116-
use_numpy = numpy_available # default option
11171115
SCREEN_W = SAMPLE_SCREEN_WIDTH
11181116
SCREEN_H = SAMPLE_SCREEN_HEIGHT
11191117
HALF_W = SCREEN_W // 2
@@ -1133,36 +1131,32 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
11331131
# example: (4x3 pixels screen)
11341132
# xc = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] # noqa: ERA001
11351133
# yc = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] # noqa: ERA001
1136-
if numpy_available:
1137-
(xc, yc) = np.meshgrid(range(SCREEN_W), range(SCREEN_H))
1138-
# translate coordinates of all pixels to center
1139-
xc = xc - HALF_W
1140-
yc = yc - HALF_H
1141-
1142-
noise2d = tcod.noise.Noise(2, hurst=0.5, lacunarity=2.0)
1143-
if numpy_available: # the texture starts empty
1144-
texture = np.zeros((RES_U, RES_V))
1134+
(xc, yc) = np.meshgrid(range(SCREEN_W), range(SCREEN_H))
1135+
# translate coordinates of all pixels to center
1136+
xc = xc - HALF_W
1137+
yc = yc - HALF_H
11451138

11461139

1140+
@dataclass(frozen=False, slots=True)
11471141
class Light:
1148-
def __init__(
1149-
self,
1150-
x: float,
1151-
y: float,
1152-
z: float,
1153-
r: int,
1154-
g: int,
1155-
b: int,
1156-
strength: float,
1157-
) -> None:
1158-
self.x, self.y, self.z = x, y, z # pos.
1159-
self.r, self.g, self.b = r, g, b # color
1160-
self.strength = strength # between 0 and 1, defines brightness
1142+
"""Lighting effect entity."""
1143+
1144+
x: float # pos
1145+
y: float
1146+
z: float
1147+
r: int # color
1148+
g: int
1149+
b: int
1150+
strength: float # between 0 and 1, defines brightness
11611151

11621152

11631153
class FastRenderSample(Sample):
11641154
name = "Python fast render"
11651155

1156+
def __init__(self) -> None:
1157+
self.texture = np.zeros((RES_U, RES_V))
1158+
self.noise2d = tcod.noise.Noise(2, hurst=0.5, lacunarity=2.0)
1159+
11661160
def on_enter(self) -> None:
11671161
sample_console.clear() # render status message
11681162
sample_console.print(1, SCREEN_H - 3, "Renderer: NumPy", fg=WHITE, bg=None)
@@ -1178,8 +1172,6 @@ def on_enter(self) -> None:
11781172
self.tex_b = 0.0
11791173

11801174
def on_draw(self) -> None:
1181-
global texture
1182-
11831175
time_delta = frame_length[-1] * SPEED # advance time
11841176
self.frac_t += time_delta # increase fractional (always < 1.0) time
11851177
self.abs_t += time_delta # increase absolute elapsed time
@@ -1207,14 +1199,14 @@ def on_draw(self) -> None:
12071199
# new pixels are based on absolute elapsed time
12081200
int_abs_t = int(self.abs_t)
12091201

1210-
texture = np.roll(texture, -int_t, 1)
1202+
self.texture = np.roll(self.texture, -int_t, 1)
12111203
# replace new stretch of texture with new values
12121204
for v in range(RES_V - int_t, RES_V):
12131205
for u in range(RES_U):
12141206
tex_v = (v + int_abs_t) / float(RES_V)
1215-
texture[u, v] = libtcodpy.noise_get_fbm(
1216-
noise2d, [u / float(RES_U), tex_v], 32.0
1217-
) + libtcodpy.noise_get_fbm(noise2d, [1 - u / float(RES_U), tex_v], 32.0)
1207+
self.texture[u, v] = libtcodpy.noise_get_fbm(
1208+
self.noise2d, [u / float(RES_U), tex_v], 32.0
1209+
) + libtcodpy.noise_get_fbm(self.noise2d, [1 - u / float(RES_U), tex_v], 32.0)
12181210

12191211
# squared distance from center,
12201212
# clipped to sensible minimum and maximum values
@@ -1229,7 +1221,7 @@ def on_draw(self) -> None:
12291221
uu = np.mod(RES_U * (np.arctan2(yc, xc) / (2 * np.pi) + 0.5), RES_U)
12301222

12311223
# retrieve corresponding pixels from texture
1232-
brightness = texture[uu.astype(int), vv.astype(int)] / 4.0 + 0.5
1224+
brightness = self.texture[uu.astype(int), vv.astype(int)] / 4.0 + 0.5
12331225

12341226
# use the brightness map to compose the final color of the tunnel
12351227
rr = brightness * self.tex_r
@@ -1253,7 +1245,7 @@ def on_draw(self) -> None:
12531245
for light in self.lights: # render lights
12541246
# move light's Z coordinate with time, then project its XYZ
12551247
# coordinates to screen-space
1256-
light.z -= float(time_delta) / TEX_STRETCH
1248+
light.z -= time_delta / TEX_STRETCH
12571249
xl = light.x / light.z * SCREEN_H
12581250
yl = light.y / light.z * SCREEN_H
12591251

0 commit comments

Comments
 (0)