summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmin Mesbah <mesbahamin@gmail.com>2017-12-14 21:44:50 -0800
committerAmin Mesbah <mesbahamin@gmail.com>2017-12-14 21:45:55 -0800
commit6ccff2e66cddee12ca3c85a2923034887a2acad9 (patch)
treeab30a43ec88fbf900fc24acaee5a64949fb02f87
parent211a7738907aff561a01a2a35b262f14f867680f (diff)
downloadohsp-6ccff2e66cddee12ca3c85a2923034887a2acad9.zip
ohsp-6ccff2e66cddee12ca3c85a2923034887a2acad9.tar.gz
Make more terse vector functions
-rw-r--r--src/entity.c22
-rw-r--r--src/entity.h6
-rw-r--r--src/game.c18
3 files changed, 26 insertions, 20 deletions
diff --git a/src/entity.c b/src/entity.c
index f8b5de5..2cda176 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -6,14 +6,20 @@
void entity_accelerate(struct Entity *e, struct Vec2d *v)
{
- struct Vec2d new_vec = vec2d_add(e->velocity.x, e->velocity.y, v->x, v->y);
+ struct Vec2d new_vec = vec2d_add(e->velocity, *v);
//printf("(%f, %f)\n", new_vec.x, new_vec.y);
e->velocity.x = new_vec.x;
e->velocity.y = new_vec.y;
}
-struct Vec2d vec2d_add(float x0, float y0, float x1, float y1)
+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 =
{
@@ -44,18 +50,24 @@ float vec2d_get_length(float x, float y)
struct Vec2d vec2d_negate(struct Vec2d v)
{
- return vec2d_scale(v.x, v.y, -1.0f);
+ 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(x, y, 1.0f / length);
+ 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(float x, float y, float s)
+struct Vec2d vec2d_scale_c(float x, float y, float s)
{
struct Vec2d new_vec =
{
diff --git a/src/entity.h b/src/entity.h
index 479f4e0..25ac435 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -37,14 +37,16 @@ struct Entity
};
void entity_accelerate(struct Entity *e, struct Vec2d *v);
-struct Vec2d vec2d_add(float x0, float y0, float x1, float y1);
+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(float x, float y, float s);
+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.c b/src/game.c
index 8412d83..127b401 100644
--- a/src/game.c
+++ b/src/game.c
@@ -71,16 +71,8 @@ void game_update(struct GameState *game_state, struct GameControllerInput game_i
game_state->thrust_vector02.y = game_input.right_stick_y;
//printf("(rx: %f, ry: %f, thrust_x: %f, thrust_y: %f)\n", game_input.right_stick_x, game_input.right_stick_y, game_state->thrust_vector02.x, game_state->thrust_vector02.y);
- game_state->thrust_vector_sum = vec2d_add(
- game_state->thrust_vector01.x,
- game_state->thrust_vector01.y,
- game_state->thrust_vector02.x,
- game_state->thrust_vector02.y);
-
- game_state->thrust_vector_sum = vec2d_scale(
- game_state->thrust_vector_sum.x,
- game_state->thrust_vector_sum.y,
- 0.01f);
+ game_state->thrust_vector_sum = vec2d_add(game_state->thrust_vector01, game_state->thrust_vector02);
+ game_state->thrust_vector_sum = vec2d_scale(game_state->thrust_vector_sum, 0.01f);
//printf("(total_thrust_x: %f, total_thrust_y: %f)\n", game_state->thrust_vector_sum.x, game_state->thrust_vector_sum.y);
struct Entity *player = &game_state->player;
@@ -106,7 +98,7 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam
game_render_vector(buffer, &game_state->thrust_vector02, player.position.x, player.position.y, 5, 0xFF0000);
game_render_vector(buffer, &game_state->thrust_vector_sum, player.position.x, player.position.y, 5, 0xFF00FF);
- struct Vec2d player_vec = vec2d_scale(player.velocity.x, player.velocity.y, 100);
+ struct Vec2d player_vec = vec2d_scale(player.velocity, 100);
game_render_circle(buffer,
player.position.x + player_vec.x,
player.position.y + player_vec.y,
@@ -320,8 +312,8 @@ void game_render_line(struct OffscreenBuffer *buffer, float x0, float y0, float
void game_render_vector(struct OffscreenBuffer *buffer, struct Vec2d *v, float x0, float y0, float radius, uint32_t color)
{
- struct Vec2d vec = vec2d_scale(v->x, v->y, 100);
- vec = vec2d_add(x0, y0, vec.x, vec.y);
+ struct Vec2d vec = vec2d_scale(*v, 100);
+ vec = vec2d_add_c(x0, y0, vec.x, vec.y);
game_render_line(buffer, x0, y0, vec.x, vec.y, color);
game_render_circle(buffer, vec.x, vec.y, radius, color);
}