External Configuration File

Added a Configuration file to be able to change values
This commit is contained in:
Syntriax 2020-06-05 19:55:00 +03:00
parent 8183ad080e
commit cd33996222
5 changed files with 78 additions and 7 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Game Configurations
Configurations
# Prerequisites # Prerequisites
*.d *.d

View File

@ -44,6 +44,10 @@
"sstream": "cpp", "sstream": "cpp",
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"filesystem": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"ratio": "cpp"
} }
} }

View File

@ -14,7 +14,11 @@ class GameConfiguration
float audioVolume; float audioVolume;
void LoadDefaultValues(); void LoadDefaultValues();
void LoadValuesFromFile(std::string);
void WriteValuesToFile(std::string);
std::istringstream GetNextNonCommentLine(std::istream &);
bool LoadSpriteSheet(std::string); bool LoadSpriteSheet(std::string);
bool IsFileExists(std::string);
public: public:
GameConfiguration(); GameConfiguration();
GameConfiguration(sf::Vector2i, std::string = std::string()); GameConfiguration(sf::Vector2i, std::string = std::string());
@ -32,15 +36,75 @@ GameConfiguration::GameConfiguration()
GameConfiguration::GameConfiguration(sf::Vector2i windowDimensions, std::string configurationPath) GameConfiguration::GameConfiguration(sf::Vector2i windowDimensions, std::string configurationPath)
{ {
if(!configurationPath.empty()) if(!configurationPath.empty())
std::cout << "External Configurations Are Not Implemented Yet!\n"; if(IsFileExists(configurationPath))
// else LoadValuesFromFile(configurationPath);
else
{
LoadDefaultValues();
WriteValuesToFile(configurationPath);
}
else
LoadDefaultValues(); LoadDefaultValues();
spriteSheetRect = sf::IntRect(sf::Vector2i(0, 0), spriteSheetCellSize);
spriteSizeMultiplier = spriteSizeMultiplier =
(float)windowDimensions.x / (float)referenceWindowDimensions.x * canvasSizeMultiplierRatio.x + (float)windowDimensions.x / (float)referenceWindowDimensions.x * canvasSizeMultiplierRatio.x +
(float)windowDimensions.y / (float)referenceWindowDimensions.y * canvasSizeMultiplierRatio.y; (float)windowDimensions.y / (float)referenceWindowDimensions.y * canvasSizeMultiplierRatio.y;
} }
bool GameConfiguration::IsFileExists(std::string path)
{
std::fstream file(path);
return file.good();
}
void GameConfiguration::LoadValuesFromFile(std::string path)
{
std::ifstream confFile(path);
std::istream &stream = confFile;
char coma; // this is stupid there has to be a better way
GetNextNonCommentLine(stream) >> referenceWindowDimensions.x >> coma >> referenceWindowDimensions.y;
GetNextNonCommentLine(stream) >> canvasSizeMultiplierRatio.x >> coma >> canvasSizeMultiplierRatio.y;
LoadSpriteSheet(GetNextNonCommentLine(stream).str());
GetNextNonCommentLine(stream) >> spriteSheetCellSize.x >> coma >> spriteSheetCellSize.y;
GetNextNonCommentLine(stream) >> audioVolume;
confFile.close();
}
std::istringstream GameConfiguration::GetNextNonCommentLine(std::istream &stream)
{
std::string line;
while (std::getline(stream, line) && !stream.eof())
switch (line[0])
{
case '#': case '\n': case ' ': case NULL: break;
default:
return std::istringstream(line);
}
return std::istringstream();
}
void GameConfiguration::WriteValuesToFile(std::string path)
{
std::ofstream confFile(path);
confFile << "# Reference Window Dimensions (x, y)\n";
confFile << referenceWindowDimensions.x << ", " << referenceWindowDimensions.y << "\n\n";
confFile << "# Canvas Size Multiplier Ratio (x, y)\n";
confFile << canvasSizeMultiplierRatio.x << ", " << canvasSizeMultiplierRatio.y << "\n\n";
confFile << "# Sprite Sheet Path\n";
confFile << spriteSheetPath << "\n\n";
confFile << "# Sprite Sheet Cell Size (x, y)\n";
confFile << spriteSheetCellSize.x << ", " << spriteSheetCellSize.y << "\n\n";
confFile << "# Audio Volume\n";
confFile << audioVolume << "\n";
confFile.close();
}
void GameConfiguration::LoadDefaultValues() void GameConfiguration::LoadDefaultValues()
{ {
// !! Placeholder values! Change after real sprites are added! !! // !! Placeholder values! Change after real sprites are added! !!
@ -51,7 +115,6 @@ void GameConfiguration::LoadDefaultValues()
LoadSpriteSheet("Data\\SpriteSheet.png"); LoadSpriteSheet("Data\\SpriteSheet.png");
// !! Placeholder values! Change after real sprites are added! !! // !! Placeholder values! Change after real sprites are added! !!
spriteSheetCellSize = sf::Vector2i(50, 50); spriteSheetCellSize = sf::Vector2i(50, 50);
spriteSheetRect = sf::IntRect(sf::Vector2i(0, 0), spriteSheetCellSize);
audioVolume = 1.0; audioVolume = 1.0;
} }
@ -76,7 +139,6 @@ sf::Sprite GameConfiguration::GetSprite(unsigned int index, sf::Vector2f positio
sprite.setPosition(position * spriteSizeMultiplier); sprite.setPosition(position * spriteSizeMultiplier);
sprite.setRotation(rotation); sprite.setRotation(rotation);
sprite.setScale(spriteSizeMultiplier, spriteSizeMultiplier); sprite.setScale(spriteSizeMultiplier, spriteSizeMultiplier);
std::cout << spriteSizeMultiplier << "\n";
return sprite; return sprite;
} }

View File

@ -1,7 +1,9 @@
#ifndef SynEngine #ifndef SynEngine
#define SynEngine #define SynEngine
#include <iostream> #include <iostream>
#include <sstream>
#include <fstream>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "GameWindow.hpp"
#include "GameConfiguration.hpp" #include "GameConfiguration.hpp"
#include "GameWindow.hpp"
#endif #endif

View File

@ -2,7 +2,7 @@
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
GameConfiguration config = GameConfiguration(sf::Vector2i(800, 600), "Alo"); GameConfiguration config = GameConfiguration(sf::Vector2i(800, 600), "Configurations");
sf::RenderWindow render(sf::VideoMode(800, 600), "Test"); sf::RenderWindow render(sf::VideoMode(800, 600), "Test");
if (render.isOpen()) if (render.isOpen())