Skip to content

Commit

Permalink
Refactoring the view
Browse files Browse the repository at this point in the history
  • Loading branch information
Organic-Code committed Oct 11, 2023
1 parent ab342e1 commit 66b9530
Show file tree
Hide file tree
Showing 46 changed files with 251 additions and 323 deletions.
27 changes: 13 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(NINJA_CLOWN_SOURCES

src/adapter/adapter.cpp
src/adapter/adapter_map_loader_v1_0_0.cpp
src/adapter/facing_dir.cpp

src/bot/bot_api.cpp
src/bot/bot_dll.cpp
Expand All @@ -29,23 +30,21 @@ set(NINJA_CLOWN_SOURCES

src/utils/dll.cpp
src/utils/logging.cpp
src/utils/loop_per_sec_limit.cpp
src/utils/resource_manager.cpp
src/utils/system.cpp

src/view/animation.cpp
src/view/configurator.cpp
src/view/facing_dir.cpp
src/view/file_explorer.cpp
src/view/fps_limiter.cpp
src/view/game_viewer.cpp
src/view/imgui_styles.cpp
src/view/map.cpp
src/view/map_explorer.cpp
src/view/map_viewer.cpp
src/view/menu.cpp
src/view/mob_object.cpp
src/view/mob_animations.cpp
src/view/overmap_collection.cpp
src/view/assets/animation.cpp
src/view/game/game_viewer.cpp
src/view/game/game_menu.cpp
src/view/game/map.cpp
src/view/game/map_viewer.cpp
src/view/game/mob.cpp
src/view/game/object.cpp
src/view/game/overmap_collection.cpp
src/view/standalones/configurator.cpp
src/view/standalones/file_explorer.cpp
src/view/standalones/imgui_styles.cpp
src/view/view.cpp
)

Expand Down
6 changes: 6 additions & 0 deletions resources/configured_resources/lang/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@
[[log.entry]]
id = "view.view.menu.unknown_request"
fmt = "unknown request from menu: {id}"
[[log.entry]]
id = "view.view.bad_state"
fmt = "unknown view state: {state}"
[[log.entry]]
id = "view.configurator.config_save_failed"
fmt = "Could not save the new configuration"

[tooltip]
[[tooltip.entry]]
Expand Down
6 changes: 6 additions & 0 deletions resources/configured_resources/lang/fr.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@
[[log.entry]]
id = "view.view.menu.unknown_request"
fmt = "Requête inconnue reçue depuis le menu : {id}"
[[log.entry]]
id = "view.view.bad_state"
fmt = "État de la vue inconnu : {state}"
[[log.entry]]
id = "view.configurator.config_save_failed"
fmt = "Échec de sauvegarde de la configuration"

[tooltip]
[[tooltip.entry]]
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "utils/logging.hpp"
#include "utils/resource_manager.hpp"
#include "utils/scope_guards.hpp"
#include "view/game_viewer.hpp"
#include "view/game/game_viewer.hpp"
#include "view/view.hpp"

using fmt::literals::operator""_a;
Expand Down
12 changes: 6 additions & 6 deletions src/adapter/adapter_map_loader_v1_0_0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include <spdlog/spdlog.h>

#include "adapter/adapter.hpp"
#include "facing_dir.hpp"
#include "model/cell.hpp"
#include "model/components.hpp"
#include "model/model.hpp"
#include "state_holder.hpp"
#include "utils/logging.hpp"
#include "utils/resource_manager.hpp"
#include "utils/visitor.hpp"
#include "view/facing_dir.hpp"
#include "view/game_viewer.hpp"
#include "view/map.hpp"
#include "view/map_viewer.hpp"
#include "view/mob.hpp"
#include "view/object.hpp"
#include "view/game/game_viewer.hpp"
#include "view/game/map.hpp"
#include "view/game/map_viewer.hpp"
#include "view/game/mob.hpp"
#include "view/game/object.hpp"
#include "view/view.hpp"

using fmt::literals::operator""_a;
Expand Down
2 changes: 1 addition & 1 deletion src/view/facing_dir.cpp → src/adapter/facing_dir.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cassert>

#include "view/facing_dir.hpp"
#include "facing_dir.hpp"
#include "utils/universal_constants.hpp"

// TODO: add NE/NW/SE/SW
Expand Down
15 changes: 0 additions & 15 deletions src/view/facing_dir.hpp → src/adapter/facing_dir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,8 @@ enum type : unsigned int { N, S, E, W, NE, NW, SE, SW, MAX_VAL };
constexpr std::array values = {N, S, E, W, NE, NW, SE, SW, MAX_VAL};
static_assert(utils::has_all_sorted(values, MAX_VAL), "values array might not contain every enum value");

constexpr std::string_view to_string(facing_direction::type val) noexcept {
constexpr std::array<std::string_view, MAX_VAL + 1> direction_map = {
"N", "S", "E", "W", "NE", "NW", "SE", "SW", "MAX_VAL",
};
return direction_map[val];
}

type from_angle(float rad);

constexpr std::optional<facing_direction::type> from_string(std::string_view str) noexcept {
for (auto val : values) {
if (to_string(val) == str) {
return val;
}
}
return {};
}
} // namespace view::facing_direction

#endif //NINJACLOWN_VIEW_FACING_DIR_HPP
6 changes: 3 additions & 3 deletions src/model/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void model::model::do_run() noexcept {
return m_state != thread_state::waiting;
});
}
m_fps_limiter.start_now();
m_lps_limiter.start_now();

while (m_state != thread_state::stopping) {
if (m_dll_await_load.load()) {
Expand All @@ -90,14 +90,14 @@ void model::model::do_run() noexcept {
adapter.clear_entities_changed_since_last_update();
world.update(adapter);

m_fps_limiter.wait();
m_lps_limiter.wait();

if (m_state == thread_state::waiting) {
std::unique_lock ul{m_wait_mutex};
m_cv.wait(ul, [this]() {
return m_state != thread_state::waiting;
});
m_fps_limiter.start_now();
m_lps_limiter.start_now();
}
}
}
4 changes: 2 additions & 2 deletions src/model/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "bot/bot_dll.hpp"
#include "model/world.hpp"
#include "view/fps_limiter.hpp"
#include "utils/loop_per_sec_limit.hpp"

namespace state {
class holder;
Expand Down Expand Up @@ -52,7 +52,7 @@ class model {
std::mutex m_wait_mutex{};
std::condition_variable m_cv{};

view::fps_limiter m_fps_limiter{15};
utils::loop_per_sec_limit m_lps_limiter{15}; // updating the model 15 times per second at most
};
} // namespace model
#endif //NINJACLOWN_MODEL_MODEL_HPP
4 changes: 2 additions & 2 deletions src/state_holder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ state::holder::holder(const std::filesystem::path &autorun_script) noexcept

using view::view;

m_pimpl->properties.emplace("average_fps", property{&view::average_fps, m_pimpl->view}); // TODO translations
m_pimpl->properties.emplace("average_lps", property{&view::average_fps, m_pimpl->view}); // TODO translations

m_pimpl->properties.emplace("target_fps", property::proxy<unsigned int>::from_accessor<view>(
m_pimpl->properties.emplace("target_lps", property::proxy<unsigned int>::from_accessor<view>(
m_pimpl->view, &view::target_fps, &view::target_fps)); // TODO translations

m_pimpl->properties.emplace("display_debug_data", property{&view::show_debug_data, m_pimpl->view}); // TODO translations
Expand Down
8 changes: 4 additions & 4 deletions src/state_holder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace view {
class game_viewer;
class map_viewer;
class view;
class menu;
class game_menu;
}

namespace adapter {
Expand Down Expand Up @@ -81,7 +81,7 @@ class holder {
friend access<view::game_viewer>;
friend access<view::view>;
friend access<view::map_viewer>;
friend access<view::menu>;
friend access<view::game_menu>;
friend access<adapter::adapter>;
friend access<model::model>;
friend access<bot::ffi>;
Expand Down Expand Up @@ -198,12 +198,12 @@ class access<adapter::adapter> {
};

template<>
class access<view::menu> {
class access<view::game_menu> {
static adapter::adapter &adapter(holder& holder) noexcept {
return holder.adapter();
}

friend view::menu;
friend view::game_menu;
};

template<>
Expand Down
2 changes: 1 addition & 1 deletion src/terminal_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "utils/resource_manager.hpp"
#include "utils/utils.hpp"
#include "utils/visitor.hpp"
#include "view/game_viewer.hpp"
#include "view/game/game_viewer.hpp"
#include "view/view.hpp"

using fmt::literals::operator""_a;
Expand Down
2 changes: 0 additions & 2 deletions src/utils/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <string_view>
#include <utility>

#include "utils/optional.hpp"

namespace utils::log {
namespace details {
[[nodiscard]] std::string_view log_for(std::string_view key) noexcept;
Expand Down
42 changes: 42 additions & 0 deletions src/utils/loop_per_sec_limit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "loop_per_sec_limit.hpp"

void utils::loop_per_sec_limit::start_now() noexcept {
m_loop_count = 0u;
m_last_tick = std::chrono::system_clock::now();
*m_starting_time.acquire() = m_last_tick;
}

void utils::loop_per_sec_limit::wait() noexcept {
if (m_refresh_loop_duration.exchange(false)) {
m_loop_count = 0;
*m_starting_time.acquire() = m_last_tick;
m_loop_duration = std::chrono::milliseconds(1000) / m_target_lps.load();
}
++m_loop_count;

const auto now = std::chrono::system_clock::now();
const auto last_loop_duration = now - m_last_tick;
const auto sleep_time = m_loop_duration - last_loop_duration;
if (sleep_time.count() > 0) {
std::this_thread::sleep_for(sleep_time);
}
m_last_tick = std::chrono::system_clock::now();

const auto target_lps = static_cast<float>(m_target_lps.load());
const auto current_lps = average_lps();

if (current_lps - .05f > target_lps) {
m_loop_duration += std::chrono::milliseconds(1);
}
else if (current_lps + .05f < target_lps) {
if (m_loop_duration >= std::chrono::milliseconds(1)) {
m_loop_duration -= std::chrono::milliseconds(1);
}
}
}

float utils::loop_per_sec_limit::average_lps() const {
using namespace std::chrono; // NOLINT
auto display_duration = static_cast<float>(duration_cast<milliseconds>(system_clock::now() - *m_starting_time.acquire()).count());
return static_cast<float>(m_loop_count.load()) * 1000.f / display_duration;
}
35 changes: 19 additions & 16 deletions src/view/fps_limiter.hpp → src/utils/loop_per_sec_limit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

// TODO: move to utils

namespace view {
class fps_limiter {
namespace utils {
/**
* Limits the amount of time a loop is updated per second
*/
class loop_per_sec_limit {
public:
fps_limiter() noexcept {};
explicit fps_limiter(unsigned int target_fps) noexcept: m_target_fps{target_fps} {}
loop_per_sec_limit() noexcept {};
explicit loop_per_sec_limit(unsigned int target_lps) noexcept: m_target_lps{target_lps} {}

// Do not call this method concurrently with itself or with .wait
void start_now() noexcept;
Expand All @@ -22,30 +25,30 @@ class fps_limiter {
void wait() noexcept;

// can be called concurrently with any other method
[[nodiscard]] unsigned int frame_count() const {
return m_frame_count;
[[nodiscard]] unsigned int loop_count() const {
return m_loop_count;
}

// can be called concurrently with any other method
[[nodiscard]] float average_fps() const;
[[nodiscard]] float average_lps() const;

// can be called concurrently with any other method
[[nodiscard]] unsigned int target_fps() const {
return m_target_fps;
[[nodiscard]] unsigned int target_lps() const {
return m_target_lps;
}

// can be called concurrently with any other method
void target_fps(unsigned int new_val) noexcept {
m_target_fps = new_val;
m_refresh_frame_duration = true;
void target_lps(unsigned int new_val) noexcept {
m_target_lps = new_val;
m_refresh_loop_duration = true;
}

private:
std::atomic_bool m_refresh_frame_duration{false};
std::atomic_bool m_refresh_loop_duration{false};

std::atomic_uint m_target_fps = 60;
std::atomic_uint m_frame_count{0};
std::chrono::milliseconds m_frame_duration{std::chrono::milliseconds(1000) / m_target_fps.load()};
std::atomic_uint m_target_lps = 60;
std::atomic_uint m_loop_count{0};
std::chrono::milliseconds m_loop_duration{std::chrono::milliseconds(1000) / m_target_lps.load()};

std::chrono::time_point<std::chrono::system_clock> m_last_tick{std::chrono::system_clock::now()};
utils::synchronized<std::chrono::time_point<std::chrono::system_clock>, utils::spinlock> m_starting_time{m_last_tick};
Expand Down
6 changes: 3 additions & 3 deletions src/utils/resource_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#include <SFML/Graphics/Texture.hpp>
#include <cpptoml/cpptoml.h>

#include "adapter/facing_dir.hpp"
#include "terminal_ids.hpp"
#include "utils/optional.hpp"
#include "view/facing_dir.hpp"

#include "view/animation.hpp"
#include "view/mob_animations.hpp"
#include "view/assets/animation.hpp"
#include "view/assets/mob_animations.hpp"

namespace utils {

Expand Down
8 changes: 2 additions & 6 deletions src/view/animation.cpp → src/view/assets/animation.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "view/animation.hpp"
#include "view/map_viewer.hpp"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Mouse.hpp>
#include <spdlog/spdlog.h>
#include "animation.hpp"
#include "view/game/map_viewer.hpp"

namespace {
void print_tile(view::map_viewer& viewer, sf::Sprite &frame,
Expand Down
1 change: 1 addition & 0 deletions src/view/animation.hpp → src/view/assets/animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class shifted_animation {
shifted_animation(shifted_animation &&) noexcept = default;
explicit shifted_animation(animation &&o) noexcept
: m_frames{std::move(o.m_frames)} { }
~shifted_animation() = default;

shifted_animation &operator=(shifted_animation &&) noexcept = default;
shifted_animation &operator=(const shifted_animation &) = default;
Expand Down
Loading

0 comments on commit 66b9530

Please sign in to comment.