commit 003bc6152dd386cfbe4d0fad42ea64827c46797d
parent e6ab15e4696e60031ceab70b7762b2b8f44341d8
Author: amin <dev@aminmesbah.com>
Date: Fri, 27 Oct 2017 04:33:20 +0000
Add line and vector rendering functions
FossilOrigin-Name: 98807372c52d4f31e83336f0996ad2897c85d6d8999a61e6893a4d01feb76283
Diffstat:
M | src/game.c | | | 70 | +++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
M | src/game.h | | | 2 | ++ |
2 files changed, 55 insertions(+), 17 deletions(-)
diff --git 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
@@ -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);