Skip to content

Commit 5f575bf

Browse files
added core_window_letterbox.py example (#178)
1 parent 8e85d28 commit 5f575bf

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
"""
2+
3+
raylib [core] example - window scale letterbox (and virtual mouse)
4+
tested with: Python 3.13.3, raylib 5.5.0.2
5+
6+
"""
7+
8+
import pyray
9+
from pyray import Color, Rectangle, Vector2
10+
11+
from pyray import (
12+
RAYWHITE,
13+
GREEN,
14+
YELLOW,
15+
BLACK,
16+
)
17+
18+
# Initialization
19+
SCREEN_WIDTH = 800
20+
SCREEN_HEIGHT = 450
21+
22+
# Enable config flags for resizable window and vertical synchro
23+
pyray.set_config_flags(
24+
pyray.ConfigFlags.FLAG_WINDOW_RESIZABLE | pyray.ConfigFlags.FLAG_VSYNC_HINT
25+
)
26+
27+
pyray.init_window(
28+
SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - window scale letterbox"
29+
)
30+
31+
pyray.set_window_min_size(320, 240)
32+
33+
34+
gameScreenWidth = 640
35+
gameScreenHeight = 480
36+
37+
# Render texture initialization, used to hold the rendering result so we can easily resize it
38+
target = pyray.load_render_texture(gameScreenWidth, gameScreenHeight)
39+
pyray.set_texture_filter(target.texture, pyray.TextureFilter.TEXTURE_FILTER_BILINEAR)
40+
41+
colors = []
42+
for _ in range(10):
43+
colors.append(
44+
Color(
45+
pyray.get_random_value(100, 250),
46+
pyray.get_random_value(50, 150),
47+
pyray.get_random_value(10, 100),
48+
255,
49+
)
50+
)
51+
52+
pyray.set_target_fps(60) # Set our game to run at 60 frames-per-second
53+
54+
55+
# Main game loop
56+
while not pyray.window_should_close(): # Detect window close button or ESC key
57+
# Update
58+
# TODO: Update your variables here
59+
60+
# Compute required framebuffer scaling
61+
scale = min(
62+
float(pyray.get_screen_width() / gameScreenWidth),
63+
float(pyray.get_screen_height() / gameScreenHeight),
64+
)
65+
66+
if pyray.is_key_pressed(pyray.KeyboardKey.KEY_SPACE):
67+
# Recalculate random colors for the bars
68+
for color in colors:
69+
color = Color(
70+
pyray.get_random_value(100, 250),
71+
pyray.get_random_value(50, 150),
72+
pyray.get_random_value(10, 100),
73+
255,
74+
)
75+
76+
# Update virtual mouse (clamped mouse value behind game screen)
77+
mouse = pyray.get_mouse_position()
78+
virtualMouse = Vector2()
79+
virtualMouse.x = (
80+
mouse.x - (pyray.get_screen_width() - (gameScreenWidth * scale)) * 0.5
81+
) / scale
82+
virtualMouse.y = (
83+
mouse.x - (pyray.get_screen_height() - (gameScreenHeight * scale)) * 0.5
84+
) / scale
85+
86+
virtualMouse = pyray.vector2_clamp(
87+
virtualMouse,
88+
Vector2(0, 0),
89+
Vector2(float(gameScreenWidth), float(gameScreenHeight)),
90+
)
91+
92+
pyray.begin_texture_mode(target)
93+
pyray.clear_background(RAYWHITE)
94+
95+
for i, color in enumerate(colors):
96+
pyray.draw_rectangle(
97+
0,
98+
int((gameScreenHeight / 10) * i),
99+
gameScreenWidth,
100+
int(gameScreenHeight / 10),
101+
color,
102+
)
103+
104+
pyray.draw_text(
105+
f"Default Mouse: [{int(mouse.x)}, {int(mouse.y)}]", 350, 25, 20, GREEN
106+
)
107+
pyray.draw_text(
108+
f"Default Mouse: [{int(virtualMouse.x)}, {int(virtualMouse.y)}]",
109+
350,
110+
55,
111+
20,
112+
YELLOW,
113+
)
114+
pyray.end_texture_mode()
115+
116+
pyray.begin_drawing()
117+
pyray.clear_background(BLACK)
118+
pyray.draw_texture_pro(
119+
target.texture,
120+
Rectangle(
121+
0.0,
122+
0.0,
123+
float(target.texture.width),
124+
float(-target.texture.height),
125+
),
126+
Rectangle(
127+
(pyray.get_screen_width() - float(gameScreenWidth) * scale) * 0.5,
128+
(pyray.get_screen_height() - float(gameScreenHeight) * scale) * 0.5,
129+
float(gameScreenWidth) * scale,
130+
float(gameScreenHeight) * scale,
131+
),
132+
Vector2(0.0, 0.0),
133+
0.0,
134+
RAYWHITE,
135+
)
136+
137+
pyray.end_drawing()
138+
139+
# De-Initialization
140+
pyray.unload_render_texture(target)
141+
pyray.close_window() # Close window and OpenGL context

0 commit comments

Comments
 (0)