-ALt+ Enter to change from fullscreen to windowed and vice versa (options will be saved)
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "menu.h"
//menus are bool, in case they return true, all the windows will close
bool MainMenu ()
{
enum menuOptions {start, options, exit, last};
int optionSelecter = 0;
int textSize = sf::VideoMode::getDesktopMode().height/10;
sf::ContextSettings settings;
settings.antialiasingLevel = 16;
sf::Vector2i res(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height);
bool fullscreen = checkIfFullscreen();
bool changeMode = false;
std::cout << "Fullscreen----: " << fullscreen << std::endl;
sf::RenderWindow window(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Fullscreen, settings);
if(!fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Default, settings);
window.setFramerateLimit(7);
sf::Event event;
bool closeVariable;
//menu options crap
sf::Font menuFont;
sf::Text startText, optionsText, exitText, arrow ;
menuFont.loadFromFile("font/menu.ttf");
startText.setCharacterSize(textSize);
optionsText.setCharacterSize(textSize);
exitText.setCharacterSize(textSize);
arrow.setCharacterSize(textSize);
startText.setFont(menuFont);
optionsText.setFont(menuFont);
exitText.setFont(menuFont);
arrow.setFont(menuFont);
startText.setString("Start game");
optionsText.setString("Options");
exitText.setString("Exit");
arrow.setString("->");
//stupid variable just so that the code is cleaner
int optionsDis = startText.getGlobalBounds().height;
startText.setPosition(res.x/2 - startText.getGlobalBounds().width/2, res.y/2 - optionsDis);
optionsText.setPosition(res.x/2 - optionsText.getGlobalBounds().width/2, res.y/2 );
exitText.setPosition(res.x/2 - exitText.getGlobalBounds().width/2, res.y/2 + optionsDis);
while(window.isOpen())
{
changeMode = changeViewMode(fullscreen);
arrow.setPosition(res.x/2 - startText.getGlobalBounds().width/2 - 2*textSize, res.y/2 - optionsDis + optionSelecter * optionsDis);
//controlling selector
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
optionSelecter++;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
optionSelecter--;
if(changeMode == true)
{
fullscreen = checkIfFullscreen();
if(fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Fullscreen, settings);
else if (!fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Default, settings);
}
if(optionSelecter < 0)
optionSelecter = last-1;
if(optionSelecter >= last)
optionSelecter = 0;
if(optionSelecter == start && sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt))
return false;
if(optionSelecter == options && sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt))
{
window.close();
if(closeVariable = OptionsMenu())
return true;
MainMenu();
}
if(optionSelecter == exit && sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt))
return true;
while(window.pollEvent(event))
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) || event.type == sf::Event::Closed)
return true;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F12))
{
sf::Image screen = window.capture();
long int randomNum = 0;
for(int i = 0; i < 5; i++)
randomNum = rand() % 125000000 + 1;
std::string fileName = "screenshot" + std::to_string(randomNum) + ".png";
screen.saveToFile(fileName);
}
//draw options
window.clear();
window.draw(startText);
window.draw(optionsText);
window.draw(exitText);
if(optionSelecter >= 0 && optionSelecter < last)
window.draw(arrow);
window.display();
}//while window is open
}//main
bool OptionsMenu()
{
enum menuOptions{viewMode, exit, last};
int optionSelecter = 0;
sf::ContextSettings settings;
settings.antialiasingLevel = 16;
bool fullscreen = checkIfFullscreen();
bool changeMode;
sf::Vector2i res(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height);
sf::RenderWindow window(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Fullscreen, settings);
if(!fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Default, settings);
window.setFramerateLimit(7);
sf::Event event;
sf::Font menuFont;
sf::Text changeRes, viewModeText, exitText, arrow, playerName;
menuFont.loadFromFile("font/menu.ttf");
int textSize = sf::VideoMode::getDesktopMode().height/10;
viewModeText.setCharacterSize(textSize);
exitText.setCharacterSize(textSize);
arrow.setCharacterSize(textSize);
viewModeText.setFont(menuFont);
exitText.setFont(menuFont);
arrow.setFont(menuFont);
viewModeText.setString("Change View Mode");
exitText.setString("Exit");
arrow.setString("->");
//stupid variable just so that the code is cleaner
int optionsDis = viewModeText.getGlobalBounds().height;
viewModeText.setPosition(res.x/2 - viewModeText.getGlobalBounds().width/2, res.y/2 - optionsDis);
exitText.setPosition(res.x/2 - exitText.getGlobalBounds().width/2, res.y/2 );
while(window.isOpen())
{
changeMode = changeViewMode(fullscreen);
arrow.setPosition(res.x/2 - viewModeText.getGlobalBounds().width/2 - 2*textSize, res.y/2 - optionsDis + optionSelecter * optionsDis);
//window controls
while(window.pollEvent(event))
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) || event.type == sf::Event::Closed)
return true;
}
if(changeMode == true)
{
fullscreen = checkIfFullscreen();
if(fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Fullscreen, settings);
else if (!fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Default, settings);
}
if(optionSelecter == viewMode && sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt))
{
changeViewModeInText(fullscreen);
fullscreen = checkIfFullscreen();
if(fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Fullscreen, settings);
else if (!fullscreen)
window.create(sf::VideoMode(res.x, res.y), "Bouncing ball thingy", sf::Style::Default, settings);
}
if(optionSelecter == exit && sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt))
window.close();
while(window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
return true;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F12))
{
sf::Image screen = window.capture();
long int randomNum = 0;
for(int i = 0; i < 5; i++)
randomNum = rand() % 125000000 + 1;
std::string fileName = "screenshot" + std::to_string(randomNum) + ".png";
screen.saveToFile(fileName);
}
//controlling selector
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
optionSelecter++;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
optionSelecter--;
if(optionSelecter < 0)
optionSelecter = last-1;
if(optionSelecter >= last)
optionSelecter = 0;
window.clear();
window.draw(viewModeText);
window.draw(exitText);
window.draw(arrow);
window.display();
}
}
bool changeViewMode(bool fullscreen)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) && sf::Keyboard::isKeyPressed(sf::Keyboard::Return))
{
changeViewModeInText(fullscreen);
return true;
}
return false;
}
void changeViewModeInText(bool fullscreen)
{
std::ofstream videoSettings;
videoSettings.open("settings/display.txt");
if(fullscreen)
{
videoSettings << "Fullscreen = No";
return;
}
if(!fullscreen)
videoSettings << "Fullscreen = Yes";
}
bool checkIfFullscreen ()
{
std::ifstream videoSettingsCheck;
std::string videoSettingsString;
videoSettingsCheck.open("settings/display.txt");
getline(videoSettingsCheck, videoSettingsString);
if(videoSettingsString != "Fullscreen = No" && videoSettingsString !="Fullscreen = Yes")
{
std::ofstream videoSettings;
videoSettings.open("settings/display.txt");
videoSettings << "Fullscreen = Yes";
}
if(videoSettingsString == "Fullscreen = Yes")
return true;
return false;
}