Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

More rectangle functions, more @safe, and default values of Camera2D #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 0 additions & 9 deletions source/raylib/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
42 changes: 40 additions & 2 deletions source/raylib/raylib_types.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
module raylib.raylib_types;

import raylib;
debug import std.stdio;

@safe:

// Vector2 type
struct Vector2
Expand Down Expand Up @@ -54,6 +57,17 @@ struct Matrix
float m15 = 0.0f;
}

// 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
// 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
struct Rectangle
{
Expand All @@ -64,7 +78,19 @@ struct Rectangle
alias w = width;
alias h = height;

Vector2 origin() { // Rectangle function exclusive to raylib-d
// 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;}

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;
Expand Down Expand Up @@ -102,6 +128,8 @@ struct Rectangle
result.opOpAssign!op(offset);
return result;
}

Vector2 opCast(T)() if (is(T==Vector2)) => origin();
}

enum Colors
Expand Down Expand Up @@ -134,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
Expand All @@ -147,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);
Expand All @@ -165,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);
}