Home > Net >  How to substitute a function argument with a string in c ?
How to substitute a function argument with a string in c ?

Time:10-27

I'm new to c , and am trying to make a fullscreen setting in SFML. But nothing I tried works.

Working code:

sf::RenderWindow window(sf::VideoMode(1920, 1080, 32), "title", sf::Style::Fullscreen);

Code that would look like what I am looking for (but doesn't work):

string str1 = "sf::Style::Fullscreen";
sf::RenderWindow window(sf::VideoMode(1920, 1080, 32), "title", str1);

CodePudding user response:

You can't convert a string to an enum with the same name. Enums are basicly ints in runtime, the name of the enum is lost when compileing. The closest you can get is making a const map<string,enum> and filling it up manually. eg.:

enum En{a,b,c};
const map<string,En> mp{
    {"a",En::a},
    {"b",En::b},
    {"c",En::c}
};
...
void foo(En);
string str;
...
foo(mp[str]);

CodePudding user response:

The 3rd parameter of sf::RenderWindow is a Uint32 (at least that's what the documentation says), but you are trying to pass a string which is rather pointless.

You probably want something like this:

Uint32 mystyle = sf::Style::Fullscreen;
sf::RenderWindow window(sf::VideoMode(1920, 1080, 32), "title", mystyle);

or better:

auto mystyle = sf::Style::Fullscreen;
sf::RenderWindow window(sf::VideoMode(1920, 1080, 32), "title", mystyle);
  • Related