Skip to content

Commit

Permalink
Resource Management System, JSON configs parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Insineer committed Sep 29, 2017
1 parent 29d8c5c commit d13ead9
Show file tree
Hide file tree
Showing 53 changed files with 13,614 additions and 316 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions GasProject Client/GasProject Client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<ClCompile Include="sources\Client.cpp" />
<ClCompile Include="Sources\Graphics\Chat.cpp" />
<ClCompile Include="Sources\Graphics\Sprite.cpp" />
<ClCompile Include="Sources\Graphics\Texture.cpp" />
<ClCompile Include="Sources\Graphics\TileGrid\Block.cpp" />
<ClCompile Include="Sources\Graphics\TileGrid\Object.cpp" />
<ClCompile Include="Sources\Graphics\TileGrid\Tile.cpp" />
Expand All @@ -185,11 +186,13 @@
<ClCompile Include="Sources\Network.cpp" />
<ClCompile Include="Sources\Graphics\TileGrid\TileGrid.cpp" />
<ClCompile Include="Sources\Graphics\UI\UI.cpp" />
<ClCompile Include="Sources\ResourceManager.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="sources\Client.hpp" />
<ClInclude Include="Sources\Graphics\Chat.hpp" />
<ClInclude Include="Sources\Graphics\Sprite.hpp" />
<ClInclude Include="Sources\Graphics\Texture.hpp" />
<ClInclude Include="Sources\Graphics\TileGrid.hpp" />
<ClInclude Include="Sources\Graphics\TileGrid\Block.hpp" />
<ClInclude Include="Sources\Graphics\TileGrid\Object.hpp" />
Expand All @@ -210,6 +213,7 @@
<ClInclude Include="Sources\Graphics\TileGrid\TileGrid.hpp" />
<ClInclude Include="Sources\Graphics\UI\UI.hpp" />
<ClInclude Include="Sources\Graphics\Window.hpp" />
<ClInclude Include="Sources\ResourceManager.hpp" />
</ItemGroup>
<ItemGroup>
<Image Include="Images\Ghost.png" />
Expand Down
12 changes: 12 additions & 0 deletions GasProject Client/GasProject Client.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<ClCompile Include="Sources\Graphics\UI\Style.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
<ClCompile Include="Sources\ResourceManager.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
<ClCompile Include="Sources\Graphics\Texture.cpp">
<Filter>Файлы исходного кода</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sources\Client.hpp">
Expand Down Expand Up @@ -149,6 +155,12 @@
<ClInclude Include="Sources\Graphics\UI\Style.hpp">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Sources\ResourceManager.hpp">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
<ClInclude Include="Sources\Graphics\Texture.hpp">
<Filter>Заголовочные файлы</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="Images\Human.png">
Expand Down
Binary file removed GasProject Client/Images/Turfs.png
Binary file not shown.
7 changes: 3 additions & 4 deletions GasProject Client/Sources/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Shared/Types.hpp"
#include "Shared/Log.hpp"
#include "ResourceManager.hpp"

using std::string;

Expand All @@ -17,15 +18,13 @@ class Player {

class ClientController {
private:
/* Unique_ptr will delete it contents in the destruction. So we don't need destructor.
But unique_ptr can be just one for it content. So we need to use links. See Get-functions. */
uptr<Player> player;
uptr<Window> window;
static ClientController * instance;

public:
/* Work of Client processing in this constructor.
Such system allow as awake just 1 function of Client from main. */
ResourceManager RM;

ClientController();
void Run();

Expand Down
87 changes: 25 additions & 62 deletions GasProject Client/Sources/Graphics/Sprite.cpp
Original file line number Diff line number Diff line change
@@ -1,91 +1,54 @@
#include "Sprite.hpp"

#include <string>

#include "Sprite.hpp"
#include "Client.hpp"
#include "Window.hpp"

#include <SFML/Graphics.hpp>

using std::string;

Texture::Texture(string path, int sizeOfTile, int numOfSprites) :
texture(new sf::Texture),
sizeOfTile(sizeOfTile),
spritesInfo(numOfSprites)
Sprite::Sprite(Texture *t, const std::string &key, uint firstFrame, uint frames, bool directed) :
texture(t), sprite(new sf::Sprite(*t->GetSFMLTexture())),
key(key), firstFrame(firstFrame), frames(frames), directed(directed),
curFrame(0), scale(1)
{
texture->loadFromFile(path);
xNumOfTiles = texture->getSize().x / sizeOfTile;
yNumOfTiles = texture->getSize().y / sizeOfTile;
}

bool Texture::PixelTransparent(const unsigned x, const unsigned y,
const int sprite,
const int direction,
const int frame) const
{
int realState = spritesInfo[sprite].firstFrame;
if (spritesInfo[sprite].directed) realState += (direction - 1) * spritesInfo[sprite].frames;
if (spritesInfo[sprite].frames > 1) realState += frame;

unsigned textureX = x + realState % xNumOfTiles * sizeOfTile;
unsigned textureY = y + realState / xNumOfTiles * sizeOfTile;

if (textureX < 0 || textureX >= texture->getSize().x || textureY < 0 || textureY >= texture->getSize().y) return true;
if (texture->copyToImage().getPixel(x, y).a == 0) return true;

return false;
}

Sprite *Texture::SetInfo(Global::Sprite key, int num, int firstFrame, bool directed, int frames) {
spritesInfo[num].firstFrame = firstFrame;
spritesInfo[num].directed = directed;
spritesInfo[num].frames = frames;
int firstFrameIndex = 0;
for (int i = 0; i < num; i++) {
if (spritesInfo[i].firstFrame == -1) {
CC::log << "Wrong order of sprites info setting" << std::endl;
break;
}
firstFrameIndex += spritesInfo[i].frames * (static_cast<int>(spritesInfo[i].directed) * 3 + 1);
}
return new Sprite(this, key, num, firstFrameIndex, frames, directed);
}

Sprite::Sprite() : sprite(new sf::Sprite()) {
texture = nullptr;
spriteIndex = frames = 0;
curFrame = -1;
scale = 1;
directed = false;
key = Global::Sprite::EMPTY;
}

Sprite::Sprite(Texture *t, Global::Sprite key, int spriteIndex, int firstFrameIndex, int frames, bool directed) :
sprite(new sf::Sprite(*t->GetSFMLTexture())),
texture(t),
spriteIndex(spriteIndex), firstFrameIndex(firstFrameIndex),
scale(1), directed(directed), frames(frames), curFrame(0), key(key)
{

}

void Sprite::Draw(sf::RenderTarget *target, const int x, const int y, const uf::Direction direction) const {
void Sprite::Draw(sf::RenderTarget *target, int x, int y, const uf::Direction direction) const {
sf::Rect<int> rect;

int realState = firstFrameIndex;
int realState = firstFrame;
if (directed && direction != uf::Direction::NONE) realState += int(direction) * frames;
if (frames > 1) realState += curFrame;

rect.left = realState % texture->GetXNumOfTiles() * texture->GetSizeOfTile();
rect.top = realState / texture->GetXNumOfTiles() * texture->GetSizeOfTile();
rect.width = rect.height = texture->GetSizeOfTile();

sprite->setPosition(static_cast<float>(x), static_cast<float>(y));
sprite->setPosition(float(x), float(y));
sprite->setTextureRect(rect);
sprite->setScale(scale, scale);
target->draw(*sprite);
}

void Sprite::Resize(int size) {
scale = float(size) / texture->GetSizeOfTile();
}
}

void Sprite::UpdateFrame() {
if (frames > 1) {
curFrame++;
if (curFrame >= frames)
curFrame = 0;
}
}

const std::string &Sprite::GetKey() const { return key; }
bool Sprite::IsAnimated() const { return frames > 1; }
bool Sprite::PixelTransparent(const int x, const int y) const {
if (texture->IsFramePixelTransparent(uint(x / scale), uint(y / scale), curFrame)) return true;
return false;
}
83 changes: 14 additions & 69 deletions GasProject Client/Sources/Graphics/Sprite.hpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,28 @@
#pragma once

#include <vector>
#include <string>

#include "Texture.hpp"
#include "Shared/Types.hpp"
#include "Shared/Global.hpp"

using std::vector;
using std::string;

class Sprite;

namespace sf {
class RenderTarget;
class Texture;
class Sprite;
}

struct TextureSpriteInfo {
int firstFrame;
int frames;
bool directed;

TextureSpriteInfo() { firstFrame = -1; frames = 0; directed = false; }
};

class Texture {
private:
uptr<sf::Texture> texture;
int sizeOfTile;
int xNumOfTiles, yNumOfTiles;
vector<TextureSpriteInfo> spritesInfo;

class Sprite {
public:
Texture(string path, int sizeOfTile, int numOfSprites);

Sprite *SetInfo(Global::Sprite, int num, int firstFrame, bool directed, int frames);

bool PixelTransparent(const unsigned x, const unsigned y,
const int sprite,
const int direction,
const int frame) const;
Sprite(Texture *t, const std::string &key, uint firstFrameIndex, uint frames, bool directed);

sf::Texture *GetSFMLTexture() const { return texture.get(); }
void Draw(sf::RenderTarget *, int x, int y, uf::Direction direction) const;
void Resize(int size);
void UpdateFrame();

int GetXNumOfTiles() const { return xNumOfTiles; }
int GetYNumOfTiles() const { return yNumOfTiles; }
int GetSizeOfTile() const { return sizeOfTile; }
};
const std::string &GetKey() const;
bool IsAnimated() const;
bool PixelTransparent(const int x, const int y) const;

class Sprite
{
private:
Texture *texture;
uptr<sf::Sprite> sprite;
Global::Sprite key;
int spriteIndex;
int firstFrameIndex;
float scale;
int frames;
std::string key;
uint firstFrame;
uint frames;
bool directed;

int curFrame;

public:
Sprite();
Sprite(Texture *t, Global::Sprite key, int spriteIndex, int firstFrameIndex, int frames, bool directed);

void Draw(sf::RenderTarget *, const int x, const int y, const uf::Direction direction) const;

void Resize(int size);

Global::Sprite GetKey() const { return key; }

bool Animated() { return frames > 1; }
void UpdateFrame() { ++curFrame %= frames; }

bool PixelTransparent(const int x, const int y) const {
if (texture->PixelTransparent(int(x / scale), int(y / scale), spriteIndex, directed, curFrame)) return true;
return false;
}
uint curFrame;
float scale;
};
64 changes: 64 additions & 0 deletions GasProject Client/Sources/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "Texture.hpp"

#include "Client.hpp"
#include "Sprite.hpp"

Texture::Texture(std::string path, nlohmann::json &config) :
texture(new sf::Texture)
{
texture->loadFromFile(path);

sizeOfTile = config["tileSize"];

xNumOfTiles = texture->getSize().x / sizeOfTile;
yNumOfTiles = texture->getSize().y / sizeOfTile;

std::string title; // Can be same for few sequential sprites
uint firstFrame = 0;
for (auto sprite_config : config["sprites"]) {
const auto titleIter = sprite_config.find("sprite");
if (titleIter != sprite_config.end()) {
title = titleIter->get<std::string>();
}

const auto framesIter = sprite_config.find("frames");
int frames = 1;
if (framesIter != sprite_config.end()) {
frames = framesIter->get<int>();
}

const auto directedIter = sprite_config.find("directed");
bool directed = false;
if (directedIter != sprite_config.end()) {
directed = directedIter->get<bool>();
}

SpriteInfo spriteInfo;
spriteInfo.firstFrame = firstFrame;
spriteInfo.directed = directed;
spriteInfo.frames = frames;

spritesInfo.push_back(std::move(spriteInfo));
sprites.push_back(new Sprite(this, title, firstFrame, frames, directed));

firstFrame += frames * (int(directed) * 3 + 1);
}
}

bool Texture::IsFramePixelTransparent(int x, int y, uint frame) const {
const uint textureX = x + frame % xNumOfTiles * sizeOfTile;
const uint textureY = y + frame / xNumOfTiles * sizeOfTile;

if (textureX < 0 || textureX >= texture->getSize().x || textureY < 0 || textureY >= texture->getSize().y) return true;
if (texture->copyToImage().getPixel(x, y).a == 0) return true;

return false;
}

const std::vector<Sprite *> &Texture::GetSprites() const { return sprites; }
sf::Texture *Texture::GetSFMLTexture() const { return texture.get(); }

int Texture::GetXNumOfTiles() const { return xNumOfTiles; }
int Texture::GetYNumOfTiles() const { return yNumOfTiles; }
int Texture::GetSizeOfTile() const { return sizeOfTile; }

Loading

0 comments on commit d13ead9

Please sign in to comment.