Use Jinja2, start to support sprites

This commit is contained in:
Pyrex 2024-02-26 16:01:47 -08:00
parent c478ee23db
commit 72599b7d00
9 changed files with 104 additions and 14 deletions

View File

@ -19,6 +19,6 @@ pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "pytools_deps",
python_version = "3.10",
requirements_lock = "//pytools:requirements.txt",
requirements_lock = "//pytools:requirements.lock",
)
use_repo(pip, "pytools_deps")

62
MODULE.bazel.lock generated
View File

@ -1,6 +1,6 @@
{
"lockFileVersion": 3,
"moduleFileHash": "f69bdb13b0df2f6d309d415437228ebef31fa559567a65ecfe60de77e328ae1d",
"moduleFileHash": "b7a605dff62d0b97615cf1feb669dc4d3ce630abf1e74374f624b71287684b40",
"flags": {
"cmdRegistries": [
"https://bcr.bazel.build/"
@ -72,7 +72,7 @@
"attributeValues": {
"hub_name": "pytools_deps",
"python_version": "3.10",
"requirements_lock": "//pytools:requirements.txt"
"requirements_lock": "//pytools:requirements.lock"
},
"devDependency": false,
"location": {
@ -1741,10 +1741,60 @@
"os:windows,arch:amd64": {
"bzlTransitiveDigest": "EY+DGyyJhrqMHBCiplU6kCYiNtiUXl3Olt6+vF4JnfE=",
"accumulatedFileDigests": {
"@@//pytools:requirements.txt": "ecb2c096486b1bb4dbe407a48fb3375ba496b6ea658c0cd7fc34571f381cd504"
"@@//pytools:requirements.lock": "5a2eed9c6e50e9034521853f6016123e3e526900ddf3ff81cc1dbaaa28683629"
},
"envVariables": {},
"generatedRepoSpecs": {
"pytools_deps_310_jinja2": {
"bzlFile": "@@rules_python~0.31.0//python/pip_install:pip_repository.bzl",
"ruleClassName": "whl_library",
"attributes": {
"name": "rules_python~0.31.0~pip~pytools_deps_310_jinja2",
"requirement": "Jinja2==3.1.3",
"repo": "pytools_deps_310",
"repo_prefix": "pytools_deps_310_",
"whl_patches": {},
"experimental_target_platforms": [],
"python_interpreter": "",
"python_interpreter_target": "@@rules_python~0.31.0~python~python_3_10_host//:python",
"quiet": true,
"timeout": 600,
"isolated": true,
"extra_pip_args": [],
"download_only": false,
"pip_data_exclude": [],
"enable_implicit_namespace_pkgs": false,
"environment": {},
"envsubst": [],
"group_name": "",
"group_deps": []
}
},
"pytools_deps_310_markupsafe": {
"bzlFile": "@@rules_python~0.31.0//python/pip_install:pip_repository.bzl",
"ruleClassName": "whl_library",
"attributes": {
"name": "rules_python~0.31.0~pip~pytools_deps_310_markupsafe",
"requirement": "MarkupSafe==2.1.5",
"repo": "pytools_deps_310",
"repo_prefix": "pytools_deps_310_",
"whl_patches": {},
"experimental_target_platforms": [],
"python_interpreter": "",
"python_interpreter_target": "@@rules_python~0.31.0~python~python_3_10_host//:python",
"quiet": true,
"timeout": 600,
"isolated": true,
"extra_pip_args": [],
"download_only": false,
"pip_data_exclude": [],
"enable_implicit_namespace_pkgs": false,
"environment": {},
"envsubst": [],
"group_name": "",
"group_deps": []
}
},
"pytools_deps_310_pillow": {
"bzlFile": "@@rules_python~0.31.0//python/pip_install:pip_repository.bzl",
"ruleClassName": "whl_library",
@ -1786,6 +1836,12 @@
"name": "rules_python~0.31.0~pip~pytools_deps",
"repo_name": "pytools_deps",
"whl_map": {
"jinja2": [
"3.10"
],
"markupsafe": [
"3.10"
],
"pillow": [
"3.10"
]

View File

@ -14,4 +14,12 @@ $ bazel run -c opt sdl_host.exe
```
$ bazel run sdl_host.exe
```
## Dump requirements
(TODO: Automate)
```
$ pip freeze -r requirements.txt > requirements.lock
```

View File

@ -2,9 +2,10 @@ load("@pytools_deps//:requirements.bzl", "requirement")
py_binary(
name = "font",
srcs = ["font.py"],
srcs = ["font.py", "shared.py"],
deps = [
requirement("pillow"),
requirement("Jinja2"),
],
visibility = ["//visibility:public"],
)

View File

@ -1,6 +1,13 @@
from typing import Tuple
from PIL import Image
import sys
import shared
TEMPLATE = """
// generated code! be nice!
#include "sys/sys.h"
sys_glyph {{ font_name }}[{{ n_glyphs }}] = { {{- glyphs|join(", ") -}} };
""".lstrip()
def main(font_name, n_glyphs, fname_png, fname_c):
@ -8,11 +15,13 @@ def main(font_name, n_glyphs, fname_png, fname_c):
assert(len(glyphs) == n_glyphs), f"must be exactly {n_glyphs} glyphs"
with open(fname_c, "wt") as output:
output.writelines([
"// generated code! be nice!\n",
"#include \"sys/sys.h\"\n",
f"sys_glyph {font_name}[{n_glyphs}] = {{{', '.join(str(g) for g in glyphs)}}};\n"
])
output.write(
shared.templates.from_string(TEMPLATE).render(
font_name=font_name,
n_glyphs=n_glyphs,
glyphs=glyphs,
)
)
def load_glyphs(fname_png: str):

View File

@ -0,0 +1,4 @@
pillow==10.2.0
Jinja2==3.1.3
## The following requirements were added by pip freeze:
MarkupSafe==2.1.5

View File

@ -1 +1,2 @@
pillow==10.2.0
pillow==10.2.0
Jinja2==3.1.3

6
pytools/shared.py Normal file
View File

@ -0,0 +1,6 @@
from jinja2 import Environment, BaseLoader, select_autoescape
templates = Environment(
loader=BaseLoader(),
autoescape=select_autoescape(),
)

View File

@ -3,10 +3,18 @@
#include <stdint.h>
#define SYS_COLOR_TRANSPARENT 255
#define SYS_COLOR_N 256
#define SYS_SPRITE_H 8
#define SYS_SPRITE_W 8
typedef int32_t sys_i32;
typedef uint8_t sys_color;
typedef uint32_t sys_screen_color;
typedef uint64_t sys_glyph;
typedef struct {
sys_color pixels[SYS_SPRITE_H][SYS_SPRITE_W];
} sys_sprite;
sys_screen_color sys_make_screen_color(uint8_t r, uint8_t g, uint8_t b);
@ -15,7 +23,4 @@ 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
#endif // CROCPARTY_SYS_DATA_H