Skip to content

Commit

Permalink
Added SFML as bundled module, added label to button entity constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Arroganz committed Jul 29, 2018
1 parent 22f1da2 commit 1163e38
Show file tree
Hide file tree
Showing 104 changed files with 6,752 additions and 2 deletions.
76 changes: 76 additions & 0 deletions projects/fender/bundledModules/SFML/AssetLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Created by clara on 1/12/18.
//

#ifdef _WIN32
# include <filesystem>
#endif
#include <experimental/filesystem>
#include <regex>
#include <string>
#include "AssetLoader.hpp"

namespace fender::systems::SFMLSystems
{
void AssetLoader::init() {
__init();
addReaction<RequestAssets>([this](futils::IMediatorPacket &){
AssetsLoaded assets;
assets.fonts = &_fonts;
assets.textures = &_textures;
events->send<AssetsLoaded>(assets);
});

std::regex patternTexture { ".*.[png|jpg|jpeg]" };
std::regex patternFont { ".*.ttf" };
// TODO: Make it easier to set the resources folder
std::string assetDir = "ressources/";

try {
std::experimental::filesystem::path path{assetDir.c_str()};
if (std::experimental::filesystem::exists(path)) {
std::string pathFile;
for (auto &p : std::experimental::filesystem::recursive_directory_iterator(path)) {

pathFile = p.path().string();

std::string file = p.path().string();
file.erase(0, assetDir.size());

if (std::regex_match(file, patternTexture)) {
sf::Texture texture;
if (!texture.loadFromFile(pathFile)) {
//events->send<std::string>("Texture " + file->d_name + " not found.");
continue;
}
events->send<std::string>("Texture " + file + " loaded.");
_textures[file] = texture;
} else if (std::regex_match(file, patternFont)) {
sf::Font font;
if (!font.loadFromFile(pathFile)) {
//events->send<std::string>("\e[31m ☒ \e[00m Font \"" + file->d_name + "\" not found.");
continue;
}
events->send<std::string>("Font " + file + " loaded.");
_fonts[file] = font;
}
}
} else {
events->send<std::string>("[Warning] Could not load assets from unexisting directory " + assetDir);
}
}
catch (...) {
std::cerr << "Something went wrong in SFMLAssetLoader" << std::endl;
return ;
}
phase = Run;
}

void AssetLoader::run(float) {
switch (phase)
{
case Init: return init();
case Run: return ;
}
}
}
39 changes: 39 additions & 0 deletions projects/fender/bundledModules/SFML/AssetLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Created by clara on 1/12/18.
//

#pragma once

# include <SFML/Graphics.hpp>
# include "System.hpp"

namespace fender::systems::SFMLSystems
{
struct RequestAssets {

};

//TODO: Maybe 1 pkg for font and an other one for textures?

struct AssetsLoaded {
std::unordered_map<std::string, sf::Texture> *textures;
std::unordered_map<std::string, sf::Font> *fonts;
};

class AssetLoader : public System {
enum States
{
Init = 0,
Run
};

std::unordered_map<std::string, sf::Texture> _textures;
std::unordered_map<std::string, sf::Font> _fonts;
void init();

public:
AssetLoader(): System("AssetLoader") {}
void run(float) override;
};
}

74 changes: 74 additions & 0 deletions projects/fender/bundledModules/SFML/Border.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Created by arroganz on 1/7/18.
//

#include "inputKeys.hpp"
#include "Border.hpp"
#include "Camera.hpp"

namespace fender::systems::SFMLSystems
{
void Border::renderBorder(components::Border const &border, sf::RenderWindow &window)
{
auto &absolute = border.getEntity().get<components::AbsoluteTransform>();
sf::RectangleShape shape;
sf::Color color;
color << border.color;
if (border.up && border.down && border.left && border.right) {
shape.setFillColor(sf::Color::Transparent);
shape.setOutlineThickness(border.thickness);
shape.setOutlineColor(color);
shape.setSize(sf::Vector2f(absolute.size.w, absolute.size.h));
shape.setPosition(absolute.position.x, absolute.position.y);
window.draw(shape);
} else {
sf::RectangleShape line;
line.setFillColor(color);
if (border.up) {
line.setPosition(absolute.position.x, absolute.position.y - border.thickness);
line.setSize(sf::Vector2f(absolute.size.w, border.thickness));
window.draw(line);
}
if (border.down) {
line.setPosition(absolute.position.x, absolute.position.y + absolute.size.h);
line.setSize(sf::Vector2f(absolute.size.w, border.thickness));
window.draw(line);
}
if (border.left) {
line.setPosition(absolute.position.x - border.thickness, absolute.position.y);
line.setSize(sf::Vector2f(border.thickness, absolute.size.h));
window.draw(line);
}
if (border.right) {
line.setPosition(absolute.position.x + absolute.size.w, absolute.position.y);
line.setSize(sf::Vector2f(border.thickness, absolute.size.h));
window.draw(line);
}
}
}

void Border::init() {
__init();
addReaction<RenderLayer>([this](futils::IMediatorPacket &pkg){
auto &packet = futils::Mediator::rebuild<RenderLayer>(pkg);
for (auto &obj: packet.objects)
{
if (!obj->has<components::Border>())
continue ;
auto &border = obj->get<components::Border>();
if (border.visible) {
renderBorder(border, *packet.window);
}
}
});
phase = Run;
}

void Border::run(float) {
switch (phase)
{
case Init: return init();
case Run: return ;
}
}
}
28 changes: 28 additions & 0 deletions projects/fender/bundledModules/SFML/Border.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Created by arroganz on 1/7/18.
//


#pragma once

# include <SFML/Graphics.hpp>
# include "System.hpp"
# include "Components/Border.hpp"

namespace fender::systems::SFMLSystems
{
class Border : public System {
enum States
{
Init = 0,
Run
};
void renderBorder(components::Border const &, sf::RenderWindow &);
void init();
bool shouldPrint; // debug only
public:
Border(): System("Border") {}
void run(float) override;
};
}

9 changes: 9 additions & 0 deletions projects/fender/bundledModules/SFML/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.9)
project(SFML VERSION 1.0 DESCRIPTION "SFML system for fender")
set(CMAKE_CXX_STANDARD 17)
FILE (GLOB SOURCES ./*.cpp ./*.hpp)
add_library(SFML SHARED ${SOURCES} include/events.hpp)
target_include_directories(SFML PUBLIC include utils)
target_link_libraries(SFML -lstdc++fs)
set_target_properties(SFML PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(SFML PROPERTIES PUBLIC_HEADER SFML.hpp)

0 comments on commit 1163e38

Please sign in to comment.