Browse Source

add fullscreen toggle

master
noerw 2 years ago
parent
commit
5b7302ff1b
1 changed files with 37 additions and 9 deletions
  1. +37
    -9
      main.cpp

+ 37
- 9
main.cpp View File

@@ -23,6 +23,36 @@ namespace Proto4 {
sf::Texture bg0, bg1;
sf::Font font;

static bool isFullscreen = false;

bool openWindow(bool fullscreen) {
// set antialising for shapes
// this doesn't affect textures (requires sf::Texture::setSmooth())
sf::ContextSettings settings;
settings.antialiasingLevel = 4;

if (fullscreen) {
//go fullscreen
sf::VideoMode fsMode = sf::VideoMode::getFullscreenModes()[0];
window.create(fsMode, gameTitle, sf::Style::Fullscreen, settings);
isFullscreen = true;
} else {
window.create(sf::VideoMode(xResolution, yResolution, colorDepth),
gameTitle, sf::Style::Default, settings);
window.setPosition(sf::Vector2i(0, 0));
isFullscreen = false;
}

window.setMouseCursorVisible(false);

if (!window.isOpen()) {
std::cout << "Unable to open window object. Aborting execution.";
return false;
}

return true;
}

bool init() {

if (!font.loadFromFile("Audiowide-Regular.ttf")) {
@@ -50,15 +80,7 @@ namespace Proto4 {

// open window after everything has loaded to avoid
// 'window not responding' warning
window.create(sf::VideoMode(xResolution, yResolution, colorDepth),
gameTitle, sf::Style::Default, settings);
window.setPosition(sf::Vector2i(0, 0));
window.setMouseCursorVisible(false);

if (!window.isOpen()) {
std::cout << "Unable to open window object. Aborting execution.";
return false;
}
if (!openWindow(isFullscreen)) return false;

return true;
}
@@ -84,6 +106,12 @@ namespace Proto4 {
break;

case sf::Event::KeyPressed:
// toggle fullscreen
if (
event.key.code == sf::Keyboard::F11 ||
(event.key.code == sf::Keyboard::Return && event.key.alt)
) openWindow(!isFullscreen);

switch (state) {
case AppState::MainMenu:
state = menu.handleKey(event);

Loading…
Cancel
Save