shorten draw_line, general cleanup

This commit is contained in:
opfez 2021-11-06 12:07:50 +01:00
parent 4c892bc967
commit 04df9ab0d5
1 changed files with 33 additions and 53 deletions

86
main.c
View File

@ -92,7 +92,11 @@ new_camera(double range, vec3 pos, vec3 target, vec3 up)
}
mesh
new_mesh(uint32_t vertex_num, uint32_t tri_num, uint32_t index_num, vec3 vertices[], uint32_t indices[])
new_mesh(uint32_t vertex_num,
uint32_t tri_num,
uint32_t index_num,
vec3 vertices[],
uint32_t indices[])
{
mesh ret;
@ -204,7 +208,6 @@ render(sdl_state state)
SDL_RenderPresent(state.renderer);
}
void
plot_rgb(rgb canvas[], int32_t x, int32_t y, rgb colour)
{
@ -241,57 +244,31 @@ draw_line(rgb canvas[], rgb c, vec2 p0, vec2 p1)
swap(y0, y1);
}
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int eps = 0;
int32_t dx = abs(x1 - x0);
int32_t dy = abs(y1 - y0);
int32_t s = y1 > y0 ? 1 : -1;
int32_t eps = 0;
if (y1 > y0) {
if (dy < dx) {
int y = y0;
for (int x = x0; x <= x1; x++) {
plot_rgb(canvas, x, y, c);
eps += dy;
if ((eps * 2) >= dx) {
y++;
eps -= dx;
}
}
}
else {
int x = x0;
for (int y = y0; y <= y1; y++) {
plot_rgb(canvas, x, y, c);
eps += dx;
if ((eps * 2) >= dy) {
x++;
eps -= dy;
}
int32_t y = y0;
int32_t x = x0;
if (dy < dx) {
for (; x <= x1; x++) {
plot_rgb(canvas, x, y, c);
eps += dy;
if ((eps * 2) >= dx) {
y += s;
eps -= dx;
}
}
}
else {
if (dy < dx) {
int y = y0;
for (int x = x0; x <= x1; x++) {
plot_rgb(canvas, x, y, c);
eps += dy;
if ((eps * 2) >= dx) {
y--;
eps -= dx;
}
}
}
else {
swap(x0, x1);
swap(y0, y1);
int x = x0;
for (int y = y0; y <= y1; y++) {
plot_rgb(canvas, x, y, c);
eps += dx;
if ((eps * 2) >= dy) {
x--;
eps -= dy;
}
for (; y <= y1; y++) {
plot_rgb(canvas, x, y, c);
eps += dx;
if ((eps * 2) >= dy) {
x += s;
eps -= dy;
}
}
}
@ -300,10 +277,9 @@ draw_line(rgb canvas[], rgb c, vec2 p0, vec2 p1)
void
draw_horizontal_line(rgb canvas[], rgb c, int32_t x0, int32_t x1, int32_t y)
{
if (y < 0) return;
if (y < 0 || y >= H) return;
if (x0 < 0) x0 = 0;
if (x1 >= W) x1 = W - 1;
if (y >= H) return;
for (int32_t x = x0; x <= x1; x++)
canvas[x + y * W] = c;
@ -454,7 +430,7 @@ fill_triangle(rgb canvas[], rgb c, triangle2 tri)
}
else {
vec2 trid = {
(tri.a.x + (tri.b.y - tri.a.y) / (tri.c.y - tri.a.y) * (tri.c.x - tri.a.x)),
(tri.a.x + (tri.b.y-tri.a.y) / (tri.c.y-tri.a.y) * (tri.c.x-tri.a.x)),
tri.b.y};
if (tri.a.y > tri.b.y) {
@ -503,7 +479,10 @@ screenshot(rgb canvas[])
{
time_t t = time(NULL);
char output[64];
strftime(output, sizeof(output), "screenshot-%Y%m%d-%H%M%S.ppm", localtime(&t));
strftime(output,
sizeof(output),
"screenshot-%Y%m%d-%H%M%S.ppm",
localtime(&t));
FILE *out = fopen(output, "w");
fprintf(out, "P6\n%d %d\n255\n", W, H);
for (int y = 0; y < H; y++) {
@ -672,7 +651,8 @@ main(void)
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0;
elapsed = (SDL_GetPerformanceCounter() - start) /
(double)SDL_GetPerformanceFrequency() * 1000.0;
SDL_Delay(clamp(16.666f - elapsed, 0, 1000));
t++;