From eb0c8533e192ff2dd3ff0556960ab4421d41f617 Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Thu, 12 Aug 2021 20:37:35 -0700 Subject: [PATCH 1/7] Moved window into public, and added fullscreen by default --- FarOut/System.cpp | 17 ++++++++++++++--- FarOut/SystemClass.h | 8 +++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/FarOut/System.cpp b/FarOut/System.cpp index 05cc2dd..5a0853b 100644 --- a/FarOut/System.cpp +++ b/FarOut/System.cpp @@ -7,12 +7,23 @@ SystemClass System; //Constructor -SystemClass::SystemClass() : - desktop(sf::VideoMode::getDesktopMode()), - window(desktop, "FarOut") +SystemClass::SystemClass() { inFocus = true; + desktop = sf::VideoMode::getDesktopMode(); + std::vector modes = sf::VideoMode::getFullscreenModes(); + + for (int i = 0; i < modes.size(); ++i) { + if (modes[i].height == desktop.height && modes[i].width == desktop.width) { + desktop = modes[i]; + break; + } + } + if (desktop.isValid()) + window.create(desktop, "FarOut", sf::Style::Fullscreen); + else window.create(desktop, "FarOut"); + addData("DesktopX", desktop.width); addData("DesktopY", desktop.height); diff --git a/FarOut/SystemClass.h b/FarOut/SystemClass.h index 9e85c60..28264ac 100644 --- a/FarOut/SystemClass.h +++ b/FarOut/SystemClass.h @@ -33,11 +33,14 @@ class SystemClass { bool addData(std::string name, float toadd); float getData(std::string name); - //Other utility functions for developers + // Other utility functions for developers void setVSync(bool); void setFPSCounter(bool); void quit(); - + + // Window + sf::RenderWindow window; + private: // Basic actions to be taken each loop, including calling update // and draw functions for the active scene, and checking some key events @@ -53,7 +56,6 @@ class SystemClass { //Window stuff sf::VideoMode desktop; - sf::RenderWindow window; sf::View view; sf::Clock clock; bool VSyncEnabled; From f70a573de3c9a953e364b8abec643385728b0f7d Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Thu, 12 Aug 2021 20:49:33 -0700 Subject: [PATCH 2/7] Added windowTitle string to System class --- FarOut/System.cpp | 16 ++++++---------- FarOut/SystemClass.h | 1 + 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/FarOut/System.cpp b/FarOut/System.cpp index 5a0853b..9596e1a 100644 --- a/FarOut/System.cpp +++ b/FarOut/System.cpp @@ -7,11 +7,13 @@ SystemClass System; //Constructor -SystemClass::SystemClass() +SystemClass::SystemClass() : + desktop(sf::VideoMode::getDesktopMode()) { inFocus = true; - desktop = sf::VideoMode::getDesktopMode(); + windowTitle = "FarOut"; + std::vector modes = sf::VideoMode::getFullscreenModes(); for (int i = 0; i < modes.size(); ++i) { @@ -21,8 +23,8 @@ SystemClass::SystemClass() } } if (desktop.isValid()) - window.create(desktop, "FarOut", sf::Style::Fullscreen); - else window.create(desktop, "FarOut"); + window.create(desktop, windowTitle, sf::Style::Fullscreen); + else window.create(desktop, windowTitle); addData("DesktopX", desktop.width); addData("DesktopY", desktop.height); @@ -39,7 +41,6 @@ SystemClass::SystemClass() //VSync VSyncEnabled = false; window.setVerticalSyncEnabled(VSyncEnabled); - //window.setMouseCursorVisible(false); //debug } @@ -238,8 +239,3 @@ void SystemClass::quit() { } - - - - - diff --git a/FarOut/SystemClass.h b/FarOut/SystemClass.h index 28264ac..ac32e77 100644 --- a/FarOut/SystemClass.h +++ b/FarOut/SystemClass.h @@ -59,6 +59,7 @@ class SystemClass { sf::View view; sf::Clock clock; bool VSyncEnabled; + std::string windowTitle; //FPS Counter void updateFPS(); From 33e26998191c8df0ff995af5f853e7ba7c578675 Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Thu, 12 Aug 2021 20:58:14 -0700 Subject: [PATCH 3/7] Moved videoMode to public, added fullscreen toggle to demo, F3 for window, F4 for fullscreen --- FarOut/SystemClass.h | 2 +- PrototypeScene.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/FarOut/SystemClass.h b/FarOut/SystemClass.h index ac32e77..5b70a45 100644 --- a/FarOut/SystemClass.h +++ b/FarOut/SystemClass.h @@ -40,6 +40,7 @@ class SystemClass { // Window sf::RenderWindow window; + sf::VideoMode desktop; private: // Basic actions to be taken each loop, including calling update @@ -55,7 +56,6 @@ class SystemClass { std::unordered_map dataCollection; //Window stuff - sf::VideoMode desktop; sf::View view; sf::Clock clock; bool VSyncEnabled; diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index bf8b2d0..9332d96 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -69,6 +69,16 @@ PrototypeScene::~PrototypeScene() { // Update data members that get updated void PrototypeScene::update(sf::Time dt) { + + // Key press checks + if (sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) { + System.window.create(System.desktop, "FarOut"); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) { + System.window.create(System.desktop, "FarOut", sf::Style::Fullscreen); + } + bg.update(dt); ship.update(dt); alien->update(dt, ship.getPosition()); From c074b4f3adfe5f19cdaef537616c0f0fd14020c0 Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Thu, 12 Aug 2021 20:59:46 -0700 Subject: [PATCH 4/7] renamed sf::VideoMode object from desktop to videoMode --- FarOut/System.cpp | 18 +++++++++--------- FarOut/SystemClass.h | 2 +- PrototypeScene.cpp | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/FarOut/System.cpp b/FarOut/System.cpp index 9596e1a..934de5d 100644 --- a/FarOut/System.cpp +++ b/FarOut/System.cpp @@ -8,7 +8,7 @@ SystemClass System; //Constructor SystemClass::SystemClass() : - desktop(sf::VideoMode::getDesktopMode()) + videoMode(sf::VideoMode::getDesktopMode()) { inFocus = true; @@ -17,17 +17,17 @@ SystemClass::SystemClass() : std::vector modes = sf::VideoMode::getFullscreenModes(); for (int i = 0; i < modes.size(); ++i) { - if (modes[i].height == desktop.height && modes[i].width == desktop.width) { - desktop = modes[i]; + if (modes[i].height == videoMode.height && modes[i].width == videoMode.width) { + videoMode = modes[i]; break; } } - if (desktop.isValid()) - window.create(desktop, windowTitle, sf::Style::Fullscreen); - else window.create(desktop, windowTitle); + if (videoMode.isValid()) + window.create(videoMode, windowTitle, sf::Style::Fullscreen); + else window.create(videoMode, windowTitle); - addData("DesktopX", desktop.width); - addData("DesktopY", desktop.height); + addData("DesktopX", videoMode.width); + addData("DesktopY", videoMode.height); view = window.getDefaultView(); @@ -35,7 +35,7 @@ SystemClass::SystemClass() : FPSActive = true; FPSFont.loadFromFile("Assets/AreaKilometer50.otf"); FPSText.setFont(FPSFont); - FPSText.setCharacterSize(desktop.height / 30); + FPSText.setCharacterSize(videoMode.height / 30); FPSText.setPosition(10, 0); //VSync diff --git a/FarOut/SystemClass.h b/FarOut/SystemClass.h index 5b70a45..106e9a1 100644 --- a/FarOut/SystemClass.h +++ b/FarOut/SystemClass.h @@ -40,7 +40,7 @@ class SystemClass { // Window sf::RenderWindow window; - sf::VideoMode desktop; + sf::VideoMode videoMode; private: // Basic actions to be taken each loop, including calling update diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index 9332d96..b5f39f7 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -72,11 +72,11 @@ void PrototypeScene::update(sf::Time dt) { // Key press checks if (sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) { - System.window.create(System.desktop, "FarOut"); + System.window.create(System.videoMode, "FarOut"); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) { - System.window.create(System.desktop, "FarOut", sf::Style::Fullscreen); + System.window.create(System.videoMode, "FarOut", sf::Style::Fullscreen); } bg.update(dt); From 9659f9b219cbee04e3cb334e0d0fd7075a4ecc50 Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Thu, 12 Aug 2021 21:01:59 -0700 Subject: [PATCH 5/7] Moved esc check to close window into demo instead of system --- FarOut/System.cpp | 5 ----- PrototypeScene.cpp | 4 ++++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/FarOut/System.cpp b/FarOut/System.cpp index 934de5d..7d6a508 100644 --- a/FarOut/System.cpp +++ b/FarOut/System.cpp @@ -153,11 +153,6 @@ void SystemClass::runWindow() { //Most events should only be checked when in focus if (inFocus) { if (event.type == sf::Event::KeyPressed) { - - //ESC - if (event.key.code == sf::Keyboard::Escape) - window.close(); - //VSync toggle if (event.key.code == sf::Keyboard::V) { if (VSyncEnabled) VSyncEnabled = false; diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index b5f39f7..f5a2d70 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -71,6 +71,10 @@ PrototypeScene::~PrototypeScene() { void PrototypeScene::update(sf::Time dt) { // Key press checks + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + System.quit(); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::F3)) { System.window.create(System.videoMode, "FarOut"); } From 2cdf1e9a95756712c2dfa42ad377039f86a06696 Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Thu, 12 Aug 2021 21:05:14 -0700 Subject: [PATCH 6/7] Added safety check for going into fullscreen without a valid VideoMode --- PrototypeScene.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PrototypeScene.cpp b/PrototypeScene.cpp index f5a2d70..6f63fde 100644 --- a/PrototypeScene.cpp +++ b/PrototypeScene.cpp @@ -80,7 +80,8 @@ void PrototypeScene::update(sf::Time dt) { } if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) { - System.window.create(System.videoMode, "FarOut", sf::Style::Fullscreen); + if(System.videoMode.isValid()) + System.window.create(System.videoMode, "FarOut", sf::Style::Fullscreen); } bg.update(dt); From e74c083fbfb7b7269581ce3578af941ee84b9c14 Mon Sep 17 00:00:00 2001 From: mackfoggia Date: Mon, 16 Aug 2021 21:54:22 -0700 Subject: [PATCH 7/7] Removed infocus functionality, replaced with built in SFML options --- FarOut/System.cpp | 12 ++---------- FarOut/SystemClass.h | 3 --- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/FarOut/System.cpp b/FarOut/System.cpp index 7d6a508..f82e35a 100644 --- a/FarOut/System.cpp +++ b/FarOut/System.cpp @@ -10,8 +10,6 @@ SystemClass System; SystemClass::SystemClass() : videoMode(sf::VideoMode::getDesktopMode()) { - inFocus = true; - windowTitle = "FarOut"; std::vector modes = sf::VideoMode::getFullscreenModes(); @@ -144,14 +142,8 @@ void SystemClass::runWindow() { if (event.type == sf::Event::Closed) window.close(); - //Pauses when out of focus - if (event.type == sf::Event::LostFocus) - inFocus = false; - if (event.type == sf::Event::GainedFocus) - inFocus = true; - //Most events should only be checked when in focus - if (inFocus) { + if (window.hasFocus()) { if (event.type == sf::Event::KeyPressed) { //VSync toggle if (event.key.code == sf::Keyboard::V) { @@ -168,7 +160,7 @@ void SystemClass::runWindow() { //time we came through this loop dt = clock.restart(); - if (inFocus) { + if (window.hasFocus()) { window.clear(); update(dt); diff --git a/FarOut/SystemClass.h b/FarOut/SystemClass.h index 106e9a1..94806bc 100644 --- a/FarOut/SystemClass.h +++ b/FarOut/SystemClass.h @@ -69,9 +69,6 @@ class SystemClass { sf::Time FPSTime; sf::Clock FPSClock; int FPSFrames; - - //Misc - bool inFocus; }; extern SystemClass System;