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

Author Topic: Game states and OOP  (Read 7143 times)

0 Members and 2 Guests are viewing this topic.

Lord Wolfram

  • Newbie
  • *
  • Posts: 11
  • Books give you knowledge, but knowledge is power!
    • ICQ Messenger - 136
    • View Profile
    • Andris Ivanovs - Programmer, Game Developer
Game states and OOP
« on: January 27, 2017, 03:53:13 am »
It's been a little while since I last came here.
I've followed many interesting tutorials and made simple games, like tetris and snake... anyways.
I've started to make a small project I hope to get some money with  ;) as I need to eat too.
But well after few hours of coding I run into some problems. I'll start with easier one.
For some reason I cannot store a value within a function, what I mean is I want to do something like this:

Main Class:
// Main
int main()
{
    // Create the main window
    RenderWindow app(VideoMode(256, 160), "coolgame");

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

         DrawingClass()::Draw(app);

}

return 0;
}



and here would be the drawing class

DrawingClass::Draw(RenderWindow window)
{
        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(cutie);

        // Update the window
        window.display();
}

I get an error:
no match for call to '(sf::Sprite) ()'



So uhh.... how do I make this work?

Other thing when I try to work with enums to change the game state, I.E. playing or menu, I don't really make it work out nice. I've searched several tutorials and all that but nope.

What I want to do each time I switch states. I want to load data and unload data I won't use.
Save specific data in a file and then read the data from the file for next state or when person wants to play the game other time, well like high score I want to save.

Right now that's all I can think off what to say.
Thank you for your help!

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Game states and OOP
« Reply #1 on: January 27, 2017, 06:40:28 am »

in this code
   window.draw(cutie);
is cutie supposed to be a global sprite ?


and what is this
  DrawingClass()::Draw(app);

specifically what is
  DrawingClass()::


Lord Wolfram

  • Newbie
  • *
  • Posts: 11
  • Books give you knowledge, but knowledge is power!
    • ICQ Messenger - 136
    • View Profile
    • Andris Ivanovs - Programmer, Game Developer
Re: Game states and OOP
« Reply #2 on: January 27, 2017, 02:43:58 pm »
I guess putting random code wasn't doing what I wanted, well I was tired and couldn't think right but here anyways is my code:

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

using namespace sf;

// Main
int main()
{

    // Create the main window
    RenderWindow app(VideoMode(256, 160), "game");

     //Setting up variables for Title screen
    Clock clock; // Clock variable for timers
    Texture ScreenTexture;
    Texture StartButtonTexture;
    ScreenTexture.loadFromFile("TitleScreen.png");
    StartButtonTexture.loadFromFile("GameStart.png");
    Sprite TitleScreen(ScreenTexture);
    IntRect GameStartRectangle(0,0,62,15);
    Sprite GameStart(StartButtonTexture, GameStartRectangle);
    GameStart.setPosition(97, 130);
    Music TitleMusic;  
    TitleMusic.openFromFile("song.ogg");
    TitleMusic.play();

        // Start the Title screen loop
    while (app.isOpen())
    {
        // Process events
        Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                app.close();
        }

        // Animating the game start text in title screen
        if (clock.getElapsedTime().asSeconds() > 0.35f)
            {
              if (GameStartRectangle.left == 62)
                GameStartRectangle.left = 0;
              else
                GameStartRectangle.left += 62;

              GameStart.setTextureRect(GameStartRectangle);
              clock.restart();
            }

        // Drawing section //

        // Clear screen
        app.clear();

        // Draw the sprite
        app.draw(TitleScreen);
        app.draw(GameStart);

        // Update the window
        app.display();
    }
    return 0;
}

Kinda nice code right, it shows a window, Title screen to be precise but I want to do this:
* Make a new class and store everything that's related to drawing the title screen and all animations.
* Render the window in main class and manage the other classes and states, also objects like player and enemies, while the new class just provides the resources like images and so on.
* When I press Return, I can change the titlescreen to game screen. (With screenfade in and out)
* Loading and unloading data when state is having some sort of event, for example the class that handles a game stage, when player dies, change the screen to new class and window state GameOver.



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Game states and OOP
« Reply #3 on: January 27, 2017, 03:03:45 pm »
So is there still a question?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Game states and OOP
« Reply #4 on: January 27, 2017, 08:38:06 pm »

that's a lot of stuff you want to do

you have a lot of ambition

since you want to do that much I'm going suggest that you get a book

do you have a Credit Card | Debit Card | PayPal ?

you can sign up for a 30 day free trial here

https://www.packtpub.com/

they have lots of good SFML books
https://www.packtpub.com/all?search=%22SFML%22

during the free trial you can read all the books online and watch the video --- all completely free (obviously you can read books on other subjects too --- linux, java, SQL)

then at the end of 30 days you can cancel your membership -- or continue for $20 a month

if you don't want to do that there are some videos on youtube
https://www.youtube.com/results?search_query=sfml

obviously the code quality of some will be better than others


Lord Wolfram

  • Newbie
  • *
  • Posts: 11
  • Books give you knowledge, but knowledge is power!
    • ICQ Messenger - 136
    • View Profile
    • Andris Ivanovs - Programmer, Game Developer
Re: Game states and OOP
« Reply #5 on: January 28, 2017, 12:18:36 am »
yea, Cannot pay, but I came up myself with a nice solution, almost....

But the Render window function is non-copyable as I Noticed, any way to bypass it?

EDIT: I solved this, now I can run screens from my functions :D

Now I can advance further and see what else to try.
« Last Edit: January 28, 2017, 02:06:21 am by Lord Wolfram »

 

anything