Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created January 13, 2024 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubukoz/7487d9433ac02cdf74d57ec0a3ca2bb2 to your computer and use it in GitHub Desktop.
Save kubukoz/7487d9433ac02cdf74d57ec0a3ca2bb2 to your computer and use it in GitHub Desktop.
Primitive Playdate game built with clang
#!/bin/bash
set -euo pipefail
# You'll have to change this, also see note about caveat below
SDK_PATH=/Users/kubukoz/Developer/PlaydateSDK/C_API
rm -rf HelloWorld.pdx || true
rm Source/pdex.elf || true
mkdir -p build/src build/dep
echo -e "\033[32mCompiling main\033[0m"
clang -target arm-none-eabi -march=armv7 \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/include \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/include-fixed \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/include \
-g3 -c -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -O2 -falign-functions=16 -fomit-frame-pointer -gdwarf-2 -Wall -Wno-unused -Wstrict-prototypes -Wno-unknown-pragmas -fverbose-asm -Wdouble-promotion -fno-common -ffunction-sections -fdata-sections -DTARGET_PLAYDATE=1 -DTARGET_EXTENSION=1 -MD -MP -MF build/dep/main.o.d -I . -I . -I $SDK_PATH src/main.c -o build/src/main.o -v
mkdir -p "$(dirname build/$SDK_PATH/buildsupport/setup.o)"
echo -e "\033[32mCompiling setup\033[0m"
clang -target arm-none-eabi -march=armv7 \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/include \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/include-fixed \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/include \
-g3 -c -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -O2 -falign-functions=16 -fomit-frame-pointer -gdwarf-2 -Wall -Wno-unused -Wstrict-prototypes -Wno-unknown-pragmas -fverbose-asm -Wdouble-promotion -fno-common -ffunction-sections -fdata-sections -DTARGET_PLAYDATE=1 -DTARGET_EXTENSION=1 -MD -MP -MF build/dep/setup.o.d -I . -I . \
-I $SDK_PATH $SDK_PATH/buildsupport/setup.c -o build/$SDK_PATH/buildsupport/setup.o -v
echo -e "\033[32mLinking\033[0m"
# Caveat about the below: /Users/kubukoz/Downloads/foo is likely a download of the fpv5-d16-no-exceptions-Release variant of https://github.com/KKoovalsky/LlvmCrossCompileArmCortexM/releases
# but frankly I don't even remember. Perhaps you can replace it with some library path in playdate's arm toolchain.
# yeah, I should've named that dir proper, it was supposed to be a "quick experiment".
clang -target arm-none-eabi -march=armv7 \
-fuse-ld=/usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/bin/ld \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/include \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/lib/gcc/arm-none-eabi/9.2.1/include-fixed \
-I /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/arm-none-eabi/include \
-L /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/thumb/v7e-m+fp/hard \
-L /usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard \
-L /Users/kubukoz/Downloads/foo/lib \
-g3 build/src/main.o build/$SDK_PATH/buildsupport/setup.o -nostartfiles -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -T$SDK_PATH/buildsupport/link_map.ld -Wl,-Map=build/pdex.map,--cref,--gc-sections,--no-warn-mismatch,--emit-relocs -o build/pdex.elf -v
mkdir -p Source
cp build/pdex.elf Source/pdex.elf
pdc Source HelloWorld.pdx
open HelloWorld.pdx #this will fail to run in the simulator, but the pdx is fine and should work on-device
#include "pd_api.h"
#define VELOCITY 5.0f
float x = 50;
float y = 50;
int update(void *userdata)
{
PlaydateAPI *pd = (PlaydateAPI *)userdata;
pd->graphics->clear(kColorWhite);
PDButtons current, pressed, released;
// uncomment these and it'll stop compiling
// pd->system->getButtonState(&current, &pressed, &released);
// if (current & kButtonRight)
// x += VELOCITY;
// else if (current & kButtonLeft)
// x -= VELOCITY;
// if (current & kButtonUp)
// y -= VELOCITY;
// else if (current & kButtonDown)
// y += VELOCITY;
pd->graphics->fillRect(x, y, 100, 100, kColorBlack);
return 1;
}
int eventHandler(PlaydateAPI *pd, PDSystemEvent event, uint32_t arg)
{
if (event == kEventInit)
{
// pd->display->setRefreshRate(50);
pd->system->setUpdateCallback(update, pd);
}
return 0;
}
# install Nix from https://nixos.org and call `nix-shell`.
# that'll set you up with the right clang in scope.
let
nixpkgs = (fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/6023efb996565eeb24c39cae6597a3d2114b9538.tar.gz";
sha256 = "1lgq7aa1x85v28rrqfl9zx9sqkhib7paf8b05llqd0lcf94xqwv4";
});
pkgs = import nixpkgs { };
in
pkgs.clangStdenvNoLibs.mkDerivation {
name = "clang-nix-shell";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment