Add lines

This commit is contained in:
Pyrex 2024-02-25 22:23:43 -08:00
parent be9c443c58
commit 3440669227
5 changed files with 58 additions and 10 deletions

View File

@ -4,12 +4,12 @@
uint32_t game_frame;
int32_t ellipse_x0 = 32;
int32_t ellipse_y0 = 32;
int32_t ellipse_x0 = 32 * 4;
int32_t ellipse_y0 = 32 * 4;
int32_t ellipse_dx0 = 1;
int32_t ellipse_dy0 = 2;
int32_t ellipse_x1 = 96;
int32_t ellipse_y1 = 96;
int32_t ellipse_x1 = 96 * 4;
int32_t ellipse_y1 = 96 * 4;
int32_t ellipse_dx1 = 3;
int32_t ellipse_dy1 = 4;
@ -47,14 +47,14 @@ void game_update() {
game_frame += 4;
ellipse_x0 += ellipse_dx0;
if (ellipse_x0 < 0 || ellipse_x0 > DEVICE_W) { ellipse_dx0 *= -1; }
if (ellipse_x0 < 0 || ellipse_x0 > DEVICE_W * 4) { ellipse_dx0 *= -1; }
ellipse_y0 += ellipse_dy0;
if (ellipse_y0 < 0 || ellipse_y0 > DEVICE_W) { ellipse_dy0 *= -1; }
if (ellipse_y0 < 0 || ellipse_y0 > DEVICE_W * 4) { ellipse_dy0 *= -1; }
ellipse_x1 += ellipse_dx1;
if (ellipse_x1 < 0 || ellipse_x1 > DEVICE_H) { ellipse_dx1 *= -1; }
if (ellipse_x1 < 0 || ellipse_x1 > DEVICE_H * 4) { ellipse_dx1 *= -1; }
ellipse_y1 += ellipse_dy1;
if (ellipse_y1 < 0 || ellipse_y1 > DEVICE_H) { ellipse_dy1 *= -1; }
if (ellipse_y1 < 0 || ellipse_y1 > DEVICE_H * 4) { ellipse_dy1 *= -1; }
}
void game_draw() {
@ -79,6 +79,14 @@ void game_draw() {
sys_pixel_set(i, 0, device_buttons[i] ? 0x00 : 0xff);
}
sys_oval_draw_ext(ellipse_x0, ellipse_y0, ellipse_x1, ellipse_y1, 10, true);
sys_oval_draw_ext(ellipse_x0, ellipse_y0, ellipse_x1, ellipse_y1, 245, false);
int x0 = ellipse_x0 / 4;
int y0 = ellipse_y0 / 4;
int x1 = ellipse_x1 / 4;
int y1 = ellipse_y1 / 4;
sys_oval_draw_ext(x0, y0, x1, y1, 10, true);
sys_oval_draw_ext(x0, y0, x1, y1, 248, false);
sys_circ_draw_ext(x0, y0, 4, 252, true);
sys_circ_draw_ext(x0, y0, 6, 248, false);
sys_circ_draw_ext(x0, y0, 8, 244, false);
sys_line_draw(x0, y0, x1, y1, 254);
}

View File

@ -6,4 +6,15 @@ sys_i32 sys_max_i32(sys_i32 x, sys_i32 y) {
sys_i32 sys_min_i32(sys_i32 x, sys_i32 y) {
return x < y ? x : y;
}
sys_i32 sys_abs_i32(sys_i32 x) {
if (x < 0) { return -x; }
return x;
}
sys_i32 sys_sgn_i32(sys_i32 x) {
if (x < 0) { return -1; }
if (x > 0) { return 1; }
return 0;
}

View File

@ -11,6 +11,8 @@ sys_screen_color sys_make_screen_color(uint8_t r, uint8_t g, uint8_t b);
sys_i32 sys_max_i32(sys_i32 x, sys_i32 y);
sys_i32 sys_min_i32(sys_i32 x, sys_i32 y);
sys_i32 sys_abs_i32(sys_i32 x);
sys_i32 sys_sgn_i32(sys_i32 x);
#define SYS_COLOR_TRANSPARENT 255
#define SYS_COLOR_N 256

View File

@ -121,9 +121,12 @@ void sys_oval_fill(
void sys_oval_draw_ext(
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c, bool fill
) {
assert(sys_get_initialized());
if (x0 == x1 || y0 == y1) { return; }
if (x0 > x1) { sys_i32 tmp = x0; x0 = x1; x1 = tmp; }
if (y0 > y1) { sys_i32 tmp = y0; y0 = y1; y1 = tmp; }
// alois' algorithm for this implies the bounds are inclusive
x1 -= 1; y1 -= 1;
@ -162,6 +165,28 @@ void sys_oval_draw_ext(
}
}
void sys_line_draw(
sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c
) {
assert(sys_get_initialized());
if (x0 == x1 || y0 == y1) { return; }
sys_i32 dx = sys_abs_i32(x1 - x0);
sys_i32 sx = sys_sgn_i32(x1 - x0);
sys_i32 dy = -sys_abs_i32(y1 - y0);
sys_i32 sy = sys_sgn_i32(y1 - y0);
sys_i32 err = dx + dy;
while (true) {
if (x0 == x1 || y0 == y1) { return; }
sys_pixel_internal_set(x0, y0, c);
sys_i32 e2 = 2 * err;
if (e2 >= dy) { err += dy; x0 += sx; }
if (e2 <= dx) { err += dx; y0 += sy; }
}
}
void sys_spal_set(sys_color c0, sys_screen_color rc1) {
assert(sys_get_initialized());

View File

@ -97,6 +97,8 @@ void sys_oval_draw_ext(
/**
* Draw a line from x0 to y0.
*
* The point `(x1, y1)` is not drawn. (because the line is a half-open interval)
*/
void sys_line_draw(sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, sys_color c);