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

Author Topic: How to structure a program that changes views (using sf::RectangleShape)  (Read 2166 times)

0 Members and 1 Guest are viewing this topic.

crdl_pls

  • Newbie
  • *
  • Posts: 5
    • View Profile
I'm trying to make a simple program, with two 'views' that look different (different shapes on each one to tell them apart) not really for a practical reason, just to get to know SFML. However I seem to hit a brick wall. This is what I have so far:
//main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Button.h"
#include "Constants.h"

sf::RenderWindow window(sf::VideoMode(windowX, windowY), "SFML works!");

void MainMenu()
{
        Button buttonQuit;
        buttonQuit.initButton(50,60,sf::Color::Blue);
        Button buttonStart;
        buttonStart.initButton(300, 300, sf::Color::Green);
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }              
                window.clear(sf::Color(153,204,255,0));
                window.draw(buttonQuit.button);
                window.draw(buttonStart.button);
                window.display();
        }
}
int main()
{
        MainMenu();
}

//button.h
#ifndef BUTTON_H
#define BUTTON_H
#include <SFML/Graphics.hpp>

class Button
{
public:
        sf::RectangleShape button;
        void initButton(float posX, float posY, sf::Color color, float sizeX = 200, float sizeY = 80)
        {
                button.setPosition(sf::Vector2f(posX, posY));
                button.setSize(sf::Vector2f(sizeX, sizeY));
                button.setFillColor(color);
        }
};
#endif

//constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H

const int windowX = 1280;
const int windowY = 720;

#endif

I know I shouldn't have the game loop inside of the MainMenu() function, but I have no idea how else I should be doing this. When I try to add another function similar to MainMenu() and go between them on mouse click, it crashes. How can I set this up so that it will work, be efficient (as in, only one game loop) and ensure that the code is as clean as possible? (My main problem is that if I do everything in a separate function, that is called by the main function, I would need to declare everything in a header or define everything in place to be able to use it, at least I think. I'm still new-ish to this).

Thanks :)

TLDR: How do I create two distinct 'views'  and be able to change between them.
« Last Edit: July 09, 2015, 10:15:55 pm by crdl_pls »

kitteh-warrior

  • Guest
(click to show/hide)

This is a long piece of code that shows absolutely no attempt of using sf::View (I think that is what you are talking about, maybe you are thinking of game states.)
Re-reading your post, you are talking about game states. Not "views". Terminology is useful ;)

sf::RenderWindow window(sf::VideoMode(windowX, windowY), "SFML works!");
Globals are bad.

void MainMenu()
Classes exist for a reason.
If the main() function only calls one function: don't bother making that function when posting a complete and minimal example.

const int windowX = 1280;
const int windowY = 720;
These values are used in one place, if this is an example: just put them in where they are used and disregard the definitions. We* don't want to set up a complex project to help something unrelated to SFML.

*maybe some people here do, but certainly not a lot.

class Button
This class is badly made. Constructors exist.

Quote from: crdl_pls
How can I set this up so that it will work, be efficient (as in, only one game loop)
This was the first result when typing into Google, "SFML game states wiki".

For future reference, use this link before posting and asking for help. :P

If you wish to get more help on what else to do, the SFML's Github Wiki is a good place.

I recommend having a firm grasp on C++ before diving straight into SFML, but there are others who believe otherwise (shout out to Hapax).

crdl_pls

  • Newbie
  • *
  • Posts: 5
    • View Profile
This is exactly what I meant. Thanks :D

Sorry about the terminology thing, I'm new to this whole thing.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
(shout out to Hapax).
Hi!  :P

I recommend having a firm grasp on C++ before diving straight into SFML, but there are others who believe otherwise
It looks like you may have slightly understood my post. I also recommended having a firm grasp on C++ before using SFML. My point was that it isn't required but it is preferable.
« Last Edit: July 10, 2015, 02:01:27 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
I also recommended having a firm grasp on C++ before using SFML. My point was that it isn't required but it is preferable.
In the other thread, you say:
Quote
It's true that to fully grasp using SFML, having a solid (preferably superhuman) understanding of C++ is required.

So is it now required or not? I'd definitely say yes. Please don't negate the efforts of numerous forum members who have repeatedly provided a detailed reasoning, by encouraging people start with SFML before knowing C++ well. Even if your opinion is a different one, such vague "compromise" statements are dangerous because they are easily misinterepreted. It's the forum community who then has to suffer the consequences.

We now even have an official answer regarding that question, so please use that as a base:
http://www.sfml-dev.org/faq.php#grl-learn
Quote
[...] don't start with SFML if you are trying to grasp the basic language features of C++. SFML makes use of basic as well as advanced features of the C++ language. You might be able to achieve something in your first hours of C++ and SFML but whether it is usable and maintainable is another question. [...]

Basics, required to program with SFML:
[a big list]
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
In the other thread, you say:
Quote
It's true that to fully grasp using SFML, having a solid (preferably superhuman) understanding of C++ is required.
So is it now required or not? I'd definitely say yes.
It was not the same statement. It's required to fully grasp it. It's not required to get some grasp of it. This is reflected to some degree in that official answer:
Quote
You might be able to achieve something in your first hours of C++ and SFML but whether it is usable and maintainable is another question. It is probable that you would have learned more and faster if you just stuck to the standard libraries C++ already provides.
I included that final sentence because it re-inforces my point that you can use it but it's slower and harder.

The list of requirements that follows is highly inaccurate as it's not impossible to use SFML without perfectly understanding all of the items on the list.

Most of my other post was saying that it's better and recommended to be awesome before using SFML and to not bring non-SFML problems to the community - as you pointed out is frustrating - but there are people who can learn differently to most and these people shouldn't necessarily be discouraged. This doesn't include people who really don't want to learn and want others to feed them the answers (I know I've been guilty of feeding) and sometimes it can be not easy to tell the difference but I think it's unfair to only help people if they learn the way you want them to.

Overall, I agree with you, Nexus. I'm not sure why you thought my statements where vague or compromising but, as you suggested, I shall submit and refer newcomers to the official answer.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

crdl_pls

  • Newbie
  • *
  • Posts: 5
    • View Profile
Whoa, this is great. Thanks guys, I'm gonna take all of this on board.

 

anything