diff --git a/d/Makefile b/d/Makefile new file mode 100644 index 0000000..e5bbd49 --- /dev/null +++ b/d/Makefile @@ -0,0 +1,3 @@ +raylib_d: main.d + dmd main.d -of=./raylib_d ../_raylib-5.5_linux_amd64/lib/libraylib.a + rm ./raylib_d.o \ No newline at end of file diff --git a/d/README.md b/d/README.md new file mode 100644 index 0000000..9f725c3 --- /dev/null +++ b/d/README.md @@ -0,0 +1,8 @@ +# Raylib in D + +## Quick start + +```sh +$ make +$ ./raylib_d +``` \ No newline at end of file diff --git a/d/d_logo.png b/d/d_logo.png new file mode 100644 index 0000000..6a78666 Binary files /dev/null and b/d/d_logo.png differ diff --git a/d/main.d b/d/main.d new file mode 100644 index 0000000..53d95a8 --- /dev/null +++ b/d/main.d @@ -0,0 +1,94 @@ +import std.stdio; +import std.datetime; + +struct Texture2D { + uint id; + int width; + int height; + int mipmaps; + int format; +} + +struct Vector2 { + float x; + float y; +} + +struct Color { + ubyte r; + ubyte g; + ubyte b; + ubyte a; +} + +extern(C): + void InitWindow(int width, int height, const char* title); + void CloseWindow(); + + void SetTargetFPS(int fps); + void SetRandomSeed(uint seed); + int GetRandomValue(int min, int max); + + Texture2D LoadTexture(const char* file_name); + void UnloadTexture(Texture2D texture); + + bool WindowShouldClose(); + float GetFrameTime(); + + void BeginDrawing(); + void ClearBackground(Color color); + void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint); + void EndDrawing(); + +const int WINDOW_WIDTH = 800; +const int WINDOW_HEIGHT = 600; +const float LOGO_SCALE = 0.1f; +const float LOGO_SPEED = 300; + +const Color BLACK = Color(0, 0, 0, 255); +const Color WHITE = Color(255, 255, 255, 255); + +void main() { + InitWindow(800, 600, "Raylib in D"); + SetTargetFPS(60); + SetRandomSeed(cast(uint)Clock.currStdTime()); + + Texture2D logo_texture = LoadTexture("./d_logo.png"); + + int logo_width = cast(int)(logo_texture.width * LOGO_SCALE); + int logo_height = cast(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); + float dy = LOGO_SPEED * (GetRandomValue(0, 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, Vector2(x, y), 0, LOGO_SCALE, WHITE); + + EndDrawing(); + } + + UnloadTexture(logo_texture); + CloseWindow(); +} \ No newline at end of file