Native SDL2 backend for mini-arcade-core, implemented in C++ with SDL2 + pybind11
and exposed to Python as a backend that plugs into your mini-arcade game framework.
The goal of this repo is to provide a native window + input + drawing layer while
keeping all game logic in Python (via mini-arcade-core).
- C++ (
SDL2+pybind11) ⇒_nativeextension module - Python adapter ⇒
NativeBackendimplementingmini_arcade_core.backend.Backend
- Opens an SDL window from Python
- Basic event polling (Quit, KeyDown, KeyUp) mapped to
Event/EventTypein mini-core - Simple rendering:
begin_frame()/end_frame()draw_rect(x, y, w, h)(filled rectangle)
- Example script that shows a moving rectangle and exits on ESC or window close
This is intentionally minimal and intended as a foundation for adding sprites, textures, audio, etc.
mini-arcade-native-backend/
├─ cpp/
│ ├─ engine.h # C++ Engine class (SDL wrapper)
│ ├─ engine.cpp
│ └─ bindings.cpp # pybind11 bindings for Engine / Event / EventType
├─ src/
│ └─ mini_arcade_native_backend/
│ ├─ __init__.py # Python adapter (NativeBackend)
│ └─ ... # (future helpers)
├─ examples/
│ └─ native_backend_demo.py # example using NativeBackend directly
├─ CMakeLists.txt # C++ build (pybind11 + SDL2)
└─ pyproject.toml # Python package & build config (scikit-build-core)
There are two ways to consume this backend:
- From PyPI (recommended for players / game users)
Prebuilt wheels for your platform (no C++ toolchain or vcpkg needed). - From source (for contributors / engine dev)
Build the C++ extension locally using CMake + vcpkg.
Once wheels are published, you can simply do:
pip install mini-arcade-core
pip install mini-arcade-native-backendAnd in your game:
from mini_arcade_core import Game, GameConfig, Scene
from mini_arcade_core.backend import EventType
from mini_arcade_native_backend import NativeBackend
class MyScene(Scene):
def handle_event(self, event):
if event.type == EventType.KEYDOWN and event.key == 27: # ESC
self.game.quit()
def update(self, dt: float):
...
def draw(self, backend):
backend.draw_rect(100, 100, 200, 150)
config = GameConfig(
width=800,
height=600,
title="Mini Arcade + Native SDL2",
backend_factory=lambda: NativeBackend(),
)
game = Game(config)
scene = MyScene(game)
game.run(scene)For normal users of your games, this is the ideal path: no vcpkg, no CMake, no compiler.
If you want to work on the native backend itself (C++ + Python), you’ll build the extension locally. For that, you need a C++ toolchain, CMake, and vcpkg.
- OS: Windows 10 or later (current dev setup)
- Compiler: MSVC via Visual Studio Build Tools or Visual Studio 2022
- Install the “Desktop development with C++” workload
- CMake: 3.16+
- Python: 3.9–3.11 (matching your
mini-arcade-coreversion) - vcpkg: for
SDL2andpybind11 - (Optional but nice) virtual environment / Poetry for Python deps
This project uses vcpkg to manage C++ dependencies.
cd C:\Users\<your_user>\work
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat# From the vcpkg folder:
.\vcpkg.exe install sdl2 pybind11You only need to do this once per machine (unless you wipe vcpkg or add new libraries).
For builds in this repo, set the toolchain environment variable once per shell:
$env:CMAKE_TOOLCHAIN_FILE = "C:/Users/<your_user>/work/vcpkg/scripts/buildsystems/vcpkg.cmake"(Adjust the path if you cloned vcpkg somewhere else.)
This project uses scikit-build-core as
the build backend, which lets pip drive CMake for you.
From the repo root (mini-arcade-native-backend/):
# Activate your virtualenv (or use Poetry's venv)
# Then, with CMAKE_TOOLCHAIN_FILE set:
pip install -e .What this does:
- Runs CMake via scikit-build-core
- Builds the
_nativeextension - Installs the package in editable mode, so changes to
src/are picked up immediately
After this, you can test in Python:
>>> from mini_arcade_native_backend import NativeBackend
>>> backend = NativeBackend()
>>> backend.init(800, 600, "Hello from native backend")To build distributable artifacts:
python -m buildThis will produce:
dist/
mini-arcade-native-backend-0.1.0-*.whl
mini-arcade-native-backend-0.1.0.tar.gz
Those wheels can be uploaded to PyPI (e.g. via twine) and installed by anyone with:
pip install mini-arcade-native-backendEnd users installing the wheel do not need vcpkg or a compiler.
The package exposes a NativeBackend that implements the Backend protocol from
mini-arcade-core and wraps the C++ Engine underneath.
from mini_arcade_native_backend import NativeBackend
from mini_arcade_core.backend import EventTypeA small standalone demo is provided under examples/:
python examples/native_backend_demo.pyThat demo:
- opens an 800×600 window,
- moves a rectangle horizontally,
- exits on ESC or window close.
(When installed via pip install -e ., you can run this from the repo root.)
On the C++ side (cpp/engine.h / cpp/engine.cpp):
mini::Enginewraps SDL:init(width, height, title)begin_frame()/end_frame()draw_rect(x, y, w, h)poll_events()→std::vector<Event>
EventTypeandEventare simple types mapping SDL events to something Python-friendly.
On the Python side (src/mini_arcade_native_backend/__init__.py):
- The compiled C++ module is imported as
._native(installed into the same package) NativeBackendimplementsmini_arcade_core.backend.Backend:init(width, height, title)→_native.Engine.init(...)poll_events()→ converts_native.Eventto coreEventbegin_frame()/end_frame()→ pass-throughdraw_rect(x, y, w, h)→ pass-through
A minimal integration with mini-arcade-core:
from mini_arcade_core import Game, GameConfig, Scene
from mini_arcade_core.backend import Backend, Event, EventType
from mini_arcade_native_backend import NativeBackend
class MyScene(Scene):
def handle_event(self, event: Event) -> None:
if event.type == EventType.KEYDOWN and event.key == 27: # ESC
self.game.quit()
def update(self, dt: float) -> None:
...
def draw(self, backend: Backend) -> None:
backend.draw_rect(100, 100, 200, 150)
config = GameConfig(
width=800,
height=600,
title="Mini Arcade + Native SDL2",
backend_factory=lambda: NativeBackend(),
)
game = Game(config)
scene = MyScene(game)
game.run(scene)-
ModuleNotFoundError: No module named '_native'- Ensure
pip install -e .(orpython -m build) completed successfully. - Confirm that the wheel contains
mini_arcade_native_backend/_native.*.pyd.
- Ensure
-
DLL load error / Python version mismatch
- Make sure you are building and running with the same Python version.
- If you have multiple Python versions installed, ensure the one used by
pip install -e .is the one used to run your game.
-
CMake can’t find SDL2 or pybind11
- Confirm vcpkg is installed and
sdl2+pybind11are installed via vcpkg. - Make sure
CMAKE_TOOLCHAIN_FILEis set correctly in your shell.
- Confirm vcpkg is installed and
- Configurable clear color (per scene / per game)
- Basic texture / sprite support
- Simple audio playback
- CI that builds wheels for Windows and uploads to PyPI
- A
mini-arcade-coreexample project that uses this backend as the default renderer