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