Hello,
i wanted to create a startscreen for my game and i also wanted to include an options screen.
I dont know if this is a good attempt, but it worked until the point i wanted to add the options screen.
Heres the code:
//startscreen class:
#include "Startscreen.h"
Startscreen::Startscreen(RenderWindow &window)
{
playButtonTexture.loadFromFile("Textures/Spiel1Button v.3.png");
playButton.buttonSprite.setTexture(playButtonTexture, 1);
playButton.buttonSprite.setOrigin(playButtonTexture.getSize().x / 2, playButtonTexture.getSize().y / 2);
playButton.buttonSprite.setPosition(window.getSize().x / 2, window.getSize().y / 2.25);
playButton.setText("Play");
optionsButtonTexture.loadFromFile("Textures/Spiel1Button v.3.png");
optionsButton.buttonSprite.setTexture(optionsButtonTexture, 1);
optionsButton.buttonSprite.setOrigin(optionsButtonTexture.getSize().x / 2, optionsButtonTexture.getSize().y / 2);
optionsButton.buttonSprite.setPosition(window.getSize().x / 2, playButton.buttonSprite.getPosition().y + 110);
optionsButton.setText("Options");
exitButtonTexture.loadFromFile("Textures/Spiel1Button v.3.png");
exitButton.buttonSprite.setTexture(exitButtonTexture, 1);
exitButton.buttonSprite.setOrigin(playButtonTexture.getSize().x / 2, playButtonTexture.getSize().y / 2);
exitButton.buttonSprite.setPosition(window.getSize().x / 2, optionsButton.buttonSprite.getPosition().y + 110);
exitButton.setText("Exit");
resolutionButtonHigherTexture.loadFromFile("Textures/Spiel1Button v.4.png");
resolutionButtonHigher.buttonSprite.setTexture(resolutionButtonHigherTexture, 1);
resolutionButtonHigher.buttonSprite.setOrigin(resolutionButtonHigherTexture.getSize().x / 2 , playButtonTexture.getSize().y / 2);
resolutionButtonHigher.buttonSprite.setPosition(window.getSize().x / 2 + 20, 300);
resolutionButtonHigher.setText("");
startBackgroundTexture.loadFromFile("Textures/Spiel1Hintergrund ohne Erde schlicht.png");
startBackground.backgroundSprite.setTexture(startBackgroundTexture, 1);
startBackground.backgroundSprite.setOrigin(startBackgroundTexture.getSize().x / 2, startBackgroundTexture.getSize().y / 2);
startBackground.backgroundSprite.setPosition(window.getSize().x / 2, window.getSize().y / 2);
headline1.setString("S");
headline1.setOrigin(0, 150);
headline1.setFont(font.Calibri);
headline1.setCharacterSize(300);
headline2.setString("pace");
headline2.setOrigin(0, 150);
headline2.setFont(font.Calibri);
headline2.setCharacterSize(300);
headline1.setPosition((window.getSize().x / 2) - ((headline1.getGlobalBounds().width + headline2.getGlobalBounds().width) / 1.9 ) , playButton.buttonSprite.getPosition().y - 250);
headline2.setPosition(headline1.getPosition().x + headline1.getGlobalBounds().width * 1.2, headline1.getPosition().y);
startActive = true;
optionsActive = false;
}
void Startscreen::Render(RenderWindow &window)
{
startBackground.render(window);
if (startActive == true)
{
playButton.render(window);
exitButton.render(window);
optionsButton.render(window);
window.draw(headline1);
window.draw(headline2);
}
if (optionsActive)
{
resolutionButtonHigher.render(window);
}
}
void Startscreen::Update(RenderWindow &window)
{
if (optionsButton.isPressed(window) || playButton.isPressed(window))
{
startActive = false;
}
if (startActive)
{
playButton.update(window);
exitButton.update(window);
optionsButton.update(window);
}
if (exitButton.isPressed(window) == 1)
{
window.close();
}
if (optionsButton.isPressed(window))
{
optionsActive = true;
}
if (optionsActive = true)
{
resolutionButtonHigher.update(window);
}
}
in the main class there are only Startscreen::update and Startscreen:render executed by now.
if i click options or play, as i wanted, the buttons and Text all dissapear, but they are still updated, so if i click for example "exit", the program still quits.
Also the "resolutionButtonHigher" is rendered from the start, and is updated from the start, although "optionsActive" and "startActive" are false by default.
the "button.isPressed" returns 1 if leftclicked, and 0 if not.
of course the "render" just draws the objects to the screen.
I think this has to be some careless mistake, so sorry if i were too idiotic.