|
1 | 1 | # TinyCircuits-Tiny-Game-Engine |
| 2 | +This is a game engine for embedded devices that can run MicroPython. |
2 | 3 |
|
3 | | -This is a 2D/3D game engine for MicroPython. Core engine features are written in C and integrated into MicroPython as external C modules. The engine API is exposed through Python modules. |
4 | | - |
5 | | -<p align="center"> |
6 | | - <img width="65%" src="images/games.png"> |
7 | | -</p> |
8 | | - |
9 | | -### **Engine Features**: |
10 | | -* Node API with hierarchy format (parent/child) to render rectangles, circles, lines, text, GUI buttons, sprites, and 3D meshes. |
11 | | -* All nodes can be rotated and scaled |
12 | | -* Resource API for loading and holding data like 1/4/8/16-bit bitmap textures, .wav sound files, RTTTL ringtone tracks, fonts, and 2D/3D noise |
13 | | -* Basic button API |
14 | | -* Audio API for 4 audio channels to play .wav, RTTTL, or tones |
15 | | -* Basic 2D physics engine with rotated rectangle and circle colliders exposed as nodes |
16 | | -* Math API for `Vector2` and `Vector3` types |
17 | | -* Tween/animation API to animate attributes of nodes |
18 | | -* Saves API for reading and writing certain common types of data |
19 | | -* Time API for utilizing hardware real-time-clock (RTC) for keeping persistent time across device power-downs |
20 | | -* Link API for connecting two devices together to make local multiplayer games |
21 | | - |
22 | | -You can also read the engine documentation, visit the arcade to download games, or make your own games using the Code Editor Web App: |
23 | | -* [Engine Documentation](https://color.thumby.us/doc/landing.html) |
24 | | -* [Arcade](https://color.thumby.us/code/arcade) |
25 | | -* [MicroPython Code Editor](https://color.thumby.us/code/) |
26 | | - |
27 | | - |
28 | | -# Platforms |
29 | | -The engine can run on most platforms that run [MicroPython](https://github.com/TinyCircuits/micropython/tree/engine). However, some drivers may need to be implemented for buttons, screen, and audio. |
30 | | - |
31 | | -The engine has been tested on and ported to the following platforms: |
32 | | -* Linux/Unix |
33 | | -* RP2350 with 16MiB flash |
34 | | -* Webassembly |
35 | | - |
36 | | - |
37 | | -# Building on Linux for RP2350 |
38 | | -1. Update package list: `sudo apt update` |
39 | | -2. Install build chain dependencies (https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf#page=33): `sudo apt install git python3 cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential g++ libstdc++-arm-none-eabi-newlib` |
40 | | -3. Clone TinyCircuits MicroPython: `git https://github.com/TinyCircuits/micropython.git mp-thumby` |
41 | | -4. `cd` into MicroPython: `cd mp-thumby` |
42 | | -5. Checkout engine branch: `git checkout engine` |
43 | | -6. Init the engine submodule: `git submodule update --init --recursive` |
44 | | -7. Setup cross compiler: |
45 | | - 1. cd `mpy-cross` (folder is inside `mp-thumby`) |
46 | | - 2. `make -j8` |
47 | | -8. Go back to MicroPython root folder: `cd ..` |
48 | | -9. Setup `rp2` port: |
49 | | - 1. `cd ports/rp2` |
50 | | - 2. `make submodules` |
51 | | - 3. `make clean` |
52 | | -10. Go back to MicroPython root folder: `cd ../..` |
53 | | -11. Go into engine folder: `cd TinyCircuits-Tiny-Game-Engine` |
54 | | -12. Run the custom Python build and upload script: `python3 build_and_upload.py` |
55 | | - |
56 | | -(NOTE: you may need to install `pyserial`: `python3 -m pip install pyserial` and the .uf2 will be output to `mp-thumby/ports/rp2/build-THUMBY_COLOR/firmware.uf2` once you see the message `Finding drive letter...`) |
57 | | - |
58 | | -# Building on Linux for Linux |
| 4 | +# Building on Linux |
59 | 5 | These instructions assume that you are cloning MicroPython from the TinyCircuits Fork that has been pre-modified |
60 | 6 | 1. Install SDL2: `sudo apt install libsdl2-dev` |
61 | | -2. Install FFI: `sudo apt install libffi-dev` |
62 | | -3. Install build tools: `sudo apt install build-essential` |
63 | | -4. Clone TinyCircuits MicroPython: `git clone https://github.com/TinyCircuits/micropython.git mp-thumby` |
64 | | -5. `cd` into MicroPython: `cd mp-thumby` |
65 | | -6. Checkout engine branch: `git checkout engine` |
66 | | -7. Init the engine submodule: `git submodule update --init --recursive` |
67 | | -8. Setup UNIX port: |
| 7 | +2. Install build tools: `sudo apt install build-essential` |
| 8 | +3. Clone TinyCircuits MicroPython: `git clone https://github.com/TinyCircuits/micropython.git mp-thumby` |
| 9 | +4. `cd` into MicroPython: `cd mp-thumby` |
| 10 | +5. Checkout engine branch: `git checkout engine-1.23.0` |
| 11 | +6. Clone latest engine module code with `lib` submodules: `git clone --recurse-submodules https://github.com/TinyCircuits/TinyCircuits-Tiny-Game-Engine.git` |
| 12 | +7. Setup UNIX port: |
68 | 13 | 1. `cd ports/unix` |
69 | 14 | 2. `make submodules` |
70 | | -9. `cd` to engine file system to build and run MicroPython and the engine |
| 15 | +8. `cd` to engine file system to build and run MicroPython and the engine |
71 | 16 | 1. `cd`: `cd ../../TinyCircuits-Tiny-Game-Engine/filesystem` |
72 | 17 | 2. build: `(cd ../../ports/unix && make -j8 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine DEBUG=1)` |
73 | | - 3. run: `../micropython_loop ../../ports/unix/build-standard/micropython -X heapsize=2617152 main.py` |
| 18 | + 3. run: `../micropython_loop ../../ports/unix/build-standard/micropython -X heapsize=532480 main.py` |
74 | 19 |
|
75 | 20 | Use `(cd ../../ports/unix && make clean)` to make clean if needed |
76 | 21 |
|
77 | | -# Building and Running On Linux Quickly |
78 | | -If you followed the one of two methods to setup everythign above, you can run the following from `micropython/TinyCircuits-Tiny-Game-Engine/filesystem` to build and run a MicroPython script on Linux quickly: |
79 | | - |
80 | | -`(cd ../../ports/unix && make -j8 DEBUG=1 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine) && (../../ports/unix/build-standard/micropython main.py)` |
81 | | - |
82 | | -# Diagnosing HARD_FAULT |
83 | | -If a hard fault blue screen occurs on the device, it will also usually occur in the Linux build too. It likely still won't be easy to solve, but you can see exactly where in MicroPython or the engine C code where the fault occurs using `gdb`. Using Linux is easier, but `gdb` can be used with the device as well but it is harder to setup. |
84 | | - |
85 | | -Refer to the above section where MicroPython and the engine are built on Linux for Linux. Make sure the `DEBUG=1` flag is added to the `make` command as shown above and then run MicroPython using `gdb` like so: |
86 | | -1. Navigate filesystem: `cd TinyCircuits-Tiny-Game-Engine/filesystem` |
87 | | -2. Build and run with `gdb`: `(cd ../../ports/unix && make -j8 DEBUG=1 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine) && (gdb ../../ports/unix/build-standard/micropython main.py)` |
88 | | -3. In the `gdb` prompt, execute `run main.py` and do it again after launching game when the window closes |
89 | | -4. Play the game until it crashes and see the line in the C code where the fault occurred. In the `gdb` prompt, execute `bt` to see a stacktrace for the steps up to this point |
| 22 | +# Building on Linux, from Scratch |
| 23 | +These instructions assume that you are cloning MicroPython from the official MicroPython repository and not the TinyCircuits Fork |
90 | 24 |
|
91 | | -The above steps will get you started, you will likely need to understand the code and use other `gdb` tools to investigate the issue. For example, to see the value of a memory address, use `x <hex address>` in the `gdb` prompt. |
92 | | - |
93 | | -# Building on Linux for WebAssembly |
94 | | -1. Follow instructions here https://emscripten.org/docs/getting_started/downloads.html and finish after executing `source ./emsdk_env.sh` (will need to execute this last command in `emsdk` in every new terminal/session) |
95 | | -2. `git clone https://github.com/TinyCircuits/micropython/tree/engine micropython` |
96 | | -3. `cd micropython/ports/webassembly` |
97 | | -4. `make -j8 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine` |
| 25 | +1. Install SDL2: `sudo apt install libsdl2-dev` |
| 26 | +2. Install build tools: `sudo apt install build-essential` |
| 27 | +3. Clone MicroPython: `git clone https://github.com/micropython/micropython.git` |
| 28 | +4. `cd` into MicroPython: `cd micropython` |
| 29 | +5. Reset to MicroPython version 1.23.0: `git reset --hard a61c446` |
| 30 | +6. Clone latest engine module code with `lib` submodules: `git clone --recurse-submodules https://github.com/TinyCircuits/TinyCircuits-Tiny-Game-Engine.git` |
| 31 | +7. Setup UNIX port: |
| 32 | + 1. `cd ports/unix` |
| 33 | + 2. `make submodules` |
| 34 | +8. Inside `micropython/ports/unix/variants/mpconfigvariant_common.h` do: |
| 35 | + 1. Change `#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)` -> `#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)` |
| 36 | + 2. Change `#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)` -> `#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG)` |
| 37 | + 3. Add `#define MICROPY_TRACKED_ALLOC (1)` anywhere |
| 38 | +9. UGLY: inside `micropython/tools/mpy-tool.py` |
| 39 | + 1. Change line 1784 to `default="longlong"` (don't know where this tool is used and how to pass it a different value, will just set it for now) |
| 40 | +10. `cd` to filesystem root: `cd micropython/TinyCircuits-Tiny-Game-Engine/filesystem` |
| 41 | +11. Build MicroPython UNIX port: `(cd ../../ports/unix && make -j8 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine DEBUG=1)` |
| 42 | +12. Run MicroPython port: `../micropython_loop ../../ports/unix/build-standard/micropython -X heapsize=532480 main.py` |
98 | 43 |
|
99 | | -# Linux and Webassembly heap size |
100 | | -To mimic the hardware, the heap needs to be `520kB` SRAM + `2MiB` of FLASH scratch = `520*1000 + 2*1024*1024 = 2617152` |
| 44 | +Use `(cd ../../ports/unix && make clean)` to make clean if needed |
101 | 45 |
|
102 | 46 | # Updating MicroPython version |
103 | 47 | Make sure to check that the copied structures in src/utility/engine_mp.h are still the same in the version of MicroPython you're updating to. Some structures are not exposed so they had to be copied to where the engine can use them. |
104 | 48 |
|
105 | 49 | # How UF2s are made |
106 | 50 | 1. Download and compile https://github.com/raspberrypi/picotool (requires pico-sdk be installed in default known location or pass `-DPICO_SDK_PATH=` to `cmake`) |
107 | | -2. Upload a firmware and all files that should be included to Thumby Color (run the `collect_files_for_image.py` and copy files using Thonny from the `production_files/tree` folder) |
108 | | -3. Connect Thumby Color to computer with picotool installed in and the device in BOOTSEL mode (turn off, press and hold down DPAD direction, turn back on) |
109 | | -4. Run: `sudo ./picotool save -r 0x10000000 0x11000000 thumby_color_full_image_00_00_0000.bin` (saves from `XIP_BASE` to `16MiB`s after to bin file) |
110 | | -5. Run: `sudo ./picotool uf2 convert thumby_color_full_image_00_00_0000.bin thumby_color_full_image_00_00_0000.uf2` |
| 51 | +2. Upload a firmware and all files that should be included to Thumby Color |
| 52 | +3. Connect Thumby Color to computer with picotool installed in BOOTSEL mode (turn off, press and hold down DPAD direction, turn back on) |
| 53 | +4. Run: `sudo ./picotool save -r 0x10000000 0x11000000 thumby_color_dev_kit_full_image_08_20_2024.bin` (saves from `XIP_BASE` to `16MiB`s after to bin file) |
| 54 | +5. Run: `sudo ./picotool uf2 convert thumby_color_dev_kit_full_image_08_20_2024.bin thumby_color_dev_kit_full_image_08_20_2024.uf2` |
0 commit comments