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,7 +1,18 @@
using System;
using System.Runtime.InteropServices;
static class Program
{
[StructLayout(LayoutKind.Sequential)]
struct Image
{
public IntPtr data;
public int width;
public int height;
public int mipmaps;
public int format;
}
[StructLayout(LayoutKind.Sequential)]
struct Texture2D
{
@@ -31,14 +42,16 @@ static class Program
private const string RAYLIB = "../_raylib-5.5_linux_amd64/lib/libraylib.so";
[DllImport(RAYLIB)] private static extern void InitWindow(int width, int height, string title);
[DllImport(RAYLIB)] private static extern void CloseWindow();
[DllImport(RAYLIB)] private static extern void SetTargetFPS(int fps);
[DllImport(RAYLIB)] private static extern void SetRandomSeed(uint seed);
[DllImport(RAYLIB)] private static extern int GetRandomValue(int min, int max);
[DllImport(RAYLIB)] private static extern Texture2D LoadTexture(string file_name);
[DllImport(RAYLIB)] private static extern Image LoadImage(string file_name);
[DllImport(RAYLIB)] private static extern void ImageResize(ref Image image, int new_width, int new_height);
[DllImport(RAYLIB)] private static extern Texture2D LoadTextureFromImage(Image image);
[DllImport(RAYLIB)] private static extern void UnloadImage(Image image);
[DllImport(RAYLIB)] private static extern void UnloadTexture(Texture2D texture);
[DllImport(RAYLIB)] private static extern bool WindowShouldClose();
@@ -46,12 +59,12 @@ static class Program
[DllImport(RAYLIB)] private static extern void BeginDrawing();
[DllImport(RAYLIB)] private static extern void ClearBackground(Color color);
[DllImport(RAYLIB)] private static extern void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);
[DllImport(RAYLIB)] private static extern void DrawTextureV(Texture2D texture, Vector2 position, Color tint);
[DllImport(RAYLIB)] private static extern void EndDrawing();
private const int WINDOW_WIDTH = 800;
private const int WINDOW_HEIGHT = 600;
private const float LOGO_SCALE = 0.05f;
private const int LOGO_HEIGHT = 64;
private const float LOGO_SPEED = 300;
private static Color BLACK = new Color() { r = 0, g = 0, b = 0, a = 255 };
@@ -62,13 +75,13 @@ static class Program
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in C#");
SetTargetFPS(60);
Texture2D logo_texture = LoadTexture("./csharp_logo.png");
Image logo_image = LoadImage("./csharp_logo.png");
ImageResize(ref logo_image, logo_image.width * LOGO_HEIGHT / logo_image.height, LOGO_HEIGHT);
Texture2D logo_texture = LoadTextureFromImage(logo_image);
UnloadImage(logo_image);
int logo_width = (int)(logo_texture.width * LOGO_SCALE);
int logo_height = (int)(logo_texture.height * LOGO_SCALE);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_height);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_texture.width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height);
float dx = LOGO_SPEED * (GetRandomValue(0, 1) == 1 ? -1 : 1);
float dy = LOGO_SPEED * (GetRandomValue(0, 1) == 1 ? -1 : 1);
@@ -80,12 +93,12 @@ static class Program
x += dx * delta_time;
y += dy * delta_time;
if (x < 0 || (x + logo_width) >= WINDOW_WIDTH - 1)
if (x < 0 || (x + logo_texture.width) >= WINDOW_WIDTH - 1)
{
dx *= -1;
x += dx * delta_time;
}
if (y < 0 || (y + logo_height) >= WINDOW_HEIGHT - 1)
if (y < 0 || (y + logo_texture.height) >= WINDOW_HEIGHT - 1)
{
dy *= -1;
y += dy * delta_time;
@@ -94,7 +107,7 @@ static class Program
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(logo_texture, new Vector2() { x = x, y = y }, 0, LOGO_SCALE, WHITE);
DrawTextureV(logo_texture, new Vector2() { x = x, y = y }, WHITE);
EndDrawing();
}
@@ -102,4 +115,4 @@ static class Program
UnloadTexture(logo_texture);
CloseWindow();
}
}
}