Add font loader

This commit is contained in:
2024-02-26 12:42:07 -08:00
parent e05f050e7d
commit cb44cab4f8
16 changed files with 1584 additions and 190 deletions

View File

@ -1,7 +1,22 @@
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
cc_library(
name = "sys",
srcs = glob(["*.c"]),
hdrs = glob(["*.h"]),
srcs = glob(["*.c"]) + [":fonts/sys_font_small.c"],
hdrs = glob(["*.h"]) + [":fonts/sys_font_small.h"],
visibility = ["//visibility:public"],
deps = ["//device:device"]
deps = ["//device:device"],
)
run_binary(
name = "sys_font_small",
args = [
"sys_font_small",
"256",
"$(location :fonts/sys_font_small.png)",
"$(location :fonts/sys_font_small.c)"
],
srcs = [":fonts/sys_font_small.png"],
outs = [":fonts/sys_font_small.c"],
tool = "//pytools:font",
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,8 @@
#ifndef CROCPARTY_SYS_FONT_SMALL_H
#define CROCPARTY_SYS_FONT_SMALL_H
#include "sys/sys.h"
extern sys_glyph sys_font_small[256];
#endif // CROCPARTY_SYS_FONT_SMALL_H

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -3,6 +3,7 @@
#include "sys_data.h"
#include "sys_graphics.h"
#include "fonts/sys_font_small.h"
/**
* Initialize sys.

View File

@ -6,6 +6,7 @@
typedef int32_t sys_i32;
typedef uint8_t sys_color;
typedef uint32_t sys_screen_color;
typedef uint64_t sys_glyph;
sys_screen_color sys_make_screen_color(uint8_t r, uint8_t g, uint8_t b);

View File

@ -11,6 +11,7 @@ void sys_pixel_internal_set(sys_i32 x, sys_i32 y, sys_color c);
void sys_scanline_internal_set(
sys_i32 x0, sys_i32 x1, sys_i32 y, sys_color c, bool fill
);
void sys_glyph_draw(sys_i32 x, sys_i32 y, sys_glyph g, sys_color c);
// == public ==
void sys_clip_set(sys_i32 x0, sys_i32 y0, sys_i32 x1, sys_i32 y1) {
@ -55,6 +56,18 @@ sys_color sys_pixel_get(sys_i32 x, sys_i32 y) {
return device_pixels[y][x];
}
void sys_print(char* str, sys_i32 x, sys_i32 y, sys_color col) {
sys_i32 x_orig = x;
for (sys_i32 i = 0;; i++) {
uint8_t c = str[i];
if (c == 0) { break; }
if (c == '\n') { x = x_orig; y += 8; continue; }
if (c == '\r') { x = x_orig; continue; }
sys_glyph_draw(x, y, sys_font_small[c], col);
x += 8;
}
}
void sys_cls(sys_color c) {
assert(sys_get_initialized());
@ -250,4 +263,17 @@ void sys_scanline_internal_set(
sys_pixel_internal_set(x0, y, c);
sys_pixel_internal_set(x1, y, c);
}
}
void sys_glyph_draw(sys_i32 x, sys_i32 y, sys_glyph g, sys_color c) {
// iterate through the bits of the glyph, and draw the character
// if that bit is set
for (int py = 0; py < 8; py++) {
for (int px = 0; px < 8; px++) {
uint64_t mask = ((uint64_t) 1) << (uint64_t) (py * 8 + px);
if ((g & mask) != 0) {
sys_pixel_internal_set(x + px, y + py, c);
}
}
}
}

View File

@ -54,7 +54,10 @@ sys_color sys_pixel_get(
// TODO: SSET/SGET
// TODO: FGET/FSET
// TODO: PRINT
/**
* Print a string `str` in the color `col`
*/
void sys_print(char* str, sys_i32 x, sys_i32 y, sys_color col);
// TODO: CURSOR? COLOR?
/**
@ -83,7 +86,7 @@ void sys_camera_reset();
* This is a special case of sys_circ_oval_draw_ext.
*/
void sys_circ_draw(sys_i32 x, sys_i32 y, sys_i32 r, sys_color c);
void sys_circ_fill(sys_i32 x, sys_i32 y, sys_i32 r);
void sys_circ_fill(sys_i32 x, sys_i32 y, sys_i32 r, sys_color c);
void sys_circ_draw_ext(sys_i32 x, sys_i32 y, sys_i32 r, sys_color c, bool fill);
/**