crocparty/sys/sys_graphics.c

126 lines
2.8 KiB
C

#include <assert.h>
#include "sys.h"
#include "device/device.h"
sys_i32 sys_cam_dx, sys_cam_dy;
sys_i32 sys_clip_x0, sys_clip_y0, sys_clip_x1, sys_clip_y1;
sys_color sys_dpal[256];
// primitives (forward declare)
void sys_pixel_internal_set(sys_i32 x, sys_i32 y, sys_color c);
// == public ==
void sys_clip_set(sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1) {
assert(sys_get_initialized());
sys_clip_set_ext(x0, y0, x1, y1, false);
}
void sys_clip_reset() {
assert(sys_get_initialized());
sys_clip_set(0, 0, DEVICE_W, DEVICE_H);
}
void sys_clip_set_ext(sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1, bool clip_previous) {
assert(sys_get_initialized());
sys_clip_x0 = sys_max_i32(x0, clip_previous ? sys_clip_x0 : 0);
sys_clip_y0 = sys_max_i32(y0, clip_previous ? sys_clip_y0 : 0);
sys_clip_x1 = sys_min_i32(x1, clip_previous ? sys_clip_x1 : DEVICE_W);
sys_clip_y1 = sys_min_i32(y1, clip_previous ? sys_clip_y1 : DEVICE_H);
}
void sys_pixel_set(sys_i32 x, sys_i32 y, sys_color c) {
assert(sys_get_initialized());
x += sys_cam_dx;
y += sys_cam_dy;
sys_pixel_internal_set(x, y, c);
}
sys_color sys_pixel_get(sys_i32 x, sys_i32 y) {
assert(sys_get_initialized());
x += sys_cam_dx;
y += sys_cam_dy;
if (x < 0 || y < 0 || x >= DEVICE_W || y >= DEVICE_H) {
return SYS_COLOR_TRANSPARENT;
}
return device_pixels[y][x];
}
void sys_cls(sys_color c) {
assert(sys_get_initialized());
for (int x = 0; x < DEVICE_W; x++) {
for (int y = 0; y < DEVICE_W; y++) {
sys_pixel_internal_set(x, y, c);
}
}
}
void sys_camera_set(sys_i32 x, sys_i32 y) {
assert(sys_get_initialized());
sys_cam_dx = -x;
sys_cam_dy = -y;
}
void sys_camera_reset() {
assert(sys_get_initialized());
sys_camera_set(0, 0);
}
// TODO: Continue from sys_circ_draw_ext
void sys_spal_set(sys_color c0, sys_screen_color rc1) {
assert(sys_get_initialized());
device_palette[c0] = rc1;
}
void sys_spal_reset() {
assert(sys_get_initialized());
for (int i = 0; i < SYS_COLOR_N; i++) {
sys_spal_set(i, 0x000000ff);
}
}
void sys_dpal_set(sys_color c0, sys_color c1) {
assert(sys_get_initialized());
sys_dpal[c0] = c1;
}
void sys_dpal_reset() {
assert(sys_get_initialized());
for (int i = 0; i < SYS_COLOR_N; i++) {
sys_dpal_set(i, i);
}
}
// == internal primitives ==
void sys_pixel_internal_set(sys_i32 x, sys_i32 y, sys_color c) {
sys_color realc = sys_dpal[c];
if (realc == SYS_COLOR_TRANSPARENT) { return; }
if (
x < sys_clip_x0 || y < sys_clip_y0 ||
x >= sys_clip_x1 || y >= sys_clip_y1
) {
return;
}
assert(x >= 0);
assert(y >= 0);
assert(x < DEVICE_W);
assert(y < DEVICE_H);
device_pixels[y][x] = realc;
}