cmdfx 0.3.2
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 "cmdfx/physics/util.h"
15#include <stdlib.h>
16}
17
18#include <vector>
19
20namespace CmdFX
21{
30class Vector final {
31 private:
32 CmdFX_Vector* vector;
33
34 public:
35 Vector(CmdFX_Vector* vector) : vector(vector) {
36 }
37 Vector(double x, double y) {
38 vector = Vector_create(x, y);
39 }
40
41 ~Vector() {
42 if (vector) {
43 free(vector);
44 }
45 }
46
47 CmdFX_Vector* getVector() {
48 return vector;
49 }
50
51 double getX() const {
52 return vector->x;
53 }
54 double getY() const {
55 return vector->y;
56 }
57 double getMagnitude() const {
58 return Vector_getMagnitude(vector);
59 }
60
61 void setX(double x) {
62 vector->x = x;
63 }
64 void setY(double y) {
65 vector->y = y;
66 }
67
68 Vector operator+(const Vector& other) const {
69 CmdFX_Vector* result = Vector_add(vector, other.vector);
70 return Vector(result);
71 }
72
73 Vector operator+(const std::vector<Vector>& vectors) const {
74 CmdFX_Vector** vArray = new CmdFX_Vector*[vectors.size() + 1];
75 vArray[0] = vector;
76
77 for (size_t i = 0; i < vectors.size(); ++i) {
78 vArray[i + 1] = vectors[i].vector;
79 }
80
81 CmdFX_Vector* result = Vector_addAll(vArray, vectors.size() + 1);
82 delete[] vArray;
83
84 return Vector(result);
85 }
86
87 Vector& operator+=(const Vector& other) {
88 CmdFX_Vector* result = Vector_add(vector, other.vector);
89 free(vector);
90 vector = result;
91
92 return *this;
93 }
94
95 Vector operator-(const Vector& other) const {
96 CmdFX_Vector* result = Vector_subtract(vector, other.vector);
97 return Vector(result);
98 }
99
100 Vector& operator-=(const Vector& other) {
101 CmdFX_Vector* result = Vector_subtract(vector, other.vector);
102 free(vector);
103 vector = result;
104
105 return *this;
106 }
107
108 Vector operator*(double scalar) const {
109 Vector result(vector->x, vector->y);
110 Vector_multiply(result.vector, scalar);
111 return result;
112 }
113
114 Vector& operator*=(double scalar) {
115 Vector_multiply(vector, scalar);
116 return *this;
117 }
118
119 Vector operator/(double scalar) const {
120 Vector result(vector->x, vector->y);
121 Vector_divide(result.vector, scalar);
122 return result;
123 }
124
125 Vector& operator/=(double scalar) {
126 Vector_divide(vector, scalar);
127 return *this;
128 }
129
130 int rotate(double radians) {
131 return Vector_rotate(vector, radians);
132 }
133
134 double getAngle() {
135 return Vector_getAngle(vector);
136 }
137
138 int flipX() {
139 return Vector_flipX(vector);
140 }
141
142 int flipY() {
143 return Vector_flipY(vector);
144 }
145
146 int flip() {
147 return Vector_flip(vector);
148 }
149
150 int dot(const Vector& other) const {
151 return Vector_dot(vector, other.vector);
152 }
153};
154} // namespace CmdFX
Primary namespace for CmdFX.
Definition cmdfx.hpp:26
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