C V.S. C++ should I learn C or C++

I've been trying to figure out a way to learn C and it's gotten me very confused on what the difference between C and C++ are. Also can you write in both C and C++ on the playdate? Which should I learn?

officially C unofficial c++ does exist using playdate-cpp repo on github. Historically C existed before c++, c++ adds classes, polymorphism, all kinds of other handy things in the standard library like vectors, maps etc and a lot of other extensions over C but playdate officially only supports C and lua. Most people code the games in lua. While playdate-cpp does work, there is always a chance an sdk update might break it, and this had been the case for quite some time with the REV B changes to the sdk but it's all been fixed but that does not mean it could not happen again. If you want to be sure things will always work use either lua or c

Full disclosure of my biases

I'm a maintainer of the playdate-cpp and playdate-cpp-extensions open source projects, so to answer one of the questions you asked directly:

Yes, you can write in both C and C++ on the Playdate.


With the extensions library, you'll barely need to interact with the C API at all. Start there and look at the examples if you're curious.

If the SDK releases a breaking change, there may be latency in support, but those projects both endeavor to keep up with Panic, and we'll try to keep the latency as short as we possibly can. After all, we want the latest and greatest too.

</shameless_plug>

An actual attempt to answer your other questions without bias:

What's the Difference?

A lot of confusion between C and C++ comes from the idea that "C++ contains the C language," which is true, for the most part. Basically, as @joyrider3774 points out, there are a ton of constructs built both into the language of C++ and its standard libraries that are exceptionally useful and make life easier, but if you're compiling C++ you can still write C syntax if you have to. This is not the case the other way around.

Which should I learn?

Let me be the person who answers questions with a question: with what languages are you most comfortable today?

At this point, well-written C++ code looks nothing like well-written C code, despite the fact that you can write C in C++, so the transferable skills between the two languages start to become quite abstract. However, if you know other languages, there may be parallels which would make picking up one or the other easier, and any suggestions will be more useful.

What would I pick in a vacuum:

General guidance, pick the one that lets you write less code.

I really like C as a language, and Panic's API is exceptionally friendly as C APIs go, but C requires you to write a lot of code, and the more code you write, the easier it is to write bugs, particularly if you're just learning a language. I don't find bugs particularly great fun to find/fix, and even less fun to find/fix the more code there is.

That is the key compromise to Panic's Lua API, and the reason they encourage people to start there: it takes care of the obnoxious bits, and allows you to write less code. You'll take a tradeoff in performance, but you'll write fewer bugs, and getting your ideas out will be easier, so you'll have more fun.

Thinking in those terms, C++ makes a similar compromise with different ratios: you're going to write more code than you would in Lua, but still far less than you would with C, and IF you do it correctly, you'll get the performance of equivalent C code.

C++ is an object oriented language, it allows you to create classes (for example: Player, Monster, Equipment, Sword, TitleScreen, GameLoop, etc.), and define what they do and how they interact together. You can create parent and child classes to define common behaviours. Memory management is also a bit easier in C++.

There a lot of other additions as well (operator overloading, parametrisation, abstracts, constructors, vectors, iterators, etc.) but it would be too long to detail here.

However, I really like C. It is the base of a lot of modern languages and learning it should help you understand how memory is handled by modern languages. If you are really new to programming, I think it is a better choice. It has a softer learning curve (in my opinion) and may be easier to start with since it is fully supported by Panic.

I’ve written all my games (Red Terror, Pop Pop, Nom Nom and Meow Meow) for playdate in C, but my main language I work in is C++.

You can emulate lots of C++ concepts in C, using structs instead of classes, emulating data encapsulation and constructors using plain old functions.
Ex:

struct Player {
int m_health;
int m_ammo;
};

void Player_Construct(struct Player* player)
{
player->m_health = 5;
player->m_ammo =10;
}

int Player_GetHealth(const struct Player* player)
{
return player->m_health;
}

void Player_SetHealth(struct Player* player, int health)
{
player->m_health = health;
}
… And so on.

I'd say the main pain point comes down to memory managment, in C you have to manage heap memory yourself using realloc (playdates version of malloc/free). Mainly you will miss not having access to the C++ standard library vector. You can of course roll your own but its still not very nice. The risk of going out of bounds of your own memory allocated areas is a real thing and can lead to some nasty bugs that can be hard to track down.
Your best bet if your sticking to C is to make it as a simple as possible for yourself to avoid issues. Lock memory on startup in fixed size rather than trying to lock/free during your game (which will cause terrible performance issues anyways).

I'd stick to C++ outside of playdate whenever possible and if you code in C for the playdate, try to emulate C++ as much as you can.

Cheers,
/Jimmie

1 Like

Fantastic suggestion for C usage there with structs and a naming scheme. C++ and C coder myself and although I'm enjoying Lua on the play.date at the moment I find myself reading the C API docs :slightly_smiling_face:

Any idea practically what sort of overhead Lua adds? I bet somebody in the forum has already answered with some performance comparisons.

1 Like

From what Ive heard the main culprit is the garbage collector.

Garbage collectors are in general a bad idea for any type of real time application, and it does show when running on limited hardware like the playdate.
I watched a youtube video where a guy (squid god) talked about his ”vampire like” game having issues as he wrote it in Lua vs some other guy who did a similiar thing in C and was getting much better performance. I would imagine the garbage collector is the main issue and perhaps also memory layout, you get much more control over that in something like C and C++.

My game Red Terror is a raycaster, I do doubt it could be done in Lua (or at least it would be more cumbersome). I did some low-level shifting of texture layout in memory that made it vastly faster, I think it might be tougher to do the same in Lua.

But as with all performance things, I guess you have to measure and check to know for sure. But in C you know for sure you dont need to fight a garbage collector at all, you are in full control yourself. And in full power to mess things up in your own beautiful way as well X)

1 Like

There are ways to reduce GC in Lua by being careful, but by the time you worry about managing pools of reused objects, then you might as well consider C since you'd likely be doing similar things for memory management.

Ah well I've got enough of a simple game going in Lua at this point that I'll forge ahead and consider it as an experiment for the next game!

1 Like