refactor: constant logo height instead of using scale

This commit is contained in:
2026-03-11 19:06:11 +01:00
parent 96a26cea5c
commit 54f2fc9bd4
6 changed files with 126 additions and 81 deletions

View File

@@ -3,19 +3,28 @@ from time import time
rl = CDLL("../_raylib-5.5_linux_amd64/lib/libraylib.so")
class Image(Structure):
_fields_ = [
("data", c_void_p),
("width", c_int),
("height", c_int),
("mipmaps", c_int),
("format", c_int),
]
class Texture2D(Structure):
_fields_ = [
("id", c_uint),
("width", c_int),
("height", c_int),
("mipmaps", c_int),
("format", c_int),
("id", c_uint),
("width", c_int),
("height", c_int),
("mipmaps", c_int),
("format", c_int),
]
class Vector2(Structure):
_fields_ = [
("x", c_float),
("y", c_float),
("x", c_float),
("y", c_float),
]
class Color(Structure):
@@ -26,28 +35,29 @@ class Color(Structure):
("a", c_ubyte),
]
rl.LoadTexture.restype = Texture2D
rl.LoadImage.restype = Image
rl.LoadTextureFromImage.restype = Texture2D
rl.GetFrameTime.restype = c_float
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
LOGO_SCALE = 0.04
LOGO_HEIGHT = 64
LOGO_SPEED = 300
BLACK = Color(r=0,g=0,b=0,a=255)
WHITE = Color(r=255,g=255,b=255,a=255)
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Python")
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, b"Raylib in Python")
rl.SetTargetFPS(60)
rl.SetRandomSeed(int(time()))
logo_texture: Texture2D = rl.LoadTexture(b"./python_logo.png")
logo_image = rl.LoadImage(b"./python_logo.png")
rl.ImageResize(byref(logo_image), logo_image.width * LOGO_HEIGHT // logo_image.height, LOGO_HEIGHT)
logo_texture = rl.LoadTextureFromImage(logo_image)
rl.UnloadImage(logo_image)
logo_width = int(logo_texture.width * LOGO_SCALE)
logo_height = int(logo_texture.height * LOGO_SCALE)
x = rl.GetRandomValue(0, WINDOW_WIDTH - logo_width)
y = rl.GetRandomValue(0, WINDOW_HEIGHT - logo_height)
x = rl.GetRandomValue(0, WINDOW_WIDTH - logo_texture.width)
y = rl.GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height)
dx = LOGO_SPEED * (-1 if rl.GetRandomValue(0, 1) else 1)
dy = LOGO_SPEED * (-1 if rl.GetRandomValue(0, 1) else 1)
@@ -58,19 +68,19 @@ while not rl.WindowShouldClose():
x += dx * delta_time
y += dy * delta_time
if x < 0 or (x + logo_width) >= WINDOW_WIDTH - 1:
if x < 0 or (x + logo_texture.width) >= WINDOW_WIDTH - 1:
dx *= -1
x += dx * delta_time
if y < 0 or (y + logo_height) >= WINDOW_HEIGHT - 1:
if y < 0 or (y + logo_texture.height) >= WINDOW_HEIGHT - 1:
dy *= -1
y += dy * delta_time
rl.BeginDrawing()
rl.ClearBackground(BLACK)
rl.DrawTextureEx(logo_texture, Vector2(x=x, y=y), c_float(0), c_float(LOGO_SCALE), WHITE)
rl.DrawTextureV(logo_texture, Vector2(x=x, y=y), WHITE)
rl.EndDrawing()
rl.UnloadTexture(logo_texture)
rl.CloseWindow()
rl.CloseWindow()