Skip to content

Commit

Permalink
Don't use bundled sprite collection, colorize them automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Smertig committed Jun 18, 2022
1 parent 3e73762 commit 24446ea
Show file tree
Hide file tree
Showing 49 changed files with 94 additions and 52 deletions.
Binary file removed res/body/0.png
Binary file not shown.
Binary file removed res/body/1.png
Binary file not shown.
Binary file removed res/body/10.png
Binary file not shown.
Binary file removed res/body/11.png
Binary file not shown.
Binary file removed res/body/2.png
Binary file not shown.
Binary file removed res/body/3.png
Binary file not shown.
Binary file removed res/body/4.png
Binary file not shown.
Binary file removed res/body/5.png
Binary file not shown.
Binary file removed res/body/6.png
Binary file not shown.
Binary file removed res/body/7.png
Binary file not shown.
Binary file removed res/body/8.png
Binary file not shown.
Binary file removed res/body/9.png
Binary file not shown.
Binary file added res/body/frame.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/body/inner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions res/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@
}
],
"player": {
"path": "res/player/{color}.png",
"frame_path": "res/player/frame.png",
"inner_path": "res/player/inner.png",
"pixel_width": 153,
"origin": [ 87, 204 ]
},
"ghost": {
"path": "res/ghost/{color}.png",
"frame_path": "res/ghost/frame.png",
"inner_path": "res/ghost/inner.png",
"pixel_width": 123,
"origin": [ 70, 141 ]
},
"body": {
"path": "res/body/{color}.png",
"frame_path": "res/body/frame.png",
"inner_path": "res/body/inner.png",
"pixel_width": 182,
"origin": [ 98, 123 ]
},
Expand Down
Binary file removed res/ghost/0.png
Binary file not shown.
Binary file removed res/ghost/1.png
Binary file not shown.
Binary file removed res/ghost/10.png
Binary file not shown.
Binary file removed res/ghost/11.png
Binary file not shown.
Binary file removed res/ghost/2.png
Binary file not shown.
Binary file removed res/ghost/3.png
Binary file not shown.
Binary file removed res/ghost/4.png
Binary file not shown.
Binary file removed res/ghost/5.png
Binary file not shown.
Binary file removed res/ghost/6.png
Binary file not shown.
Binary file removed res/ghost/7.png
Binary file not shown.
Binary file removed res/ghost/8.png
Binary file not shown.
Binary file removed res/ghost/9.png
Diff not rendered.
Binary file added res/ghost/frame.png
Binary file added res/ghost/inner.png
Binary file removed res/player/0.png
Diff not rendered.
Binary file removed res/player/1.png
Diff not rendered.
Binary file removed res/player/10.png
Diff not rendered.
Binary file removed res/player/11.png
Diff not rendered.
Binary file removed res/player/2.png
Diff not rendered.
Binary file removed res/player/3.png
Diff not rendered.
Binary file removed res/player/4.png
Diff not rendered.
Binary file removed res/player/5.png
Diff not rendered.
Binary file removed res/player/6.png
Diff not rendered.
Binary file removed res/player/7.png
Diff not rendered.
Binary file removed res/player/8.png
Diff not rendered.
Binary file removed res/player/9.png
Diff not rendered.
Binary file added res/player/frame.png
Binary file added res/player/inner.png
9 changes: 6 additions & 3 deletions res_hq/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@
}
],
"player": {
"path": "res/player/{color}.png",
"frame_path": "res/player/frame.png",
"inner_path": "res/player/inner.png",
"pixel_width": 153,
"origin": [ 87, 204 ]
},
"ghost": {
"path": "res/ghost/{color}.png",
"frame_path": "res/ghost/frame.png",
"inner_path": "res/ghost/inner.png",
"pixel_width": 123,
"origin": [ 70, 141 ]
},
"body": {
"path": "res/body/{color}.png",
"frame_path": "res/body/frame.png",
"inner_path": "res/body/inner.png",
"pixel_width": 182,
"origin": [ 98, 123 ]
},
Expand Down
36 changes: 15 additions & 21 deletions source/resources/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void from_json(const nlohmann::json& j, sf::Vector2<T>& result) {
namespace resources {

void from_json(const nlohmann::json& j, config::texture_config& result) {
result.path = j.at("path").get<std::string>();
result.frame_path = j.at("frame_path").get<std::string>();
result.inner_path = j.at("inner_path").get<std::string>();
result.width = j.at("pixel_width").get<float>();
result.origin = j.at("origin").get<sf::Vector2f>();
}
Expand Down Expand Up @@ -74,7 +75,12 @@ resources::config::config() {
throw std::runtime_error("expected color in form #RRGGBB");
}

m_colors.emplace_back(std::stol(color_string.substr(1), nullptr, 16));
std::uint32_t color = std::stol(color_string.substr(1), nullptr, 16);
m_colors.emplace_back(rgb_color{
/* .r = */ static_cast<std::uint8_t>(color >> 16),
/* .g = */ static_cast<std::uint8_t>(color >> 8),
/* .b = */ static_cast<std::uint8_t>(color >> 0)
});
}
}

Expand All @@ -83,18 +89,6 @@ const config &config::instance() {
return instance;
}

const sf::Vector2f& config::get_player_origin() {
return instance().m_player.origin;
}

const sf::Vector2f& config::get_ghost_origin() {
return instance().m_ghost.origin;
}

const sf::Vector2f& config::get_body_origin() {
return instance().m_body.origin;
}

float config::get_player_scale(int map_id) {
return instance().m_maps.at(map_id).player_pixel_width / instance().m_player.width;
}
Expand Down Expand Up @@ -123,19 +117,19 @@ const sf::Vector2f &config::get_center(int map_id) {
return instance().m_maps.at(map_id).center;
}

std::string config::get_player_path(int color) {
return fmt::format(instance().m_player.path, fmt::arg("color", color));
const config::texture_config& config::get_player_texture() {
return instance().m_player;
}

std::string config::get_ghost_path(int color) {
return fmt::format(instance().m_ghost.path, fmt::arg("color", color));
const config::texture_config& config::get_ghost_texture() {
return instance().m_ghost;
}

std::string config::get_body_path(int color) {
return fmt::format(instance().m_body.path, fmt::arg("color", color));
const config::texture_config& config::get_body_texture() {
return instance().m_body;
}

std::optional<std::uint32_t> config::try_get_color(int color_id) {
std::optional<rgb_color> config::try_get_color(int color_id) {
const auto& colors = instance().m_colors;
if (color_id >= colors.size()) {
return std::nullopt;
Expand Down
21 changes: 11 additions & 10 deletions source/resources/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace resources {

struct rgb_color {
std::uint8_t r, g, b;
};

class config {
public:
struct map_config {
Expand All @@ -22,7 +26,8 @@ class config {
};

struct texture_config {
std::string path;
std::string frame_path;
std::string inner_path;
float width;
sf::Vector2f origin;
};
Expand All @@ -32,17 +37,13 @@ class config {
texture_config m_player;
texture_config m_ghost;
texture_config m_body;
std::vector<std::uint32_t> m_colors;
std::vector<rgb_color> m_colors;

config();

static const config& instance();

public:
static const sf::Vector2f& get_player_origin();
static const sf::Vector2f& get_ghost_origin();
static const sf::Vector2f& get_body_origin();

static float get_player_scale(int map_id);
static float get_ghost_scale(int map_id);
static float get_body_scale(int map_id);
Expand All @@ -52,11 +53,11 @@ class config {
static float get_default_zoom(int map_id);
static const sf::Vector2f& get_center(int map_id);

static std::string get_player_path(int color);
static std::string get_ghost_path(int color);
static std::string get_body_path(int color);
static const texture_config& get_player_texture();
static const texture_config& get_ghost_texture();
static const texture_config& get_body_texture();

static std::optional<std::uint32_t> try_get_color(int color_id);
static std::optional<rgb_color> try_get_color(int color_id);
};

} // namespace resources
Expand Down
30 changes: 20 additions & 10 deletions source/scene/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@

namespace scene {

player::player(std::uint8_t id, std::uint8_t color, const std::string& name, bool is_impostor) {
player::player(std::uint8_t id, std::uint8_t color_id, const std::string& name, bool is_impostor) {
m_id = id;

m_alive_sprite.load(resources::config::get_player_path(color));
m_alive_sprite.setOrigin(resources::config::get_player_origin());
auto color = [color_id] {
const auto result = resources::config::try_get_color(color_id);
if (!result) {
throw std::runtime_error(fmt::format("unexpected color id #{}", color_id));
}

return *result;
}();

m_dead_sprite.load(resources::config::get_ghost_path(color));
m_dead_sprite.setOrigin(resources::config::get_ghost_origin());
m_dead_sprite.setColor(sf::Color(255, 255, 255, 100));
auto init_sprite = [color](colored_sprite& sprite, const auto& config, std::uint8_t alpha = 0xFF) {
sprite.load(config.frame_path, config.inner_path);
sprite.setColor(sf::Color(color.r, color.g, color.b, alpha));
sprite.setOrigin(config.origin);
};

m_dead_body_sprite.load(resources::config::get_body_path(color));
m_dead_body_sprite.setOrigin(resources::config::get_body_origin());
init_sprite(m_alive_sprite, resources::config::get_player_texture());
init_sprite(m_dead_sprite, resources::config::get_ghost_texture(), 130);
init_sprite(m_dead_body_sprite, resources::config::get_body_texture());

static sf::Font font = []{
sf::Font font;
Expand Down Expand Up @@ -45,11 +54,12 @@ void player::set_scale(float alive_scale, float dead_scale, float dead_body_scal

// dirty hacks..
const auto expected_width = 87.f;
const auto real_width = m_alive_sprite.getLocalBounds().width * alive_scale;
const auto actual_size = m_alive_sprite.frame.getLocalBounds();
const auto real_width = actual_size.width * alive_scale;

m_name.setCharacterSize(static_cast<unsigned>(25 * real_width / expected_width));
m_name.setOutlineThickness(2.0f * real_width / expected_width);
m_name.setPosition(std::round(-m_name.getLocalBounds().width / 2.f), std::round(-m_alive_sprite.getLocalBounds().height * 1.2f * alive_scale));
m_name.setPosition(std::round(-m_name.getLocalBounds().width / 2.f), std::round(-actual_size.height * 1.2f * alive_scale));
}

void player::set_position(sf::Vector2f pos, bool dir_is_right) {
Expand Down
39 changes: 35 additions & 4 deletions source/scene/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,41 @@
namespace scene {

class player {
struct colored_sprite : public sf::Drawable {
resources::textured_sprite frame;
resources::textured_sprite inner;

void load(const std::string& frame_path, const std::string& inner_path) {
frame.load(frame_path);
inner.load(inner_path);
}

void setColor(sf::Color color) {
frame.setColor(sf::Color(255, 255, 255, color.a));
inner.setColor(color);
}

void setOrigin(const sf::Vector2f& origin) {
frame.setOrigin(origin);
inner.setOrigin(origin);
}

void setScale(float scale_x, float scale_y) {
frame.setScale(scale_x, scale_y);
inner.setScale(scale_x, scale_y);
}

protected:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
target.draw(frame, states);
target.draw(inner, states);
}
};

std::uint8_t m_id;
resources::textured_sprite m_alive_sprite;
resources::textured_sprite m_dead_sprite;
resources::textured_sprite m_dead_body_sprite;
colored_sprite m_alive_sprite;
colored_sprite m_dead_sprite;
colored_sprite m_dead_body_sprite;
sf::Transform m_body_transform;
sf::Transform m_body_direction;
sf::Transform m_dead_body_transform;
Expand All @@ -17,7 +48,7 @@ class player {
sf::Text m_name;

public:
explicit player(std::uint8_t id, std::uint8_t color, const std::string& name, bool is_impostor);
explicit player(std::uint8_t id, std::uint8_t color_id, const std::string& name, bool is_impostor);

std::uint8_t get_id() const;

Expand Down
2 changes: 1 addition & 1 deletion source/ui/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const char* get_map_name(int map_id) {

ImColor ui::utils::convert_color(int color_id) {
const auto color = resources::config::try_get_color(color_id);
return color ? ImColor(*color) : ImColor();
return color ? ImColor(color->r, color->g, color->b) : ImColor();
}

} // namespace ui::utils

0 comments on commit 24446ea

Please sign in to comment.