Port Vector3I to C

This commit is contained in:
UnknownShadow200 2017-05-09 09:15:52 +10:00
parent 35aeb82fcf
commit b3beb74d84
6 changed files with 99 additions and 67 deletions

View File

@ -203,6 +203,7 @@
<ClCompile Include="DefaultSet.c" />
<ClCompile Include="Bitmap.c" />
<ClCompile Include="Direct3D9Api.c" />
<ClCompile Include="ExtMath.c" />
<ClCompile Include="FastColour.c" />
<ClCompile Include="GraphicsCommon.c" />
<ClCompile Include="Matrix.c" />
@ -212,6 +213,7 @@
<ClCompile Include="String.c" />
<ClCompile Include="Texture.c" />
<ClCompile Include="TextureRec.c" />
<ClCompile Include="Vector3I.c" />
<ClCompile Include="Vectors.c" />
<ClCompile Include="WinPlatform.c" />
</ItemGroup>

View File

@ -200,5 +200,11 @@
<ClCompile Include="Direct3D9Api.c">
<Filter>Source Files\Graphics</Filter>
</ClCompile>
<ClCompile Include="Vector3I.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
<ClCompile Include="ExtMath.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
</ItemGroup>
</Project>

6
src/Client/ExtMath.c Normal file
View File

@ -0,0 +1,6 @@
#include "ExtMath.h"
Int32 Math_Floor(Real32 value) {
Int32 valueI = (Int32)value;
return value < valueI ? valueI - 1 : valueI;
}

View File

@ -1,6 +1,10 @@
#ifndef CS_MATH_H
#define CS_MATH_H
#include <math.h>
#include "Typedefs.h"
/* Simple math functions.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
#define MATH_PI 3.1415926535897931f
@ -16,4 +20,6 @@
#define Math_Sqrt(x) sqrtf(x)
/* Integer floor of a floating-point value. */
Int32 Math_Floor(Real32 value);
#endif

59
src/Client/Vector3I.c Normal file
View File

@ -0,0 +1,59 @@
#include "Vector3I.h"
#include "Funcs.h"
#include "ExtMath.h"
Vector3I Vector3I_Create1(Int32 value) {
Vector3I v; v.X = value; v.Y = value; v.Z = value; return v;
}
Vector3I Vector3I_Create3(Int32 x, Int32 y, Int32 z) {
Vector3I v; v.X = x; v.Y = y; v.Z = z; return v;
}
void Vector3I_Add(Vector3I* a, Vector3I* b, Vector3I* result) {
result->X = a->X + b->X;
result->Y = a->Y + b->Y;
result->Z = a->Z + b->Z;
}
void Vector3I_Subtract(Vector3I* a, Vector3I* b, Vector3I* result) {
result->X = a->X - b->X;
result->Y = a->Y - b->Y;
result->Z = a->Z - b->Z;
}
void Vector3I_Multiply1(Vector3I* a, Int32 scale, Vector3I* result) {
result->X = a->X * scale;
result->Y = a->Y * scale;
result->Z = a->Z * scale;
}
void Vector3I_Negate(Vector3I* a, Vector3I* result) {
result->X = -a->X;
result->Y = -a->Y;
result->Z = -a->Z;
}
bool Vector3I_Equals(Vector3I* a, Vector3I* b) {
return a->X == b->X && a->Y == b->Y && a->Z == b->Z;
}
void Vector3I_Floor(Vector3* a, Vector3I* result) {
result->X = Math_Floor(a->X);
result->Y = Math_Floor(a->Y);
result->Z = Math_Floor(a->Z);
}
void Vector3I_Min(Vector3I* a, Vector3I* b, Vector3I* result) {
result->X = min(a->X, b->X);
result->Y = min(a->Y, b->Y);
result->Z = min(a->Z, b->Z);
}
void Vector3I_Max(Vector3I* a, Vector3I* b, Vector3I* result) {
result->X = max(a->X, b->X);
result->Y = max(a->Y, b->Y);
result->Z = max(a->Z, b->Z);
}

View File

@ -1,6 +1,7 @@
#ifndef CS_VECTOR3I_H
#define CS_VECTOR3I_H
#include "Typedefs.h"
#include "Vectors.h"
/* Represents 3 dimensional integer vector.
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
@ -13,12 +14,6 @@ Vector3I Vector3I_Create1(Int32 value);
/* Constructs a 3D vector from the given coordinates. */
Vector3I Vector3I_Create3(Int32 x, Int32 y, Int32 z);
/* Returns the length of the given vector. */
Real32 Vector3I_Length(Vector3I* v);
/* Returns the squared length of the given vector. */
Int32 Vector3I_LengthSquared(Vector3I* v);
#define Vector3I_UnitX Vector3I_Create3(1, 0, 0)
#define Vector3I_UnitY Vector3I_Create3(0, 1, 0)
@ -28,70 +23,28 @@ Int32 Vector3I_LengthSquared(Vector3I* v);
#define Vector3I_MinusOne Vector3I_Create3(-1, -1, -1)
/* Adds a and b. */
void Vector3I_Add(Vector3I* a, Vector3I* b, Vector3I* result);
/* Subtracts b from a. */
void Vector3I_Subtract(Vector3I* a, Vector3I* b, Vector3I* result);
/* Multiplies all components of a by scale. */
void Vector3I_Multiply1(Vector3I* a, Int32 scale, Vector3I* result);
/* Negates components of a. */
void Vector3I_Negate(Vector3I* a, Vector3I* result);
public Vector3 Normalise() {
float len = Length;
return new Vector3(X / len, Y / len, Z / len);
}
/* Returns whether the two vectors are exact same on all axes. */
bool Vector3I_Equals(Vector3I* a, Vector3I* b);
public static Vector3I operator * (Vector3I left, int right) {
return new Vector3I(left.X * right, left.Y * right, left.Z * right);
}
/* Returns a vector such that each component is floor of input floatin-point component.*/
void Vector3I_Floor(Vector3* a, Vector3I* result);
public static Vector3I operator + (Vector3I left, Vector3I right) {
return new Vector3I(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}
/* Returns a vector such that each component is minimum of corresponding a and b component.*/
void Vector3I_Min(Vector3I* a, Vector3I* b, Vector3I* result);
public static Vector3I operator - (Vector3I left, Vector3I right) {
return new Vector3I(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
}
public static Vector3I operator - (Vector3I left) {
return new Vector3I(-left.X, -left.Y, -left.Z);
}
public static explicit operator Vector3I(Vector3 value) {
return Truncate(value);
}
public static explicit operator Vector3(Vector3I value) {
return new Vector3(value.X, value.Y, value.Z);
}
public bool Equals(Vector3I other) {
return X == other.X && Y == other.Y && Z == other.Z;
}
public static Vector3I Truncate(Vector3 vector) {
int x = (int)vector.X;
int y = (int)vector.Y;
int z = (int)vector.Z;
return new Vector3I(x, y, z);
}
public static Vector3I Floor(Vector3 value) {
return new Vector3I(Utils.Floor(value.X), Utils.Floor(value.Y), Utils.Floor(value.Z));
}
public static Vector3I Floor(float x, float y, float z) {
return new Vector3I(Utils.Floor(x), Utils.Floor(y), Utils.Floor(z));
}
public static Vector3I Min(Vector3I p1, Vector3I p2) {
return new Vector3I(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y), Math.Min(p1.Z, p2.Z));
}
public static Vector3I Min(int x1, int y1, int z1, int x2, int y2, int z2) {
return new Vector3I(Math.Min(x1, x2), Math.Min(y1, y2), Math.Min(z1, z2));
}
public static Vector3I Max(Vector3I p1, Vector3I p2) {
return new Vector3I(Math.Max(p1.X, p2.X), Math.Max(p1.Y, p2.Y), Math.Max(p1.Z, p2.Z));
}
public static Vector3I Max(int x1, int y1, int z1, int x2, int y2, int z2) {
return new Vector3I(Math.Max(x1, x2), Math.Max(y1, y2), Math.Max(z1, z2));
}
}
/* Returns a vector such that each component is maximum of corresponding a and b component.*/
void Vector3I_Max(Vector3I* a, Vector3I* b, Vector3I* result);
#endif