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.