summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmin Mesbah <mesbahamin@gmail.com>2017-12-18 00:26:53 -0800
committerAmin Mesbah <mesbahamin@gmail.com>2017-12-18 00:26:53 -0800
commit94fca2bb4d579ac4dbb51dc57ee395b29c21a887 (patch)
tree6f517ae95c50e080c77bd365ac1d187472f9bb28
parentb372c824cbe993191425a7315350bde81ba3e67b (diff)
downloadohsp-94fca2bb4d579ac4dbb51dc57ee395b29c21a887.zip
ohsp-94fca2bb4d579ac4dbb51dc57ee395b29c21a887.tar.gz
Move vector stuff to it's own files
-rwxr-xr-xMakefile4
-rw-r--r--src/entity.c69
-rw-r--r--src/entity.h37
-rw-r--r--src/game.h1
-rw-r--r--src/vector.c68
-rw-r--r--src/vector.h42
6 files changed, 115 insertions, 106 deletions
diff --git a/Makefile b/Makefile
index ce7f3eb..097e2d8 100755
--- a/Makefile
+++ b/Makefile
@@ -7,11 +7,11 @@ SDL_LDFLAGS := $(shell sdl2-config --libs)
override CFLAGS += $(SDL_CFLAGS)
-SRC_FILES = platform_sdl.c game.c entity.c
+SRC_FILES = platform_sdl.c game.c entity.c vector.c
SRC = $(addprefix src/, $(SRC_FILES))
EXE_FILE = ohsp
-LIB_FILES = game.c entity.c
+LIB_FILES = game.c entity.c vector.c
LIB = $(addprefix src/, $(LIB_FILES))
LIB_NAME = game.so
diff --git a/src/entity.c b/src/entity.c
index 2cda176..41210d3 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -1,7 +1,5 @@
#include "entity.h"
-
-#include <math.h>
-#include <stdio.h>
+#include "vector.h"
void entity_accelerate(struct Entity *e, struct Vec2d *v)
@@ -11,68 +9,3 @@ void entity_accelerate(struct Entity *e, struct Vec2d *v)
e->velocity.x = new_vec.x;
e->velocity.y = new_vec.y;
}
-
-
-struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1)
-{
- return vec2d_add_c(v0.x, v0.y, v1.x, v1.y);
-}
-
-
-struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1)
-{
- struct Vec2d new_vec =
- {
- .x = x0 + x1,
- .y = y0 + y1
- };
- return new_vec;
-}
-
-
-bool vec2d_equal(struct Vec2d v0, struct Vec2d v1)
-{
- return (v0.x == v1.x) && (v0.y == v1.y);
-}
-
-
-float vec2d_get_angle(float x, float y)
-{
- return 0.5f * M_PI - atan2f(y, x);
-}
-
-
-float vec2d_get_length(float x, float y)
-{
- return hypotf(x, y);
-}
-
-
-struct Vec2d vec2d_negate(struct Vec2d v)
-{
- return vec2d_scale_c(v.x, v.y, -1.0f);
-}
-
-
-struct Vec2d vec2d_normalize(float x, float y)
-{
- float length = vec2d_get_length(x, y);
- return vec2d_scale_c(x, y, 1.0f / length);
-}
-
-
-struct Vec2d vec2d_scale(struct Vec2d v, float s)
-{
- return vec2d_scale_c(v.x, v.y, s);
-}
-
-
-struct Vec2d vec2d_scale_c(float x, float y, float s)
-{
- struct Vec2d new_vec =
- {
- .x = x * s,
- .y = y * s
- };
- return new_vec;
-}
diff --git a/src/entity.h b/src/entity.h
index 81762b2..f15b040 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -1,34 +1,10 @@
#ifndef ENTITY_H
#define ENTITY_H
-#include <stdbool.h>
-#include <stdint.h>
+#include "vector.h"
#define ENTITIES_MAX 100
-#ifndef M_PI
-#define M_PI 3.141592f
-#endif
-
-struct Vec2d
-{
- union
- {
- struct
- {
- float x;
- float y;
- };
- struct
- {
- float w;
- float h;
- };
- float xy[2];
- float d[2];
- };
-};
-
// TODO: get rid of this or rename it
struct Entity
{
@@ -60,16 +36,5 @@ struct Entities
};
void entity_accelerate(struct Entity *e, struct Vec2d *v);
-struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1);
-struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1);
-struct Vec2d vec2d_al_to_xy(struct Vec2d v);
-float vec2d_get_angle(float x, float y);
-bool vec2d_equal(struct Vec2d v0, struct Vec2d v1);
-float vec2d_get_length(float x, float y);
-struct Vec2d vec2d_negate(struct Vec2d v);
-struct Vec2d vec2d_normalize(float x, float y);
-struct Vec2d vec2d_scale(struct Vec2d v, float s);
-struct Vec2d vec2d_scale_c(float x, float y, float s);
-struct Vec2d vec2d_xy_to_al(struct Vec2d v);
#endif
diff --git a/src/game.h b/src/game.h
index 7a74ac2..1d1998c 100644
--- a/src/game.h
+++ b/src/game.h
@@ -1,6 +1,7 @@
#ifndef GAME_H
#include "entity.h"
+#include "vector.h"
#include <stdint.h>
diff --git a/src/vector.c b/src/vector.c
new file mode 100644
index 0000000..baef5fc
--- /dev/null
+++ b/src/vector.c
@@ -0,0 +1,68 @@
+#include "vector.h"
+
+#include <math.h>
+
+
+struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1)
+{
+ return vec2d_add_c(v0.x, v0.y, v1.x, v1.y);
+}
+
+
+struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1)
+{
+ struct Vec2d new_vec =
+ {
+ .x = x0 + x1,
+ .y = y0 + y1
+ };
+ return new_vec;
+}
+
+
+bool vec2d_equal(struct Vec2d v0, struct Vec2d v1)
+{
+ return (v0.x == v1.x) && (v0.y == v1.y);
+}
+
+
+float vec2d_get_angle(float x, float y)
+{
+ return 0.5f * M_PI - atan2f(y, x);
+}
+
+
+float vec2d_get_length(float x, float y)
+{
+ return hypotf(x, y);
+}
+
+
+struct Vec2d vec2d_negate(struct Vec2d v)
+{
+ return vec2d_scale_c(v.x, v.y, -1.0f);
+}
+
+
+struct Vec2d vec2d_normalize(float x, float y)
+{
+ float length = vec2d_get_length(x, y);
+ return vec2d_scale_c(x, y, 1.0f / length);
+}
+
+
+struct Vec2d vec2d_scale(struct Vec2d v, float s)
+{
+ return vec2d_scale_c(v.x, v.y, s);
+}
+
+
+struct Vec2d vec2d_scale_c(float x, float y, float s)
+{
+ struct Vec2d new_vec =
+ {
+ .x = x * s,
+ .y = y * s
+ };
+ return new_vec;
+}
diff --git a/src/vector.h b/src/vector.h
new file mode 100644
index 0000000..f36eb92
--- /dev/null
+++ b/src/vector.h
@@ -0,0 +1,42 @@
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#ifndef M_PI
+#define M_PI 3.141592f
+#endif
+
+struct Vec2d
+{
+ union
+ {
+ struct
+ {
+ float x;
+ float y;
+ };
+ struct
+ {
+ float w;
+ float h;
+ };
+ float xy[2];
+ float d[2];
+ };
+};
+
+struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1);
+struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1);
+struct Vec2d vec2d_al_to_xy(struct Vec2d v);
+float vec2d_get_angle(float x, float y);
+bool vec2d_equal(struct Vec2d v0, struct Vec2d v1);
+float vec2d_get_length(float x, float y);
+struct Vec2d vec2d_negate(struct Vec2d v);
+struct Vec2d vec2d_normalize(float x, float y);
+struct Vec2d vec2d_scale(struct Vec2d v, float s);
+struct Vec2d vec2d_scale_c(float x, float y, float s);
+struct Vec2d vec2d_xy_to_al(struct Vec2d v);
+
+#endif