From e236ce10d4b7b38890a533e7e520f661b09c30a8 Mon Sep 17 00:00:00 2001 From: kdx Date: Thu, 23 Mar 2023 22:42:37 +0100 Subject: [PATCH] optimize spike drawing --- src/game.c | 4 +++- src/game.h | 4 +++- src/rotrect.c | 28 ++++++++++++++++++++++------ src/rotrect.h | 4 ++++ src/spike.c | 4 ++-- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/game.c b/src/game.c index 55cb303..95522b8 100644 --- a/src/game.c +++ b/src/game.c @@ -3,6 +3,7 @@ #include "map.h" #include "player.h" #include "cfg.h" +#include "rotrect.h" #include "spike.h" #include @@ -24,7 +25,8 @@ void game_update(Game *this) { extern double tick; - this->spike_angle = tick / 16; + this->spike_rect = rotrect_create(10, 10, tick / 16); + this->spike_irect = rotrect_create(10, 10, -tick / 16); if (this->queue_restart_scene > 0) { if (--this->queue_restart_scene == 0) game_restart_scene(this); diff --git a/src/game.h b/src/game.h index 2127fb9..aeaeb48 100644 --- a/src/game.h +++ b/src/game.h @@ -1,5 +1,6 @@ #pragma once #include "entity.h" +#include "rotrect.h" enum { MAX_ENTITIES = 128 }; @@ -9,7 +10,8 @@ typedef struct Game { int queue_restart_scene; int player_dir; int show_ui; - double spike_angle; + Rect spike_rect; + Rect spike_irect; Entity entities[MAX_ENTITIES]; } Game; diff --git a/src/rotrect.c b/src/rotrect.c index 5abae13..6721d10 100644 --- a/src/rotrect.c +++ b/src/rotrect.c @@ -15,8 +15,8 @@ rotate(double *x, double *y, double angle) *y = ox * s + oy * c; } -void -rotrect(double x, double y, double width, double height, double angle) +Rect +rotrect_create(double width, double height, double angle) { double xs[4] = { width / 2.0, @@ -30,14 +30,30 @@ rotrect(double x, double y, double width, double height, double angle) height / 2.0, -height / 2.0, }; - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) rotate(&xs[i], &ys[i], angle); - xs[i] = round(xs[i] + x); - ys[i] = round(ys[i] + y); - } + return (Rect){ xs[0], ys[0], xs[1], ys[1], xs[2], ys[2], xs[3], ys[3] }; +} + +void +rotrect_draw(Rect rect, double x, double y) +{ + const int xs[4] = { + round(rect.x0 + x), round(rect.x1) + x, + round(rect.x2 + x), round(rect.x3) + x + }; + const int ys[4] = { + round(rect.y0 + y), round(rect.y1) + y, + round(rect.y2 + y), round(rect.y3) + y, + }; LZY_DrawLine(xs[0], ys[0], xs[1], ys[1]); LZY_DrawLine(xs[1], ys[1], xs[2], ys[2]); LZY_DrawLine(xs[2], ys[2], xs[3], ys[3]); LZY_DrawLine(xs[3], ys[3], xs[0], ys[0]); } +void +rotrect(double x, double y, double width, double height, double angle) +{ + rotrect_draw(rotrect_create(width, height, angle), x, y); +} diff --git a/src/rotrect.h b/src/rotrect.h index db0f426..f707d9c 100644 --- a/src/rotrect.h +++ b/src/rotrect.h @@ -1,3 +1,7 @@ #pragma once +typedef struct { double x0, y0, x1, y1, x2, y2, x3, y3; } Rect; + +Rect rotrect_create(double width, double height, double angle); +void rotrect_draw(Rect rect, double x, double y); void rotrect(double x, double y, double width, double height, double angle); diff --git a/src/spike.c b/src/spike.c index f09537f..5dcd433 100644 --- a/src/spike.c +++ b/src/spike.c @@ -7,8 +7,8 @@ IMPL_UPDATE() { } IMPL_END IMPL_DRAW() { - rotrect(this->pos[0], this->pos[1], 10, 10, g->spike_angle); - rotrect(this->pos[0], this->pos[1], 10, 10, -g->spike_angle); + rotrect_draw(g->spike_rect, this->pos[0], this->pos[1]); + rotrect_draw(g->spike_irect, this->pos[0], this->pos[1]); } IMPL_END IMPL_INIT(spike) {