Writing Pokitto Games in pure C?


#1

Hi guys,

I am very intersted in the pokitto for educational purposes. I am an ongoing IT teacher who would like
to make some small games for the pokitto in class. The programming language I want to teach will be C or C++, currently I am more interested in C since I am programming with it the last 1 1/2 years and I would like to dive more into it.

Arduino works with C and C++, I found a lot of articles how you can develop the Arduino Uno with pure C.
(e.g.: Programming Arduino Uno in pure C).
Now the tutorial section of pokitto (eg hello world example) is in C++ - how could I write the same programm for the pokitto with pure C? Can I change the C++ into C code (e.g. getting rid of classes like this method: how to use c objects in c).
I guess the pokitto.h library works also in C.

thanks for any help, didn’t find any information on this so I had to ask,
-eder


#2

Hi

hmm… I would have to ask @FManga, @drummyfish and @Pharap for expert opinion on this.

Fmanga has made start-up files that can boot the pokitto without additional libraries, and drummyfish has made pokitto libre version so he knows what else there is to consider. Pharap on the other hand is the resident C / C++ standards afficionado.

Guys? Any comments on this?


#3

More C++ than C.

I haven’t read any of the C standard,
but I have read and quoted (parts of) the C++ standard a handful of times.
(If anyone has actually read the whole thing and understood it they deserve some sort of award,
that thing is verbosity personified. The C++14 version is ~1300 pages, the C++17 version is ~1400 pages.)


If you have the choice, I’d recommend going for C++11 (and later, i.e. C++11, C++14 or C++17).

Even if you don’t touch much on classes, the better type safety and feature set (constexpr variables, enum classes/scoped enumerations) is a big upgrade over C.

And if you do go for C, make sure it’s C11 and you encourage the use of the restrict keyword when dealing with pointers.
(That’s one feature C++ hasn’t adopted that I think would be useful.)

Actually you wouldn’t need the create and free functions.

Many people don’t realise that the vast majority of functions in the Pokitto Lib are actually static member functions, which means they can be called without an object (as I often do in my own code).

So essentially you could do something like this:

// Wrapper.h
#pragma once

// Alternative way of conditionally externing
#if defined(__cplusplus)
#define C_FUNC extern "C"
#else
#define C_FUNC
#endif

C_FUNC void pokitto_begin(void);
// Wrapper.cpp
#include "Wrapper.h"

#include <Pokitto.h>

C_FUNC void pokitto_begin(void)
{
    Pokitto::Core::begin();
}

Then any C code can #include "Wrapper.h" and it doesn’t have to worry about the fact the functions are implemented with C++ because extern "C" prevents name mangling and makes the functions callable from C code.

Accessing public static member variables (like the palette array) would be a bit more complicated, but also doable.

E.g.

// In Wrapper.h

#include <stddef.h>
#include <stdint.h>

C_FUNC uin8_t pokitto_get_buttons_state(size_t index);

C_FUNC void pokitto_set_buttons_state(size_t index, uint8_t state);
// In Wrapper.cpp

C_FUNC uin8_t pokitto_get_buttons_state(size_t index)
{
    return Pokitto::Buttons::buttons_state[index];
}

C_FUNC void pokitto_set_buttons_state(size_t index, uint8_t state)
{
    Pokitto::Buttons::buttons_state[index] = state;
}

#4

This can be tackled with a few different approaches, depending on the exact needs. One can simply use C++ and write C-style code, if that’s good enough. Maybe use defines to hide away the C++ namespaces and the code will look a lot like regular C.

On the other hand, if you need real C, or other C-compatible languages (non-inline assembly), then things get a bit trickier.
The PokittoLib uses mbed APIs, which are C++, so that’d be a problem. One way would be to make extern "C" functions wrapping the PokittoLib. Relatively simple work, since the PokittoLib isn’t really Object Oriented, despite the use of classes.

Another alternative would be to write a pure C lib that isn’t based on mbed. That’s mostly what I’m using in my loader (the exception being the filesystem API, which is using the wrapper approach above). Technically it’s C-style C++, but it shouldn’t be hard to convert into proper C if needed. If it’s of interest to others, it could easily be extracted into a lib of its own. It doesn’t replicate all of the PokittoLib’s functionality yet, but probably enough for games.


#5

This. @eder13 , could you state if your aim is to teach C or completely remove C++ elements. Because if latter, then things get a whole lot more difficult. Remember that a standard Arduino has no display etc that requires extensive libraries


#6

Sorry I am no expert and I don’t know the details of PokittoLib very well, but yes I do prefer C if possible. I simply do this

PokittoLib is C++ but you can write a very simple thin C wrapper, as basically all functions are just static functions of the Pokitto class. So you can have just function like pokitto_display_write(...) instead of Pokitto::display::write(...). This way your games can be pure C.

You can do things like make an empty version of that wrapper that does nothing and try to compile that game with GCC or clang, set strictly to C (-x c for gcc), to make sure it really is C. This can be part of a test script that will reject compiling C++ code. I do something like this with my raycastlib (I compile the tests as C).

However, I think a reasonable approach for a teacher would be to teach C, or “C++ without OOP”, but not strictly ban C++ or OOP for those that want to try it. E.g. working with strings and containers can be a real pain with C and can add a lot of friction when explaining other concepts, so you could teach C-like imperative programming, but use the String class at the same time. A reasonable middle way basically.


#7

First, thank you for all the replies! I appreciate any help!

@Pharap WOW… I would’ve never thought about your approaches. But I think it will be to tricky and complicated in the end, and also explaining (to my students) why our C-File now looks like this could also be difficult.

I agree, I think the easiest method is to “hide” the C++ Code and make it “C-like”. @FManga could you give me a hello world example, how I could change C++ code to look like C Code?


#8

Sure, but first: are you also teaching how to write games for the PC? Using SDL/Allegro or something else?


#9

thanks :slight_smile:

I am currently diving into SDL 2.0 C-Programming (for myself), making little/small games (my gf does the artwork).

Teaching SDL in school would be absolutely awesome but I think this could be to complex. My basic idea was getting a pokitto for everyone in class so they can built their own gameboy console (there is even a gba converter, so you can play gameboy games quite decent) and then to make some easy games for the pokitto (e.g. pong, tictactoe, space invaders or tetris, nothing special) - and this in C.

What do you guys think of it?


#10

SDL programming can be one of the simplest things if you hide the “weird/confusing stuff” (like the initialization with right pixel formats, writing individual pixels etc.) into a nice simple function that the students can call (init(), write_pixel(x,y,r,g,b)). I think the Allegro library that @FManga mentioned does something like this.

The pygame library for python is even simpler, it’s basically the simplest thing after Scratch. It’s in Python but that has support on Pokitto as well (though I’m not sure how good and friendly that support is at the current stage). I think @Hanski is porting pygame, he will be able to tell you more about this.

I don’t know how much work you are willing to put into it, but it wouldn’t be so difficult to create a very simple library that would compile for both SDL and Pokitto. Basically you could make gamelibrary_sdl.h and gamelibrary_pokitto.h, which would both have the same interface (e.g. both would have an init() and write_pixel() functions). Then any game could run on both SDL and Pokitto, just by switching the library in the #include.


#11

It’s not complicated, it’s just a bit tedious if you need a lot of functions.
The functions themselves are trivial.

It could probably be automated with the right tools.
(I don’t know of any tools that could do it, but they probably exist.)

Then don’t.
You could tell them not to look at it/worry about it and explain that “it’s written in a different language to what you’ll be learning”.

Or possibly you could precompile the library as a static library and teach them how to set up their compiler to link a static library.
That way they’d never see the C++, they’d only see the .h file,
and it would be as if they had been dealing with a static library written in C.

I’m somewhat against this approach.
Mainly because code that is both valid C and valid C++ does not always behave the same in both languages.
Here’s a few links to some examples: 1, 2, 3.

Also if you tell your students “we’re learning C” and you’re actually teaching them a subset of C++ then you’re lying to them.
Then when they go off and try to write C elsewhere they’ll encounter some strange edgecases and if they realise that what they were working with wasn’t actually C then they might be a bit annoyed.
So at the very least you should explain to them that you’re teaching them a subset of C++ that’s similar to C and not actual C.

I agree with @drummyfish, I don’t think SDL would be that complex.
The hardest part is setting up the environment to link the static and dynamic libraries.
(If you’re using Visual Studio, I have a project template for that.)
The library itself is very easy to work with as long as they understand pointers.

Unless the students are particularly young, of course.
How old are the students?


#12

@drummyfish and @Pharap:

Currently, I am not familiar with SDL, but my next goal is to learn it, as mentioned so writing an own library for SDL/Pokitto could be tricky for a SDL beginner. I would rather stay in “pure pokitto C” than SDL since I think it is less time investment (I can’t do school stuff 24/7).

I agree with Pharap that “hiding C++ Code” is somewhat not a good idea, especially because I am lying.
I guess the only option (other than going SDL) would be Pharap approach with “adding C++ elements”.

So since I am not that familiar with precompilers and libraries and mixing C with C++, could you maybe give me a Hello World example @Pharap? I guess this is the “easiest” way to analyse what’s going on (for me).

Thank you a lot guys! I guess I will order a pokitto soon. :smiley:

Edit: The students will be 16-18 years old.


#13

@eder13: The PokittoLib comes with some examples which, while being C++, are probably easy enough to understand for someone who only knows C. For example, look at this one. The (only?) difference between that code and what it would look like if it was C is that it would use long names (something like pokitto_display_clear()) instead of namespaces (Pokitto::Display::clear() or game.display.clear(), they’re equivalent).
It shouldn’t be confusing, especially considering the age you’re targeting. It should actually be easier to start with C++, since they’d be able to use strings instead of char arrays.


#14

I recommend this tutorial: http://lazyfoo.net/tutorials/SDL/
And the ‘API by Category’ part of the docs: https://wiki.libsdl.org/APIByCategory

SDL is actually a really easy API to learn,
basic setting up and tearing down of a window and basic shape drawing can be learnt in less than a day.

Loading images and handling sound are a bit trickier.

That said, I agree that trying to make a library that would compile for both SDL and Pokitto is probably a bit over-the-top if you haven’t got much free time (and not a simple thing to attempt if you do).

It’s not just bad because lying is bad, but more importantly it could be setting them up for a fall later on.
If they turned up to a job thinking “I can program in C” and then they started working and discovered that what they learnt wasn’t C then they might be in trouble.

Hiding it by compiling it to machine code (as a static library) isn’t too bad, because although you’re hiding the implementation details from them, you don’t have to lie about it and you’re actually giving them some real-world experience (they would no doubt have to

You don’t need any special ‘precompiler’, the regular compiler should already be capable of it,
you’d just need to be able to set it up in a particular way.

In all honesty I’m not quite sure how to do that in GCC,
but I’m sure I could figure it out from reading the docs,
and @FManga or @drummyfish might know how given their experience with makefiles and command line compiling.

The code itself would be pretty much the same as what I posted before
(possibly a few extra function decorations),
but the .cpp files would all be compiled into a static library
(which is a .lib file on Windows, and an .a file on Linux),
so your students would end up only seeing the .h files.

To be honest I still think the beter options are:

  • Teach them C++ instead of C
  • Teach them a subset of C++ instead of C and explain that you are only teaching them part of the language
  • Teach them C and just trust the students to ignore the .cpp files (and explain the situation to them if they get curious, reminding them that understanding those files won’t earn them any extra marks)

But if you want to go down the static library route then I’m reasonably confident we can figure it out.
I’m not saying it’s a bad option as such, I just think it’s a lot of work for something that might not be necessary.
I would hope 16-18 year olds could be trusted to not get distracted by a library wrapper.

They shouldn’t have too much trouble then.

Do you mind if I ask which country as well?
It shouldn’t make too much difference really, but I’m interested.
(And I suppose technically different nationalities have different amounts of foreknowledge.)


And while I think of it, have you decided what IDE you’re going to be using?

Unfortunately we are still lacking guides on how to set up some of the IDEs
(hopefully we’ll work on that in the new year) but we can explain the options.

EmBitz is the most popular, but it’s Windows-only.
We have various different PlatformIO-oriented solutions in use,
and we also have makefiles (or a makefile), which can usually be combined with most IDEs.


@FManga is right about this, a lot of the more simple code examples aren’t too heavy on C++ features.

In fact in all honestly a lot of these are very C-like (to the point that I’m actually disappointed at the lack of C++-isms).

Again, I agree with @FManga, I think C++ would actually be easier to learn because access to the C++ library would make things easier for the students.
For example, std::vector is a dynamically resizing array, std::swap for swapping variables,
std::array (assuming you’re using C++11) for better arrays -
crucially they provide an easy way of getting the size of the array.

You could still use C++ without going into the advanced stuff like inheritance and virtuality (and templates).
And like I say, if you’ve got C++11 then some of the newer features would be really useful without being too difficult to understand (like constexpr variables).

When I was at college they taught us Visual Basic,
and to begin with they made us work entirely on command line programs in the ‘procedural programming’ unit.
We didn’t do any UI work until ‘event driven programming’ and even at that point we had no idea what a class was until the ‘object oriented programming’ unit came the next year (depite having been using static methods on classes for a long time).


#15

@eder13 If I was to teach C++ game programming on a PC, I would teach SFML instead of SDL

SDL takes days to learn for a competent programmer, SFML can be picked up in a day


#16

Thank yous guys for all the answers!

Since there are a lot of examples from C++ which could be easily changed to C I will maybe try that.
@Pharap I am from Austria, the school is a “generall educating high school”, where there exist different emphases (e.g. IT, Art, Music etc.). So this is not a ‘technic schoqol’ (IT schools in austria are often in ‘HTLs’, this type is the more q/hardcore IT approach where you also can work after thar school).
So my class is more to show students how awesome IT can be that they maybe go to a University of Technology.

So I have to try all this stuff myself and see if it isn’t too complicated.
Again, thank you guys for all the effort, I appreciate every help!

Cheers, wish you all a happy new year. :slight_smile:


#17

I see, you want to get the students hyped? I’ve been preparing to lead a similar course in an elementary school, and have created a webpage with interesting material. You can steal from it, it’s here (in Czech, but mostly pictures). It’s just a collection of stuff related to computer science I found generally interesting.

Yep, this is even beter than SDL, I forgot about it.


Happy new year and public domain day.


#18

I started to try SFML once a long time ago.
I can’t remember why but I didn’t get it working for some reason.

Looking at some example code, I’d agree that it’s more idiomatic for C++.

It’s good to see someone using return EXIT_SUCCESS and return EXIT_FAILURE instead of return 0 and return -1.
But the way they’re construcing the objects is liable to fall foul to the ‘most vexing parse’ problem.

Judging by this the students would most certainly have to learn about member functions and constructors to use SFML.
Personally I don’t think those are too difficult though.


You would still need to make the wrapper library to be able to use proper C.
But like I say, I think trusting your students not to go digging in the library files is a good enough solution.

So the course is a vocational course?
And it’s something the students choose to do?

If they are chosing to do it then they will already have some interest in technology.
Admittedly not all will want to be programmers if it’s just a general IT course and not a programming-oriented course or a computer science course, but if they’ve got interest it’s easier to give them more complex stuff.

The context is important for deciding what is suitable.
If it’s only a general IT course then I think it would make sense to stick with C and produce a C wrapper library.

I find that education systems are drastically different between countries,
so it can be hard to get a sense of what is expected from the students.

(Also don’t be afraid to use the actual German words for the school types. We have so many users from different countries that we’re used to seeing foreign languages.)

If C++ is at all on the table, I recommend having a read of this tutorial:

It might seem like a lot, but there’s a lot of this your students wouldn’t need to know.
Chapters 1-8, 10, 17 and 18 are the most important ones,
chapters 9 and 11-16 are the more advanced topics that can be skipped.
The first 7 chapters cover the basics and much of that will be familiar to you if you’re experienced with C.
8, 10, 17 and 18 are much newer.

Unfortunately it still teaches one or two bad habits, but I’ve never seen a tutorial that doesn’t teach at least a few bad habits.
As far as most tutorials go, this one is more up to date and covers the all-important C++11 features.

No problem, that’s what we’re here for.
If you’re planning to use the Pokitto to teach, we’ll try to make that happen.


If this is supposed to be for children then you might want to add a bit more colour.
At the moment it looks a bit too academic.

There’s some very interesting stuff on here.
I worry some of it might be a bit too advanced for children though.

I have some other thoughts but I’d best not clutter the thread.