Default Configuration Class

Spritesheet Implementation
Sprite Display
Sprite Size by Display Resolution
Placeholder Spritesheet Image
This commit is contained in:
Syntriax 2020-06-04 11:47:16 +03:00
parent 85a7a090b5
commit 8183ad080e
7 changed files with 183 additions and 6 deletions

46
.vscode/settings.json vendored
View File

@ -1,5 +1,49 @@
{
"files.associations": {
"iostream": "cpp"
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}

BIN
Data/SpriteSheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -1,4 +0,0 @@
#ifndef Engine
#define Engine
#include <iostream>
#endif

95
GameConfiguration.hpp Normal file
View File

@ -0,0 +1,95 @@
#include "SynEngine.hpp"
class GameConfiguration
{
private:
sf::Vector2i referenceWindowDimensions;
sf::Vector2f canvasSizeMultiplierRatio;
sf::Texture spriteSheet;
std::string spriteSheetPath;
sf::Vector2i spriteSheetCellSize;
sf::IntRect spriteSheetRect;
float spriteSizeMultiplier;
float audioVolume;
void LoadDefaultValues();
bool LoadSpriteSheet(std::string);
public:
GameConfiguration();
GameConfiguration(sf::Vector2i, std::string = std::string());
float GetSpriteSizeMultiplier();
float GetAudioVolume();
sf::Sprite GetSprite(unsigned int, sf::Vector2f = sf::Vector2f(0, 0), float = 0.0);
~GameConfiguration();
};
GameConfiguration::GameConfiguration()
{
LoadDefaultValues();
}
GameConfiguration::GameConfiguration(sf::Vector2i windowDimensions, std::string configurationPath)
{
if(!configurationPath.empty())
std::cout << "External Configurations Are Not Implemented Yet!\n";
// else
LoadDefaultValues();
spriteSizeMultiplier =
(float)windowDimensions.x / (float)referenceWindowDimensions.x * canvasSizeMultiplierRatio.x +
(float)windowDimensions.y / (float)referenceWindowDimensions.y * canvasSizeMultiplierRatio.y;
}
void GameConfiguration::LoadDefaultValues()
{
// !! Placeholder values! Change after real sprites are added! !!
referenceWindowDimensions = sf::Vector2i(600, 500);
canvasSizeMultiplierRatio = sf::Vector2f(0.0, 1.0f);
spriteSizeMultiplier = 1.0;
LoadSpriteSheet("Data\\SpriteSheet.png");
// !! Placeholder values! Change after real sprites are added! !!
spriteSheetCellSize = sf::Vector2i(50, 50);
spriteSheetRect = sf::IntRect(sf::Vector2i(0, 0), spriteSheetCellSize);
audioVolume = 1.0;
}
bool GameConfiguration::LoadSpriteSheet(std::string path)
{
spriteSheetPath = path;
return spriteSheet.loadFromFile(path);
}
sf::Sprite GameConfiguration::GetSprite(unsigned int index, sf::Vector2f position, float rotation)
{
int offset = spriteSheetRect.width * index;
int xSize = spriteSheet.getSize().x;
sf::Sprite sprite;
spriteSheetRect.left = offset % xSize;
spriteSheetRect.top = offset / xSize * spriteSheetCellSize.y;
sprite.setTexture(spriteSheet);
sprite.setTextureRect(spriteSheetRect);
sprite.setPosition(position * spriteSizeMultiplier);
sprite.setRotation(rotation);
sprite.setScale(spriteSizeMultiplier, spriteSizeMultiplier);
std::cout << spriteSizeMultiplier << "\n";
return sprite;
}
float GameConfiguration::GetSpriteSizeMultiplier()
{
return spriteSizeMultiplier;
}
float GameConfiguration::GetAudioVolume()
{
return audioVolume;
}
GameConfiguration::~GameConfiguration()
{
}

19
GameWindow.hpp Normal file
View File

@ -0,0 +1,19 @@
#include "SynEngine.hpp"
class GameWindow
{
private:
sf::Vector2i windowDimensions;
sf::RenderWindow window;
public:
GameWindow(/* args */);
~GameWindow();
};
GameWindow::GameWindow(/* args */)
{
}
GameWindow::~GameWindow()
{
}

7
SynEngine.hpp Normal file
View File

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

View File

@ -1,7 +1,23 @@
#include "Engine.hpp"
#include "SynEngine.hpp"
int main(int argc, char const *argv[])
{
GameConfiguration config = GameConfiguration(sf::Vector2i(800, 600), "Alo");
sf::RenderWindow render(sf::VideoMode(800, 600), "Test");
if (render.isOpen())
{
render.clear();
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)));
render.display();
sf::sleep(sf::milliseconds(3000));
}
std::cout << "/)(\\";
return 0;
}