Skip to content

Commit

Permalink
Implement the ability to store, load, read, save global game settings
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasia-v-r authored Oct 4, 2019
2 parents 522538f + 371e030 commit f38e0d1
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ else()
if(EXISTS ${CMAKE_SOURCE_DIR}/conanfile.txt)
message( FATAL_ERROR
" A conanfile.txt file exists in the source directory\n"
" indicating conan is used for dependences but a conanbuildinfo.cmake\n"
" indicating Conan is used for dependences but a conanbuildinfo.cmake\n"
" file does not exist. Make sure to execute:\n\n"
" conan install ${CMAKE_SOURCE_DIR} --build=missing -if ${CMAKE_BINARY_DIR}" )
" Conan install ${CMAKE_SOURCE_DIR} --build=missing -if ${CMAKE_BINARY_DIR}" )
endif()
endif()

Expand Down
4 changes: 4 additions & 0 deletions assets/settings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# fpslock
false
# gamespeed
.10f
3 changes: 2 additions & 1 deletion include/GameMode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
class GameMode : public Mode
{
public:
GameMode(std::mutex* mutex);
GameMode(std::mutex*, float);
virtual std::pair<ModeAction, ModeOption> Run(sf::Time, sf::RenderWindow&) override;
private:
void processKeys(sf::Keyboard::Key, bool);
private:
Player mPlayer;
float gameSpeed;
};
16 changes: 16 additions & 0 deletions include/Settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

class Settings
{
public:
Settings();
~Settings() = default;
bool getFpsLock() { return fpsLock; };
float getGameSpeed() { return gameSpeed; };
void setFpsLock(bool temp) { fpsLock = temp; };
void setGameSpeed(float temp) { gameSpeed = temp; };
void saveToFile();
private:
bool fpsLock;
float gameSpeed;
};
8 changes: 4 additions & 4 deletions src/GameMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#include <chrono>
#include <iostream>

GameMode::GameMode(std::mutex* mutex)
GameMode::GameMode(std::mutex* mutex, float speed)
: Mode("GameMode.json", mutex, ModeOption::Game)
, mPlayer(objectTextures, mutex) {
, mPlayer(objectTextures, mutex)
, gameSpeed{ speed } {
screenObjects.emplace_back(&mPlayer);
sf::RectangleShape food(sf::Vector2f(50, 50));
food.setPosition(500, 500);
Mode::pushObject("food", food, "blue");
Mode::pushObject("food", food, "Blue");
}

std::pair<ModeAction, ModeOption> GameMode::Run(sf::Time time, sf::RenderWindow& window) {
float gameSpeed = .10f;
// Handle Input
sf::Event evnt;
while (window.pollEvent(evnt)) {
Expand Down
6 changes: 3 additions & 3 deletions src/MenuMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std::pair<ModeAction, ModeOption> MenuMode::Run(sf::Time time, sf::RenderWindow&
case sf::Event::MouseButtonPressed: {
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f mousePosF(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
std::cout << "Mouse Pos [" << mousePosF.x << ", " << mousePosF.y << "]" << std::endl;
std::cout << "Mouse Position [" << mousePosF.x << ", " << mousePosF.y << "]" << std::endl;
if (screenObjectsMap["playButton"].getGlobalBounds().contains(mousePosF)) {
return std::make_pair(ModeAction::Add, ModeOption::Game);
} else if (screenObjectsMap["settingsButton"].getGlobalBounds().contains(mousePosF)) {
Expand All @@ -34,7 +34,7 @@ std::pair<ModeAction, ModeOption> MenuMode::Run(sf::Time time, sf::RenderWindow&
return std::make_pair(ModeAction::DropTo, ModeOption::None);
break;
default:
std::cout << "Key [" << evnt.key.code << "] is not handeled by State [MenuMode]" << std::endl;
std::cout << "Key [" << evnt.key.code << "] is not handled by State [MenuMode]" << std::endl;
break;
}
break;
Expand All @@ -47,6 +47,6 @@ std::pair<ModeAction, ModeOption> MenuMode::Run(sf::Time time, sf::RenderWindow&
}
// Update Game Logic

// Im case of no state changes
// In case of no state changes
return std::make_pair(ModeAction::None, ModeOption::None);
}
46 changes: 46 additions & 0 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Settings.hpp"
#include <fstream>
#include <iostream>
#include <string>

Settings::Settings() {
std::fstream file;
file.open("assets/settings.txt", std::ios::in);
std::string line;
if (!file.fail()) {
std::getline(file, line, '\n');
std::cout << line << std::endl;
std::getline(file, line, '\n');
std::cout << line << std::endl;
if (line == "true") {
fpsLock = true;
} else {
fpsLock = false;
}
std::getline(file, line, '\n');
std::cout << line << std::endl;
std::getline(file, line, '\n');
std::cout << line << std::endl;
gameSpeed = std::stof(line);
} else {
file.open("assets/settings.txt", std::ios::out);
file << "# fpslock\n" << "false\n" << "# gamespeed\n" << ".10f\n";
fpsLock = false;
gameSpeed = .10f; //
}
file.close();
}

void Settings::saveToFile() {
std::fstream file;
file.open("assets/settings.txt", std::ios::out);
file << "# fpslock\n";
if (fpsLock) {
file << "true\n";
} else {
file << "false\n";
}
file << "# gamespeed\n";
file << std::to_string(gameSpeed);
file.close();
}
15 changes: 11 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Player.hpp>
#include <ModeList.hpp>
#include <RuntimeStats.hpp>
#include <Settings.hpp>
// Entry Point
int main() {
static std::mutex mu;
Expand All @@ -18,6 +19,11 @@ int main() {
desktop.width += 1;
sf::RenderWindow window(desktop, "Cnake", sf::Style::None);
window.setActive(false);
// Load Settings
Settings gameSettings;
if (gameSettings.getFpsLock()) {
window.setFramerateLimit(60);
}
// Prepare Stack
std::stack<std::unique_ptr<Mode>> ModeStack;
ModeStack.push(std::make_unique<IntroMode>(&mu));
Expand All @@ -27,7 +33,7 @@ int main() {
std::atomic<bool> isPaused = false;
std::atomic<bool> isWaiting = false;
// Create Rendering Thread
// TODO: Fix wierd rendering error that occurs when repushing old states
// TODO: Fix weird rendering error that occurs when re-pushing old states
std::thread RenderThread([&window, &ModeStack, &isRunning, &showStats, &isPaused, &isWaiting] {
// Window Settings
window.setActive(true);
Expand Down Expand Up @@ -60,7 +66,7 @@ int main() {
}
}
}
std::cout << "Yeh" << std::endl;
std::cout << "Rendering thread closed!" << std::endl;
});
// Begin Game
sf::Clock GameClock;
Expand All @@ -85,7 +91,7 @@ int main() {
ModeStack.push(std::make_unique<SettingsMode>(&mu));
break;
case ModeOption::Game:
ModeStack.push(std::make_unique<GameMode>(&mu));
ModeStack.push(std::make_unique<GameMode>(&mu, gameSettings.getGameSpeed()));
break;
case ModeOption::Paused:
case ModeOption::Lose:
Expand Down Expand Up @@ -132,7 +138,8 @@ int main() {
}
}
}
std::cout << "All done" << std::endl;
gameSettings.saveToFile();
std::cout << "Application closing" << std::endl;
//std::cin.ignore(1);
}

0 comments on commit f38e0d1

Please sign in to comment.