0

Intro :

Im a computer engineer student and we have to make in a group of 2 students a game like Wolfeinstein3D with our own raycasting code, in C and with the theme we want, of course we chose the best game of all time AKA Minecraft for theme, and we have to do this with a garbage-tier graphic library (it's a thing like openGL but 100 time worse). And because we are young and stupid we wanted to make much more than the result expected from the subject. Items, GUI, Blocks, Action bar, crafting.. We are working on the project since 2 months, but we have finished the 'normal' and wanted by subject part in 2 weeks and we are enjoying a lot.

Context: Our project are built like this : A core struct who have all things

typedef struct s_core
{
    mlx_t           *mlx; //its like core of the graph libs
    t_player        player; //all the player infos (move speed, position, health..)
    t_imgs          imgs; //all our images
    t_sounds        sounds; //all our sounds
    t_item          items[7]; //all our items
    t_animation     animations[7]; //all our anims
    t_block         blocks[2]; //all our blocks
    t_map           *maps; //all informations about maps (the char **maps, height, weidth..)
    int             screen_size[2];
}   t_core;

And we try as much as possible to respect the principle of encapsulation..

problem:

What is a Block ? A block is a think with a name, with an image to draw in a world, but also an item because you can have it in your hand, build think with him, craft.. And a block has a special song, and maybe an animation, because the block can by animated (for exemple a nether portal..)

So for create a Block, i have to set a item, so i have to acces to core->items[the_wanted](knowing that the items are created before blocks), but i have to get the sound so i have to acces to core->sounds[the_wanted], but i also need a image and more and more and more..

Its the same probleme for lot of think, sounds, items, blocks, maps, players, animation, imgs.. they are all interdependent and I have no idea how to do otherwise.

We therefore find ourselves sending either the entire core as a parameter, or more than 4 elements of the core to a function to do the necessary..

Debat:

I think that we have to delete core struct and allowing the acces of all thinks in it by making them global, my teammate think we have to create a pointer in each of them to the core, exemple

typedef struct s_item
{
    {...variables}
    t_core *core;
}   t_item;

Questions:

  • What is encapulsation in C, does the concept of encapulsation exist and is it relevant in C as there is no POO ?
  • In theory, how to manager a project like this ?
  • In our case, how should we do ?
  • Have we reached a certain limit of the C possiblity ?
  • Any others advice to us, we want to be elegant coders.

We try to be elegant and good at coding but we are not

AliHaine
  • 1
  • 1
  • Related: [Data encapsulation in C](https://stackoverflow.com/questions/29119284/data-encapsulation-in-c) but I wouldn't recommend this unless it's absolutely necessary. – jarmod Jul 31 '23 at 18:17
  • 3
    *We wanted to make much more than the result expected from the subject.* If you have only two weeks until submission, draw a line under the development and focus on checking, polishing and **documenting** what you already have. Which I hope you agree, should also *make much more than the result expected from the subject.* Trying to extend the functionality at this stage, might leave you floundering as you approach submission time. – Weather Vane Jul 31 '23 at 18:26
  • What is the point of these structures? – 0___________ Jul 31 '23 at 21:25
  • Well, it seems that a `Block` is an `Item` but an `item` is not a `Block` per se. If it is the case a `Block` should have pointers to components like song or animation. What about a `Block` having inside a simple list of pointers to related `item`s? – arfneto Jul 31 '23 at 21:59
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Aug 01 '23 at 07:16

1 Answers1

0

Encapsulation is more a software engineering technique than a programming language feature. It exists with non-object oriented programming languages (e.g. CLIPSrules, Caml, Lisp, Scheme, C, RefPerSys)

On most present day (2023) Von Neuman computers and operating systems (Linux or Windows, x86-64 or RISC-V or ARM) every pointer (in practice) has the same size (the "word" of 32 or 64 bits).

Then the C casting of pointers is just a matter of telling the computer what to do with such a word.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547