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

Author Topic: Question about Images and Windows?  (Read 3318 times)

0 Members and 1 Guest are viewing this topic.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Question about Images and Windows?
« on: June 10, 2014, 06:02:19 am »
So i was playing this game http://www.solarus-games.org/ Which is a zelda clone using C++! And i stumble across a very confusing perspective. A while back i was creating my own clone of pong, i wanted to implement a starter window that allow the user to toggle between either playing "1 player" or "2 player", however, because i code using xcode, window threading was a problem but while i played this game i realize that the window wasnt what was been switched (From start window to game window), it was the images. So in conclusion, is that how programmers get to have like a main title window were it says "New Game" etc. If so how can i simple draw the images, then remove the images from displaying?

Cirrus Minor

  • Full Member
  • ***
  • Posts: 121
    • View Profile
Re: Question about Images and Windows?
« Reply #1 on: June 10, 2014, 07:58:33 am »
You mean a starting screen, with a menu ?
It's a screen with images and texts, like a "in game" screen.

You can, for example, have a "state" variable (menu, game), in the game loop you check this variable to know what you have to render, when starting the game you initialise the game data and switch the game state, something like that.
Do you know what I mean ?

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Question about Images and Windows?
« Reply #2 on: June 10, 2014, 06:32:57 pm »
You mean a starting screen, with a menu ?
It's a screen with images and texts, like a "in game" screen.

You can, for example, have a "state" variable (menu, game), in the game loop you check this variable to know what you have to render, when starting the game you initialise the game data and switch the game state, something like that.
Do you know what I mean ?
Yes im looking for an instart game option, that can either have the player quit or have them start the game. I was assuming that images render, and once the user clicks on a certain thing, it starts rending the actual game.

I some what understand but im still confused, so the state variable will determine whether your in the menu or game. Should that state be a bool? So if lets say, the state is set to true, it renders all the menu entities, but once the state becomes false (After the user hovers over a section), the images will unrender and begin rendering that of the game? Is that what you mean?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question about Images and Windows?
« Reply #3 on: June 10, 2014, 09:49:30 pm »
Yes, except there is no such thing as "unrender".  The window should be getting cleared every frame.

Also you should probably use an enum for the state since it's more self-documenting and allows more than two values.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Question about Images and Windows?
« Reply #4 on: June 11, 2014, 10:33:47 pm »
Yes, except there is no such thing as "unrender".  The window should be getting cleared every frame.

Also you should probably use an enum for the state since it's more self-documenting and allows more than two values.

I'm not sure how to go about doing that? Is there any way you or anyone else can show me a small example in code? Because i dont know how to go about it!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question about Images and Windows?
« Reply #5 on: June 11, 2014, 10:53:13 pm »
Do you mean you don't know how to use an enum?  That's a basic C++ feature so you should go read a proper C++ book on that stuff.  Or settle for a quick internet tutorial: http://www.cprogramming.com/tutorial/enum.html

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Question about Images and Windows?
« Reply #6 on: June 11, 2014, 11:04:14 pm »
Do you mean you don't know how to use an enum?  That's a basic C++ feature so you should go read a proper C++ book on that stuff.  Or settle for a quick internet tutorial: http://www.cprogramming.com/tutorial/enum.html
No i understand enum perfectly, what i dont understand is how to place two entities and have one render and then clear and render something else!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question about Images and Windows?
« Reply #7 on: June 11, 2014, 11:29:27 pm »
You should be doing that every single frame in your main game loop anyway with clear/draw/display, if you follow any of the basic examples in the SFML tutorials.

int main() {
    sf::Window window(sf::VideoMode(800, 600), "My window");
    // set up some drawables

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(/* stuff */);
        window.display();
    }

    return 0;
}
 

Adding a state enum to this is trivial:

enum GameState {Menu, Game};

int main() {
    GameState gameState = GameState::Menu;
    sf::Window window(sf::VideoMode(800, 600), "My window");
    // set up some menu drawables
    // set up some game drawables

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
            // if some other event, change gameState
        }

        window.clear();
        switch(gameState) {
        case GameState::Menu:
            window.draw(/* menu stuff */);
            break;
        case GameState::Game:
            window.draw(/* game stuff */);
            break;
        }
        window.display();
    }

    return 0;
}
 

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Question about Images and Windows?
« Reply #8 on: June 11, 2014, 11:32:52 pm »
thank you so much that is 10x more understandable ! Now i need to test that and add some entities! Thanks Again!