Watch
2
0
Fork
You've already forked raylib-cs
0

Initial update to raylib 2.5

- Still a work in progress
This commit is contained in:
Chris Dill 2019-07-07 13:12:06 +01:00
commit 711b59d8b8
6 changed files with 3153 additions and 2850 deletions

View file

@ -1,21 +1,58 @@
/* Raylib-cs
* Extensions.cs - Higher level features over bindings. This file is not automatically generated.
* Copyright 2019 Chris Dill
*
* Release under zLib License.
* See LICENSE for details.
*/
/* Extensions.cs
*
* Copyright 2019 Chris Dill
*
* Release under zLib License.
* See LICENSE for details.
*/
using System;
using System.Text;
using System.Numerics;
using System.Runtime.InteropServices;
namespace Raylib
{
// Extensions to the raylib bindings.
// Seperate for easier code generation.
// Extra functions over exsiting bindings
// Feel free to modifiy this to fit your project.
public partial class Raylib
{
public static PhysicsBodyData CreatePhysicsBodyCircleEx(Vector2 pos, float radius, float density)
{
var body = CreatePhysicsBodyCircle(pos, radius, density);
var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData));
return data;
}
public static PhysicsBodyData CreatePhysicsBodyRectangleEx(Vector2 pos, float width, float height, float density)
{
var body = CreatePhysicsBodyRectangle(pos, width, height, density);
var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData));
return data;
}
public static PhysicsBodyData GetPhysicsBodyEx(int index)
{
var body = GetPhysicsBodyImport(index);
var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData));
return data;
}
public static PhysicsBodyData CreatePhysicsBodyPolygonEx(Vector2 pos, float radius, int sides, float density)
{
var body = CreatePhysicsBodyPolygon(pos, radius, sides, density);
var data = (PhysicsBodyData)Marshal.PtrToStructure(body, typeof(PhysicsBodyData));
return data;
}
public static void DrawRenderTexture(RenderTexture2D target)
{
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTexturePro(target.texture, new Rectangle(0, 0, target.texture.width, -target.texture.height), new Rectangle(0, 0, screenWidth, screenHeight), new Vector2(0, 0), 0, Color.WHITE);
}
// extension providing SubText
public static string SubText(this string input, int position, int length)
{
@ -24,14 +61,14 @@ namespace Raylib
// Here (in the public method) we hide some low level details
// memory allocation, string manipulations etc.
public static bool CoreGuiTextBox(Rectangle bounds, ref string text, int textSize, bool freeEdit)
public static bool CoreGuiTextBox(Rectangle bounds, ref string text, int textSize, bool freeEdit)
{
if (null == text)
{
return false; // or throw exception; or assign "" to text
}
StringBuilder sb = new StringBuilder(text);
StringBuilder sb = new StringBuilder(text);
// If we allow editing we should allocate enough size (Length) within StringBuilder
if (textSize > sb.Length)
@ -39,7 +76,7 @@ namespace Raylib
sb.Length = textSize;
}
bool result = GuiTextBox(bounds, sb, sb.Length, freeEdit);
bool result = GuiTextBox(bounds, sb, sb.Length, freeEdit);
// Back to string (StringBuilder can have been edited)
// You may want to add some logic here; e.g. trim trailing '\0'
@ -56,7 +93,7 @@ namespace Raylib
return false; // or throw exception; or assign "" to text
}
StringBuilder sb = new StringBuilder(text);
StringBuilder sb = new StringBuilder(text);
// If we allow editing we should allocate enough size (Length) within StringBuilder
if (textSize > sb.Length)
@ -64,52 +101,13 @@ namespace Raylib
sb.Length = textSize;
}
bool result = GuiTextBoxMulti(bounds, sb, sb.Length, freeEdit);
bool result = GuiTextBoxMulti(bounds, sb, sb.Length, freeEdit);
// Back to string (StringBuilder can have been edited)
// You may want to add some logic here; e.g. trim trailing '\0'
text = sb.ToString();
return result;
}
}
// Small utility for tweening values
public struct Tween
{
public delegate float Callback(float t, float b, float c, float d);
public Callback easer;
public float start;
public float end;
public float currentTime;
public float duration;
public bool completed;
public Tween(Callback easer, float start, float end, float duration)
{
this.easer = easer;
this.start = start;
this.end = end;
this.currentTime = 0f;
this.duration = duration;
this.completed = false;
}
public void Reset()
{
currentTime = 0f;
completed = false;
}
public float Apply(float dt)
{
currentTime += dt;
if (currentTime > duration)
{
currentTime = duration;
completed = true;
}
return easer(currentTime, start, end - start, duration);
}
}
@ -160,29 +158,9 @@ namespace Raylib
this.a = Convert.ToByte(a);
}
internal string DebugDisplayString
public override string ToString()
{
get
{
return string.Concat(
r.ToString(), " ",
g.ToString(), " ",
b.ToString(), " ",
a.ToString()
);
}
}
// Performs linear interpolation of <see cref="Color"/>.
public static Color Lerp(Color value1, Color value2, float amount)
{
amount = Raylib.Clamp(amount, 0.0f, 1.0f);
return new Color(
(int)Raylib.Lerp(value1.r, value2.r, amount),
(int)Raylib.Lerp(value1.g, value2.g, amount),
(int)Raylib.Lerp(value1.b, value2.b, amount),
(int)Raylib.Lerp(value1.a, value2.a, amount)
);
return string.Concat(r.ToString(), " ", g.ToString(), " ", b.ToString(), " ", a.ToString());
}
}
@ -197,15 +175,6 @@ namespace Raylib
}
}
public partial struct BoundingBox
{
public BoundingBox(Vector3 min, Vector3 max)
{
this.min = min;
this.max = max;
}
}
public partial struct Camera3D
{
public Camera3D(Vector3 position, Vector3 target, Vector3 up, float fovy = 90, CameraType type = CameraType.CAMERA_PERSPECTIVE)
@ -227,17 +196,6 @@ namespace Raylib
}
}
public partial struct RayHitInfo
{
public RayHitInfo(bool hit, float distance, Vector3 position, Vector3 normal)
{
this.hit = hit;
this.distance = distance;
this.position = position;
this.normal = normal;
}
}
// Utlity for accessing math functions through struct
public partial struct Vector2
{
@ -253,11 +211,25 @@ namespace Raylib
this.y = value;
}
public override bool Equals(object obj) => (obj is Vector2) && Equals((Vector2)obj);
public override int GetHashCode() => x.GetHashCode() + y.GetHashCode();
public override bool Equals(object obj)
{
return (obj is Vector2) && Equals((Vector2)obj);
}
public float Length() => Raylib.Vector2Length(this);
public float LengthSquared() => (x * x) + (y * y);
public override int GetHashCode()
{
return x.GetHashCode() + y.GetHashCode();
}
public float Length()
{
return Raylib.Vector2Length(this);
}
public float LengthSquared()
{
return (x * x) + (y * y);
}
public override string ToString()
{
@ -266,77 +238,69 @@ namespace Raylib
// common values
public static Vector2 Zero { get { return Raylib.Vector2Zero(); } }
public static Vector2 One { get { return Raylib.Vector2One(); } }
public static Vector2 UnitX { get { return new Vector2(1, 0); } }
public static Vector2 UnitY { get { return new Vector2(0, 1); } }
// convienient operators
public static bool operator ==(Vector2 v1, Vector2 v2) => (v1.x == v2.x && v1.y == v2.y);
public static bool operator !=(Vector2 v1, Vector2 v2)
{
public static bool operator ==(Vector2 v1, Vector2 v2)
{
return (v1.x == v2.x && v1.y == v2.y);
}
public static bool operator !=(Vector2 v1, Vector2 v2)
{
return !(v1 == v2);
}
public static bool operator >(Vector2 v1, Vector2 v2)
public static bool operator >(Vector2 v1, Vector2 v2)
{
return v1.x > v2.x && v1.y > v2.y;
}
public static bool operator <(Vector2 v1, Vector2 v2)
{
public static bool operator <(Vector2 v1, Vector2 v2)
{
return v1.x < v2.x && v1.y < v2.y;
}
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return Raylib.Vector2Add(v1, v2);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return Raylib.Vector2Subtract(v1, v2);
}
public static Vector2 operator *(Vector2 v1, Vector2 v2)
public static Vector2 operator *(Vector2 v1, Vector2 v2)
{
return Raylib.Vector2Multiplyv(v1, v2);
return Raylib.Vector2MultiplyV(v1, v2);
}
public static Vector2 operator *(Vector2 v, float scale)
public static Vector2 operator *(Vector2 v, float scale)
{
return Raylib.Vector2Scale(v, scale);
}
public static Vector2 operator *(float scale, Vector2 v)
public static Vector2 operator *(float scale, Vector2 v)
{
return Raylib.Vector2Scale(v, scale);
}
public static Vector2 operator /(Vector2 v1, Vector2 v2)
{
public static Vector2 operator /(Vector2 v1, Vector2 v2)
{
return Raylib.Vector2DivideV(v1, v2);
}
public static Vector2 operator /(Vector2 v1, float div)
{
public static Vector2 operator /(Vector2 v1, float div)
{
return Raylib.Vector2Divide(v1, div);
}
public static Vector2 operator -(Vector2 v1)
{
return Raylib.Vector2Negate(v1);
}
public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount)
public static Vector2 operator -(Vector2 v1)
{
return new Vector2(
Raylib.Lerp(value1.x, value2.x, amount),
Raylib.Lerp(value1.y, value2.y, amount)
);
return Raylib.Vector2Negate(v1);
}
public static float Length(Vector2 v)
@ -405,8 +369,7 @@ namespace Raylib
{
return new Vector2(
v1.x > v2.x ? v1.x : v2.x,
v1.y > v2.y ? v1.y : v2.y
);
v1.y > v2.y ? v1.y : v2.y);
}
// Creates a new <see cref="Vector2"/> that contains a minimal values from the two vectors.
@ -414,8 +377,7 @@ namespace Raylib
{
return new Vector2(
v1.x < v2.x ? v1.x : v2.x,
v1.y < v2.y ? v1.y : v2.y
);
v1.y < v2.y ? v1.y : v2.y);
}
// Clamps the specified value within a range.
@ -423,12 +385,10 @@ namespace Raylib
{
return new Vector2(
Raylib.Clamp(value1.x, min.x, max.x),
Raylib.Clamp(value1.y, min.y, max.y)
);
Raylib.Clamp(value1.y, min.y, max.y));
}
}
// Vector3 type
public partial struct Vector3
{
@ -447,8 +407,15 @@ namespace Raylib
}
// extensions
public override bool Equals(object obj) => (obj is Vector3) && Equals((Vector3)obj);
public override int GetHashCode() => x.GetHashCode() + y.GetHashCode() + z.GetHashCode();
public override bool Equals(object obj)
{
return (obj is Vector3) && Equals((Vector3)obj);
}
public override int GetHashCode()
{
return x.GetHashCode() + y.GetHashCode() + z.GetHashCode();
}
public override string ToString()
{
@ -463,26 +430,62 @@ namespace Raylib
public static Vector3 UnitZ { get { return new Vector3(0, 0, 1); } }
// convienient operators
public static bool operator ==(Vector3 v1, Vector3 v2) => (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z);
public static bool operator !=(Vector3 v1, Vector3 v2) => !(v1 == v2);
public static bool operator >(Vector3 v1, Vector3 v2) => v1.x > v2.x && v1.y > v2.y && v1.z > v2.z;
public static bool operator <(Vector3 v1, Vector3 v2) => v1.x < v2.x && v1.y < v2.y && v1.z < v2.z;
public static Vector3 operator +(Vector3 v1, Vector3 v2) => Raylib.Vector3Add(v1, v2);
public static Vector3 operator -(Vector3 v1, Vector3 v2) => Raylib.Vector3Subtract(v1, v2);
public static Vector3 operator *(Vector3 v1, Vector3 v2) => Raylib.Vector3MultiplyV(v1, v2);
public static Vector3 operator *(Vector3 v, float scale) => Raylib.Vector3Scale(v, scale);
public static Vector3 operator *(float scale, Vector3 v) => Raylib.Vector3Scale(v, scale);
public static Vector3 operator /(Vector3 v1, Vector3 v2) => Raylib.Vector3DivideV(v1, v2);
public static Vector3 operator /(Vector3 v1, float div) => Raylib.Vector3Divide(v1, div);
public static Vector3 operator -(Vector3 v1) => Raylib.Vector3Negate(v1);
public static bool operator ==(Vector3 v1, Vector3 v2)
{ return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z);}
public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
public static bool operator !=(Vector3 v1, Vector3 v2)
{
return new Vector3(
Raylib.Lerp(value1.x, value2.x, amount),
Raylib.Lerp(value1.y, value2.y, amount),
Raylib.Lerp(value1.z, value2.z, amount)
);
return !(v1 == v2);
}
public static bool operator >(Vector3 v1, Vector3 v2)
{
return v1.x > v2.x && v1.y > v2.y && v1.z > v2.z;
}
public static bool operator <(Vector3 v1, Vector3 v2)
{
return v1.x < v2.x && v1.y < v2.y && v1.z < v2.z;
}
public static Vector3 operator +(Vector3 v1, Vector3 v2)
{
return Raylib.Vector3Add(v1, v2);
}
public static Vector3 operator -(Vector3 v1, Vector3 v2)
{
return Raylib.Vector3Subtract(v1, v2);
}
public static Vector3 operator *(Vector3 v1, Vector3 v2)
{
return Raylib.Vector3MultiplyV(v1, v2);
}
public static Vector3 operator *(Vector3 v, float scale)
{
return Raylib.Vector3Scale(v, scale);
}
public static Vector3 operator *(float scale, Vector3 v)
{
return Raylib.Vector3Scale(v, scale);
}
public static Vector3 operator /(Vector3 v1, Vector3 v2)
{
return Raylib.Vector3DivideV(v1, v2);
}
public static Vector3 operator /(Vector3 v1, float div)
{
return Raylib.Vector3Divide(v1, div);
}
public static Vector3 operator -(Vector3 v1)
{
return Raylib.Vector3Negate(v1);
}
}
@ -505,9 +508,15 @@ namespace Raylib
w = value;
}
public override bool Equals(object obj) => (obj is Vector4) && Equals((Vector4)obj);
public override bool Equals(object obj)
{
return (obj is Vector4) && Equals((Vector4)obj);
}
public override int GetHashCode() => x.GetHashCode() + y.GetHashCode() + z.GetHashCode() + w.GetHashCode();
public override int GetHashCode()
{
return x.GetHashCode() + y.GetHashCode() + z.GetHashCode() + w.GetHashCode();
}
public override string ToString()
{
@ -515,12 +524,24 @@ namespace Raylib
}
// convienient operators
public static bool operator ==(Vector4 v1, Vector4 v2) => (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w);
public static bool operator ==(Vector4 v1, Vector4 v2)
{
return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w);
}
public static bool operator !=(Vector4 v1, Vector4 v2) => !(v1 == v2);
public static bool operator !=(Vector4 v1, Vector4 v2)
{
return !(v1 == v2);
}
public static bool operator >(Vector4 v1, Vector4 v2) => v1.x > v2.x && v1.y > v2.y && v1.z > v2.z && v2.w > v2.w;
public static bool operator >(Vector4 v1, Vector4 v2)
{
return v1.x > v2.x && v1.y > v2.y && v1.z > v2.z && v1.w > v2.w;
}
public static bool operator <(Vector4 v1, Vector4 v2) => v1.x < v2.x && v1.y < v2.y && v1.z < v2.z && v1.w < v2.w;
public static bool operator <(Vector4 v1, Vector4 v2)
{
return v1.x < v2.x && v1.y < v2.y && v1.z < v2.z && v1.w < v2.w;
}
}
}