Skip to content
44 changes: 19 additions & 25 deletions FarOut/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,37 @@ SystemClass System;

//Constructor
SystemClass::SystemClass() :
desktop(sf::VideoMode::getDesktopMode()),
window(desktop, "FarOut")
videoMode(sf::VideoMode::getDesktopMode())
{
inFocus = true;
windowTitle = "FarOut";

addData("DesktopX", desktop.width);
addData("DesktopY", desktop.height);
std::vector<sf::VideoMode> modes = sf::VideoMode::getFullscreenModes();

for (int i = 0; i < modes.size(); ++i) {
if (modes[i].height == videoMode.height && modes[i].width == videoMode.width) {
videoMode = modes[i];
break;
}
}
if (videoMode.isValid())
window.create(videoMode, windowTitle, sf::Style::Fullscreen);
else window.create(videoMode, windowTitle);

addData("DesktopX", videoMode.width);
addData("DesktopY", videoMode.height);

view = window.getDefaultView();

//FPS Stuff
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
VSyncEnabled = false;
window.setVerticalSyncEnabled(VSyncEnabled);
//window.setMouseCursorVisible(false); //debug
}


Expand Down Expand Up @@ -132,20 +142,9 @@ 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) {

//ESC
if (event.key.code == sf::Keyboard::Escape)
window.close();

//VSync toggle
if (event.key.code == sf::Keyboard::V) {
if (VSyncEnabled) VSyncEnabled = false;
Expand All @@ -161,7 +160,7 @@ void SystemClass::runWindow() {
//time we came through this loop
dt = clock.restart();

if (inFocus) {
if (window.hasFocus()) {
window.clear();

update(dt);
Expand Down Expand Up @@ -227,8 +226,3 @@ void SystemClass::quit() {
}







14 changes: 7 additions & 7 deletions FarOut/SystemClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ 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;
sf::VideoMode videoMode;

private:
// Basic actions to be taken each loop, including calling update
// and draw functions for the active scene, and checking some key events
Expand All @@ -52,11 +56,10 @@ class SystemClass {
std::unordered_map<std::string, float> dataCollection;

//Window stuff
sf::VideoMode desktop;
sf::RenderWindow window;
sf::View view;
sf::Clock clock;
bool VSyncEnabled;
std::string windowTitle;

//FPS Counter
void updateFPS();
Expand All @@ -66,9 +69,6 @@ class SystemClass {
sf::Time FPSTime;
sf::Clock FPSClock;
int FPSFrames;

//Misc
bool inFocus;
};

extern SystemClass System;
Expand Down
15 changes: 15 additions & 0 deletions PrototypeScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ PrototypeScene::~PrototypeScene() {

// Update data members that get updated
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");
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::F4)) {
if(System.videoMode.isValid())
System.window.create(System.videoMode, "FarOut", sf::Style::Fullscreen);
}

bg.update(dt);
ship.update(dt);
alien->update(dt, ship.getPosition());
Expand Down