ohsp

Prototype for a game with dual thruster controls.
git clone git://git.amin.space/ohsp.git
Log | Files | Refs | LICENSE

commit f067aef70930873d14de3a217d86dba28b4a8c54
parent 9c351ad49f9342d7393aa8dc8e523db6136da2de
Author: amin <dev@aminmesbah.com>
Date:   Mon, 18 Dec 2017 08:26:52 +0000

Move vector stuff to it's own files

FossilOrigin-Name: 423eeb188d88a4f53147748fdc76816bc10bd8106e548df0ee08cb9850c73139
Diffstat:
MMakefile | 4++--
Msrc/entity.c | 69+--------------------------------------------------------------------
Msrc/entity.h | 37+------------------------------------
Msrc/game.h | 1+
Asrc/vector.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/vector.h | 42++++++++++++++++++++++++++++++++++++++++++
6 files changed, 115 insertions(+), 106 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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