From 61e336786ffc3ffe7e96afbf5895fac799522e75 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 30 Mar 2021 16:38:25 +0900 Subject: [PATCH] WIP attempt to fix resizing not working --- src/helpers.hpp | 17 ++++++++----- src/state.cpp | 64 +++++++++++++++++++++++++++---------------------- src/state.hpp | 5 ++-- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/helpers.hpp b/src/helpers.hpp index d9127b9..c71b3d0 100644 --- a/src/helpers.hpp +++ b/src/helpers.hpp @@ -14,6 +14,11 @@ #define SHOW_HELP_WIDTH (state->get_width() / 2.0f) #define SHOW_HELP_HEIGHT (state->get_height() / 2.0f) +#define CHANGE_SIZE_MIN_X 800 +#define CHANGE_SIZE_MAX_X 1920 +#define CHANGE_SIZE_MIN_Y 600 +#define CHANGE_SIZE_MAX_Y 1080 + #ifndef NDEBUG # include #endif @@ -171,16 +176,16 @@ namespace Tri { {384.0f, 328.0f, 80.0f, 16.0f}, "Width", state->get_input_width(), - 800, - 1920, - state->get_flags().test(State::F_TAB_TOGGLE)); + CHANGE_SIZE_MIN_X, + CHANGE_SIZE_MAX_X, + !state->get_flags().test(State::F_TAB_TOGGLE)); GuiValueBox( {384.0f, 348.0f, 80.0f, 16.0f}, "Height", state->get_input_height(), - 600, - 1080, - !state->get_flags().test(State::F_TAB_TOGGLE)); + CHANGE_SIZE_MIN_Y, + CHANGE_SIZE_MAX_Y, + state->get_flags().test(State::F_TAB_TOGGLE)); const std::string &failMessage = state->failed_message(); if(!failMessage.empty()) { GuiLabel({304.0f, 368.0f, 284.0f, 16.0f}, failMessage.c_str()); diff --git a/src/state.cpp b/src/state.cpp index 2d00d34..908c9ff 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -11,15 +12,11 @@ #define STARTING_HELP_FADE_RATE 0.2f -#ifndef NDEBUG -# include -#endif - Tri::State::State(int argc, char **argv) : width(800), height(600), dt(1.0f/60.0f), -notification_alpha(1.0f), +notificationAlpha(1.0f), trisIndex(0), currentTri_state(CurrentState::NONE), currentTri_maxState(CurrentState::NONE), @@ -144,7 +141,7 @@ void Tri::State::handle_events() { "to what was\n" "clicked on"); } else { - notification_alpha = 0.0f; + notificationAlpha = 0.0f; } break; case KEY_I: @@ -259,10 +256,10 @@ void Tri::State::handle_events() { void Tri::State::update() { dt = GetFrameTime(); - if(notification_alpha > 0.0f) { - notification_alpha -= dt * STARTING_HELP_FADE_RATE; - if(notification_alpha < 0.0f) { - notification_alpha = 0.0f; + if(notificationAlpha > 0.0f) { + notificationAlpha -= dt * STARTING_HELP_FADE_RATE; + if(notificationAlpha < 0.0f) { + notificationAlpha = 0.0f; } } @@ -373,19 +370,31 @@ const Tri::State::BitsetType Tri::State::get_flags() const { } float Tri::State::get_notification_alpha() const { - return notification_alpha; + return notificationAlpha; } const char* Tri::State::get_notification_text() const { - return notification_text.data(); + return notificationText.data(); } void Tri::State::set_notification_text(const char *text) { - notification_text.fill(0); - std::strncpy(notification_text.data(), + notificationText.fill(0); + std::strncpy(notificationText.data(), text, - notification_text.max_size() - 1); - notification_alpha = 1.0f; + notificationText.max_size() - 1); + notificationAlpha = 1.0f; +} + +void Tri::State::append_notification_text(const char *text) { + auto length = std::strlen(notificationText.data()); + if(length + 1 >= notificationText.max_size()) { + return; + } + std::strncpy( + notificationText.data() + length, + text, + notificationText.max_size() - length - 1); + notificationAlpha = 1.0f; } std::array& Tri::State::get_color() { @@ -471,34 +480,33 @@ void Tri::State::close_bg_color_picker() { } bool Tri::State::change_width_height() { - std::bitset<2> warnings; if(inputWidth < 0 || inputHeight < 0) { failedMessage = "Width or Height cannot be less than 0"; return false; } if(inputWidth < 800) { inputWidth = 800; - warnings.set(0); } if(inputHeight < 600) { inputHeight = 600; - warnings.set(1); } - if(warnings.test(0) && warnings.test(1)) { - set_notification_text("Width set to 800\nHeight set to 600"); - } else if(warnings.test(0)) { - set_notification_text("Width set to 800"); - } else if(warnings.test(1)) { - set_notification_text("Height set to 600"); - } + notificationText.fill(0); + std::array tempBuf = {0, 0, 0, 0, 0}; + + append_notification_text("Width set to "); + snprintf(tempBuf.data(), 5, "%u", inputWidth); + append_notification_text(tempBuf.data()); + + append_notification_text(", Height set to "); + snprintf(tempBuf.data(), 5, "%u", inputHeight); + append_notification_text(tempBuf.data()); this->width = inputWidth; this->height = inputHeight; - SetWindowSize(this->width, this->height); - UnloadRenderTexture(drawCache); + SetWindowSize(this->width, this->height); drawCache = LoadRenderTexture(this->width, this->height); flags.set(F_DRAW_CACHE_DIRTY); diff --git a/src/state.hpp b/src/state.hpp index 4ebece4..0a09c33 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -44,8 +44,8 @@ namespace Tri { unsigned int width; unsigned int height; float dt; - float notification_alpha; - std::array notification_text; + float notificationAlpha; + std::array notificationText; std::vector tris; unsigned int trisIndex; @@ -91,6 +91,7 @@ namespace Tri { private: void set_notification_text(const char *text); + void append_notification_text(const char *text); public: std::array& get_color();