#anonymous unions / structs

32 messages · Page 1 of 1 (latest)

full roost
#

Hello ! I'm trying to make an AssetLoader in my game and I'm having issues with the whole 'anonymous unions / structs'.
What I would like is to be able to access the textures from wherever I want in the code using AssetLoader::TEXTURE_NAME.
However, I would like to be able to access my textures not only with this TEXTURE_NAME but also with a table with indexes (so AssetLoader::TEXTURE_NAME for instance would also be AssetLoader::table[0]).
The code down below feels like it should work and I don't understand why it doesn't. The error is :
error: member 'sf::Texture AssetLoader::<unnamed union>::<unnamed struct>::ACE' with constructor not allowed in anonymous aggregate

AssetLoader.h ```Cpp
#pragma once
#include <SFML/Graphics/Texture.hpp>

struct AssetLoader final
{
inline static union {
sf::Texture table[4] {};
struct { sf::Texture ACE;
sf::Texture KING;
sf::Texture QUEEN;
sf::Texture JACK; };
} CARDS;

inline static sf::Texture BACKGROUND;

};

`main.cpp` ```Cpp
#include "AssetLoader.h"

int main()
{
    if ( AssetLoader::CARDS.ACE.loadFromFile( "../assets/cards/ace.png" ) == false ) throw;
    if ( AssetLoader::CARDS.KING.loadFromFile( "../assets/cards/king.png" ) == false ) throw;
    if ( AssetLoader::CARDS.QUEEN.loadFromFile( "../assets/cards/queen.png" ) == false ) throw;
    if ( AssetLoader::CARDS.JACK.loadFromFile( "../assets/cards/jack.png" ) == false ) throw;
    return 0;
}

Does anyone have an idea of how to do what I'm trying to do ?

chrome heartBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

full roost
#

I looked online and saw that the issue was with the fact that sf::Texture has a 'human-made' constructor and so it can't be in an anonymous struct

#

But the thing is, if I name the struct then I'll have to do something like this : ```Cpp
struct { ... } NAMES;

AssetLoader::CARDS.NAMES.ACE.loadFromFile( ... )

full roost
#

Any suggestions would be greatly appreciated ❤

#

anonymous unions / structs

split cobalt
#

I would avoid unions all together, they're simply not worth the hassle

full roost
#

so rather have like a method with a switch inside ?

#

to get card by index

barren tapir
#

wtf it is, union struct..... hmmm

#

maybe not use it?

full roost
#

what do you mean ?

split cobalt
#

one quick solution is this

enum cardId {
    ACE,
    KING,
    QUEEN,
    JACK,
    NUM_CARDS
};

...
sf::Texture table[NUM_CARDS];```
#

you can do table[ACE] to get the ACE texture etc etc

#

or use explicit methods that return the appropriate reference

barren tapir
#

why there only 4 pieces pepereally

full roost
split cobalt
#
struct ... {
      sf::Texture textures[4];
      auto& get_ace() { return textures[0]; }
};```
full roost
#

and about having a struct with static variables for textures : is it a good idea ?

split cobalt
#

wdym

full roost
#

this :

struct AssetLoader
{
    inline static sf::Texture BACKGROUND;
    inline static sf::Texture SQUARES[4];
};
split cobalt
#

it's fine

full roost
#

alr

#

thx

split cobalt
#

usually with games you want the assets to be freely available to use

#

although I don't think AssetLoader to be a nice name

full roost
#

what would you name it ?

split cobalt
#

maybe Assets or AssetStore or Textures or smth

full roost
#

alr

#

Well, thank you !

#

!solved