Configuration Update
Clamp Values Fullscreen Background Color Grid Size
This commit is contained in:
parent
cd33996222
commit
a4bce8c766
|
@ -4,26 +4,34 @@ class GameConfiguration
|
|||
{
|
||||
private:
|
||||
sf::Vector2i referenceWindowDimensions;
|
||||
sf::Vector2i windowDimensions;
|
||||
sf::Vector2f canvasSizeMultiplierRatio;
|
||||
sf::Color backgroundColor;
|
||||
sf::Texture spriteSheet;
|
||||
std::string spriteSheetPath;
|
||||
sf::Vector2i spriteSheetCellSize;
|
||||
sf::IntRect spriteSheetRect;
|
||||
sf::Vector2i gridSize;
|
||||
float spriteSizeMultiplier;
|
||||
|
||||
float audioVolume;
|
||||
bool fullscreen;
|
||||
|
||||
template <class T> T Clamp(T &, T = 0.0, T = 1.0);
|
||||
void NormalizeVector2f(sf::Vector2f &);
|
||||
void LoadDefaultValues();
|
||||
void LoadValuesFromFile(std::string);
|
||||
void WriteValuesToFile(std::string);
|
||||
void CalculateSprites();
|
||||
std::istringstream GetNextNonCommentLine(std::istream &);
|
||||
bool LoadSpriteSheet(std::string);
|
||||
bool IsFileExists(std::string);
|
||||
public:
|
||||
GameConfiguration();
|
||||
GameConfiguration(sf::Vector2i, std::string = std::string());
|
||||
GameConfiguration(std::string = std::string());
|
||||
float GetSpriteSizeMultiplier();
|
||||
float GetAudioVolume();
|
||||
sf::Color GetBackgroundColor();
|
||||
sf::Sprite GetSprite(unsigned int, sf::Vector2f = sf::Vector2f(0, 0), float = 0.0);
|
||||
~GameConfiguration();
|
||||
};
|
||||
|
@ -31,9 +39,10 @@ class GameConfiguration
|
|||
GameConfiguration::GameConfiguration()
|
||||
{
|
||||
LoadDefaultValues();
|
||||
CalculateSprites();
|
||||
}
|
||||
|
||||
GameConfiguration::GameConfiguration(sf::Vector2i windowDimensions, std::string configurationPath)
|
||||
GameConfiguration::GameConfiguration(std::string configurationPath)
|
||||
{
|
||||
if(!configurationPath.empty())
|
||||
if(IsFileExists(configurationPath))
|
||||
|
@ -46,12 +55,31 @@ GameConfiguration::GameConfiguration(sf::Vector2i windowDimensions, std::string
|
|||
else
|
||||
LoadDefaultValues();
|
||||
|
||||
CalculateSprites();
|
||||
}
|
||||
|
||||
void GameConfiguration::CalculateSprites()
|
||||
{
|
||||
spriteSheetRect = sf::IntRect(sf::Vector2i(0, 0), spriteSheetCellSize);
|
||||
spriteSizeMultiplier =
|
||||
(float)windowDimensions.x / (float)referenceWindowDimensions.x * canvasSizeMultiplierRatio.x +
|
||||
(float)windowDimensions.y / (float)referenceWindowDimensions.y * canvasSizeMultiplierRatio.y;
|
||||
}
|
||||
|
||||
void GameConfiguration::NormalizeVector2f(sf::Vector2f &vector)
|
||||
{
|
||||
float magnitude = sqrt(vector.x * vector.x + vector.y * vector.y);
|
||||
vector.x /= magnitude;
|
||||
vector.y /= magnitude;
|
||||
}
|
||||
|
||||
template <class T> T GameConfiguration::Clamp(T &value, T min, T max)
|
||||
{
|
||||
if(value < min) value = min;
|
||||
else if(value > max) value = max;
|
||||
return value;
|
||||
}
|
||||
|
||||
bool GameConfiguration::IsFileExists(std::string path)
|
||||
{
|
||||
std::fstream file(path);
|
||||
|
@ -62,13 +90,26 @@ 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
|
||||
char coma; // This looks so stupid there has to be a better way
|
||||
int bgColor[3];
|
||||
|
||||
GetNextNonCommentLine(stream) >> gridSize.x >> coma >> gridSize.y;
|
||||
GetNextNonCommentLine(stream) >> windowDimensions.x >> coma >> windowDimensions.y;
|
||||
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;
|
||||
GetNextNonCommentLine(stream) >> fullscreen;
|
||||
GetNextNonCommentLine(stream) >> bgColor[0] >> coma >> bgColor[1] >> coma >> bgColor[2];
|
||||
|
||||
Clamp(audioVolume);
|
||||
Clamp(gridSize.x, 5, 100);
|
||||
Clamp(gridSize.y, 5, 100);
|
||||
backgroundColor.r = Clamp(bgColor[0], 0, 255);
|
||||
backgroundColor.g = Clamp(bgColor[1], 0, 255);
|
||||
backgroundColor.b = Clamp(bgColor[2], 0, 255);
|
||||
NormalizeVector2f(canvasSizeMultiplierRatio);
|
||||
|
||||
confFile.close();
|
||||
}
|
||||
|
@ -91,30 +132,44 @@ void GameConfiguration::WriteValuesToFile(std::string path)
|
|||
{
|
||||
std::ofstream confFile(path);
|
||||
|
||||
confFile << "# Grid Size (x, y) (5 - 100)\n";
|
||||
confFile << gridSize.x << ", " << gridSize.y << "\n\n";
|
||||
confFile << "# Window Dimensions (x, y)\n";
|
||||
confFile << windowDimensions.x << ", " << windowDimensions.y << "\n\n";
|
||||
confFile << "# Reference Window Dimensions (x, y)\n";
|
||||
confFile << referenceWindowDimensions.x << ", " << referenceWindowDimensions.y << "\n\n";
|
||||
confFile << "# Canvas Size Multiplier Ratio (x, y)\n";
|
||||
confFile << "# Canvas Size Multiplier Ratio (x, y) (0.0 - 1.0)\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 << "# Audio Volume (0.0 - 1.0)\n";
|
||||
confFile << audioVolume << "\n\n";
|
||||
confFile << "# Fullscreen 1 = on, 0 = off\n";
|
||||
confFile << fullscreen << "\n";
|
||||
confFile << "# Background Color (r, g, b) (0 - 255)\n";
|
||||
confFile << (int)backgroundColor.r << ", " << (int)backgroundColor.g << ", " << (int)backgroundColor.b << "\n";
|
||||
|
||||
confFile.close();
|
||||
}
|
||||
|
||||
void GameConfiguration::LoadDefaultValues()
|
||||
{
|
||||
windowDimensions = sf::Vector2i(800, 600);
|
||||
// !! Placeholder values! Change after real sprites are added! !!
|
||||
referenceWindowDimensions = sf::Vector2i(600, 500);
|
||||
referenceWindowDimensions = sf::Vector2i(800, 600);
|
||||
canvasSizeMultiplierRatio = sf::Vector2f(0.0, 1.0f);
|
||||
spriteSizeMultiplier = 1.0;
|
||||
|
||||
backgroundColor = sf::Color::White;
|
||||
|
||||
gridSize = sf::Vector2i(10, 10);
|
||||
|
||||
LoadSpriteSheet("Data\\SpriteSheet.png");
|
||||
// !! Placeholder values! Change after real sprites are added! !!
|
||||
spriteSheetCellSize = sf::Vector2i(50, 50);
|
||||
fullscreen = false;
|
||||
|
||||
audioVolume = 1.0;
|
||||
}
|
||||
|
@ -142,6 +197,11 @@ sf::Sprite GameConfiguration::GetSprite(unsigned int index, sf::Vector2f positio
|
|||
return sprite;
|
||||
}
|
||||
|
||||
sf::Color GameConfiguration::GetBackgroundColor()
|
||||
{
|
||||
return backgroundColor;
|
||||
}
|
||||
|
||||
float GameConfiguration::GetSpriteSizeMultiplier()
|
||||
{
|
||||
return spriteSizeMultiplier;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <cmath>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "GameConfiguration.hpp"
|
||||
#include "GameWindow.hpp"
|
||||
|
|
4
main.cpp
4
main.cpp
|
@ -2,12 +2,12 @@
|
|||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
GameConfiguration config = GameConfiguration(sf::Vector2i(800, 600), "Configurations");
|
||||
GameConfiguration config = GameConfiguration("Configurations");
|
||||
sf::RenderWindow render(sf::VideoMode(800, 600), "Test");
|
||||
|
||||
if (render.isOpen())
|
||||
{
|
||||
render.clear();
|
||||
render.clear(config.GetBackgroundColor());
|
||||
render.draw(config.GetSprite(0, sf::Vector2f(0, 0)));
|
||||
render.draw(config.GetSprite(1, sf::Vector2f(50, 0)));
|
||||
render.draw(config.GetSprite(2, sf::Vector2f(100, 0)));
|
||||
|
|
Loading…
Reference in New Issue