diff --git a/README.md b/README.md index 9a28b67..03b9133 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ and runs on Linux and Windows desktops, and in the browser under pygbag pip install "graphik[strata] @ https://github.com/Stephenson-Software/graphik/archive/refs/tags/v0.5.3.zip" ``` +![A 9 by 7 grid3d board drawn headless by Strata: green checker tiles in a wooden frame, a red snake of chained segments with a pale green head and eyes, a glowing red pickup, a crate, a blue post, a yellow token and a "Score 120" label](docs/images/grid3d.png) + +`Grid3DView.open(..., headless=True)` and `savePng`, from `scripts/make_readme_images.py`. Ophidian, Apex and Tic Tak Toe draw their 3D views with the same calls, on the desktop and [in the browser](https://danielstephenson.dev/play/ophidian). + ```python from preponderous.graphik.grid3d import ( SPHERE, Grid3DView, GridBoard, Label, Piece, PieceStyle, Theme, Unavailable, @@ -138,6 +142,10 @@ So that games stop hand-building the same pieces: - **Phones held upright.** Since 0.5.2 `fitCamera` frames a portrait window (aspect below 1) for its shape: the camera looks down more steeply (70 degrees rather than about 55), the board fills the window's width and stands in the middle of the space below a HUD, and a large board no longer runs off the sides. Landscape and square windows keep 0.5.1's framing exactly; between an aspect of 0.8 and 1 the two blend, so a resize across square doesn't jump. `fitCamera(columns, rows, aspect, top=..., bottom=...)` (and `board.camera`, `board.scene`) keep fractions of the window's height clear at the top (a HUD) and the bottom (a bar of touch keys), in any window shape; a view takes them in pixels with `view.setInsets(top=..., bottom=...)`, and picking, `cellScreenPosition` and the web view's camera follow. - **On-screen keys for touch screens.** `TouchKeys([TouchKey("Pause", "space"), TouchKey("Shop", "p"), ...])` lays out a bar of labelled buttons along the bottom of the screen, each at least 44 CSS pixels each way, in rows of equal buttons (up to three rows on a portrait screen, one on a landscape one; when the keys don't all fit, the last button is *More*, which pages through the rest). A tap on one, `keys.press(position, width, height, pygame, scale)`, posts the same `KEYDOWN` and `KEYUP` the keyboard would (marked `touchKey=True`), so the game's own key handling does the work in both views; a press off the bar returns `None` and is the game's. The 2D view draws the bar with `keys.draw(graphik, width, height, scale)`, the 3D view adds `keys.overlay(width, height)` (labels and panels) to its HUD, both from one `keys.layout(...)` and one hit test. Sizes are worked out in CSS pixels: `scale` is the surface's CSS pixels per pixel (1 for the web view; for a 2D canvas the browser scales, see `keys.below(width, height, pageWidth, pageHeight)`, which gives the band to add under a 2D board and the canvas's scale). `keys.bandHeight(...)` is how much of the screen the bar takes, for `view.setInsets(bottom=...)` and the HUD. Show the keys only on touch screens: `grid3d.web.touchScreen()` is true when the page sees a coarse pointer or a touch (`?touch=1` or `?touch=0` in the address says so for testing; `GRAPHIK_TOUCH=1` outside a browser). + Ophidian on a 390 by 844 phone with graphik 0.5.2: in 3D (left) the board framed for portrait with the HUD above and a bar of Pause, Shop, Retry, Skin and Speed keys along the bottom; in 2D (right) the same five keys drawn under the 2D board + + Ophidian's browser build on a phone, from its end-to-end run on graphik 0.5.2: the same `TouchKeys` bar in the 3D view's HUD and drawn with Graphik under the 2D board. + ### In the browser Under [pygbag](https://pypi.org/project/pygbag/) (`sys.platform == "emscripten"`), `Grid3DView.open()` returns a `grid3d.web.Grid3DWebView`: the same calls, drawn by Strata's grid relay, a small Strata web-kit scene in the page (Strata's `web/kit/grid-relay`, whose `WIRE.md` specifies the messages). Python sends the board, each tick's pieces, the HUD and the camera as small versioned binary messages; the page slides the pieces between ticks itself, so nothing is sent per display frame. A game that builds whole `Scene`s can keep calling `drawScene()` every frame; only what changed is sent. @@ -176,7 +184,7 @@ pytest The suite is headless: `src/test/python/conftest.py` sets `SDL_VIDEODRIVER`/`SDL_AUDIODRIVER` to `dummy` before pygame touches a display, so no window is created and the tests run on a machine with no display attached. `pytest` alone works from the repo root because `[tool.pytest.ini_options]` in `pyproject.toml` points `testpaths` at `src/test/python` and `pythonpath` at `src/main/python`. `.github/workflows/test.yml` runs the same `pip install -e ".[test]"` + `pytest` sequence — preceded by a compile check and an import smoke test, across the supported Python versions — on every pull request and on every push to `main`. A second job, *Install from a release archive*, packs the commit the way GitHub serves a tag's archive, installs it into a fresh venv as a consumer would, and draws with it. A push to a branch with no open pull request is not covered, so run `pytest` locally before opening one. The README images are generated, not hand-made. After changing how something draws, regenerate them with -`SDL_VIDEODRIVER=dummy python scripts/make_readme_images.py` and commit the updated `docs/images/*.png`. +`SDL_VIDEODRIVER=dummy python scripts/make_readme_images.py` and commit the updated `docs/images/*.png` (`docs/images/grid3d.png` is drawn only where Strata's Python binding is installed and can render headless). ## Projects The games that use graphik are listed under [Dependents](#dependents) above. diff --git a/docs/images/grid3d.png b/docs/images/grid3d.png new file mode 100644 index 0000000..1d132d5 Binary files /dev/null and b/docs/images/grid3d.png differ diff --git a/docs/images/touch-keys.png b/docs/images/touch-keys.png new file mode 100644 index 0000000..565c90a Binary files /dev/null and b/docs/images/touch-keys.png differ diff --git a/scripts/make_readme_images.py b/scripts/make_readme_images.py index a4eaca4..1b41e04 100644 --- a/scripts/make_readme_images.py +++ b/scripts/make_readme_images.py @@ -5,7 +5,9 @@ SDL_VIDEODRIVER=dummy python scripts/make_readme_images.py Writes docs/images/hero.png (a sample of every draw method) and -docs/images/hover.png (buttons with the mouse away and over them). Uses the +docs/images/hover.png (buttons with the mouse away and over them), and, when +Strata's Python binding is installed, docs/images/grid3d.png (a board drawn +headless by the 3D grid view; skipped with a note otherwise). Uses the graphik in src/main/python, not an installed copy, so the images always show this checkout. No window is opened. """ @@ -100,6 +102,51 @@ def makeHover(path): pygame.image.save(display, path) +def makeGrid3D(path): + """A board drawn by grid3d, headless: a chained snake with eyes, a glowing + pickup, a few other piece kinds and a HUD label. Returns False when Strata + can't run here.""" + from preponderous.graphik.grid3d import ( + CAPSULE, CUBE, CYLINDER, SPHERE, Grid3DView, GridBoard, Label, Piece, + PieceStyle, Shape, Theme, Unavailable, + ) + + def eyes(piece, x, z, baseY): + dx, dz = piece.facing + sx, sz = -dz * 0.18, dx * 0.18 + return [Shape(SPHERE, (x + dx * 0.32 + sx * side, baseY + 0.55, z + dz * 0.32 + sz * side), + (0.14, 0.14, 0.14), (0.05, 0.05, 0.05, 1.0)) for side in (-1, 1)] + + theme = Theme(pieces={ + "head": PieceStyle(SPHERE, (0.55, 0.9, 0.7), height=0.75, size=0.75, extras=eyes), + "body": PieceStyle(SPHERE, (0.85, 0.12, 0.12), height=0.6, size=0.6, tones=(1.0, 0.85)), + "food": PieceStyle(SPHERE, (0.95, 0.2, 0.15), height=0.45, size=0.45, glow=(2.5, 1.2)), + "crate": PieceStyle(CUBE, (0.75, 0.55, 0.3), height=0.55, size=0.7), + "post": PieceStyle(CYLINDER, (0.35, 0.45, 0.85), height=0.9, size=0.4), + "token": PieceStyle(CAPSULE, (0.95, 0.85, 0.2), height=0.8, size=0.35), + }) + try: + view = Grid3DView.open(960, 540, "graphik grid3d", headless=True) + except Unavailable as reason: + print("skipped docs/images/grid3d.png:", reason) + return False + try: + view.setBoard(GridBoard(9, 7, theme)) + body = [(4, 2), (4, 3), (4, 4), (5, 4), (6, 4), (6, 5), (6, 6)] + pieces = [Piece("head", 3, 2, "head", chain="snake", link=0, facing=(-1, 0))] + pieces += [Piece(f"s{i}", c, r, "body", chain="snake", link=i + 1, scale=1.0 - i * 0.05) + for i, (c, r) in enumerate(body)] + pieces += [Piece("food", 1, 2, "food"), Piece("crate", 7, 1, "crate"), + Piece("post", 1, 5, "post"), Piece("token", 2, 5, "token")] + view.setPieces(pieces, snap=True) + view.setLabels([Label("Score 120", 18, 14, 26)]) + view.frame(present=False) + view.savePng(path) + finally: + view.close() + return True + + def main(): os.makedirs(OUT, exist_ok=True) pygame.init() @@ -109,6 +156,8 @@ def main(): finally: pygame.quit() print("wrote docs/images/hero.png and docs/images/hover.png") + if makeGrid3D(os.path.join(OUT, "grid3d.png")): + print("wrote docs/images/grid3d.png") if __name__ == "__main__":