Using cpp with pd api

Hi all, I got quite far with being able to use cpp to build and run on simulator with one single exception- when interacting with play date->lua it appears that I can no longer push values to the stack, or at least, it just doesn’t pick up on the lua side. I was curious if anyone else has also tried using cpp with play date and if there was any tips you might have.

My ultimate solution will likely be to leave most things in C and compile the C++ separate instead of converting everything to C++, but would love advice.

1 Like

I think I have managed to figure this out, working with cmake instead. You can compile with clang & clang++ separately, then link with clang++ so the symbols from c++ show up properly, of course you need extern C in some places too. My cmakelists.txt looks something like this:

cmake_minimum_required(VERSION 3.14)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

set(ENVSDK $ENV{PLAYDATE_SDK_PATH})

if (NOT ${ENVSDK} STREQUAL "")
	# Convert path from Windows
	file(TO_CMAKE_PATH ${ENVSDK} SDK)
else()
	execute_process(
			COMMAND bash -c "egrep '^\\s*SDKRoot' $HOME/.Playdate/config"
			COMMAND head -n 1
			COMMAND cut -c9-
			OUTPUT_VARIABLE SDK
			OUTPUT_STRIP_TRAILING_WHITESPACE
	)
endif()

if (NOT EXISTS ${SDK})
	message(FATAL_ERROR "SDK Path not found; set ENV value PLAYDATE_SDK_PATH")
	return()
endif()

set(CMAKE_CONFIGURATION_TYPES "Debug;Release")

# Game Name Customization
set(PLAYDATE_GAME_NAME uiLibrary)
set(PLAYDATE_GAME_DEVICE uiLibrary_DEVICE)

project(${PLAYDATE_GAME_NAME} C CXX ASM)

set(YOGA_LIB src/yoga/Yoga.cpp src/yoga/YGEnums.cpp src/yoga/YGValue.cpp src/yoga/YGStyle.cpp src/yoga/YGNodePrint.cpp src/yoga/YGNode.cpp src/yoga/YGLayout.cpp src/yoga/YGConfig.cpp src/yoga/Utils.cpp src/yoga/log.cpp src/yoga/internal/experiments.cpp src/yoga/event/event.cpp)

if (TOOLCHAIN STREQUAL "armgcc")
	add_executable(${PLAYDATE_GAME_DEVICE} ${SDK}/C_API/buildsupport/setup.c src/main.c src/yoga_bindings.cpp ${YOGA_LIB})
else()
	add_library(${PLAYDATE_GAME_NAME} SHARED src/main.c src/yoga_bindings.cpp ${YOGA_LIB})
endif()

include(${SDK}/C_API/buildsupport/playdate_game.cmake)

3 Likes