diff --git a/csharp/Makefile b/csharp/Makefile new file mode 100644 index 0000000..bc0c384 --- /dev/null +++ b/csharp/Makefile @@ -0,0 +1,2 @@ +raylib_csharp: main.cs + mcs main.cs -out:raylib_csharp.exe \ No newline at end of file diff --git a/csharp/README.md b/csharp/README.md new file mode 100644 index 0000000..4be34c4 --- /dev/null +++ b/csharp/README.md @@ -0,0 +1,14 @@ +# Raylib in C# + +I'm using Mono because i don't like Microsoft. + +## Quick start + +```shell +$ make +$ mono raylib_csharp.exe +``` + +## Credits + +- Logo: [Wikipedia](https://wikipedia.org/wiki/File:Logo_C_sharp.svg) \ No newline at end of file diff --git a/csharp/csharp_logo.png b/csharp/csharp_logo.png new file mode 100644 index 0000000..dae65b3 Binary files /dev/null and b/csharp/csharp_logo.png differ diff --git a/csharp/main.cs b/csharp/main.cs new file mode 100644 index 0000000..4fc591d --- /dev/null +++ b/csharp/main.cs @@ -0,0 +1,105 @@ +using System.Runtime.InteropServices; + +static class Program +{ + [StructLayout(LayoutKind.Sequential)] + struct Texture2D + { + public uint id; + public int width; + public int height; + public int mipmaps; + public int format; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2 + { + public float x; + public float y; + } + + [StructLayout(LayoutKind.Sequential)] + struct Color + { + public byte r; + public byte g; + public byte b; + public byte a; + } + + 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 void UnloadTexture(Texture2D texture); + + [DllImport(RAYLIB)] private static extern bool WindowShouldClose(); + [DllImport(RAYLIB)] private static extern float GetFrameTime(); + + [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 EndDrawing(); + + private const int WINDOW_WIDTH = 800; + private const int WINDOW_HEIGHT = 600; + private const float LOGO_SCALE = 0.05f; + private const float LOGO_SPEED = 300; + + private static Color BLACK = new Color() { r = 0, g = 0, b = 0, a = 255 }; + private static Color WHITE = new Color() { r = 255, g = 255, b = 255, a = 255 }; + + public static void Main() + { + InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in C#"); + SetTargetFPS(60); + + Texture2D logo_texture = LoadTexture("./csharp_logo.png"); + + 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 dx = LOGO_SPEED * (GetRandomValue(0, 1) == 1 ? -1 : 1); + float dy = LOGO_SPEED * (GetRandomValue(0, 1) == 1 ? -1 : 1); + + while (!WindowShouldClose()) + { + float delta_time = GetFrameTime(); + + x += dx * delta_time; + y += dy * delta_time; + + if (x < 0 || (x + logo_width) >= WINDOW_WIDTH - 1) + { + dx *= -1; + x += dx * delta_time; + } + if (y < 0 || (y + logo_height) >= WINDOW_HEIGHT - 1) + { + dy *= -1; + y += dy * delta_time; + } + + BeginDrawing(); + + ClearBackground(BLACK); + DrawTextureEx(logo_texture, new Vector2() { x = x, y = y }, 0, LOGO_SCALE, WHITE); + + EndDrawing(); + } + + UnloadTexture(logo_texture); + CloseWindow(); + } +} \ No newline at end of file