Grid Update

Grid System
Rotation and Updateing Cells
Game Manager Base
This commit is contained in:
Syntriax 2020-06-08 15:51:54 +03:00
parent 596200e329
commit 7b2841235d
7 changed files with 203 additions and 39 deletions

65
Cell.hpp Normal file
View File

@ -0,0 +1,65 @@
#include "SynEngine.hpp"
class Cell : public Drawable
{
private:
unsigned int spriteIndex;
sf::Vector2f position;
sf::Sprite sprite;
bool CreateSprite();
public:
Cell();
Cell(unsigned int, unsigned int, unsigned int = 1);
void Set(unsigned int, unsigned int, unsigned int = 1);
void UpdateSprite(unsigned int);
void UpdateRotation(unsigned);
void Draw(sf::RenderWindow *);
~Cell();
};
Cell::Cell()
{
spriteIndex = 0;
position = sf::Vector2f(0, 0);
}
Cell::Cell(unsigned int x, unsigned int y, unsigned int index)
{
Set(x, y, index);
}
void Cell::Set(unsigned int x, unsigned int y, unsigned int index)
{
spriteIndex = index;
position = sf::Vector2f(x, y);
CreateSprite();
}
bool Cell::CreateSprite()
{
GameConfiguration *config = GameManager::GetConfig();
if(!config) return false;
sprite = config -> GetSprite(spriteIndex, position);
return true;
}
void Cell::UpdateSprite(unsigned int index)
{
if(index == spriteIndex) return;
sprite.setTextureRect(GameManager::GetConfig() -> GetSpriteRect(index));
}
void Cell::UpdateRotation(unsigned direction)
{
sprite.setRotation(direction * 90.0);
}
void Cell::Draw(sf::RenderWindow *window)
{
window -> draw(sprite);
}
Cell::~Cell()
{
}

7
Drawable.hpp Normal file
View File

@ -0,0 +1,7 @@
#include "SynEngine.hpp"
class Drawable
{
public:
virtual void Draw(sf::RenderWindow *) = 0;
};

View File

@ -4,7 +4,6 @@ class GameConfiguration
{ {
private: private:
sf::Vector2i windowDimensions; sf::Vector2i windowDimensions;
sf::Vector2f canvasSizeMultiplierRatio;
sf::Color backgroundColor; sf::Color backgroundColor;
sf::Texture spriteSheet; sf::Texture spriteSheet;
std::string spriteSheetPath; std::string spriteSheetPath;
@ -22,7 +21,7 @@ class GameConfiguration
void LoadDefaultValues(); void LoadDefaultValues();
void LoadValuesFromFile(std::string); void LoadValuesFromFile(std::string);
void WriteValuesToFile(std::string); void WriteValuesToFile(std::string);
void CalculateSprites(); void CalculateCommonValues();
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);
@ -35,6 +34,8 @@ class GameConfiguration
sf::Vector2i GetScreenDimensions(); sf::Vector2i GetScreenDimensions();
sf::Vector2i GetGridSize(); sf::Vector2i GetGridSize();
sf::Vector2i GetGridOffset(); sf::Vector2i GetGridOffset();
sf::Vector2i GetSpriteCellSize();
sf::IntRect GetSpriteRect(unsigned int);
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();
}; };
@ -42,7 +43,7 @@ class GameConfiguration
GameConfiguration::GameConfiguration() GameConfiguration::GameConfiguration()
{ {
LoadDefaultValues(); LoadDefaultValues();
CalculateSprites(); CalculateCommonValues();
} }
GameConfiguration::GameConfiguration(std::string configurationPath) GameConfiguration::GameConfiguration(std::string configurationPath)
@ -58,10 +59,10 @@ GameConfiguration::GameConfiguration(std::string configurationPath)
else else
LoadDefaultValues(); LoadDefaultValues();
CalculateSprites(); CalculateCommonValues();
} }
void GameConfiguration::CalculateSprites() void GameConfiguration::CalculateCommonValues()
{ {
float offset; float offset;
sf::Vector2i gridDimensions = sf::Vector2i sf::Vector2i gridDimensions = sf::Vector2i
@ -69,29 +70,33 @@ void GameConfiguration::CalculateSprites()
gridSize.x * spriteSheetCellSize.x, gridSize.x * spriteSheetCellSize.x,
gridSize.y * spriteSheetCellSize.y gridSize.y * spriteSheetCellSize.y
); );
sf::Vector2f canvasSizeMultiplierRatio = sf::Vector2f
canvasSizeMultiplierRatio = sf::Vector2f
( (
(float)windowDimensions.x / (float)gridDimensions.x, (float)windowDimensions.x / (float)gridDimensions.x,
(float)windowDimensions.y / (float)gridDimensions.y (float)windowDimensions.y / (float)gridDimensions.y
); );
spriteSheetRect = sf::IntRect(sf::Vector2i(0, 0), spriteSheetCellSize);
if(canvasSizeMultiplierRatio.x < canvasSizeMultiplierRatio.y) if(canvasSizeMultiplierRatio.x < canvasSizeMultiplierRatio.y)
{ {
spriteSizeMultiplier = canvasSizeMultiplierRatio.x; spriteSizeMultiplier = canvasSizeMultiplierRatio.x;
offset = gridDimensions.y * spriteSizeMultiplier - windowDimensions.y; offset = gridDimensions.y * spriteSizeMultiplier - windowDimensions.y;
if(offset < 0) offset = -offset; if(offset < 0) offset = -offset;
gridOffset = sf::Vector2i(0, offset / 2); offset /= 2;
gridOffset = sf::Vector2i(0, offset);
} }
else else
{ {
spriteSizeMultiplier = canvasSizeMultiplierRatio.y; spriteSizeMultiplier = canvasSizeMultiplierRatio.y;
offset = gridDimensions.x * spriteSizeMultiplier - windowDimensions.x; offset = gridDimensions.x * spriteSizeMultiplier - windowDimensions.x;
if(offset < 0) offset = -offset; if(offset < 0) offset = -offset;
gridOffset = sf::Vector2i(offset / 2, 0); offset /= 2;
gridOffset = sf::Vector2i(offset, 0);
} }
// Center Origin Offset
gridOffset.x += spriteSheetCellSize.x / 2;
gridOffset.y += spriteSheetCellSize.y / 2;
spriteSheetRect = sf::IntRect(sf::Vector2i(0, 0), spriteSheetCellSize);
} }
void GameConfiguration::NormalizeVector2f(sf::Vector2f &vector) void GameConfiguration::NormalizeVector2f(sf::Vector2f &vector)
@ -130,8 +135,8 @@ void GameConfiguration::LoadValuesFromFile(std::string path)
GetNextNonCommentLine(stream) >> bgColor[0] >> coma >> bgColor[1] >> coma >> bgColor[2]; GetNextNonCommentLine(stream) >> bgColor[0] >> coma >> bgColor[1] >> coma >> bgColor[2];
Clamp(audioVolume); Clamp(audioVolume);
Clamp(gridSize.x, 10, 100); Clamp(gridSize.x, 5, 100);
Clamp(gridSize.y, 10, 100); Clamp(gridSize.y, 5, 100);
backgroundColor.r = Clamp(bgColor[0], 0, 255); backgroundColor.r = Clamp(bgColor[0], 0, 255);
backgroundColor.g = Clamp(bgColor[1], 0, 255); backgroundColor.g = Clamp(bgColor[1], 0, 255);
backgroundColor.b = Clamp(bgColor[2], 0, 255); backgroundColor.b = Clamp(bgColor[2], 0, 255);
@ -186,7 +191,6 @@ void GameConfiguration::LoadDefaultValues()
{ {
windowDimensions = sf::Vector2i(800, 600); windowDimensions = sf::Vector2i(800, 600);
// !! Placeholder values! Change after real sprites are added! !! // !! Placeholder values! Change after real sprites are added! !!
canvasSizeMultiplierRatio = sf::Vector2f(0.0, 1.0f);
spriteSizeMultiplier = 1.0; spriteSizeMultiplier = 1.0;
backgroundColor = sf::Color::White; backgroundColor = sf::Color::White;
@ -208,20 +212,26 @@ bool GameConfiguration::LoadSpriteSheet(std::string path)
} }
sf::Sprite GameConfiguration::GetSprite(unsigned int index, sf::Vector2f position, float rotation) sf::Sprite GameConfiguration::GetSprite(unsigned int index, sf::Vector2f position, float rotation)
{
sf::Sprite sprite;
sprite.setTexture(spriteSheet);
sprite.setTextureRect(GetSpriteRect(index));
sprite.setPosition(position);
sprite.setRotation(rotation);
sprite.setOrigin(spriteSheetCellSize.x / 2, spriteSheetCellSize.y / 2);
sprite.setScale(spriteSizeMultiplier, spriteSizeMultiplier);
return sprite;
}
sf::IntRect GameConfiguration::GetSpriteRect(unsigned int index)
{ {
int offset = spriteSheetRect.width * index; int offset = spriteSheetRect.width * index;
int xSize = spriteSheet.getSize().x; int xSize = spriteSheet.getSize().x;
sf::Sprite sprite;
spriteSheetRect.left = offset % xSize; spriteSheetRect.left = offset % xSize;
spriteSheetRect.top = offset / xSize * spriteSheetCellSize.y; spriteSheetRect.top = offset / xSize * spriteSheetCellSize.y;
return spriteSheetRect;
sprite.setTexture(spriteSheet);
sprite.setTextureRect(spriteSheetRect);
sprite.setPosition(position);
sprite.setRotation(rotation);
sprite.setScale(spriteSizeMultiplier, spriteSizeMultiplier);
return sprite;
} }
sf::Vector2i GameConfiguration::GetGridSize() sf::Vector2i GameConfiguration::GetGridSize()
@ -234,6 +244,11 @@ sf::Vector2i GameConfiguration::GetGridOffset()
return gridOffset; return gridOffset;
} }
sf::Vector2i GameConfiguration::GetSpriteCellSize()
{
return spriteSheetCellSize;
}
sf::Vector2i GameConfiguration::GetScreenDimensions() sf::Vector2i GameConfiguration::GetScreenDimensions()
{ {
return windowDimensions; return windowDimensions;

25
GameManager.hpp Normal file
View File

@ -0,0 +1,25 @@
#include "SynEngine.hpp"
class GameConfiguration;
class GameManager
{
private:
static GameConfiguration *config;
public:
static void SetConfig(GameConfiguration &);
static GameConfiguration *GetConfig();
};
GameConfiguration *GameManager::config = NULL;
void GameManager::SetConfig(GameConfiguration &config)
{
GameManager::config = &config;
}
GameConfiguration *GameManager::GetConfig()
{
return GameManager::config;
}

View File

@ -1,36 +1,87 @@
#include "SynEngine.hpp" #include "SynEngine.hpp"
class Grid class Grid : public Drawable
{ {
private: private:
sf::Vector2i offset; sf::Vector2i offset;
sf::Vector2i gridSize; sf::Vector2i gridSize;
GameConfiguration *config; Cell *cellArray;
unsigned int cellCount;
Cell *CreateCellArray(sf::Vector2i);
public: public:
Grid(); Grid();
bool SetGrid(GameConfiguration &); bool SetGrid();
void UpdateCell(unsigned int, unsigned int, unsigned int, int = -1);
void Draw(sf::RenderWindow *);
~Grid(); ~Grid();
}; };
Grid::Grid() Grid::Grid()
{ {
config = NULL; cellArray = NULL;
} }
bool Grid::SetGrid(GameConfiguration &config) Cell *Grid::CreateCellArray(sf::Vector2i)
{ {
this -> config = &config; cellCount = gridSize.x *gridSize.y;
Cell *array = new Cell[cellCount];
if(!this -> config) return false; if(!array) return NULL;
sf::Vector2i screenDimensions = config.GetScreenDimensions(); GameConfiguration *config = GameManager::GetConfig();
gridSize = config.GetGridSize();
offset = config.GetGridOffset(); sf::Vector2i cellSize = config -> GetSpriteCellSize();
float sizeMultiplier = config -> GetSpriteSizeMultiplier();
int xPos;
int yPos;
int i;
for (i = 0; i < cellCount; i++)
{
xPos = (i % gridSize.x) * cellSize.x * sizeMultiplier + offset.x;
yPos = (i / gridSize.x) * cellSize.y * sizeMultiplier + offset.y;
(array + i) -> Set(xPos, yPos);
}
return array;
}
bool Grid::SetGrid()
{
GameConfiguration *config = GameManager::GetConfig();
if(!config) return false;
sf::Vector2i screenDimensions = config -> GetScreenDimensions();
gridSize = config -> GetGridSize();
offset = config -> GetGridOffset();
cellArray = CreateCellArray(gridSize);
return true; return true;
} }
void Grid::UpdateCell(unsigned int x, unsigned int y, unsigned int spriteIndex, int direction)
{
int index = x + gridSize.x * y;
Cell *cell = cellArray + index;
cell -> UpdateSprite(spriteIndex);
if(direction >= 0)
cell -> UpdateRotation(direction);
}
void Grid::Draw(sf::RenderWindow *window)
{
int i;
for (i = 0; i < cellCount; i++)
(cellArray + i) -> Draw(window);
}
Grid::~Grid() Grid::~Grid()
{ {
delete[] cellArray;
} }

View File

@ -5,7 +5,10 @@
#include <fstream> #include <fstream>
#include <cmath> #include <cmath>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "GameManager.hpp"
#include "GameConfiguration.hpp" #include "GameConfiguration.hpp"
#include "Drawable.hpp"
#include "Cell.hpp"
#include "Grid.hpp" #include "Grid.hpp"
#include "GameWindow.hpp" #include "GameWindow.hpp"
#endif #endif

View File

@ -3,20 +3,18 @@
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
GameConfiguration config = GameConfiguration("Configurations"); GameConfiguration config = GameConfiguration("Configurations");
GameManager::SetConfig(config);
Grid grid = Grid(); Grid grid = Grid();
grid.SetGrid(config); grid.SetGrid();
sf::RenderWindow render(sf::VideoMode(800, 600), "Test"); sf::RenderWindow render(sf::VideoMode(800, 600), "Test");
if (render.isOpen()) // Quick Test 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.clear(config.GetBackgroundColor());
for (int i = 0; i < count; i++) grid.UpdateCell(0, 0, 2);
render.draw(config.GetSprite(i % 6, sf::Vector2f(i % config.GetGridSize().x * 50 * multiplier + offset.x, i / config.GetGridSize().x * 50 * multiplier + offset.y))); grid.UpdateCell(0, 1, 2, 1);
grid.Draw(&render);
render.display(); render.display();
sf::sleep(sf::milliseconds(5000)); sf::sleep(sf::milliseconds(5000));
} }