Skip to content

Commit 3e6e6fe

Browse files
committed
Fix old test, filesystem, and readme
1 parent 5c216f3 commit 3e6e6fe

20 files changed

Lines changed: 462 additions & 190 deletions

File tree

README.md

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,110 @@
11
# TinyCircuits-Tiny-Game-Engine
2-
This is a game engine for embedded devices that can run MicroPython.
32

4-
# Building on Linux
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
559
These instructions assume that you are cloning MicroPython from the TinyCircuits Fork that has been pre-modified
660
1. Install SDL2: `sudo apt install libsdl2-dev`
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:
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:
1368
1. `cd ports/unix`
1469
2. `make submodules`
15-
8. `cd` to engine file system to build and run MicroPython and the engine
70+
9. `cd` to engine file system to build and run MicroPython and the engine
1671
1. `cd`: `cd ../../TinyCircuits-Tiny-Game-Engine/filesystem`
1772
2. build: `(cd ../../ports/unix && make -j8 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine DEBUG=1)`
18-
3. run: `../micropython_loop ../../ports/unix/build-standard/micropython -X heapsize=532480 main.py`
73+
3. run: `../micropython_loop ../../ports/unix/build-standard/micropython -X heapsize=2617152 main.py`
1974

2075
Use `(cd ../../ports/unix && make clean)` to make clean if needed
2176

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
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:
2479

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`
80+
`(cd ../../ports/unix && make -j8 DEBUG=1 USER_C_MODULES=../../TinyCircuits-Tiny-Game-Engine) && (../../ports/unix/build-standard/micropython main.py)`
4381

44-
Use `(cd ../../ports/unix && make clean)` to make clean if needed
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
90+
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`
98+
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`
45101

46102
# Updating MicroPython version
47103
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.
48104

49105
# How UF2s are made
50106
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`)
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`
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`

doc/landing.html

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,5 @@ <h3>Importable Modules:</h3>
2222
<a href="build/engine_save.html">engine_save</a><br>
2323
<a href="build/engine_time.html">engine_time</a><br>
2424
<a href="build/engine_link.html">engine_link</a><br>
25-
26-
<hr>
27-
28-
<h3>Launcher Folder Structure:</h3>
29-
Games have to be in subdirectories of <code>/Games</code>. They can be organised in any directory structure.
30-
31-
<h4>Minimal example:</h4>
32-
<pre>
33-
─ /Games/mygame
34-
├─ main.py
35-
</pre>
36-
This type of format will show up with a question mark as the icon and the folder name as the game name.
37-
38-
<h4>Minimal example with icon:</h4>
39-
<pre>
40-
─ /Games/mygame
41-
├─ main.py
42-
├─ icon.bmp
43-
</pre>
44-
Instead of a question mark, the game will show up with <code>icon.bmp</code> as the icon. For now, the only supported resolution is 64x64.
45-
46-
<h4>Full example:</h4>
47-
<pre>
48-
─ /Games/mygame
49-
├─ manifest.ini
50-
├─ game.py
51-
├─ game_icon.bmp
52-
</pre>
53-
In this example, the names of all the assets are configured in <code>manifest.ini</code>. In the manifest the following lines can be present:
54-
<pre>
55-
name = Any Name I Want
56-
version = 0.3
57-
main = game.py
58-
icon = game_icon.bmp
59-
</pre>
60-
This format is recommended, and at least the version entry should appear in the manifest. The other entries are optional, defaulting
61-
to directory name, main.py and icon.bmp.
62-
6325
</body>
6426
</html>

filesystem/Games/TestGames/Collision/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def tick(self, dt):
8181

8282
def on_collide(self, contact):
8383
self.count = self.count + 1
84-
# print("Collision!", self.count)
85-
# Circle2DNode(position=contact.position, radius=1)
84+
print("Collision!", self.count)
85+
Circle2DNode(position=contact.position, radius=1)
8686

8787
camera = CameraNode()
8888

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import engine_main
2-
2+
import engine
33
from engine_resources import FontResource
4-
from engine_nodes import Text2DNode
4+
from engine_nodes import Text2DNode, CameraNode
55
from engine_math import Vector2
66

7-
roboto_font = FontResource("9pt-roboto-font.bmp")
7+
roboto_font = FontResource("/Games/TestGames/Font/Portfolio6x8-1bit.bmp")
8+
# roboto_font = FontResource("/Games/TestGames/Font/9pt-roboto-font.bmp")
89

910
class MessageSprite(Text2DNode):
1011
def __init__(self):
1112
super().__init__(self)
12-
self.text = "testing"
13+
self.text = "Hello"
1314
self.layer = 7
1415
self.position = Vector2(0, 0)
1516
self.font = roboto_font
1617
self.opacity = 1.0
18+
self.scale.x = 2
19+
self.scale.y = 2
20+
21+
current_msg = MessageSprite()
22+
23+
cam = CameraNode()
1724

18-
current_msg = MessageSprite()
25+
engine.start()

0 commit comments

Comments
 (0)