summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAmin Mesbah <mesbahamin@gmail.com>2017-10-17 23:25:20 -0700
committerAmin Mesbah <mesbahamin@gmail.com>2017-10-17 23:25:20 -0700
commitb751c775026e8333e5f423958ee52c896e5eae71 (patch)
tree9e4333407df851af0924be471552fcd5a87bbf5d /src
parentf4c90e1c003a73ed1363229107e3d3e4c3b2090d (diff)
downloadohsp-b751c775026e8333e5f423958ee52c896e5eae71.zip
ohsp-b751c775026e8333e5f423958ee52c896e5eae71.tar.gz
Move a circle across the screen
Diffstat (limited to 'src')
-rw-r--r--src/entity.c21
-rw-r--r--src/entity.h26
-rw-r--r--src/game.c23
-rw-r--r--src/game.h4
-rw-r--r--src/platform_sdl.c20
5 files changed, 84 insertions, 10 deletions
diff --git a/src/entity.c b/src/entity.c
new file mode 100644
index 0000000..08ba3a9
--- /dev/null
+++ 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
new file mode 100644
index 0000000..198fcd9
--- /dev/null
+++ 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
index fe622a0..e3e5dc0 100644
--- 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
index 4b2d84d..e2a49a1 100644
--- 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
index 18b0ebf..b760ec3 100644
--- 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;
}