I gotta admit. I lack experience with C and therefore I am unsure how to implement a function for a struct. There are plenty of answers out there: Define functions in structs, Can I define a function inside a C structure?, C - function inside struct, but they don't answer the result I am looking for. Now what I want is the following:
Imagine I have a struct like this:
typedef struct Ball {
float x, y;
float speedX, speedY;
float radius;
void (*Draw)();
} Ball;
Now I want the Draw fuction to be able to access the members of the Ball variable, as if I had an instance of a struct in C++. The function should be able to access the variables and modify them as much as I want it to. Is this even possible?
I tried some ridiculous stuff like this, but that didn't lead anywhere.
typedef struct Ball {
float x, y;
float speedX, speedY;
float radius;
void (*Draw)();
} Ball;
void Draw(float *x, float *y, float *speedX, float *speedY, float *radius) {
DrawCircle((int)*x, (int)*y, (int)*radius, WHITE);
}
Here the C++ equivilant:
struct Ball {
float x{}, y{};
float speedX{}, speedY{};
float radius{};
void Draw() {
DrawCircle((int)x, (int)y, (int)radius, WHITE); //I know I could static_cast, but who cares :)
}
};
int main(int argc, char ** argv) {
Ball ball;
ball.x = 100.f;
...
ball.Draw();
}
As you can see, the C++-way is pretty darn simple, I just couldn't figure it out for C.