-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource Management System, JSON configs parsing
- Loading branch information
Showing
53 changed files
with
13,614 additions
and
316 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
|
Oops, something went wrong.