ohsp

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

commit 362265b50f915208ac6320cae696beb273202db5
parent c191ef7c25f198bd7fa34d16a176087216995d43
Author: amin <dev@aminmesbah.com>
Date:   Fri, 15 Dec 2017 05:45:54 +0000

Make more terse vector functions

FossilOrigin-Name: f36743d9938a4ad1abd9befbf1e4f8c413728241e517e1da98d5b64b7db12a9b
Diffstat:
Msrc/entity.c | 22+++++++++++++++++-----
Msrc/entity.h | 6++++--
Msrc/game.c | 18+++++-------------
3 files changed, 26 insertions(+), 20 deletions(-)

diff --git 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 @@ -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 @@ -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); }