Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@ jobs:
SDL_AUDIODRIVER: dummy
run: python -m pytest

grid3d-render:
# The rest of the suite runs without Strata, so its render tests (a board
# drawn headless and compared with golden/board-4x3.png, a frame mid-tick)
# skip there. This job installs the strata extra and draws them for real,
# headless through EGL on Mesa's llvmpipe (no GPU, no display), and
# GRAPHIK_REQUIRE_STRATA turns a skip into a failure. The binding fetches
# the pinned Strata release library (view.STRATA_VERSION) from GitHub and
# checks its SHA-256. The browser backend has no render here: Strata's own
# CI draws the grid relay in Chromium against desktop stills.
name: grid3d render (Strata, llvmpipe)
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Install Mesa (EGL, llvmpipe)
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends libegl1 libegl-mesa0 libgl1-mesa-dri libgles2

- name: Install package with test and strata dependencies
run: python -m pip install --upgrade pip && python -m pip install -e ".[test,strata]"

- name: grid3d tests, rendering included
env:
SDL_VIDEODRIVER: dummy
SDL_AUDIODRIVER: dummy
GALLIUM_DRIVER: llvmpipe
GRAPHIK_REQUIRE_STRATA: "1"
run: python -m pytest -rs src/test/python/preponderous/graphik/grid3d

archive-install:
# Consumers install graphik from a release tag's source archive, e.g.
# graphik @ https://github.com/Stephenson-Software/graphik/archive/refs/tags/v0.3.0.zip
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This library assists developers with graphics programming.
graphik is released as git tags, not on PyPI. Install a release from its tag's
archive (no `git` needed):
```bash
pip install "graphik @ https://github.com/Stephenson-Software/graphik/archive/refs/tags/v0.5.2.zip"
pip install "graphik @ https://github.com/Stephenson-Software/graphik/archive/refs/tags/v0.5.3.zip"
```
In a `requirements.txt`, use the same line without the quotes. See
[RELEASING.md](RELEASING.md) for how releases are cut.
Expand Down Expand Up @@ -95,7 +95,7 @@ and runs on Linux and Windows desktops, and in the browser under pygbag
(see [In the browser](#in-the-browser)):

```bash
pip install "graphik[strata] @ https://github.com/Stephenson-Software/graphik/archive/refs/tags/v0.5.2.zip"
pip install "graphik[strata] @ https://github.com/Stephenson-Software/graphik/archive/refs/tags/v0.5.3.zip"
```

```python
Expand All @@ -122,14 +122,16 @@ else:
- Everything except `Grid3DView.open` is plain Python, so scene code can be unit-tested without a GPU. Importing `grid3d` loads neither Strata nor pygame.
- `PieceStyle.extras(piece, x, z, baseY)` returns shapes placed relative to the piece: x and z measured from the given `(x, z)`, so moving the piece moves them all the same way. The browser backend sends them as offsets from the piece and relies on this.

Every public class and function, with an example for each part, is in the [grid3d API reference](docs/grid3d.md).

### Helpers for a game's 3D view

So that games stop hand-building the same pieces:

- **Picking.** `view.pickCell(x, y)` (or `pickCell(camera, board, x, y, width, height)`) gives the `(column, row)` under a pointer, or `None`; the whole cell counts, gaps between tiles included. `projectToScreen(camera, point, width, height)` and `cellScreenPosition(camera, board, column, row, width, height, y)` go the other way, for aiming a tap at a piece or putting a note beside a tile. They use the same look-at and perspective the view asks Strata for.
- **Text.** Strata's built-in font is an 8 x 8 bitmap font in which every character is exactly `pixelSize` wide. `measureText(text, pixelSize)`, `fitText(text, width, pixelSize)` (cut with `...`), `wrapText(text, maxWidth, pixelSize)` (greedy word wrap) and `centredLabel(text, x, y, pixelSize, color, rise=0.5)` build on that.
- **Screen buttons.** `Button(x, y, width, height, text, color, textColor, pixelSize, function)` draws a panel and a centred label (`button.draw(pointer)`), hit-tests with `drawButton`'s edges (`contains`, `click`), and lifts under the pointer as `drawButton` does (`hoverColor`). Colours are 0-255, as for `drawButton`; `padding` widens a button whose label doesn't fit.
- **2D screens under a 3D view.** `ScreenRecorder` stands in for `Graphik`: a game's 2D screens draw into it as usual (`drawText`, `drawRectangle`, `drawButton`), the 3D view shows what they drew, and `recorder.click(pos)` runs the 2D button's own function.
- **2D screens under a 3D view.** `ScreenRecorder` stands in for `Graphik`: a game's 2D screens draw into it as usual (`drawText`, `drawRectangle`, `drawButton`, `drawImage`; `beginFrame`, `endFrame`, `getVersion` and the colour constants are there too), the 3D view shows what they drew, and `recorder.click(pos)` runs the 2D button's own function.
- **Bigger boards.** `GridBoard(columns, rows, theme, tiles=..., mergeTiles=True, viewport=...)`: `tiles` maps a cell to its own `Tile(color, raised)`; `mergeTiles` draws each row's neighbouring tiles that look the same as one box (a 64 x 64 board of one colour is 64 boxes, not 4096); `viewport=Viewport.around(column, row, reach)` draws only a window of the board, framed and fitted as if it were the whole board, with pieces and picking still in whole-board cells.
- **Hover.** `board.scene(..., highlight=(column, row))` lays a thin slab in `Theme.highlight` on the tile under the pointer.
- **Chains.** `board.bridgePairs(pieces)` names the joined pieces and `board.bridgeShape(a, b, aAt, bAt)` draws one bridge.
Expand All @@ -142,9 +144,10 @@ Under [pygbag](https://pypi.org/project/pygbag/) (`sys.platform == "emscripten"`

- `view.ready` turns true once the page has loaded the 3D view; `view.failure()` says why it couldn't. `open()` raises `Unavailable` on a page without the relay.
- `view.pollEvents()` returns Strata-style events (keys with desktop Strata's scancodes, mouse, wheel, resize, and `FINGER_*` for touch), and `grid3d.web.toPygameEvents(events, pygame)` turns them into pygame events, so a game's 2D input code runs unchanged. It converts desktop Strata events too.
- The page: build with `pygbag --template <grid3d.web.templatePath()> ...`, a copy of pygbag 0.9.3's default template with the relay's canvas and a **2D/3D** button. The relay has to be served with the game (a browser can't fetch GitHub release assets cross-origin): `python -m preponderous.graphik.grid3d.relay --strata 0.9.0 --dest <pygbag output>` downloads Strata's `strata-<version>-wasm32-emscripten.tar.gz`, checks it against the release's `SHA256SUMS` and copies its prebuilt `kit/grid-relay/dist/` next to the page, leaving files already there (pygbag's `index.html`) alone.
- The page: build with `pygbag --template <grid3d.web.templatePath()> ...`, a copy of pygbag 0.9.3's default template with the relay's canvas and a **2D/3D** button. The relay has to be served with the game (a browser can't fetch GitHub release assets cross-origin): `python -m preponderous.graphik.grid3d.relay --strata 0.9.0 --dest <pygbag output>` downloads Strata's `strata-<version>-wasm32-emscripten.tar.gz`, checks it against the release's `SHA256SUMS` and copies its prebuilt `kit/grid-relay/dist/` next to the page, leaving files already there (pygbag's `index.html`) alone; `--replace` replaces an earlier relay's files (never `index.html`) when moving to a newer Strata.
- Since 0.5.1 the page starts in **3D**, as a desktop `Grid3DView` game does: the relay's scripts and engine load at once, alongside pygbag, and the 2D canvas stays on screen until the 3D view is drawing the game. If 3D can't start (no WebGL 2, a file missing, too big, the engine failed), the 2D view stays, the button reads *3D unavailable* and a note under it says why in plain words. Which view a page starts in: `?view=2d` or `?view=3d` in the address (for testing and sharing; not remembered), else the player's last choice on the button (kept in `localStorage`; a stored 2D wins over the default), else the game's default, which `templatePath(defaultView="2d")` makes 2D. A page that starts in 2D downloads nothing of the 3D view (not even the relay's three small scripts) until the button is pressed. `grid3d.web.wants3D()` tells the game 3D is on and the relay's scripts are in, so `Grid3DWebView.open()` can begin, and `set3D(False, reason)` turns it off.
- The button sits in the page's top right corner. `grid3d.web.buttonArea()` gives its `(x, y, width, height)` in CSS pixels, the units of the web view's labels and panels, so a HUD can keep text out from under it (`None` outside a browser). The page never scrolls (`overflow: hidden`), so a phone's browser can't widen it to fit pygbag's 50000-pixel measuring element and push the button off screen, and its one viewport tag sets `width=device-width` (pygbag 0.9.3's default page has a second one that overrides it). Strata's own loading and failure overlay is hidden, so it never covers the 2D game.
- **Too slow: back to 2D.** Since 0.5.3, when the 3D view stays slow once the game is drawing in it, the page goes back to 2D and the note says why in plain words ("this device draws the 3D view too slowly"). Strata's kit first gives up pixel density (down to 1) and then its quality tier; only once it has nothing left to give up, and the drawing itself stays slow for 5 seconds, does the page switch: second by second, the typical frame (the median time between the page's frames) is over 66 ms, the relay's own drawing (its `stats()` `cpuMs`) is at least a quarter of it, and the kit's own average frame (`frameMs`) is over 66 ms too. A game that stops the page now and then (building and sending a board in Python) makes a few long gaps, not a slow typical frame, and a game that is slow every frame while the drawing is cheap wouldn't be helped by 2D, so neither switches. Counting starts 3 seconds after the game's first message to the 3D view, so pygbag's boot and the first shader compile aren't counted, and stops while the tab is hidden. The switch isn't remembered as the player's choice: the button still offers 3D, and once the player chooses 3D again the page leaves it alone until it is reloaded. `?slow=0` in the address turns the switch off (for testing on a machine that draws in software). `Graphik3D.slowed()` in the page says it happened, and `Graphik3D.speed()` gives the last second's readings.
- The button sits in the page's top right corner. Since 0.5.3 it is a switch with both views on it, *2D* and *3D*, the one on show lit, so it reads as a choice rather than as a status; to a screen reader it is a toggle named "3D view" (`aria-pressed` while 3D is on), and it is at least 44 CSS pixels each way. `grid3d.web.buttonArea()` gives its `(x, y, width, height)` in CSS pixels, the units of the web view's labels and panels, so a HUD can keep text out from under it (`None` outside a browser). The page never scrolls (`overflow: hidden`), so a phone's browser can't widen it to fit pygbag's 50000-pixel measuring element and push the button off screen, and its one viewport tag sets `width=device-width` (pygbag 0.9.3's default page has a second one that overrides it). Strata's own loading and failure overlay is hidden, so it never covers the 2D game.

In a game's `browser.yml`, where graphik is installed into `src/` for pygbag:

Expand Down
Loading
Loading