ohsp

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

commit 898cb12bb25b79536937b2db49d3469d3ba3203e
parent a3076ec14c1e8c79ee73fec8e853be796b9f47df
Author: amin <dev@aminmesbah.com>
Date:   Wed, 18 Oct 2017 06:25:20 +0000

Move a circle across the screen

FossilOrigin-Name: 956a9d02b3ee5bb1c3a052fce2cf8c252ac293502211a95db16266d2bc30060f
Diffstat:
Asrc/entity.c | 21+++++++++++++++++++++
Asrc/entity.h | 26++++++++++++++++++++++++++
Msrc/game.c | 23+++++++++++++++++++++++
Msrc/game.h | 4++++
Msrc/platform_sdl.c | 20++++++++++----------
5 files changed, 84 insertions(+), 10 deletions(-)

diff --git a/src/entity.c b/src/entity.c @@ -0,0 +1,21 @@ +#include "entity.h" + + +struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2) +{ + float x = sinf(angle1) * length1 + sinf(angle2) * length2; + float y = cosf(angle1) * length1 + cosf(angle2) * length2; + + struct Vec2d new_vec; + new_vec->angle = 0.5 * M_PI - atan2f(y, x); + new_vec->length = hypotf(x, y); + return new_vec; +} + + +void entity_accelerate(struct Entity *e, float angle, float acceleration) +{ + struct Vec2d new_vec = vec2d_add(e->angle, e->speed, angle, acceleration); + e->angle = new_vec->angle; + e->speed = new_vec->length; +} diff --git a/src/entity.h b/src/entity.h @@ -0,0 +1,26 @@ +#ifndef ENTITY_H +#define ENTITY_H + +#include <stdint.h> + +struct Entity +{ + float angle; + float speed; + float mass; + float size; + float x; + float y; + uint32_t color; +}; + +struct Vec2d +{ + float angle; + float length; +}; + +struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2); +void entity_accelerate(struct Entity *e, float angle, float acceleration); + +#endif diff --git a/src/game.c b/src/game.c @@ -16,6 +16,13 @@ void game_init(struct GameState *game_state, int field_width, int field_height) game_state->view.dx = 0; game_state->view.dy = 0; game_state->view.zoom = 1; + game_state->player.angle = 0; + game_state->player.speed = 0; + game_state->player.mass = 10; + game_state->player.size = 100; + game_state->player.x = field_width / 2; + game_state->player.y = field_height / 2; + game_state->player.color = 0xFFFFFF; } @@ -36,9 +43,25 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam // TODO: handle invalid pointer error return; } + struct Entity player = game_state->player; + game_render_circle(buffer, player.x, player.y, player.size, player.color); } +void game_render_circle(struct OffscreenBuffer *buffer, float center_x, float center_y, float radius, uint32_t color) +{ + for(int y = -radius; y <= radius; y++) + { + for(int x = -radius; x <= radius; x++) + { + if(x * x + y * y <= radius * radius) + { + game_set_pixel(buffer, center_x + x, center_y + y, color); + } + } + } +} + float game_calc_render_offset(float zoom, float delta, float pos, float center) { return ((1 - zoom) * center) + (pos + delta) * zoom; diff --git a/src/game.h b/src/game.h @@ -1,5 +1,7 @@ #ifndef GAME_H +#include "entity.h" + #include <stdint.h> #define TITLE "Obsolete Human Space Pilot" @@ -36,6 +38,7 @@ struct GameView struct GameState { + struct Entity player; struct GameView view; }; @@ -51,6 +54,7 @@ struct OffscreenBuffer void game_init(struct GameState *game_state, int field_width, int field_height); void game_update(struct GameState *game_state, int field_width, int field_height); void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *game_state); +void game_render_circle(struct OffscreenBuffer *buffer, float x, float y, float radius, uint32_t color); float game_calc_render_offset(float zoom, float delta, float pos, float center); void game_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color); void game_cleanup(struct GameState *game_state); diff --git a/src/platform_sdl.c b/src/platform_sdl.c @@ -190,35 +190,35 @@ int main(int argc, char *argv[]) // TODO: move this to a function if (keystate[SDL_SCANCODE_A] || keystate[SDL_SCANCODE_H]) { - game_state.view.dx += 5 / game_state.view.zoom; + game_state.player.x -= 5 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_D] || keystate[SDL_SCANCODE_L]) { - game_state.view.dx -= 5 / game_state.view.zoom; + game_state.player.x += 5 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_W] || keystate[SDL_SCANCODE_K]) { - game_state.view.dy += 5 / game_state.view.zoom; + game_state.player.y -= 5 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_S] || keystate[SDL_SCANCODE_J]) { - game_state.view.dy -= 5 / game_state.view.zoom; + game_state.player.y += 5 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_LEFT]) { - game_state.view.dx += 1 / game_state.view.zoom; + game_state.player.x -= 1 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_RIGHT]) { - game_state.view.dx -= 1 / game_state.view.zoom; + game_state.player.x += 1 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_UP]) { - game_state.view.dy += 1 / game_state.view.zoom; + game_state.player.y -= 1 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_DOWN]) { - game_state.view.dy -= 1 / game_state.view.zoom; + game_state.player.y += 1 / game_state.view.zoom; } if (keystate[SDL_SCANCODE_EQUALS]) { @@ -234,8 +234,8 @@ int main(int argc, char *argv[]) } if (keystate[SDL_SCANCODE_HOME]) { - game_state.view.dx = 0; - game_state.view.dy = 0; + game_state.player.x = 0; + game_state.player.y = 0; game_state.view.zoom = 1; }