From d6d9b841db419e2b29d6dbe0a389a2bf8c91b5bd Mon Sep 17 00:00:00 2001 From: electric-gecko Date: Sun, 14 Apr 2024 14:09:21 -0700 Subject: [PATCH 1/3] Gave `Camera2D` struct better initial conditions in `enhancedD` configuration. --- source/raylib/package.d | 9 --------- source/raylib/raylib_types.d | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/source/raylib/package.d b/source/raylib/package.d index ddeb78f..5f73d4c 100644 --- a/source/raylib/package.d +++ b/source/raylib/package.d @@ -270,15 +270,6 @@ struct Camera3D alias Camera = Camera3D; // Camera type fallback, defaults to Camera3D -// Camera2D, defines position/orientation in 2d space -struct Camera2D -{ - Vector2 offset; // Camera offset (displacement from target) - Vector2 target; // Camera target (rotation and zoom origin) - float rotation; // Camera rotation in degrees - float zoom; // Camera zoom (scaling), should be 1.0f by default -} - // Mesh, vertex data and vao/vbo struct Mesh { diff --git a/source/raylib/raylib_types.d b/source/raylib/raylib_types.d index d1f4199..b1dff0e 100644 --- a/source/raylib/raylib_types.d +++ b/source/raylib/raylib_types.d @@ -2,6 +2,7 @@ module raylib.raylib_types; import raylib; +debug import std.stdio; // Vector2 type struct Vector2 @@ -54,6 +55,30 @@ struct Matrix float m15 = 0.0f; } +// Camera2D, defines position/orientation in 2d space +struct Camera2D +{ + version (enhancedD) { + Vector2 offset; // Camera offset (displacement from target) + Vector2 target; // Camera target (rotation and zoom origin) + float rotation; // Camera rotation in degrees + float zoom; // Camera zoom (scaling), should be 1.0f by default + + this() {initialize(); return this;} + } else { + Vector2 offset; // Camera offset (displacement from target) + Vector2 target; // Camera target (rotation and zoom origin) + float rotation; // Camera rotation in degrees + float zoom; // Camera zoom (scaling), should be 1.0f by default + } + + void initialize() { + this.zoom = 1.0f; + if (IsWindowReady) offset = Vector2(GetScreenWidth/2.0f, GetScreenHeight/2.0f); + else debug writeln("Didn't initialize camera offset, as Window isn't open."); + } +} + // Rectangle type struct Rectangle { From 2948321f5a333ccc22920cffa3ead60fca3bcc83 Mon Sep 17 00:00:00 2001 From: electric-gecko Date: Wed, 1 May 2024 18:58:27 -0700 Subject: [PATCH 2/3] Set `raylib_types` module to `@safe`. Added more functions to `Rectangle`. --- dub.json | 26 ++++++++++++------------- source/raylib/raylib_types.d | 37 ++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/dub.json b/dub.json index f5a92b9..d99b348 100644 --- a/dub.json +++ b/dub.json @@ -5,19 +5,19 @@ "authors": [ "ONROUNDIT" ], - "configurations" : [ - { - "name": "library" - }, - { - "name": "unittest", - "versions" : ["raylib_test"], - "libs" : ["raylib"], - "lflags-posix": ["-L."], - "lflags-osx": ["-rpath", "@executable_path/"], - "lflags-linux" : ["-rpath=$$ORIGIN"] - } - ], + "configurations" : [ + { + "name": "library" + }, + { + "name": "unittest", + "versions" : ["raylib_test"], + "libs" : ["raylib"], + "lflags-posix": ["-L."], + "lflags-osx": ["-rpath", "@executable_path/"], + "lflags-linux" : ["-rpath=$$ORIGIN"] + } + ], "copyright": "Copyright (c) Ramon Santamaria (@raysan5), Petro Romanovych (@onroundit), Jan Hoenig (@m3m0ry), Steven Schveighoffer (@schveiguy), Liam McGillivray (@LiamM32)", "excludedSourceFiles": ["source/rlgl.d", "source/raymath.d", "source/easings.d", "source/raymathext.d", "source/raylib_types.d"], "targetType": "sourceLibrary", diff --git a/source/raylib/raylib_types.d b/source/raylib/raylib_types.d index b1dff0e..9b945e4 100644 --- a/source/raylib/raylib_types.d +++ b/source/raylib/raylib_types.d @@ -4,6 +4,11 @@ module raylib.raylib_types; import raylib; debug import std.stdio; +version (enhancedD) const ED = true; +else const ED = false; + +@safe: + // Vector2 type struct Vector2 { @@ -58,25 +63,12 @@ struct Matrix // Camera2D, defines position/orientation in 2d space struct Camera2D { - version (enhancedD) { - Vector2 offset; // Camera offset (displacement from target) - Vector2 target; // Camera target (rotation and zoom origin) - float rotation; // Camera rotation in degrees - float zoom; // Camera zoom (scaling), should be 1.0f by default - - this() {initialize(); return this;} - } else { - Vector2 offset; // Camera offset (displacement from target) - Vector2 target; // Camera target (rotation and zoom origin) - float rotation; // Camera rotation in degrees - float zoom; // Camera zoom (scaling), should be 1.0f by default - } - - void initialize() { - this.zoom = 1.0f; - if (IsWindowReady) offset = Vector2(GetScreenWidth/2.0f, GetScreenHeight/2.0f); - else debug writeln("Didn't initialize camera offset, as Window isn't open."); - } + + Vector2 offset; // Camera offset (displacement from target) + Vector2 target; // Camera target (rotation and zoom origin) + float rotation; // Camera rotation in degrees + // Warning: In Raylib, & previous versions of Raylib-D, `zoom` is initially `0.0f`. + float zoom = 1f; // Camera zoom (scaling), should be 1.0f by default } // Rectangle type @@ -89,6 +81,11 @@ struct Rectangle alias w = width; alias h = height; + float top() {return y;} + float bottom() {return y + height;} + float left() {return x;} + float right() {return x + width;} + Vector2 origin() { // Rectangle function exclusive to raylib-d return Vector2(x, y); } @@ -127,6 +124,8 @@ struct Rectangle result.opOpAssign!op(offset); return result; } + + Vector2 opCast(T)() if (is(T==Vector2)) => origin(); } enum Colors From 89c737a160031b6591ba54b7889b54c1c1b9dd6b Mon Sep 17 00:00:00 2001 From: electric-gecko Date: Sat, 1 Jun 2024 16:03:23 -0700 Subject: [PATCH 3/3] Added more `Rectangle` functions. Expanded `Rectangle` unittest. Changes in comments. --- README.md | 3 +++ source/raylib/raylib_types.d | 32 +++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 26f671d..f0497cd 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ # raylib-d [![DUB](https://img.shields.io/dub/v/raylib-d?style=for-the-badge)](https://code.dlang.org/packages/raylib-d) (static) D bindings for [raylib](https://www.raylib.com/), a simple and easy-to-use library to learn videogames programming. +# Differences from Raylib +All functions, types, and enum values are the same as in upstream Raylib. The only difference is that the `zoom` property of the `Camera2D` struct is initially set to `1.0f` instead of `0.0f`. + # Installation ## Adding the dependency diff --git a/source/raylib/raylib_types.d b/source/raylib/raylib_types.d index 9b945e4..a294bc0 100644 --- a/source/raylib/raylib_types.d +++ b/source/raylib/raylib_types.d @@ -4,9 +4,6 @@ module raylib.raylib_types; import raylib; debug import std.stdio; -version (enhancedD) const ED = true; -else const ED = false; - @safe: // Vector2 type @@ -81,12 +78,19 @@ struct Rectangle alias w = width; alias h = height; - float top() {return y;} - float bottom() {return y + height;} - float left() {return x;} - float right() {return x + width;} + // The following rectangle functions are exclusive to Raylib-D. + + float top() const {return y;} + float bottom() const {return y + height;} + float left() const {return x;} + float right() const {return x + width;} - Vector2 origin() { // Rectangle function exclusive to raylib-d + void top(const float yPosition) {y = yPosition;} + void bottom(const float yPosition) {y = yPosition - height;} + void left(const float xPosition) {x = xPosition;} + void right(const float xPosition) {x = xPosition - width;} + + Vector2 origin() { return Vector2(x, y); } alias position = origin; @@ -158,7 +162,7 @@ enum Colors BLACK = Color(0, 0, 0, 255), // Black BLANK = Color(0, 0, 0, 0), // Blank (Transparent) MAGENTA = Color(255, 0, 255, 255), // Magenta - RAYWHITE = Color(245, 245, 245, 255), // My own White (raylib logo) + RAYWHITE = Color(245, 245, 245, 255), // Grey-white colour from Raylib logo } unittest @@ -171,7 +175,10 @@ unittest float height = 20.0f; Rectangle rect = Rectangle(x, y, width, height); assert(rect.origin.x == 543.3f, "`rect.origin.x` should be 543.3, not "~to!string(rect.origin.x)); + assert(rect.left == 543.3f, "`rect.left` should be 543.3, not "~to!string(rect.left)); + assert(rect.right == 593.3f, "`rect.right` should be 593.3, not "~to!string(rect.right)); assert(rect.origin.y == 235.9f, "`rect.centre.y` should be 235.9, not "~to!string(rect.origin.y)); + assert(rect.bottom == 255.9f, "`rect.bottom` should be 255.9, not "~to!string(rect.bottom)); assert(rect.dimensions == Vector2(50.0f, 20.0f)); assert(rect.topLeft == Vector2(x:543.3f, y:235.9f), "`rect.topLeft` should be Vector2(543.3, 235.9), not "~to!string(rect.topLeft)); assert(rect.topRight.x == 593.3f); @@ -189,4 +196,11 @@ unittest rect -= Vector2(4.5f, 2.3f); assert(rect.x == 24.5f, "`rect.x` should be 24.5f. Instead it is "~to!string(rect.x)); assert(rect.y == 19.0f, "`rect.y` should be 20.7f. Instead it is "~to!string(rect.y)); + + rect.right = 100f; + rect.bottom = 60f; + assert(rect.x == 50.0f); + assert(rect.left == 50.0f); + assert(rect.y == 40.0f); + assert(rect.top == 40.0f); }