diff --git a/makefile b/makefile index af8f773..831afc7 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ CC=g++ -C_FLAGS=-g -Wall -Wextra -O0 -Wno-unused-function +C_FLAGS=-g -Wall -Wextra -O0 -Wno-unused-function -MMD -MP VAL_FLAGS=--leak-check=full --show-leak-kinds=all --track-origins=yes -s DIR_SRC=src @@ -11,6 +11,7 @@ OUTBIN=$(DIR_BUILD)/bin/main SRCS=$(shell find $(DIR_SRC)/ -type f -name '*.cpp') OBJS=$(patsubst $(DIR_SRC)/%.cpp,$(DIR_BUILD)/obj/%.o,$(SRCS)) +DEPMF=$(patsubst $(DIR_SRC)/%.cpp,$(DIR_BUILD)/obj/%.d,$(SRCS)) DEPS_EXT=$(patsubst %,-l%,$(DEPS)) INCS_EXT=$(patsubst %,-I%,$(DIR_INC)) @@ -38,6 +39,8 @@ $(DIR_BUILD)/obj/%.o: $(DIR_SRC)/%.cpp @mkdir -p $(@D) $(CC) $(C_FLAGS) $(INCS_EXT) -c $< -o $@ +-include $(DEPMF) + dbg: $(DBG_BIN) gdb $(GDB_FLAGS) ./$(OUTBIN) diff --git a/src/dxd_math.cpp b/src/dxd_math.cpp new file mode 100644 index 0000000..82c416d --- /dev/null +++ b/src/dxd_math.cpp @@ -0,0 +1,106 @@ +#include "dxd_math.hpp" + +vec2 operator+(vec2 const &v) +{ + return { .x = v.x, .y = v.y }; +} + +vec2 operator-(vec2 const &v) +{ + return { .x = -v.x, .y = -v.y }; +} + +vec2 operator+(vec2 const &a, vec2 const &b) +{ + return { .x = a.x + b.x, .y = a.y + b.y }; +} + +vec2 operator-(vec2 const &a, vec2 const &b) +{ + return { .x = a.x - b.x, .y = a.y - b.y }; +} + +vec2 operator*(vec2 const &a, float const &b) +{ + return { .x = a.x * b, .y = a.y * b }; +} + +vec2 operator/(vec2 const &a, float const &b) +{ + return { .x = a.x / b, .y = a.y / b }; +} + +bool operator<(vec2 const &a, vec2 const &b) +{ + return a.x < b.x && a.y < b.y; +} + +bool operator>(vec2 const &a, vec2 const &b) +{ + return a.x > b.x && a.y > b.y; +} + +float dot(vec2 const &a, vec2 const &b) +{ + return a.x * b.x + a.y * b.y; +} + +float norm2(vec2 const &v) +{ + return v.x * v.x + v.y * v.y; +} + +float norm(vec2 const &v) +{ + return std::sqrtf(v.x * v.x + v.y * v.y); +} + +vec2 normalize(vec2 const &v) +{ + return v / std::sqrt(v.x * v.x + v.y * v.y); +} + +vec2 min2(vec2 const &a, vec2 const &b) +{ + return { + .x = a.x < b.x ? a.x : b.x, + .y = a.y < b.y ? a.y : b.y, + }; +} + +vec2 max2(vec2 const &a, vec2 const &b) +{ + return { + .x = a.x > b.x ? a.x : b.x, + .y = a.y > b.y ? a.y : b.y, + }; +} + +vec2 polar_to_vec2(const float angle, const float len) +{ + return { + .x = len * sinf(angle), + .y = len * cosf(angle) + }; +} + +float vec2_angle(const vec2 v) +{ + return atan2f(v.x, v.y); +} + +float normalize_angle(const float angle) +{ + float a = angle; + while (a > 2*M_PIf) a -= 2*M_PIf; + while (a < 0) a += 2*M_PIf; + return a; +} + +float normalize_angle_diff(const float diff) +{ + float d = diff; + while (d > M_PIf) d -= 2*M_PIf; + while (d < -M_PIf) d += 2*M_PIf; + return d; +} diff --git a/src/dxd_math.hpp b/src/dxd_math.hpp new file mode 100644 index 0000000..5447d42 --- /dev/null +++ b/src/dxd_math.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + + + +typedef struct vec2 { float x, y; } vec2; +typedef struct vec2i { int x, y; } vec2i; + +static const vec2 vec2_zero = { 0, 0 }; +static const vec2 vec2_one = { 1, 1 }; +static const vec2 vec2_unitx = { 1, 0 }; +static const vec2 vec2_unity = { 0, 1 }; +static const vec2 vec2_max = { FLT_MAX, FLT_MAX }; +static const vec2 vec2_min = { -FLT_MAX, -FLT_MAX }; +static const vec2i vec2i_zero = { 0, 0 }; +static const vec2i vec2i_one = { 1, 1 }; + +vec2 operator+(vec2 const &v); +vec2 operator-(vec2 const &v); +vec2 operator+(vec2 const &a, vec2 const &b); +vec2 operator-(vec2 const &a, vec2 const &b); +vec2 operator*(vec2 const &a, float const &b); +vec2 operator/(vec2 const &a, float const &b); +bool operator<(vec2 const &a, vec2 const &b); +bool operator>(vec2 const &a, vec2 const &b); + +float dot(vec2 const &a, vec2 const &b); +float norm2(vec2 const &v); +float norm(vec2 const &v); +vec2 normalize(vec2 const &v); +vec2 min2(vec2 const &a, vec2 const &b); +vec2 max2(vec2 const &a, vec2 const &b); + +vec2 polar_to_vec2(const float angle, const float len); +float vec2_angle(const vec2 v); + +float normalize_angle(const float angle); +float normalize_angle_diff(const float diff); diff --git a/src/math.hpp b/src/math.hpp deleted file mode 100644 index 9b9e9db..0000000 --- a/src/math.hpp +++ /dev/null @@ -1,131 +0,0 @@ -#pragma once - -#include -#include - -//#define epsilon 0.0000001 - - - -typedef struct vec2 { float x, y; } vec2; -typedef struct vec2i { int x, y; } vec2i; - -static const vec2 vec2_zero = { 0, 0 }; -static const vec2 vec2_one = { 1, 1 }; -static const vec2 vec2_unitx = { 1, 0 }; -static const vec2 vec2_unity = { 0, 1 }; -static const vec2 vec2_max = { FLT_MAX, FLT_MAX }; -static const vec2 vec2_min = { -FLT_MAX, -FLT_MAX }; -static const vec2i vec2i_zero = { 0, 0 }; -static const vec2i vec2i_one = { 1, 1 }; - -static vec2 operator+(vec2 const &v) -{ - return { .x = v.x, .y = v.y }; -} - -static vec2 operator-(vec2 const &v) -{ - return { .x = -v.x, .y = -v.y }; -} - -static vec2 operator+(vec2 const &a, vec2 const &b) -{ - return { .x = a.x + b.x, .y = a.y + b.y }; -} - -static vec2 operator-(vec2 const &a, vec2 const &b) -{ - return { .x = a.x - b.x, .y = a.y - b.y }; -} - -static vec2 operator*(vec2 const &a, float const &b) -{ - return { .x = a.x * b, .y = a.y * b }; -} - -static vec2 operator/(vec2 const &a, float const &b) -{ - return { .x = a.x / b, .y = a.y / b }; -} - -static bool operator<(vec2 const &a, vec2 const &b) -{ - return a.x < b.x && a.y < b.y; -} - -static bool operator>(vec2 const &a, vec2 const &b) -{ - return a.x > b.x && a.y > b.y; -} - -static float dot(vec2 const &a, vec2 const &b) -{ - return a.x * b.x + a.y * b.y; -} - -static float norm2(vec2 const &v) -{ - return v.x * v.x + v.y * v.y; -} - -static float norm(vec2 const &v) -{ - return std::sqrtf(v.x * v.x + v.y * v.y); -} - -static vec2 normalize(vec2 const &v) -{ - return v / norm(v); -} - -static inline vec2 normalize_h(vec2 const &v) -{ - return v / std::sqrt(v.x * v.x + v.y * v.y); -} - -static vec2 min2(vec2 const &a, vec2 const &b) -{ - return { - .x = a.x < b.x ? a.x : b.x, - .y = a.y < b.y ? a.y : b.y, - }; -} - -static vec2 max2(vec2 const &a, vec2 const &b) -{ - return { - .x = a.x > b.x ? a.x : b.x, - .y = a.y > b.y ? a.y : b.y, - }; -} - -static vec2 polar_to_vec2(const float angle, const float len) -{ - // 0 (north) up, PI/2 (east) right - return { - .x = len * sinf(angle), - .y = len * cosf(angle) - }; -} - -static float vec2_angle(const vec2 v) -{ - return atan2f(v.x, v.y); -} - -static float normalize_angle(const float angle) -{ - float a = angle; - while (a > 2*M_PIf) a -= 2*M_PIf; - while (a < 0) a += 2*M_PIf; - return a; -} - -static float normalize_angle_diff(const float diff) -{ - float d = diff; - while (d > M_PIf) d -= 2*M_PIf; - while (d < -M_PIf) d += 2*M_PIf; - return d; -} diff --git a/src/renderer.hpp b/src/renderer.hpp index 87a5d11..3e5972e 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -4,7 +4,7 @@ #include -#include "math.hpp" +#include "dxd_math.hpp" namespace dxd { diff --git a/src/sim/af_spawner.hpp b/src/sim/af_spawner.hpp index 22f00a3..0e1ffd5 100644 --- a/src/sim/af_spawner.hpp +++ b/src/sim/af_spawner.hpp @@ -2,7 +2,7 @@ #include -#include "../math.hpp" +#include "../dxd_math.hpp" #include "world_object.hpp" #include "airfield.hpp" diff --git a/src/sim/aircraft.hpp b/src/sim/aircraft.hpp index 838b21c..1f7efd5 100644 --- a/src/sim/aircraft.hpp +++ b/src/sim/aircraft.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../math.hpp" +#include "../dxd_math.hpp" #include "world_object.hpp" namespace dxd::sim diff --git a/src/sim/aircraft_trail.hpp b/src/sim/aircraft_trail.hpp index 2290a1c..d33d1b9 100644 --- a/src/sim/aircraft_trail.hpp +++ b/src/sim/aircraft_trail.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../math.hpp" +#include "../dxd_math.hpp" #include "world_object.hpp" namespace dxd::sim diff --git a/src/sim/airfield.hpp b/src/sim/airfield.hpp index 22607db..46567df 100644 --- a/src/sim/airfield.hpp +++ b/src/sim/airfield.hpp @@ -2,7 +2,7 @@ #include -#include "../math.hpp" +#include "../dxd_math.hpp" #include "world_object.hpp" namespace dxd::sim diff --git a/src/sim/world.hpp b/src/sim/world.hpp index fa9e229..f0eccc6 100644 --- a/src/sim/world.hpp +++ b/src/sim/world.hpp @@ -2,7 +2,7 @@ #include -#include "../math.hpp" +#include "../dxd_math.hpp" #include "world_object.hpp" namespace dxd::sim diff --git a/src/sim/world_object.hpp b/src/sim/world_object.hpp index c43fe1c..44619b7 100644 --- a/src/sim/world_object.hpp +++ b/src/sim/world_object.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../math.hpp" +#include "../dxd_math.hpp" #include "../renderer.hpp" #include "world.hpp"