A simple and rough implementation of Classes for the C Programming Language utilizing macros!
So I've been working on a game decompilation, which was created using C++, and even though it WOULD be easier to do it with C++... I just don't like it. However, I thought that implementing a somewhat simple abstraction for classes would be both really simple and make the process a LOT quicker, by not having to deal with function pointers and having generally ugly syntax, and this is my solution to that problem, and I can see it having a little extra usage for those that might want to mess around with.
It's just syntax sugar for C structs, so it can be used for most things that structs can be used for, however it is not ideal to use if you already use C++ or have your own systems or simply don't want to bother with Classes.
C11 or higher. And the standard libraries (Not sure if I needed to specify that).
You can find a bunch of examples for the usage of this library in here, go over them starting at the Class() usage example.
You should also consult the API and included macros to have them at hand.
Here's a quick syntax example to get you started though:
#include "cooperative.h"
Class(Player);
float x, y;
float xSpeed, ySpeed;
int hp;
bool isGrounded;
Constructor(Player);
this->x = 0;
this->y = 0;
this->xSpeed = 0;
this->ySpeed = 0;
this->hp = 100;
this->isGrounded = true;
EndConstructor();
Method(Player, void, Jump);
if (this->isGrounded) {
this->isGrounded = false;
this->ySpeed += 20.0f;
}
EndMethod();
EndClass();
Player *p = new_Player();
Jump(p);This is a single header library, so you don't need to compile anything, but if you want to try compiling the examples here's what i recommend (make sure you're in the root directory of the project!):
# Examples 0–9:
# Replace {example_name} with the program you want to compile
gcc -I./include examples/{example_name.c} -o {example_name}
chmod +x {example_name}
./{example_name}
# Example 10: (Requires raylib!)
# Note that the -l arguments may change depending on your system
cd examples/10_working_emulator
gcc -I./include -I../../include src/main.c src/chip8.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o chip8
chmod +x chip8
./chip8 [Your .ch8 rom!]There's a few ways of doing this so I'll go over the more common ones. But you should first start by cloning it somewhere safe.
git clone https://www.github.com/Celizte/Cooperative.git
cd Cooperative
# Install locally
cp include/cooperative.h /path/to/your/project/libs
# Install globally
# Be careful when executing sudo commands online!
# Not recommended
sudo cp include/cooperative.h /usr/local/includeYou could also download the header file separately via GitHub's GUI or curl it. Here's the fun way:
cd /path/to/your/project/libs
# CURL
curl -O https://raw.githubusercontent.com/Celizte/Cooperative/refs/heads/main/include/cooperative.h
# WGET
wget https://raw.githubusercontent.com/Celizte/Cooperative/refs/heads/main/include/cooperative.hNo private members, uses a bunch of macros, the RTTI is really basic, no VTable, can't declare constants and initialize them inside the Classes (though the declaring part is fine), no braces (big loss), you have to access the members of the classes by using 'this->', no overriding, no overloading, no templates, no fun.
The reason there's no braces is because the macros add them for the sake of abstraction, for example, Class() adds an open brace because it also adds the TypeDescriptor, so I needed to design the macros with this in mind. Another example is that Method() adds a closing brace right before the method definition, this is so it can be added inside the Class, though as a downside you can't declare Methods past the EndClass() macro, you'll get how it works if you read the header file.
You can contribute in any way you want, or fork and modify as you see fit, or steal or claim it as yours (preferably don't do that! but you don't have to credit me), it uses the Unlicense so do whatever you want, go my child!