summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAmin Mesbah <mesbahamin@gmail.com>2017-10-26 21:33:20 -0700
committerAmin Mesbah <mesbahamin@gmail.com>2017-10-26 21:33:20 -0700
commite463d95f6c4c4bb89062585077adf3fe127530fb (patch)
tree2f86639b7de063d83db9d15259e08ceaf4a992e0 /src
parent753cd63c53a67a1d45203ca7e9ae8c49747c3328 (diff)
downloadohsp-e463d95f6c4c4bb89062585077adf3fe127530fb.zip
ohsp-e463d95f6c4c4bb89062585077adf3fe127530fb.tar.gz
Add line and vector rendering functions
Diffstat (limited to 'src')
-rw-r--r--src/game.c70
-rw-r--r--src/game.h2
2 files changed, 55 insertions, 17 deletions
diff --git a/src/game.c b/src/game.c
index fdcce68..83f89f1 100644
--- a/src/game.c
+++ b/src/game.c
@@ -19,7 +19,7 @@ void game_init(struct GameState *game_state, int field_width, int field_height)
game_state->player.angle = 0;
game_state->player.speed = 0;
game_state->player.mass = 10;
- game_state->player.size = 100;
+ game_state->player.size = 30;
game_state->player.x = field_width / 2;
game_state->player.y = field_height / 2;
game_state->player.color = 0x888888;
@@ -82,26 +82,14 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam
}
struct Entity player = game_state->player;
game_render_circle(buffer, player.x, player.y, player.size, player.color);
- game_render_circle(buffer,
- player.x + 100 * (game_state->thrust_vector01.length * cos(game_state->thrust_vector01.angle)),
- player.y + 100 * (game_state->thrust_vector01.length * sin(game_state->thrust_vector01.angle)),
- 10,
- 0x0000FF);
- game_render_circle(buffer,
- player.x + 100 * (game_state->thrust_vector02.length * cos(game_state->thrust_vector02.angle)),
- player.y + 100 * (game_state->thrust_vector02.length * sin(game_state->thrust_vector02.angle)),
- 10,
- 0xFF0000);
- game_render_circle(buffer,
- player.x + 100 * (game_state->thrust_vector_sum.length * cos(game_state->thrust_vector_sum.angle)),
- player.y + 100 * (game_state->thrust_vector_sum.length * sin(game_state->thrust_vector_sum.angle)),
- 10,
- 0xFF00FF);
+ game_render_vector(buffer, &game_state->thrust_vector01, player.x, player.y, 5, 0x0000FF);
+ game_render_vector(buffer, &game_state->thrust_vector02, player.x, player.y, 5, 0xFF0000);
+ game_render_vector(buffer, &game_state->thrust_vector_sum, player.x, player.y, 5, 0xFF00FF);
game_render_circle(buffer,
player.x + 100 * (player.speed * cos(player.angle)),
player.y + 100 * (player.speed * sin(player.angle)),
- 10,
+ 5,
0x00FF00);
}
@@ -121,6 +109,54 @@ void game_render_circle(struct OffscreenBuffer *buffer, float center_x, float ce
}
+void game_render_line(struct OffscreenBuffer *buffer, float x0, float y0, float x1, float y1, uint32_t color)
+{
+ float delta_x = x1 - x0;
+ float delta_y = y1 - y0;
+
+ if (0 == (int)delta_x)
+ {
+ for (int y = y0; y <= y1; ++y)
+ {
+ game_set_pixel(buffer, x0, y, color);
+ }
+ }
+ else if (0 == (int)delta_y)
+ {
+ for (int x = x0; x <= x1; ++x)
+ {
+ game_set_pixel(buffer, x, y0, color);
+ }
+ }
+ else
+ {
+ // Bresenham's line algorithm
+ float delta_err = fabsf(delta_y / delta_x);
+ float error = 0.0;
+ int y = y0;
+ for (int x = x0; x <= x1; ++x)
+ {
+ game_set_pixel(buffer, x, y, color);
+ error += delta_err;
+ if (error >= 0.5)
+ {
+ y += 1;
+ error -= 1.0;
+ }
+ }
+ }
+}
+
+
+void game_render_vector(struct OffscreenBuffer *buffer, struct Vec2d *v, float x0, float y0, float radius, uint32_t color)
+{
+ float x1 = x0 + 100 * (v->length * cos(v->angle));
+ float y1 = y0 + 100 * (v->length * sin(v->angle));
+ game_render_line(buffer, x0, y0, x1, y1, color);
+ game_render_circle(buffer, x1, y1, radius, color);
+}
+
+
void game_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color)
{
if (!buffer)
diff --git a/src/game.h b/src/game.h
index 2cec081..335de09 100644
--- a/src/game.h
+++ b/src/game.h
@@ -67,6 +67,8 @@ void game_init(struct GameState *game_state, int field_width, int field_height);
void game_update(struct GameState *game_state, struct GameControllerInput game_input, 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);
+void game_render_line(struct OffscreenBuffer *buffer, float x0, float y0, float x1, float y1, uint32_t color);
+void game_render_vector(struct OffscreenBuffer *buffer, struct Vec2d *v, float x0, float y0, 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);