Grid Configuration Update

Removed Reference Window Dimensions and changed the sizing calculations accordingly.
This commit is contained in:
Syntriax 2020-06-07 22:14:08 +03:00
parent a4bce8c766
commit 596200e329
4 changed files with 106 additions and 24 deletions

View File

@ -3,7 +3,6 @@
class GameConfiguration
{
private:
sf::Vector2i referenceWindowDimensions;
sf::Vector2i windowDimensions;
sf::Vector2f canvasSizeMultiplierRatio;
sf::Color backgroundColor;
@ -12,6 +11,7 @@ class GameConfiguration
sf::Vector2i spriteSheetCellSize;
sf::IntRect spriteSheetRect;
sf::Vector2i gridSize;
sf::Vector2i gridOffset;
float spriteSizeMultiplier;
float audioVolume;
@ -32,6 +32,9 @@ class GameConfiguration
float GetSpriteSizeMultiplier();
float GetAudioVolume();
sf::Color GetBackgroundColor();
sf::Vector2i GetScreenDimensions();
sf::Vector2i GetGridSize();
sf::Vector2i GetGridOffset();
sf::Sprite GetSprite(unsigned int, sf::Vector2f = sf::Vector2f(0, 0), float = 0.0);
~GameConfiguration();
};
@ -60,10 +63,35 @@ GameConfiguration::GameConfiguration(std::string configurationPath)
void GameConfiguration::CalculateSprites()
{
float offset;
sf::Vector2i gridDimensions = sf::Vector2i
(
gridSize.x * spriteSheetCellSize.x,
gridSize.y * spriteSheetCellSize.y
);
canvasSizeMultiplierRatio = sf::Vector2f
(
(float)windowDimensions.x / (float)gridDimensions.x,
(float)windowDimensions.y / (float)gridDimensions.y
);
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;
if(canvasSizeMultiplierRatio.x < canvasSizeMultiplierRatio.y)
{
spriteSizeMultiplier = canvasSizeMultiplierRatio.x;
offset = gridDimensions.y * spriteSizeMultiplier - windowDimensions.y;
if(offset < 0) offset = -offset;
gridOffset = sf::Vector2i(0, offset / 2);
}
else
{
spriteSizeMultiplier = canvasSizeMultiplierRatio.y;
offset = gridDimensions.x * spriteSizeMultiplier - windowDimensions.x;
if(offset < 0) offset = -offset;
gridOffset = sf::Vector2i(offset / 2, 0);
}
}
void GameConfiguration::NormalizeVector2f(sf::Vector2f &vector)
@ -95,8 +123,6 @@ void GameConfiguration::LoadValuesFromFile(std::string path)
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;
@ -104,12 +130,17 @@ void GameConfiguration::LoadValuesFromFile(std::string path)
GetNextNonCommentLine(stream) >> bgColor[0] >> coma >> bgColor[1] >> coma >> bgColor[2];
Clamp(audioVolume);
Clamp(gridSize.x, 5, 100);
Clamp(gridSize.y, 5, 100);
Clamp(gridSize.x, 10, 100);
Clamp(gridSize.y, 10, 100);
backgroundColor.r = Clamp(bgColor[0], 0, 255);
backgroundColor.g = Clamp(bgColor[1], 0, 255);
backgroundColor.b = Clamp(bgColor[2], 0, 255);
NormalizeVector2f(canvasSizeMultiplierRatio);
if(fullscreen)
{
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
windowDimensions = sf::Vector2i(desktop.width, desktop.height);
}
confFile.close();
}
@ -135,11 +166,8 @@ void GameConfiguration::WriteValuesToFile(std::string path)
confFile << "# Grid Size (x, y) (5 - 100)\n";
confFile << gridSize.x << ", " << gridSize.y << "\n\n";
confFile << "# Window Dimensions (x, y)\n";
confFile << "# If fullscreen set to 1 this will be skipped\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) (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";
@ -147,7 +175,7 @@ void GameConfiguration::WriteValuesToFile(std::string path)
confFile << "# Audio Volume (0.0 - 1.0)\n";
confFile << audioVolume << "\n\n";
confFile << "# Fullscreen 1 = on, 0 = off\n";
confFile << fullscreen << "\n";
confFile << fullscreen << "\n\n";
confFile << "# Background Color (r, g, b) (0 - 255)\n";
confFile << (int)backgroundColor.r << ", " << (int)backgroundColor.g << ", " << (int)backgroundColor.b << "\n";
@ -158,7 +186,6 @@ void GameConfiguration::LoadDefaultValues()
{
windowDimensions = sf::Vector2i(800, 600);
// !! Placeholder values! Change after real sprites are added! !!
referenceWindowDimensions = sf::Vector2i(800, 600);
canvasSizeMultiplierRatio = sf::Vector2f(0.0, 1.0f);
spriteSizeMultiplier = 1.0;
@ -191,12 +218,27 @@ sf::Sprite GameConfiguration::GetSprite(unsigned int index, sf::Vector2f positio
sprite.setTexture(spriteSheet);
sprite.setTextureRect(spriteSheetRect);
sprite.setPosition(position * spriteSizeMultiplier);
sprite.setPosition(position);
sprite.setRotation(rotation);
sprite.setScale(spriteSizeMultiplier, spriteSizeMultiplier);
return sprite;
}
sf::Vector2i GameConfiguration::GetGridSize()
{
return gridSize;
}
sf::Vector2i GameConfiguration::GetGridOffset()
{
return gridOffset;
}
sf::Vector2i GameConfiguration::GetScreenDimensions()
{
return windowDimensions;
}
sf::Color GameConfiguration::GetBackgroundColor()
{
return backgroundColor;

36
Grid.hpp Normal file
View File

@ -0,0 +1,36 @@
#include "SynEngine.hpp"
class Grid
{
private:
sf::Vector2i offset;
sf::Vector2i gridSize;
GameConfiguration *config;
public:
Grid();
bool SetGrid(GameConfiguration &);
~Grid();
};
Grid::Grid()
{
config = NULL;
}
bool Grid::SetGrid(GameConfiguration &config)
{
this -> config = &config;
if(!this -> config) return false;
sf::Vector2i screenDimensions = config.GetScreenDimensions();
gridSize = config.GetGridSize();
offset = config.GetGridOffset();
return true;
}
Grid::~Grid()
{
}

View File

@ -6,5 +6,6 @@
#include <cmath>
#include <SFML/Graphics.hpp>
#include "GameConfiguration.hpp"
#include "Grid.hpp"
#include "GameWindow.hpp"
#endif

View File

@ -3,19 +3,22 @@
int main(int argc, char const *argv[])
{
GameConfiguration config = GameConfiguration("Configurations");
Grid grid = Grid();
grid.SetGrid(config);
sf::RenderWindow render(sf::VideoMode(800, 600), "Test");
if (render.isOpen())
if (render.isOpen()) // Quick Test
{
float multiplier = config.GetSpriteSizeMultiplier();
int count = config.GetGridSize().x * config.GetGridSize().y;
sf::Vector2i offset = config.GetGridOffset();
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)));
render.draw(config.GetSprite(3, sf::Vector2f(0, 50)));
render.draw(config.GetSprite(4, sf::Vector2f(50, 50)));
render.draw(config.GetSprite(5, sf::Vector2f(100, 50)));
for (int i = 0; i < count; i++)
render.draw(config.GetSprite(i % 6, sf::Vector2f(i % config.GetGridSize().x * 50 * multiplier + offset.x, i / config.GetGridSize().x * 50 * multiplier + offset.y)));
render.display();
sf::sleep(sf::milliseconds(3000));
sf::sleep(sf::milliseconds(5000));
}
std::cout << "/)(\\";