cmdfx 0.2.1
Lightweight game engine for your terminal
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1
11#pragma once
12
13extern "C" {
14 #include <stdlib.h>
15 #include "cmdfx/physics/util.h"
16}
17
18#include <vector>
19
20namespace CmdFX {
29 class Vector final {
30 private:
31 CmdFX_Vector* vector;
32
33 public:
34 Vector(CmdFX_Vector* vector) : vector(vector) {}
35 Vector(double x, double y) {
36 vector = Vector_create(x, y);
37 }
38
39 ~Vector() {
40 if (vector) { free(vector); }
41 }
42
43 CmdFX_Vector* getVector() { return vector; }
44
45 double getX() const { return vector->x; }
46 double getY() const { return vector->y; }
47 double getMagnitude() const { return Vector_getMagnitude(vector); }
48
49 void setX(double x) { vector->x = x; }
50 void setY(double y) { vector->y = y; }
51
52 Vector operator+(const Vector& other) const {
53 CmdFX_Vector* result = Vector_add(vector, other.vector);
54 return Vector(result);
55 }
56
57 Vector operator+(const std::vector<Vector>& vectors) const {
58 CmdFX_Vector** vArray = new CmdFX_Vector*[vectors.size() + 1];
59 vArray[0] = vector;
60
61 for (size_t i = 0; i < vectors.size(); ++i) {
62 vArray[i + 1] = vectors[i].vector;
63 }
64
65 CmdFX_Vector* result = Vector_addAll(vArray, vectors.size() + 1);
66 delete[] vArray;
67
68 return Vector(result);
69 }
70
71 Vector& operator+=(const Vector& other) {
72 CmdFX_Vector* result = Vector_add(vector, other.vector);
73 free(vector);
74 vector = result;
75
76 return *this;
77 }
78
79 Vector operator-(const Vector& other) const {
80 CmdFX_Vector* result = Vector_subtract(vector, other.vector);
81 return Vector(result);
82 }
83
84 Vector& operator-=(const Vector& other) {
85 CmdFX_Vector* result = Vector_subtract(vector, other.vector);
86 free(vector);
87 vector = result;
88
89 return *this;
90 }
91
92 Vector operator*(double scalar) const {
93 Vector result(vector->x, vector->y);
94 Vector_multiply(result.vector, scalar);
95 return result;
96 }
97
98 Vector& operator*=(double scalar) {
99 Vector_multiply(vector, scalar);
100 return *this;
101 }
102
103 Vector operator/(double scalar) const {
104 Vector result(vector->x, vector->y);
105 Vector_divide(result.vector, scalar);
106 return result;
107 }
108
109 Vector& operator/=(double scalar) {
110 Vector_divide(vector, scalar);
111 return *this;
112 }
113
114 int rotate(double radians) {
115 return Vector_rotate(vector, radians);
116 }
117
118 double getAngle() {
119 return Vector_getAngle(vector);
120 }
121
122 int flipX() {
123 return Vector_flipX(vector);
124 }
125
126 int flipY() {
127 return Vector_flipY(vector);
128 }
129
130 int flip() {
131 return Vector_flip(vector);
132 }
133
134 int dot(const Vector& other) const {
135 return Vector_dot(vector, other.vector);
136 }
137 };
138}
Primary namespace for CmdFX.
Definition cmdfx.hpp:22
Physics utilities for cmdfx.
CmdFX_Vector * Vector_addAll(CmdFX_Vector **vectors, int count)
Adds multiple 2D Vectors.
int Vector_dot(CmdFX_Vector *v1, CmdFX_Vector *v2)
Gets the dot product of two vectors.
double Vector_getAngle(CmdFX_Vector *v)
Gets the angle of a 2D Vector.
double Vector_getMagnitude(CmdFX_Vector *v)
Gets the magnitude of a 2D Vector.
CmdFX_Vector * Vector_subtract(CmdFX_Vector *v1, CmdFX_Vector *v2)
Subtracts two 2D Vectors.
CmdFX_Vector * Vector_create(double x, double y)
Creates a new 2D Vector.
int Vector_multiply(CmdFX_Vector *v, double scalar)
Multiplies a 2D Vector by a scalar.
CmdFX_Vector * Vector_add(CmdFX_Vector *v1, CmdFX_Vector *v2)
Adds two 2D Vectors.
int Vector_rotate(CmdFX_Vector *v, double radians)
Rotates a 2D Vector by a specific number of radians.
int Vector_flipX(CmdFX_Vector *v)
Flips a 2D Vector horizontally.
int Vector_flip(CmdFX_Vector *v)
Flips a 2D Vector.
int Vector_divide(CmdFX_Vector *v, double scalar)
Divides a 2D Vector by a scalar.
int Vector_flipY(CmdFX_Vector *v)
Flips a 2D Vector vertically.
A 2D Vector.
Definition util.h:22
double x
The x coordinate.
Definition util.h:26