Help handling data in a non OOP way

One more thing, I tried using anonymous structs inside anonymous unions and got an error that "its forbidden by ISO C++".

Then, I tried to give the struct a name and leave only the union anonymous, but I got another error now: "an anonymous union can only have non-static data members".

Should I just disable these errors? I'm kinda worried that this will make my code not work on other compilers or cause some undefined behavior. What should I do here?

EDIT: Nevermind, I'll just use Delix's suggestion.

Edited by Italo on
nyeecola
What do you mean by name collisions?

This:
1
2
3
4
5
union U
{
  struct { float x, y, z; };
  struct { float x, y, z, w; };
}

If you have U.x, which x it is?

nyeecola
One more thing, I tried using anonymous structs inside anonymous unions and got an error that "its forbidden by ISO C++".

Those both are not errors, but warnings. If you want to target ISO C++ without any compiler extensions, then anonymous unions are not in standad, you cannot use them. That said, all reasonable compilers (MSVC, gcc, clang) support them perfectly fine. Unless you'll need to use some crazy embedded platform with custom C/C++ compiler, you'll have no issues.

Edited by Mārtiņš Možeiko on
nyeecola
Ok, thanks for the help, you guys are awesome. Can I ask one more question?

I have 3 structures like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
typedef struct
{
  double x;
  double y;
  v2 velocity;
  texture_t image;
  int w;
  int h;
} player_h;

typedef struct
{
  double x;
  double y;
  v2 velocity;
  texture_t image;
  int w;
  int h;
  behavior_t behavior;
  skill_t skills[5]; // array of skills
  int skills_count; // how many skills does he have
} enemy_t;

typedef struct
{
  double x;
  double y;
  v2 velocity;
  texture_t image;
  int w;
  int h;
} particle_t;


So, it seems like there's a lot of duplicated data above. How would you guys go about "fixing" that? Or should I just leave it as is and not care about this?
And, on the enemy_t structure, is that a dumb way of handling enemies with different skills/skill counts?


There is, in my opinion, nothing wrong with duplicated code. The main thing to Think about here, is to not overcomplicate things to make it "pretty". It will just bite you in the ass.

You could ofcource do nested structs, like they all contain x and y and velocity and so on. But nested structs is more annoying to work on, and you work on them alot. You use them way less.
BUT, this is, as I see it still OOP. Your structs is obviosly designed as object. This is fine if you handle 100 object, or 1000 or (some number), but if you will handle a big number of "objects", it will be slow.

I would think about how data is USED. Most likely you will make a function that updates ALL objects x and y.
Then I would do:

typedef struct
{
double x; (by the way, why doubles?)
double y;
}Position;

Position position[NUMBER_OF_OBEJCTS_IN_GAME];

Then the same for w+h. Then an array for textures, and so on. Thts way you will get a much more efficient engine, with less cache misses.