optimize spike drawing

This commit is contained in:
kdx 2023-03-23 22:42:37 +01:00
parent 303393050d
commit e236ce10d4
5 changed files with 34 additions and 10 deletions

View File

@ -3,6 +3,7 @@
#include "map.h"
#include "player.h"
#include "cfg.h"
#include "rotrect.h"
#include "spike.h"
#include <string.h>
@ -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);

View File

@ -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;

View File

@ -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);
}

View File

@ -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);

View File

@ -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) {