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

@@ -1,17 +1,20 @@
use std::{
ffi::{c_float, c_int, c_uchar, c_uint},
ffi::{c_char, c_float, c_int, c_uchar, c_uint, c_void},
time::{SystemTime, UNIX_EPOCH},
};
extern "C" {
fn InitWindow(width: c_int, height: c_int, title: *const u8);
fn InitWindow(width: c_int, height: c_int, title: *const c_char);
fn CloseWindow();
fn SetTargetFPS(fps: c_int);
fn SetRandomSeed(seed: c_uint);
fn GetRandomValue(min: c_int, max: c_int) -> c_int;
fn LoadTexture(file_name: *const u8) -> Texture2D;
fn LoadImage(file_name: *const c_char) -> Image;
fn ImageResize(image: *mut Image, new_width: c_int, new_height: c_int);
fn LoadTextureFromImage(image: Image) -> Texture2D;
fn UnloadImage(image: Image);
fn UnloadTexture(texture: Texture2D);
fn WindowShouldClose() -> bool;
@@ -19,16 +22,20 @@ extern "C" {
fn BeginDrawing();
fn ClearBackground(color: Color);
fn DrawTextureEx(
texture: Texture2D,
position: Vector2,
rotation: c_float,
scale: c_float,
tint: Color,
);
fn DrawTextureV(texture: Texture2D, position: Vector2, tint: Color);
fn EndDrawing();
}
#[derive(Clone, Copy)]
#[repr(C)]
struct Image {
data: *mut c_void,
width: c_int,
height: c_int,
mipmaps: c_int,
format: c_int,
}
#[derive(Clone, Copy)]
#[repr(C)]
struct Texture2D {
@@ -55,7 +62,7 @@ struct Color {
const WINDOW_WIDTH: c_int = 800;
const WINDOW_HEIGHT: c_int = 600;
const LOGO_SCALE: f32 = 0.04;
const LOGO_HEIGHT: c_int = 64;
const LOGO_SPEED: f32 = 300.0;
const BLACK: Color = Color {
@@ -73,7 +80,7 @@ const WHITE: Color = Color {
fn main() {
unsafe {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Rust".as_ptr());
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, c"Raylib in Rust".as_ptr());
SetTargetFPS(60);
SetRandomSeed(
SystemTime::now()
@@ -82,13 +89,17 @@ fn main() {
.as_secs() as u32,
);
let logo_texture = LoadTexture("./rust_logo.png".as_ptr());
let mut logo_image = LoadImage(c"./rust_logo.png".as_ptr());
ImageResize(
&mut logo_image,
logo_image.width * LOGO_HEIGHT / logo_image.height,
LOGO_HEIGHT,
);
let logo_texture = LoadTextureFromImage(logo_image);
UnloadImage(logo_image);
let logo_width = (logo_texture.width as f32 * LOGO_SCALE) as i32;
let logo_height = (logo_texture.height as f32 * LOGO_SCALE) as i32;
let mut x = GetRandomValue(0, WINDOW_WIDTH - logo_width) as f32;
let mut y = GetRandomValue(0, WINDOW_HEIGHT - logo_height) as f32;
let mut x = GetRandomValue(0, WINDOW_WIDTH - logo_texture.width) as f32;
let mut y = GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height) as f32;
let mut dx = LOGO_SPEED * if GetRandomValue(0, 1) == 1 { -1.0 } else { 1.0 };
let mut dy = LOGO_SPEED * if GetRandomValue(0, 1) == 1 { -1.0 } else { 1.0 };
@@ -99,11 +110,11 @@ fn main() {
x += dx * delta_time;
y += dy * delta_time;
if x < 0.0 || x + logo_width as f32 >= WINDOW_WIDTH as f32 - 1.0 {
if x < 0.0 || x + logo_texture.width as f32 >= WINDOW_WIDTH as f32 - 1.0 {
dx *= -1.0;
x += dx * delta_time;
}
if y < 0.0 || y + logo_height as f32 >= WINDOW_HEIGHT as f32 - 1.0 {
if y < 0.0 || y + logo_texture.height as f32 >= WINDOW_HEIGHT as f32 - 1.0 {
dy *= -1.0;
y += dy * delta_time;
}
@@ -111,7 +122,7 @@ fn main() {
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(logo_texture, Vector2 { x, y }, 0.0, LOGO_SCALE, WHITE);
DrawTextureV(logo_texture, Vector2 { x, y }, WHITE);
EndDrawing();
}