Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Main function parameter not working ?  (Read 1873 times)

0 Members and 1 Guest are viewing this topic.

M1SMR

  • Newbie
  • *
  • Posts: 2
    • View Profile
Main function parameter not working ?
« on: February 24, 2016, 05:22:36 am »
Hello, I´m writing a Menu for my Game at the moment.
I have made an enum GameMode with Menu,Game and Exit in the main file.
The main function has a GameMode GAMEMODE as a parameter with the standard value Menu.
In the main function is a switch case wich asks for the value of GAMEMODE.
But it seems like the standard value of the main funtion is not working, because it executes the Game first.

#include "Game.h"

enum GameMode{Menu,Game,Exit};
#define g_Framework (Game.getFramework())
#define g_Window (Game.getFramework()->getWindow())


void Game_Menu();
void Game_Play();

int main(GameMode GAMEMODE = Menu) {

        while (GAMEMODE!=Exit)
        {

                switch (GAMEMODE) {
                case Menu:
                        Game_Menu(); break;
                       
                case Game:
                        Game_Play(); break;
                       
                }

        }


        return 0;

}


void Game_Menu() {
        sf::RenderWindow menu;
        menu.create(sf::VideoMode(600, 400), "Menu");
       
        while (menu.isOpen()) {
                sf::Event event;
                while (menu.pollEvent(event)) {
                        switch (event.type) {
                        case sf::Event::Closed:
                                menu.close();
                                break;
                        }


                }
                menu.clear(sf::Color::White);

                menu.display();

        }
       
}

void Game_Play(){...}

 

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Main function parameter not working ?
« Reply #1 on: February 24, 2016, 05:58:09 am »

I don't see how that even compiles 

this line
int main(GameMode GAMEMODE = Menu) {

seems like it should not work at all

main is supposed to look like this
https://wwwx.cs.unc.edu/~sparkst/howto/cpp_main.php

or this for more details (read the one with 97 answers and marked with a Green check mark)
http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main

the purpose of main is to supply an argument (some information) to your program

so if your  program is named my_first_game.exe

and your game had a little Italian running around and you wanted to give him a custom name when the program starts you could type

my_first_game.exe Mario
and his name would be Mario
or
my_first_game.exe George
and his name would be George

you could even do
my_first_game.exe Mario 10
and his name would be Mario
and he would have 10 health

but you're supposed to pass strings (or things that can be converted to strings)
I guess an enum can be converted to a string 

but main is supposed to receive an array of strings
not an enum


M1SMR

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Main function parameter not working ?
« Reply #2 on: February 24, 2016, 06:12:37 am »
The funny thing is Visual Studio gave no errors or warnings about this   :D

Thank you, i fixed it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Main function parameter not working ?
« Reply #3 on: February 24, 2016, 08:25:39 am »
It might produce a warning with higher warning level, however the standard allows for other implementation of int main(...).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/