[CPP] Guide: C++ on Playdate

I was able to run the C++ hello world and found it strange that the FPS indicator goes down to 20. I'm on a M2 Max, so this machine should be plenty powerful (The C-Hello world goes to 30fps).

I'm using the 2.1.1 (latest?) version of the SDK, fwiw.
Is there anything I could have done wrong?

I think the example sets the refresh rate to 20, as 20 was initially the default refresh rate of the Playdate. https://github.com/nstbayless/playdate-cpp/blob/main/examples%2Fhello_world%2Fmain.cpp#L116

1 Like

OMFG -- i feel stupid for not seeing that.

Thank you @Jackson_57

Pretty awesome. The Arduboy StarField lib was ported in ~ 10 min due to the CPP work y'all did. <3

1 Like

not sure if it has any extra benefits but maybe we should start with a made with playdate-cpp page somewhere ? with links to source code or so, or a collection on itch.io ?

For me porting my old games has become very easy now, as i no longer have to convert c++ code to C code first which was tedious and time consuming.

Currently these games of me, use playdate-cpp (links to source code on github can be found in the more information section on itch.io):

3 Likes

I love the idea of building an aggregator for pdcpp projects somewhere.

Itch.IO makes a lot of sense, so I put this together real quick.

I've added @joyrider3774 as a contributor, DM me if you want to be added too.

it doesn't feel particularly ideal that we have to go through one person to build a such a community page, but let's try this out for a while, maybe we'll find it does the trick, or maybe we find a smoother solution down the line.

2 Likes

Love this idea!

I plan to port Evade 2 (a game i co-developed for the arduboy), along with the forked version of LibXMP and I'll be in touch afterwards. :slight_smile:

i've added my games.

It's also possible to find projects using playdate-cpp on github by searching for code for it, i found a project named klangstrom and a gameboy emulator using it ( https://github.com/search?q=playdate-cpp&type=code ) but did not find the projects on itch.io so could not add them

@MrBZapp ,

Given that there isn't a specific C++ category, I figured it would be best to ask this question here.

In porting a C++ game, I learned that for some reason, use of malloc() before eventHandler() is called, cause a SIGSEGV.

An example of this is below.

Seems that any use of malloc invoked by __cxx_global_var_init would crash the program. I can't seem to find any technical reason for this. Any thoughts would be greatly appreciated =)

Ah yeah, that makes sense.

Part of the boilerplate for C++ is telling malloc to use the playdate's pd->system->realloc under the hood. The pointer to that function is going to be uninitialized before the eventHandlerShim gets called, so declaring an object which manages some kind of dynamic memory before the event handler runs is going to result in some pretty fire-y crashes.

An unsolicited recommendation: wrap your entire execution in an object, use a std::unique_ptr declared in main.cpp to hold that object, initialize/destroy it in the relevant sections of the eventHandler, and use a C shim to call a method on the object every update:

1 Like

Thank you. I appreciate your insights for sure. =)

Looks like converting this game is going to require a bit more surgery :grin:.

I wanted to share my gratitude to @MrBZapp & @NaOH for the work you've done to enable C++ devs to write software for the Playdate. I was able to get the basic graphics & controls working today from our Arduboy game.

Loads more work to do.

  • Get audio working
  • Figure out how to plot pixels
  • Calibrate the gameplay to adhere to the 30fps limit
  • Loads of cleanup (a call to incbin is hard-coded with a path right now)
  • Remove code from other consoles & devices (SDL, LDK Game, ODROID GO, etc..)
  • Some more stuff can't think of right now.
3 Likes

It's worth mentioning that you're not limited to 30 fps. If your app is fast enough, you should be able to push it to 60 fps.

1 Like

Thank you. I did brig it up to 50, as I read somewhere that 50 was the limit. I hadn't considered bringing it up to 60 though. :man_facepalming:t2:. I'm still very very new to this platform.

I'm going to attempt to run it on the device today and see if I can even get it to run as/is. :sweat_smile:

Hey gents, how does one overcome the situation where ld fails with an error like .data has both ordered ['.ARM.exidx.text.__aeabi_atexit' ... and unordered ['.data.__atexit_recursive_mutex' ... sections. ?

Full ld error:

I'm going to try this solution: Updating ARM toolchain - #3 by ziziman


Ultimately, i had to install the latest ARM toolchain and then elected to the symlinks in /usr/local/bin:

#MacOS
sudo ln -sf /Applications/ARM/bin/* /usr/local/bin

Then dealt with a undefined reference to '_gettimeofday' error by having to define it (incorrectly). The code now compiles, runs, albeit incorrectly as I don't have the

#include <sys/time.h>
// This function is called once upon boot up of the game and not through the gameplay. 
// Returning 0 seems to be fine ¯\_(ツ)_/¯ 
extern "C" int _gettimeofday( struct timeval *tv, void *tzvp ) {
  return 0; 
} 

Do you know from where this is being called/linked? I've never seen that error as a missing function before, but I know you're working on porting something that comes from a different platform. If I can get a super minimal repro case I may be able to add it to the boilerplate and have it not be completely broken either.

1 Like

check if there isn't a srand(Time(null)) somewhere, the time functions do not work on playdate as far as i'm aware and you need to use the playdate equivalents, that time function is usually used in combinations with srand or maybe what you are porting uses it some other way, but you should not use those functions. I had encountered the same issue at some point

1 Like

Hi, I have a dumb question... Is there anything special I should do to include an asset folder? Like fonts, images, etc. I tried to setup the project like in the particles example but when I press build, the resulting pdx does not contain the asset folder (which is named Source, placed at the same level as main.cpp). For info I am using CLion on Windows. Other non-I/O code can run btw.

Oh nvm, something somehow replaced CMAKE_CURRENT_SOURCE_DIR with an actual path (clion, is that you?), I reverted the changes and its working.

Just a side question, I am planning to use a library but it throws lots of things... I read that exception handling is on schedule, is it going well at the moment?

Hey, sorry for the delay in response w/r/t/ the question about exceptions. They're still something I want to be able to support. I've done some tinkering with little progress, so it's not clear how that's going to happen, but it is a key goal for the project to be able to support this. If anyone has any ideas I'm all ears.