diff --git a/Libraries/SDLLib b/Libraries/SDLLib index 9fa1be9..e890bf3 160000 --- a/Libraries/SDLLib +++ b/Libraries/SDLLib @@ -1 +1 @@ -Subproject commit 9fa1be94b7c0393f479f47dc9a74908a38112fe8 +Subproject commit e890bf32fe42695fc6448921d1c503c3d1eaf8d6 diff --git a/Makefile b/Makefile index c6e2e07..1c14ed2 100644 --- a/Makefile +++ b/Makefile @@ -183,7 +183,7 @@ clean: #--------------------------------------------------------------------------------- send: $(BUILD) - @nxlink $(TARGET).nro + @nxlink -a 192.168.0.171 $(TARGET).nro #--------------------------------------------------------------------------------- diff --git a/include/JKSV.hpp b/include/JKSV.hpp index a63a5b7..163788b 100644 --- a/include/JKSV.hpp +++ b/include/JKSV.hpp @@ -30,6 +30,21 @@ class JKSV static void request_quit() noexcept; private: + /// @brief SDL2 instance. + sdl2::SDL2 m_sdl2{}; + + /// @brief SDL2 Window. + sdl2::Window m_window{}; + + /// @brief SDL2 Renderer. + sdl2::Renderer m_renderer{}; + + /// @brief SDL2 Audio. + sdl2::Audio m_audio{}; + + /// @brief Not really a part of SDL2, but... + sdl2::Input m_input{}; + /// @brief Whether or not initialization was successful and JKSV is still running. static inline std::atomic_bool sm_isRunning{}; @@ -37,7 +52,13 @@ class JKSV bool m_showTranslationInfo{}; /// @brief JKSV icon in upper left corner. - sdl::SharedTexture m_headerIcon{}; + sdl2::SharedTexture m_headerIcon{}; + + /// @brief This is used for rendering JKSV. + sdl2::SharedFont m_titleFont{}; + + /// @brief This is used to render the build date. + sdl2::SharedFont m_buildFont{}; /// @brief Stores the translation string. std::string m_translationInfo{}; @@ -60,9 +81,6 @@ class JKSV // Creates the needed directories on SD. bool create_directories(); - /// @brief Adds the text color changing characters. - void add_color_chars(); - /// @brief Retrieves the strings from the map and sets them up for printing. void setup_translation_info_strings(); diff --git a/include/StateManager.hpp b/include/StateManager.hpp index 539022a..82d4006 100644 --- a/include/StateManager.hpp +++ b/include/StateManager.hpp @@ -1,6 +1,8 @@ #pragma once #include "appstates/BaseState.hpp" +#include "sdl.hpp" +#include #include #include @@ -14,10 +16,10 @@ class StateManager StateManager &operator=(StateManager &&) = delete; /// @brief Runs the state update routine. - static void update(); + static void update(const sdl2::Input &input); /// @brief Runs the state rendering routine(s); - static void render() noexcept; + static void render(sdl2::Renderer &renderer) noexcept; /// @brief Returns whether the back of the vector is a closable state. static bool back_is_closable() noexcept; diff --git a/include/appstates/AppletModeState.hpp b/include/appstates/AppletModeState.hpp index 6b67fea..a40c1d4 100644 --- a/include/appstates/AppletModeState.hpp +++ b/include/appstates/AppletModeState.hpp @@ -15,10 +15,10 @@ class AppletModeState final : public BaseState static inline std::shared_ptr create() { return std::make_shared(); } /// @brief Runs the update routine. Basically does nothing. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Renders the message to the screen. - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief Pointer to the string rendered to the screen. diff --git a/include/appstates/BackupMenuState.hpp b/include/appstates/BackupMenuState.hpp index 3f7595f..5829d15 100644 --- a/include/appstates/BackupMenuState.hpp +++ b/include/appstates/BackupMenuState.hpp @@ -26,9 +26,7 @@ class BackupMenuState final : public BaseState static inline std::shared_ptr create(data::User *user, data::TitleInfo *titleInfo, const FsSaveDataInfo *saveInfo) - { - return std::make_shared(user, titleInfo, saveInfo); - } + { return std::make_shared(user, titleInfo, saveInfo); } /// @brief Creates and pushes a new BackupMenuState to the vector. static inline std::shared_ptr create_and_push(data::User *user, @@ -41,10 +39,10 @@ class BackupMenuState final : public BaseState } /// @brief Required. Inherited virtual function from AppState. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Required. Inherited virtual function from AppState. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief Refreshes the directory listing and menu. void refresh(); @@ -153,7 +151,10 @@ class BackupMenuState final : public BaseState static inline std::shared_ptr sm_slidePanel{}; /// @brief Inner render target so the menu only renders to a certain area. - static inline sdl::SharedTexture sm_menuRenderTarget{}; + static inline sdl2::SharedTexture sm_menuRenderTarget{}; + + /// @brief Font used for rendering text. + static inline sdl2::SharedFont sm_font{}; /// @brief Initializes the static members all instances share if they haven't been already. void initialize_static_members(); @@ -174,7 +175,7 @@ class BackupMenuState final : public BaseState void initialize_remote_storage(); /// @brief This is the function called when New Backup is selected. - void name_and_create_backup(); + void name_and_create_backup(const sdl2::Input &input); /// @brief This is the function called when a backup is selected to be overwritten. void confirm_overwrite(); diff --git a/include/appstates/BaseState.hpp b/include/appstates/BaseState.hpp index d00b3a2..eb195e9 100644 --- a/include/appstates/BaseState.hpp +++ b/include/appstates/BaseState.hpp @@ -15,13 +15,13 @@ class BaseState virtual ~BaseState(); /// @brief Every derived class is required to have this function. - virtual void update() = 0; + virtual void update(const sdl2::Input &input) = 0; /// @brief Sub update routine. Meant to handle minor background tasks. Not meant for full update routines. virtual void sub_update() {}; /// @brief Every derived class is required to have this function. - virtual void render() = 0; + virtual void render(sdl2::Renderer &renderer) = 0; /// @brief Deactivates state and allows JKSV to purge it from the vector. void deactivate(); diff --git a/include/appstates/BaseTask.hpp b/include/appstates/BaseTask.hpp index 5813b60..9829ac4 100644 --- a/include/appstates/BaseTask.hpp +++ b/include/appstates/BaseTask.hpp @@ -1,5 +1,6 @@ #pragma once #include "appstates/BaseState.hpp" +#include "sdl.hpp" #include "sys/sys.hpp" #include "ui/ColorMod.hpp" @@ -15,10 +16,10 @@ class BaseTask : public BaseState /// @brief Runs the update routine for rendering the loading glyph animation. /// @param - virtual void update() = 0; + virtual void update(const sdl2::Input &input) = 0; /// @brief Virtual render function. - virtual void render() = 0; + virtual void render(sdl2::Renderer &renderer) = 0; protected: /// @brief Underlying system task. This needs to be allocated by the derived classes. @@ -28,15 +29,12 @@ class BaseTask : public BaseState void update_loading_glyph(); /// @brief Displays the "can't quit JKSV" when plus is pressed. - void pop_on_plus(); + void pop_on_plus(const sdl2::Input &input); /// @brief This function renders the loading glyph in the bottom left corner. /// @note This is mostly just so users don't think JKSV has frozen when operations take a long time. void render_loading_glyph(); - /// @brief This is the font size used for displaying text during tasks. - static inline constexpr int FONT_SIZE = 20; - private: /// @brief This is the current frame of the loading glyph animation. int m_currentFrame{}; @@ -53,4 +51,10 @@ class BaseTask : public BaseState /// @brief This array holds the glyphs of the loading sequence. I think it's from the Wii? static inline constexpr std::array sm_glyphArray = {"\ue020", "\ue021", "\ue022", "\ue023", "\ue024", "\ue025", "\ue026", "\ue027"}; + + /// @brief Font for rendering the glyph. + static inline sdl2::SharedFont sm_font{}; + + /// @brief Ensures the font is loaded. + void initialize_static_members(); }; diff --git a/include/appstates/BlacklistEditState.hpp b/include/appstates/BlacklistEditState.hpp index 0c472be..2c4038f 100644 --- a/include/appstates/BlacklistEditState.hpp +++ b/include/appstates/BlacklistEditState.hpp @@ -1,6 +1,7 @@ #pragma once #include "StateManager.hpp" #include "appstates/BaseState.hpp" +#include "sdl.hpp" #include "ui/ui.hpp" #include @@ -24,10 +25,10 @@ class BlacklistEditState final : public BaseState } /// @brief Update override. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Render override. - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief Local copy of the blacklist. diff --git a/include/appstates/ConfirmState.hpp b/include/appstates/ConfirmState.hpp index a2a9e84..0c802f6 100644 --- a/include/appstates/ConfirmState.hpp +++ b/include/appstates/ConfirmState.hpp @@ -5,8 +5,8 @@ #include "appstates/ProgressState.hpp" #include "appstates/TaskState.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include "sdl.hpp" #include "strings/strings.hpp" @@ -108,9 +108,7 @@ class ConfirmState final : public BaseState sys::threadpool::JobFunction onConfirm, sys::threadpool::JobFunction onCancel, sys::Task::TaskData taskData) - { - return std::make_shared(query, holdRequired, onConfirm, onCancel, taskData); - } + { return std::make_shared(query, holdRequired, onConfirm, onCancel, taskData); } /// @brief Returns a new ConfirmState. See constructor. static inline std::shared_ptr create(std::string &query, @@ -118,9 +116,7 @@ class ConfirmState final : public BaseState sys::threadpool::JobFunction onConfirm, sys::threadpool::JobFunction onCancel, sys::Task::TaskData taskData) - { - return std::make_shared(query, holdRequired, onConfirm, onCancel, taskData); - } + { return std::make_shared(query, holdRequired, onConfirm, onCancel, taskData); } /// @brief Creates and returns a new ConfirmState and pushes it. static inline std::shared_ptr create_and_push(std::string_view query, @@ -173,23 +169,22 @@ class ConfirmState final : public BaseState } /// @brief Just updates the ConfirmState. - void update() override + void update(const sdl2::Input &input) override { switch (m_state) { case State::Opening: ConfirmState::update_dimensions(); break; - case State::Displaying: ConfirmState::update_handle_input(); break; + case State::Displaying: ConfirmState::update_handle_input(input); break; case State::Closing: ConfirmState::update_dimensions(); break; } } /// @brief Renders the state to screen. - void render() override + void render(sdl2::Renderer &renderer) override { // These are the rendering coordinates that aren't affected by the transition. static constexpr int TEXT_X = 312; static constexpr int TEXT_Y_OFFSET = 24; - static constexpr int TEXT_FONT_SIZE = 20; static constexpr int TEXT_WRAP_WIDTH = 656; // Divider line A. @@ -204,59 +199,28 @@ class ConfirmState final : public BaseState static constexpr int LINE_Y_OFFSET = 192; // Yes/NO - static constexpr int OPTION_Y_OFFSET = 214; - static constexpr int OPTION_FONT_SIZE = 22; + static constexpr int OPTION_Y_OFFSET = 214; const bool hasFocus = BaseState::has_focus(); const int y = m_transition.get_y(); // This is the dimming rectangle. - sdl::render_rect_fill(sdl::Texture::Null, - 0, - 0, - graphics::SCREEN_WIDTH, - graphics::SCREEN_HEIGHT, - colors::DIM_BACKGROUND); + renderer.render_rectangle(0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); // Render the dialog. Only render the rest if we're in the display state. - sm_dialog->render(sdl::Texture::Null, hasFocus); + sm_dialog->render(renderer, hasFocus); if (!m_transition.in_place() || m_state != State::Displaying) { return; } // Main string. - sdl::text::render(sdl::Texture::Null, - TEXT_X, - y + TEXT_Y_OFFSET, - TEXT_FONT_SIZE, - TEXT_WRAP_WIDTH, - colors::WHITE, - m_query); + sm_textFont->render_text_wrapped(TEXT_X, y + TEXT_Y_OFFSET, colors::WHITE, TEXT_WRAP_WIDTH, m_query); // Divider lines. - sdl::render_line(sdl::Texture::Null, - LINE_A_X_A, - y + LINE_Y_OFFSET, - LINE_A_X_B, - y + LINE_Y_OFFSET, - colors::DIV_COLOR); - sdl::render_line(sdl::Texture::Null, LINE_B_X, y + LINE_Y_OFFSET, LINE_B_X, y + LINE_B_Y_B, colors::DIV_COLOR); + renderer.render_line(LINE_A_X_A, y + LINE_Y_OFFSET, LINE_A_X_B, y + LINE_Y_OFFSET, colors::DIV_COLOR); + renderer.render_line(LINE_B_X, y + LINE_Y_OFFSET, LINE_B_X, y + LINE_B_Y_B, colors::DIV_COLOR); // Yes - sdl::text::render(sdl::Texture::Null, - m_yesX, - y + OPTION_Y_OFFSET, - OPTION_FONT_SIZE, - sdl::text::NO_WRAP, - colors::WHITE, - sm_yes); - - // No - sdl::text::render(sdl::Texture::Null, - m_noX, - y + OPTION_Y_OFFSET, - OPTION_FONT_SIZE, - sdl::text::NO_WRAP, - colors::WHITE, - sm_no); + sm_optionFont->render_text(m_yesX, y + OPTION_Y_OFFSET, colors::WHITE, sm_yes); + sm_optionFont->render_text(m_noX, y + OPTION_Y_OFFSET, colors::WHITE, sm_no); } private: @@ -313,20 +277,26 @@ class ConfirmState final : public BaseState /// @brief This dialog is shared between all instances. static inline std::shared_ptr sm_dialog{}; + /// @brief This is the font used to render the actual text. + static inline sdl2::SharedFont sm_textFont{}; + + /// @brief This is the font used to render the Yes or No options. + static inline sdl2::SharedFont sm_optionFont{}; + /// @brief This sound is shared. - static inline sdl::SharedSound sm_dialogPop{}; + static inline sdl2::SharedSound sm_dialogPop{}; void initialize_static_members() { // Name and path to the sound used. - static constexpr std::string_view POP_SOUND = "ConfirmPop"; - static constexpr const char *POP_PATH = "romfs:/Sound/ConfirmPop.wav"; + static constexpr std::string_view POP_PATH = "romfs:/Sound/ConfirmPop.wav"; // To do: Not sure about checking the holding text. - if (sm_dialog && sm_dialogPop && sm_yes && sm_no) { return; } + if (sm_dialog && sm_dialogPop && sm_yes && sm_no && sm_textFont && sm_optionFont) { return; } + // Init dialog and get it started. sm_dialog = ui::DialogBox::create(0, 0, 0, 0); - sm_dialogPop = sdl::SoundManager::load(POP_SOUND, POP_PATH); + sm_dialogPop = sdl2::SoundManager::create_load_resource(POP_PATH, POP_PATH); sm_dialog->set_from_transition(m_transition, true); // Load yes and no. @@ -336,6 +306,13 @@ class ConfirmState final : public BaseState // Loop load the holding strings. const char *hold{}; for (int i = 0; (hold = strings::get_by_name(strings::names::HOLDING_STRINGS, i)); i++) { sm_hold[i] = hold; } + + // Fonts for rendering text. + sm_textFont = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_FOUR_PIXEL, + graphics::fonts::sizes::TWENTY_PIXEL); + sm_optionFont = + sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_FOUR_PIXEL, + graphics::fonts::sizes::TWENTY_FOUR_PIXEL); } /// @brief Updates the dimensions of the dialog. @@ -353,13 +330,13 @@ class ConfirmState final : public BaseState } /// @brief Handles the input and updating. - void update_handle_input() noexcept + void update_handle_input(const sdl2::Input &input) noexcept { // Grab our input bools. - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool aHeld = input::button_held(HidNpadButton_A); - const bool aReleased = input::button_released(HidNpadButton_A); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); + const bool aHeld = input.button_held(HidNpadButton_A); + const bool aReleased = input.button_released(HidNpadButton_A); // This is to prevent A from auto triggering the dialog. m_triggerGuard = m_triggerGuard || (aPressed && !m_triggerGuard); @@ -379,13 +356,13 @@ class ConfirmState final : public BaseState // This just centers the Yes or holding text. void center_yes() { - const int yesWidth = sdl::text::get_width(22, sm_yes); + const int yesWidth = sm_optionFont->get_text_width(sm_yes); m_yesX = COORD_YES_X - (yesWidth / 2); } void center_no() { - const int noWidth = sdl::text::get_width(22, sm_no); + const int noWidth = sm_optionFont->get_text_width(sm_no); m_noX = COORD_NO_X - (noWidth / 2); } diff --git a/include/appstates/DataLoadingState.hpp b/include/appstates/DataLoadingState.hpp index 50aa706..712571b 100644 --- a/include/appstates/DataLoadingState.hpp +++ b/include/appstates/DataLoadingState.hpp @@ -15,41 +15,45 @@ class DataLoadingState final : public BaseTask using DestructFunction = std::function; DataLoadingState(data::DataContext &context, + sdl2::Renderer &renderer, DestructFunction destructFunction, sys::threadpool::JobFunction function, sys::Task::TaskData taskData); static inline std::shared_ptr create(data::DataContext &context, + sdl2::Renderer &renderer, DestructFunction destructFunction, sys::threadpool::JobFunction function, sys::Task::TaskData taskData) - { - return std::make_shared(context, destructFunction, function, taskData); - } + { return std::make_shared(context, renderer, destructFunction, function, taskData); } static inline std::shared_ptr create_and_push(data::DataContext &context, + sdl2::Renderer &renderer, DestructFunction destructFunction, sys::threadpool::JobFunction function, sys::Task::TaskData taskData) { - auto newState = DataLoadingState::create(context, destructFunction, function, taskData); + auto newState = DataLoadingState::create(context, renderer, destructFunction, function, taskData); StateManager::push_state(newState); return newState; } /// @brief Update override. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Updates the loading glyph. void sub_update() override; /// @brief Render override. - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief Reference to the data context to run post-init operations. data::DataContext &m_context; + /// @brief Reference to SDL2 renderer for loading icons. + sdl2::Renderer &m_renderer; + /// @brief X coord of the status text. int m_statusX{}; @@ -57,7 +61,10 @@ class DataLoadingState final : public BaseTask DestructFunction m_destructFunction{}; /// @brief Icon displayed in the center of the screen. - static inline sdl::SharedTexture sm_jksvIcon{}; + static inline sdl2::SharedTexture sm_jksvIcon{}; + + /// @brief Font used for rendering the status. + static inline sdl2::SharedFont sm_font{}; /// @brief Loads the icon if it hasn't been already. void initialize_static_members(); diff --git a/include/appstates/ExtrasMenuState.hpp b/include/appstates/ExtrasMenuState.hpp index 3314c23..37836f2 100644 --- a/include/appstates/ExtrasMenuState.hpp +++ b/include/appstates/ExtrasMenuState.hpp @@ -9,26 +9,30 @@ class ExtrasMenuState final : public BaseState { public: /// @brief Constructor. - ExtrasMenuState(); + ExtrasMenuState(sdl2::Renderer &renderer); /// @brief Returns a new ExtrasMenuState - static inline std::shared_ptr create() { return std::make_shared(); } + static inline std::shared_ptr create(sdl2::Renderer &renderer) + { return std::make_shared(renderer); } /// @brief Updates the menu. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Sub-update routine. void sub_update() override; /// @brief Renders the menu to screen. - void render() override; + void render(sdl2::Renderer &renderer) override; private: + /// @brief Reference to renderer for launching refresh. + sdl2::Renderer &m_renderer; + /// @brief Menu std::shared_ptr m_extrasMenu{}; /// @brief Render target for menu. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief Control guider for bottom right corner. std::shared_ptr m_controlGuide{}; diff --git a/include/appstates/FadeState.hpp b/include/appstates/FadeState.hpp index a8c03af..3d29202 100644 --- a/include/appstates/FadeState.hpp +++ b/include/appstates/FadeState.hpp @@ -18,19 +18,17 @@ class FadeState final : public BaseState /// @brief Creates a new fade in state. /// @param nextState The next state to push after the the fade is finished. - FadeState(sdl::Color baseColor, uint8_t startAlpha, uint8_t endAlpha, std::shared_ptr nextState); + FadeState(SDL_Color baseColor, uint8_t startAlpha, uint8_t endAlpha, std::shared_ptr nextState); /// @brief Returns a new fade in state. See constructor. - static inline std::shared_ptr create(sdl::Color baseColor, + static inline std::shared_ptr create(SDL_Color baseColor, uint8_t startAlpha, uint8_t endAlpha, std::shared_ptr nextState) - { - return std::make_shared(baseColor, startAlpha, endAlpha, nextState); - } + { return std::make_shared(baseColor, startAlpha, endAlpha, nextState); } /// @brief Creates, returns and pushes a new FadeInState to the statemanager. - static std::shared_ptr create_and_push(sdl::Color baseColor, + static std::shared_ptr create_and_push(SDL_Color baseColor, uint8_t startAlpha, uint8_t endAlpha, std::shared_ptr nextState) @@ -41,13 +39,14 @@ class FadeState final : public BaseState } /// @brief Update override. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Render override. - void render() override; + void render(sdl2::Renderer &renderer) override; private: - sdl::Color m_baseColor{}; + /// @brief Base color of the fade. + SDL_Color m_baseColor{}; /// @brief Alpha value. uint8_t m_alpha{}; diff --git a/include/appstates/FileModeState.hpp b/include/appstates/FileModeState.hpp index 7deb9f9..7633d95 100644 --- a/include/appstates/FileModeState.hpp +++ b/include/appstates/FileModeState.hpp @@ -17,9 +17,7 @@ class FileModeState final : public BaseState std::string_view mountB, int64_t journalSize = 0, bool isSystem = false) - { - return std::make_shared(mountA, mountB, journalSize, isSystem); - } + { return std::make_shared(mountA, mountB, journalSize, isSystem); } static inline std::shared_ptr create_and_push(std::string_view mountA, std::string_view mountB, @@ -32,10 +30,10 @@ class FileModeState final : public BaseState } /// @brief Update override. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Render override. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief This thing is a headache without this. friend class FileOptionState; @@ -95,7 +93,7 @@ class FileModeState final : public BaseState static inline std::shared_ptr sm_frame{}; /// @brief This is the render target the browsers are rendered to. - static inline sdl::SharedTexture sm_renderTarget{}; + static inline sdl2::SharedTexture sm_renderTarget{}; /// @brief Control guide shared by all instances. static inline std::shared_ptr sm_controlGuide{}; @@ -116,7 +114,7 @@ class FileModeState final : public BaseState void update_y() noexcept; /// @brief Handles input. - void update_handle_input() noexcept; + void update_handle_input(const sdl2::Input &input) noexcept; /// @brief Handles changing the current directory or opening the options. void enter_selected(fslib::Path &path, fslib::Directory &directory, ui::Menu &menu); diff --git a/include/appstates/FileOptionState.hpp b/include/appstates/FileOptionState.hpp index 52925cc..1c9232a 100644 --- a/include/appstates/FileOptionState.hpp +++ b/include/appstates/FileOptionState.hpp @@ -17,9 +17,7 @@ class FileOptionState final : public BaseState /// @brief Inline creation function. static inline std::shared_ptr create(FileModeState *spawningState) - { - return std::make_shared(spawningState); - } + { return std::make_shared(spawningState); } /// @brief Same as above. Pushes state before returning it. static inline std::shared_ptr create_and_push(FileModeState *spawningState) @@ -30,10 +28,10 @@ class FileOptionState final : public BaseState } /// @brief Update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief Signals to this state to update the source/target menu on the next update() call. void update_source(); @@ -95,7 +93,7 @@ class FileOptionState final : public BaseState void update_dimensions() noexcept; /// @brief Updates and handles input. - void update_handle_input() noexcept; + void update_handle_input(const sdl2::Input &input) noexcept; /// @brief Updates the FileModeState's source data. void update_filemode_source(); diff --git a/include/appstates/MainMenuState.hpp b/include/appstates/MainMenuState.hpp index a5c5084..801d30b 100644 --- a/include/appstates/MainMenuState.hpp +++ b/include/appstates/MainMenuState.hpp @@ -13,27 +13,28 @@ class MainMenuState final : public BaseState { public: /// @brief Creates and initializes the main menu. - MainMenuState(); + MainMenuState(sdl2::Renderer &renderer); /// @brief Returns a new MainMenuState - static inline std::shared_ptr create() { return std::make_shared(); } + static inline std::shared_ptr create(sdl2::Renderer &renderer) + { return std::make_shared(renderer); } /// @brief Creates and returns a new MainMenuState. Pushes it automatically. - static inline std::shared_ptr create_and_push() + static inline std::shared_ptr create_and_push(sdl2::Renderer &renderer) { - auto newState = MainMenuState::create(); + auto newState = MainMenuState::create(renderer); StateManager::push_state(newState); return newState; } /// @brief Runs update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Runs the sub-update routine. void sub_update() override; /// @brief Renders menu to screen. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief Allows the update task to signal it found an update. void signal_update_found(); @@ -54,16 +55,16 @@ class MainMenuState final : public BaseState private: /// @brief Render target this state renders to. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief The background gradient. - sdl::SharedTexture m_background{}; + sdl2::SharedTexture m_background{}; /// @brief Icon for the settings option, - sdl::SharedTexture m_settingsIcon{}; + sdl2::SharedTexture m_settingsIcon{}; /// @brief Icon for the extras option. - sdl::SharedTexture m_extrasIcon{}; + sdl2::SharedTexture m_extrasIcon{}; /// @brief Special menu type that uses icons. std::shared_ptr m_mainMenu{}; @@ -93,7 +94,7 @@ class MainMenuState final : public BaseState static inline std::vector> sm_states{}; /// @brief Creates the settings and extras. - void initialize_settings_extras(); + void initialize_settings_extras(sdl2::Renderer &renderer); /// @brief Pushes the icons to the main menu. void initialize_menu(); diff --git a/include/appstates/MessageState.hpp b/include/appstates/MessageState.hpp index 7958fc7..e8db799 100644 --- a/include/appstates/MessageState.hpp +++ b/include/appstates/MessageState.hpp @@ -21,15 +21,11 @@ class MessageState final : public BaseState /// @brief Creates and returns a new MessageState. See constructor. static inline std::shared_ptr create(std::string_view message) - { - return std::make_shared(message); - } + { return std::make_shared(message); } /// @brief Creates and returns a new MessageState. See constructor. static inline std::shared_ptr create(std::string &message) - { - return std::make_shared(message); - } + { return std::make_shared(message); } /// @brief Same as above, only pushed to the StateManager before return. static inline std::shared_ptr create_and_push(std::string_view message) @@ -66,10 +62,10 @@ class MessageState final : public BaseState } /// @brief Update override. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Render override - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief States this state can be in. @@ -102,7 +98,13 @@ class MessageState final : public BaseState static inline std::shared_ptr sm_dialog{}; /// @brief This is the same sound that the confirmation uses. - static inline sdl::SharedSound sm_dialogPop{}; + static inline sdl2::SharedSound sm_dialogPop{}; + + /// @brief Font used to render the message. + static inline sdl2::SharedFont sm_textFont{}; + + /// @brief Font used to render the OK. + static inline sdl2::SharedFont sm_optionFont{}; /// @brief Allocates and ensures ^ void initialize_static_members(); @@ -111,7 +113,7 @@ class MessageState final : public BaseState void update_dimensions() noexcept; /// @brief Updates and handles the input. - void update_handle_input() noexcept; + void update_handle_input(const sdl2::Input &input) noexcept; /// @brief Closes and "hides" the dialog. void close_dialog(); diff --git a/include/appstates/ProgressState.hpp b/include/appstates/ProgressState.hpp index b726c4e..8cf28eb 100644 --- a/include/appstates/ProgressState.hpp +++ b/include/appstates/ProgressState.hpp @@ -20,9 +20,7 @@ class ProgressState final : public BaseTask /// @brief Creates and returns a new ProgressState static inline std::shared_ptr create(sys::threadpool::JobFunction function, sys::Task::TaskData taskData) - { - return std::make_shared(function, taskData); - } + { return std::make_shared(function, taskData); } /// @brief Creates, pushes, then returns a new ProgressState. static inline std::shared_ptr create_and_push(sys::threadpool::JobFunction function, @@ -42,10 +40,10 @@ class ProgressState final : public BaseTask } /// @brief Checks if the thread is finished and deactivates this state. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Renders the current progress to screen. - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief States this state can be in. @@ -84,7 +82,10 @@ class ProgressState final : public BaseTask static inline std::shared_ptr sm_dialog{}; /// @brief This is rendered over the edges of the bar to give it a slightly rounded look. - static inline sdl::SharedTexture sm_barEdges{}; + static inline sdl2::SharedTexture sm_barEdges{}; + + /// @brief Font used for rendering text. + static inline sdl2::SharedFont sm_font{}; /// @brief Initializes the shared dialog box. void initialize_static_members(); diff --git a/include/appstates/SaveCreateState.hpp b/include/appstates/SaveCreateState.hpp index 0bac497..996082f 100644 --- a/include/appstates/SaveCreateState.hpp +++ b/include/appstates/SaveCreateState.hpp @@ -19,9 +19,7 @@ class SaveCreateState final : public BaseState /// @brief Returns a new SaveCreate state. See constructor for arguments. static inline std::shared_ptr create(data::User *user, TitleSelectCommon *titleSelect) - { - return std::make_shared(user, titleSelect); - } + { return std::make_shared(user, titleSelect); } /// @brief Creates, pushes, returns and new SaveCreateState. static inline std::shared_ptr create_and_push(data::User *user, TitleSelectCommon *titleSelect) @@ -32,10 +30,10 @@ class SaveCreateState final : public BaseState } /// @brief Runs the update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Runs the render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief This signals so data and the view can be refreshed on the next update() to avoid threading shenanigans. void refresh_required(); diff --git a/include/appstates/SaveImportState.hpp b/include/appstates/SaveImportState.hpp index 3b30a8c..eebaec7 100644 --- a/include/appstates/SaveImportState.hpp +++ b/include/appstates/SaveImportState.hpp @@ -15,9 +15,7 @@ class SaveImportState final : public BaseState SaveImportState(data::User *user); static inline std::shared_ptr create(data::User *user) - { - return std::make_shared(user); - } + { return std::make_shared(user); } static inline std::shared_ptr create_and_push(data::User *user) { @@ -26,9 +24,9 @@ class SaveImportState final : public BaseState return newState; } - void update() override; + void update(const sdl2::Input &input) override; - void render() override; + void render(sdl2::Renderer &renderer) override; // clang-format off // Struct used to pass data to the task. diff --git a/include/appstates/SettingsState.hpp b/include/appstates/SettingsState.hpp index b0b133b..a168246 100644 --- a/include/appstates/SettingsState.hpp +++ b/include/appstates/SettingsState.hpp @@ -15,13 +15,13 @@ class SettingsState final : public BaseState static inline std::shared_ptr create() { return std::make_shared(); } /// @brief Runs the update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Sub update routine. void sub_update() override; /// @brief Runs the render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief Menu for selecting and toggling settings. @@ -35,7 +35,7 @@ class SettingsState final : public BaseState std::shared_ptr m_controlGuide{}; /// @brief Render target to render to. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief Loads the settings menu strings. void load_settings_menu(); diff --git a/include/appstates/TaskState.hpp b/include/appstates/TaskState.hpp index b77f765..342eb59 100644 --- a/include/appstates/TaskState.hpp +++ b/include/appstates/TaskState.hpp @@ -16,9 +16,7 @@ class TaskState final : public BaseTask /// @brief Constructs and returns a TaskState. static inline std::shared_ptr create(sys::threadpool::JobFunction function, sys::Task::TaskData taskData) - { - return std::make_shared(function, taskData); - } + { return std::make_shared(function, taskData); } /// @brief Constructs, pushes, then returns a new TaskState. static inline std::shared_ptr create_and_push(sys::threadpool::JobFunction function, @@ -38,13 +36,19 @@ class TaskState final : public BaseTask } /// @brief Runs update routine. Waits for thread function to signal finish and deactivates. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Run render routine. Prints m_task's status string to screen, basically. /// @param - void render() override; + void render(sdl2::Renderer &renderer) override; private: + /// @brief Font used for rendering text. + static inline sdl2::SharedFont sm_font{}; + + /// @brief Ensures static members are initialized. + void initialize_static_members(); + /// @brief Performs some operations and marks the state for deletion. void deactivate_state(); }; diff --git a/include/appstates/TextTitleSelectState.hpp b/include/appstates/TextTitleSelectState.hpp index e32de3f..e5862e7 100644 --- a/include/appstates/TextTitleSelectState.hpp +++ b/include/appstates/TextTitleSelectState.hpp @@ -15,9 +15,7 @@ class TextTitleSelectState final : public TitleSelectCommon /// @brief Creates and returns a new TextTitleSelect. See constructor. static inline std::shared_ptr create(data::User *user) - { - return std::make_shared(user); - } + { return std::make_shared(user); } /// @brief Creates, pushes, and returns a new TextTitleSelect. static std::shared_ptr create_and_push(data::User *user) @@ -28,10 +26,10 @@ class TextTitleSelectState final : public TitleSelectCommon } /// @brief Runs update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Runs render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief Refreshes view for changes. void refresh() override; @@ -44,7 +42,7 @@ class TextTitleSelectState final : public TitleSelectCommon std::shared_ptr m_titleSelectMenu{}; /// @brief Target to render to. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief Creates a new backup menu instance. void create_backup_menu(); diff --git a/include/appstates/TitleInfoState.hpp b/include/appstates/TitleInfoState.hpp index c7f25c3..ec09bcc 100644 --- a/include/appstates/TitleInfoState.hpp +++ b/include/appstates/TitleInfoState.hpp @@ -21,9 +21,7 @@ class TitleInfoState final : public BaseState static inline std::shared_ptr create(data::User *user, data::TitleInfo *titleInfo, const FsSaveDataInfo *saveInfo) - { - return std::make_shared(user, titleInfo, saveInfo); - } + { return std::make_shared(user, titleInfo, saveInfo); } /// @brief Creates, pushes, and returns a new TitleInfoState. static inline std::shared_ptr create_and_push(data::User *user, @@ -36,10 +34,10 @@ class TitleInfoState final : public BaseState } /// @brief Runs update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Runs render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; private: /// @brief States this state can be in. @@ -60,7 +58,7 @@ class TitleInfoState final : public BaseState const FsSaveDataInfo *m_saveInfo{}; /// @brief This is a pointer to the title's icon. - sdl::SharedTexture m_icon{}; + sdl2::SharedTexture m_icon{}; /// @brief Transition for the open/close effect. ui::Transition m_transition{}; @@ -84,7 +82,7 @@ class TitleInfoState final : public BaseState static inline std::shared_ptr sm_frame{}; /// @brief This is the little chime that plays when this is opened. - static inline sdl::SharedSound sm_openChime{}; + static inline sdl2::SharedSound sm_openChime{}; /// @brief Initializes the static members if they haven't been already. void initialize_static_members(); @@ -126,10 +124,10 @@ class TitleInfoState final : public BaseState void update_dimensions() noexcept; /// @brief Handles input and updating. - void update_handle_input() noexcept; + void update_handle_input(const sdl2::Input &input) noexcept; /// @brief Returns the color to clear the field with. - inline sdl::Color get_field_color() noexcept; + inline SDL_Color get_field_color() noexcept; /// @brief Signals to close the state. void close() noexcept; diff --git a/include/appstates/TitleOptionState.hpp b/include/appstates/TitleOptionState.hpp index 9a874c2..7489806 100644 --- a/include/appstates/TitleOptionState.hpp +++ b/include/appstates/TitleOptionState.hpp @@ -23,9 +23,7 @@ class TitleOptionState final : public BaseState data::TitleInfo *titleInfo, const FsSaveDataInfo *saveInfo, TitleSelectCommon *titleSelect) - { - return std::make_shared(user, titleInfo, saveInfo, titleSelect); - } + { return std::make_shared(user, titleInfo, saveInfo, titleSelect); } /// @brief Creates, pushes, and returns a new TitleOptionState static std::shared_ptr create_and_push(data::User *user, @@ -39,13 +37,13 @@ class TitleOptionState final : public BaseState } /// @brief Runs update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Handles hiding the panel. void sub_update() override; /// @brief Runs the render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief This function allows tasks to signal to the spawning state to close itself on the next update() call. void close_on_update(); diff --git a/include/appstates/TitleSelectCommon.hpp b/include/appstates/TitleSelectCommon.hpp index a152665..3f96104 100644 --- a/include/appstates/TitleSelectCommon.hpp +++ b/include/appstates/TitleSelectCommon.hpp @@ -14,13 +14,13 @@ class TitleSelectCommon : public BaseState virtual ~TitleSelectCommon() {}; /// @brief Required, inherited. - virtual void update() = 0; + virtual void update(const sdl2::Input &input) = 0; /// @brief Sub-update routine. Normally in a file, but I didn't feel like it really needed one. void sub_update() override; /// @brief Required, inherited. - virtual void render() = 0; + virtual void render(sdl2::Renderer &renderer) = 0; /// @brief Both derived classes need this function. virtual void refresh() = 0; diff --git a/include/appstates/TitleSelectState.hpp b/include/appstates/TitleSelectState.hpp index 35b1e12..134b08e 100644 --- a/include/appstates/TitleSelectState.hpp +++ b/include/appstates/TitleSelectState.hpp @@ -15,9 +15,7 @@ class TitleSelectState final : public TitleSelectCommon /// @brief Returns a new TitleSelect state. static inline std::shared_ptr create(data::User *user) - { - return std::make_shared(user); - } + { return std::make_shared(user); } /// @brief Creates, pushes, and returns a new TitleSelectState. static inline std::shared_ptr create_and_push(data::User *user) @@ -28,10 +26,10 @@ class TitleSelectState final : public TitleSelectCommon } /// @brief Runs the update routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Runs the render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief Refreshes the view. void refresh() override; @@ -41,7 +39,7 @@ class TitleSelectState final : public TitleSelectCommon data::User *m_user{}; /// @brief Target to render to. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief Tiled title selection view. std::shared_ptr m_titleView{}; diff --git a/include/appstates/UserOptionState.hpp b/include/appstates/UserOptionState.hpp index 22b004a..b1d15cf 100644 --- a/include/appstates/UserOptionState.hpp +++ b/include/appstates/UserOptionState.hpp @@ -18,9 +18,7 @@ class UserOptionState final : public BaseState /// @brief Returns a new UserOptionState. See constructor. static inline std::shared_ptr create(data::User *user, TitleSelectCommon *titleSelect) - { - return std::make_shared(user, titleSelect); - } + { return std::make_shared(user, titleSelect); } /// @brief Creates, pushes, and returns a new UserOptionState. static inline std::shared_ptr create_and_push(data::User *user, TitleSelectCommon *titleSelect) @@ -31,13 +29,13 @@ class UserOptionState final : public BaseState } /// @brief Runs the render routine. - void update() override; + void update(const sdl2::Input &input) override; /// @brief Handles hiding the panel. void sub_update() override; /// @brief Runs the render routine. - void render() override; + void render(sdl2::Renderer &renderer) override; /// @brief Signals to the main update() function that a refresh is needed. /// @note Like this to prevent threading headaches. diff --git a/include/data/DataCommon.hpp b/include/data/DataCommon.hpp index 8b7c242..c994e19 100644 --- a/include/data/DataCommon.hpp +++ b/include/data/DataCommon.hpp @@ -10,16 +10,16 @@ namespace data DataCommon() = default; /// @brief Function to load the icon to a texture. - virtual void load_icon() = 0; + virtual void load_icon(sdl2::Renderer &renderer) = 0; /// @brief Returns the icon. - sdl::SharedTexture get_icon() { return m_icon; }; + sdl2::SharedTexture &get_icon() { return m_icon; }; /// @brief Sets the icon. - void set_icon(sdl::SharedTexture &icon) { m_icon = icon; }; + void set_icon(sdl2::SharedTexture &icon) { m_icon = icon; }; protected: /// @brief Shared texture of the icon. - sdl::SharedTexture m_icon{}; + sdl2::SharedTexture m_icon{}; }; } diff --git a/include/data/DataContext.hpp b/include/data/DataContext.hpp index 2f2d2d1..4c21138 100644 --- a/include/data/DataContext.hpp +++ b/include/data/DataContext.hpp @@ -2,6 +2,7 @@ #include "data/DataCommon.hpp" #include "data/TitleInfo.hpp" #include "data/User.hpp" +#include "sdl.hpp" #include "sys/Task.hpp" #include @@ -56,7 +57,7 @@ namespace data bool write_cache(sys::Task *task); /// @brief Processes the icon queue. - void process_icon_queue(); + void process_icon_queue(sdl2::Renderer &renderer); private: /// @brief User vector. diff --git a/include/data/TitleInfo.hpp b/include/data/TitleInfo.hpp index 353e2b5..9edaa08 100644 --- a/include/data/TitleInfo.hpp +++ b/include/data/TitleInfo.hpp @@ -83,16 +83,12 @@ namespace data /// @return True on success. False on failure. bool has_save_data_type(uint8_t saveType) const noexcept; - /// @brief Returns a pointer to the icon texture. - /// @return Icon - sdl::SharedTexture get_icon() const noexcept; - /// @brief Allows the path safe title to be set to a new path. /// @param newPathSafe Buffer containing the new safe path to use. void set_path_safe_title(const char *newPathSafe) noexcept; /// @brief Loads the icon from the nacp. - void load_icon() override; + void load_icon(sdl2::Renderer &renderer) override; private: /// @brief This defines how long the buffer is for the path safe version of the title. @@ -114,7 +110,7 @@ namespace data char m_pathSafeTitle[TitleInfo::SIZE_PATH_SAFE]{}; /// @brief Shared icon texture. - sdl::SharedTexture m_icon{}; + sdl2::SharedTexture m_icon{}; /// @brief Private function to get/create the path safe title. void get_create_path_safe_title() noexcept; diff --git a/include/data/User.hpp b/include/data/User.hpp index bf925e3..7e7ae0c 100644 --- a/include/data/User.hpp +++ b/include/data/User.hpp @@ -109,7 +109,7 @@ namespace data void load_user_data(); /// @brief Loads the icon from the system and converts it to a texture. - void load_icon() override; + void load_icon(sdl2::Renderer &renderer) override; private: /// @brief Account's ID diff --git a/include/data/data.hpp b/include/data/data.hpp index 8a307cf..c56a0e4 100644 --- a/include/data/data.hpp +++ b/include/data/data.hpp @@ -2,6 +2,7 @@ #include "data/TitleInfo.hpp" #include "data/User.hpp" #include "data/accountUID.hpp" +#include "sdl.hpp" #include "sys/sys.hpp" #include @@ -13,7 +14,7 @@ namespace data /// @brief Launches the data loading/initialization state. /// @param clear Whether or not the cache should be cleared. /// @param onDestruction Function that is executed upon destruction of the data loading screen. - void launch_initialization(bool clear, std::function onDestruction); + void launch_initialization(bool clear, sdl2::Renderer &renderer, std::function onDestruction); /// @brief Writes pointers to users to vectorOut /// @param userList List to push the pointers to. diff --git a/include/graphics/ScopedRender.hpp b/include/graphics/ScopedRender.hpp new file mode 100644 index 0000000..5975b66 --- /dev/null +++ b/include/graphics/ScopedRender.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "sdl.hpp" + +#include + +namespace graphics +{ + /// @brief This is a workaround class because of SDLLib changes and I'm not willing to sink a ton of time into JKSV anymore. + class ScopedRender final + { + public: + /// @brief Constructs a new scoped rendering sequence. + /// @param renderer Reference to the renderer. + /// @param target Render target. + ScopedRender(sdl2::Renderer &renderer, sdl2::SharedTexture target) + : m_renderer{renderer} + { + // Push the target. + sm_renderTargets.push(target); + + // Set the target. + renderer.set_render_target(sm_renderTargets.top()); + } + + /// @brief Ends the ScopedRender'ing. + ~ScopedRender() + { + // Pop. + sm_renderTargets.pop(); + + // Set. + m_renderer.set_render_target(sm_renderTargets.top()); + } + + private: + /// @brief Reference to the renderer. + sdl2::Renderer &m_renderer; + + /// @brief Stack of render targets. Init'd with null as the base. + static inline std::stack sm_renderTargets = + std::stack({sdl2::Texture::null}); + }; +} \ No newline at end of file diff --git a/include/graphics/colors.hpp b/include/graphics/colors.hpp index 485f87c..77b8f27 100644 --- a/include/graphics/colors.hpp +++ b/include/graphics/colors.hpp @@ -3,26 +3,26 @@ namespace colors { - inline constexpr sdl::Color WHITE = {0xFFFFFFFF}; - inline constexpr sdl::Color BLACK = {0x000000FF}; - inline constexpr sdl::Color RED = {0xFF0000FF}; - inline constexpr sdl::Color DARK_RED = {0xDD0000FF}; - inline constexpr sdl::Color GREEN = {0x00FF00FF}; - inline constexpr sdl::Color BLUE = {0x0099EEFF}; - inline constexpr sdl::Color YELLOW = {0xF8FC00FF}; - inline constexpr sdl::Color PINK = {0xFF4444FF}; - inline constexpr sdl::Color BLUE_GREEN = {0x00FFC5FF}; - inline constexpr sdl::Color CLEAR_COLOR = {0x2D2D2DFF}; - inline constexpr sdl::Color CLEAR_PANEL = {0x0D0D0DFF}; - inline constexpr sdl::Color DIALOG_DARK = {0x505050FF}; - inline constexpr sdl::Color DIALOG_LIGHT = {0xDCDCDCFF}; - inline constexpr sdl::Color DIM_BACKGROUND = {0x00061180}; - inline constexpr sdl::Color TRANSPARENT = {0x00000000}; - inline constexpr sdl::Color SLIDE_PANEL_CLEAR = {0x000000CC}; - inline constexpr sdl::Color DIV_COLOR = {0x707070FF}; - inline constexpr sdl::Color GUIDE_COLOR = {0x0000007F}; - inline constexpr sdl::Color BAR_GREEN = {0x11CC33FF}; - inline constexpr sdl::Color GOLD = {0xFFD700FF}; + inline constexpr SDL_Color WHITE = {0xFF, 0xFF, 0xFF, 0xFF}; + inline constexpr SDL_Color BLACK = {0x00, 0x00, 0x00, 0xFF}; + inline constexpr SDL_Color RED = {0xFF, 0x00, 0x00, 0xFF}; + inline constexpr SDL_Color DARK_RED = {0xDD, 0x00, 0x00, 0xFF}; + inline constexpr SDL_Color GREEN = {0x00, 0xFF, 0x00, 0xFF}; + inline constexpr SDL_Color BLUE = {0x00, 0x99, 0xEE, 0xFF}; + inline constexpr SDL_Color YELLOW = {0xF8, 0xFC, 0x00, 0xFF}; + inline constexpr SDL_Color PINK = {0xFF, 0x44, 0x44, 0xFF}; + inline constexpr SDL_Color BLUE_GREEN = {0x00, 0xFF, 0xC5, 0xFF}; + inline constexpr SDL_Color CLEAR_COLOR = {0x2D, 0x2D, 0x2D, 0xFF}; + inline constexpr SDL_Color CLEAR_PANEL = {0x0D, 0x0D, 0x0D, 0xFF}; + inline constexpr SDL_Color DIALOG_DARK = {0x50, 0x50, 0x50, 0xFF}; + inline constexpr SDL_Color DIALOG_LIGHT = {0xDC, 0xDC, 0xDC, 0xFF}; + inline constexpr SDL_Color DIM_BACKGROUND = {0x00, 0x06, 0x11, 0x80}; + inline constexpr SDL_Color TRANSPARENT = {0x00, 0x00, 0x00, 0x00}; + inline constexpr SDL_Color SLIDE_PANEL_CLEAR = {0x00, 0x00, 0x00, 0xCC}; + inline constexpr SDL_Color DIV_COLOR = {0x70, 0x70, 0x70, 0xFF}; + inline constexpr SDL_Color GUIDE_COLOR = {0x00, 0x00, 0x00, 0x7F}; + inline constexpr SDL_Color BAR_GREEN = {0x11, 0xCC, 0x33, 0xFF}; + inline constexpr SDL_Color GOLD = {0xFF, 0xD7, 0x00, 0xFF}; inline constexpr uint8_t ALPHA_FADE_BEGIN = 0x00; inline constexpr uint8_t ALPHA_FADE_END = 0x80; diff --git a/include/graphics/fonts.hpp b/include/graphics/fonts.hpp new file mode 100644 index 0000000..29f63d8 --- /dev/null +++ b/include/graphics/fonts.hpp @@ -0,0 +1,28 @@ +#pragma once +#include + +namespace graphics::fonts +{ + namespace names + { + // These are all the internal names of the fonts so the mananger pulls and reuses them. + inline constexpr std::string_view FOURTEEN_PIXEL = "FourteenPixel"; + inline constexpr std::string_view TWENTY_PIXEL = "TwentyPixel"; + inline constexpr std::string_view TWENTY_TWO_PIXEL = "TwentyTwoPixel"; + inline constexpr std::string_view TWENTY_FOUR_PIXEL = "TwentyFourPixel"; + inline constexpr std::string_view THIRTY_TWO_PIXEL = "ThirtyTwoPixel"; + inline constexpr std::string_view THIRTY_FOUR_PIXEL = "ThirtyFourPixel"; + } + + namespace sizes + { + // These are the pixel sizes. + inline constexpr int FOURTEEN_PIXEL = 14; + inline constexpr int TWENTY_PIXEL = 20; + inline constexpr int TWENTY_TWO_PIXEL = 22; + inline constexpr int TWENTY_FOUR_PIXEL = 24; + inline constexpr int THIRTY_TWO_PIXEL = 32; + inline constexpr int THIRTY_FOUR_PIXEL = 34; + } + +} \ No newline at end of file diff --git a/include/graphics/gfxutil.hpp b/include/graphics/gfxutil.hpp index a6f2407..7d5cc57 100644 --- a/include/graphics/gfxutil.hpp +++ b/include/graphics/gfxutil.hpp @@ -12,5 +12,9 @@ namespace gfxutil /// @param background Background color to use. /// @param foreground Color to use to render the text. /// @return sdl::SharedTexture of the icon. - sdl::SharedTexture create_generic_icon(std::string_view text, int fontSize, sdl::Color background, sdl::Color foreground); + sdl2::SharedTexture create_generic_icon(sdl2::Renderer &renderer, + std::string_view text, + int fontSize, + SDL_Color background, + SDL_Color textColor); } // namespace gfxutil diff --git a/include/graphics/targets.hpp b/include/graphics/targets.hpp new file mode 100644 index 0000000..31969d3 --- /dev/null +++ b/include/graphics/targets.hpp @@ -0,0 +1,17 @@ +#pragma once +#include + +namespace graphics::targets +{ + namespace names + { + inline constexpr std::string_view SECONDARY = "SecondaryTarget"; + } + + namespace dims + { + // Dimensions of the secondary render target. + inline constexpr int SECONDARY_WIDTH = 1080; + inline constexpr int SECONDARY_HEIGHT = 555; + } +} \ No newline at end of file diff --git a/include/input.hpp b/include/input.hpp deleted file mode 100644 index 00fef10..0000000 --- a/include/input.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include - -namespace input -{ - /// @brief Initializes PadState and input. - void initialize(); - - /// @brief Updates the PadState. - void update() noexcept; - - /// @brief Returns if a button was pressed the current frame, but not the previous. - /// @param button Button to check. - bool button_pressed(HidNpadButton button) noexcept; - - /// @brief Returns if the button was pressed or held the previous and current frame. - /// @param button Button to check. - bool button_held(HidNpadButton button) noexcept; - - /// @brief Returns if the button was pressed or held the previous frame, but not the current. - /// @param button Button to check. - bool button_released(HidNpadButton button) noexcept; -} // namespace input diff --git a/include/mathutil.hpp b/include/mathutil.hpp index 8d1689c..9ffafc8 100644 --- a/include/mathutil.hpp +++ b/include/mathutil.hpp @@ -7,7 +7,11 @@ namespace math class Util { public: - static inline Type absolute_distance(Type a, Type b) noexcept { return a > b ? a - b : b - a; } + /// @brief Calculates the distance between two Types and returns the result as a positive Type. + static inline constexpr Type absolute_distance(Type a, Type b) noexcept { return a > b ? a - b : b - a; } + + /// @brief Returns size centered within area. + static inline constexpr Type center_within(Type area, Type size) noexcept { return (area / 2) - (size / 2); } }; } diff --git a/include/ui/BoundingBox.hpp b/include/ui/BoundingBox.hpp index ec45566..be275d5 100644 --- a/include/ui/BoundingBox.hpp +++ b/include/ui/BoundingBox.hpp @@ -19,15 +19,13 @@ namespace ui /// @brief Creates a returns a new BoundingBox. See constructor. static inline std::shared_ptr create(int x, int y, int width, int height) - { - return std::make_shared(x, y, width, height); - } + { return std::make_shared(x, y, width, height); } /// @brief Update override. - void update(bool hasFocus) override; + void update(const sdl2::Input &input, bool hasFocus) override; /// @brief Render override. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Sets the X coord. void set_x(int x) noexcept; @@ -58,7 +56,7 @@ namespace ui ui::ColorMod m_colorMod{}; /// @brief This is shared by all instances. - static inline sdl::SharedTexture sm_corners{}; + static inline sdl2::SharedTexture sm_corners{}; /// @brief Loads ^ void initialize_static_members(); diff --git a/include/ui/ColorMod.hpp b/include/ui/ColorMod.hpp index e76f92a..0237a91 100644 --- a/include/ui/ColorMod.hpp +++ b/include/ui/ColorMod.hpp @@ -17,7 +17,7 @@ namespace ui /// @brief Operator that allows using this as an sdl::Color directly. /// @note Since all of these pulse the same color, no sense in not doing this. - operator sdl::Color() const noexcept; + operator SDL_Color() const noexcept; private: /// @brief Whether we're adding or subtracting from the color value. diff --git a/include/ui/ControlGuide.hpp b/include/ui/ControlGuide.hpp index 7af21e1..6425c9d 100644 --- a/include/ui/ControlGuide.hpp +++ b/include/ui/ControlGuide.hpp @@ -14,18 +14,16 @@ namespace ui /// @brief Factory function to return a control guide. static inline std::shared_ptr create(const char *guide) - { - return std::make_shared(guide); - } + { return std::make_shared(guide); } /// @brief Update routine. Opens the guide. - void update(bool hasFocus) override; + void update(const sdl2::Input &input, bool hasFocus) override; /// @brief Sub update routine. Handles hiding the control guide. void sub_update(); /// @brief Renders the control guide. Both arguments are ignored in this case. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief This is a workaround for states where the guide can't really be hidden correctly. Ex: FileMode. void reset() noexcept; @@ -59,7 +57,10 @@ namespace ui ControlGuide::State m_state{}; /// @brief This is shared by all instances. - static inline sdl::SharedTexture sm_controlCap{}; + static inline sdl2::SharedTexture sm_controlCap{}; + + /// @brief Font used to render text. + static inline sdl2::SharedFont sm_font{}; /// @brief Ensures the control guide cap is loaded for all instances. void initialize_static_members(); diff --git a/include/ui/DialogBox.hpp b/include/ui/DialogBox.hpp index e0dfbb8..1715f3b 100644 --- a/include/ui/DialogBox.hpp +++ b/include/ui/DialogBox.hpp @@ -29,17 +29,15 @@ namespace ui int width, int height, DialogBox::Type type = DialogBox::Type::Dark) - { - return std::make_shared(x, y, width, height, type); - } + { return std::make_shared(x, y, width, height, type); } /// @brief Update override. This does NOTHING! - void update(bool hasFocus) override {}; + void update(const sdl2::Input &input, bool hasFocus) override {}; /// @brief Renders the dialog box to screen. /// @param target Render target to render to. /// @param hasFocus This is ignored. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Sets the X render coord. void set_x(int x) noexcept; @@ -75,10 +73,10 @@ namespace ui DialogBox::Type m_type{}; /// @brief All instances shared this and the other. - static inline sdl::SharedTexture sm_darkCorners{}; + static inline sdl2::SharedTexture sm_darkCorners{}; /// @brief This is the light cer - static inline sdl::SharedTexture sm_lightCorners{}; + static inline sdl2::SharedTexture sm_lightCorners{}; /// @brief Initializes and loads the corners texture. void initialize_static_members(); diff --git a/include/ui/Element.hpp b/include/ui/Element.hpp index 4535f31..4d2489f 100644 --- a/include/ui/Element.hpp +++ b/include/ui/Element.hpp @@ -15,11 +15,11 @@ namespace ui /// @brief Virtual update method. All derived classes must have this. /// @param HasFocus Whether or not the state containing the element currently has focus. - virtual void update(bool HasFocus) = 0; + virtual void update(const sdl2::Input &input, bool HasFocus) = 0; /// @brief Virtual render method. All derived classes must have this. /// @param target Target to render to. /// @param hasFocus Whether or not the containing state has focus. - virtual void render(sdl::SharedTexture &target, bool hasFocus) = 0; + virtual void render(sdl2::Renderer &renderer, bool hasFocus) = 0; }; } // namespace ui diff --git a/include/ui/Frame.hpp b/include/ui/Frame.hpp index 376e7b7..8695036 100644 --- a/include/ui/Frame.hpp +++ b/include/ui/Frame.hpp @@ -15,15 +15,13 @@ namespace ui /// @brief Inline function to make constructing nicer. static inline std::shared_ptr create(int x, int y, int width, int height) - { - return std::make_shared(x, y, width, height); - } + { return std::make_shared(x, y, width, height); } /// @brief Doesn't need to do anything for this. - void update(bool hasFocus) override {}; + void update(const sdl2::Input &input, bool hasFocus) override {}; /// @brief Renders the frame to the target passed. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Sets the X coord. void set_x(int x) noexcept; @@ -54,7 +52,7 @@ namespace ui int m_height{}; /// @brief This texture is shared by all instances. - static inline sdl::SharedTexture sm_frameCorners{}; + static inline sdl2::SharedTexture sm_frameCorners{}; /// @brief Ensures the texture is loading if it hasn't been. void initialize_static_members(); diff --git a/include/ui/IconMenu.hpp b/include/ui/IconMenu.hpp index 7821b4b..81a33a5 100644 --- a/include/ui/IconMenu.hpp +++ b/include/ui/IconMenu.hpp @@ -20,9 +20,7 @@ namespace ui /// @brief Creates and returns a new IconMenu instance. static inline std::shared_ptr create(int x, int y, int renderTargetHeight) - { - return std::make_shared(x, y, renderTargetHeight); - } + { return std::make_shared(x, y, renderTargetHeight); } /// @brief Initializes the menu. /// @param x X coordinate to render the menu to. @@ -33,19 +31,19 @@ namespace ui /// @brief Runs the update routine. /// @param hasFocus Whether or not the containing state has focus. - void update(bool hasFocus) override; + void update(const sdl2::Input &input, bool hasFocus) override; /// @brief Runs the render routine. /// @param target Target to render to. /// @param hasFocus Whether or not the containing state has focus. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Adds a new icon to the menu. /// @param newOption Icon to add. - void add_option(sdl::SharedTexture newOption); + void add_option(sdl2::SharedTexture newOption); private: /// @brief Vector of shared texture pointers to textures used. - std::vector m_options; + std::vector m_options; }; } // namespace ui diff --git a/include/ui/Menu.hpp b/include/ui/Menu.hpp index 7c0983b..bec3680 100644 --- a/include/ui/Menu.hpp +++ b/include/ui/Menu.hpp @@ -26,18 +26,16 @@ namespace ui /// @brief Creates and returns a new ui::Menu instance. static inline std::shared_ptr create(int x, int y, int width, int fontSize, int renderTargetHeight) - { - return std::make_shared(x, y, width, fontSize, renderTargetHeight); - } + { return std::make_shared(x, y, width, fontSize, renderTargetHeight); } /// @brief Runs the update routine. /// @param hasFocus Whether or not the calling state has focus. - void update(bool HasFocus) override; + void update(const sdl2::Input &input, bool HasFocus) override; /// @brief Renders the menu. /// @param target Target to render to. /// @param hasFocus Whether or not the calling state has focus. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Adds an option to the menu. /// @param newOption Option to add to menu. @@ -94,7 +92,7 @@ namespace ui int m_optionHeight{}; /// @brief Target options are rendered to. - sdl::SharedTexture m_optionTarget{}; + sdl2::SharedTexture m_optionTarget{}; /// @brief Bounding box for the selected option. std::shared_ptr m_boundingBox{}; @@ -127,8 +125,11 @@ namespace ui /// @brief Text scroll for when the current option is too long to on screen. std::shared_ptr m_optionScroll{}; + /// @brief Font used to render the text. + sdl2::SharedFont m_font{}; + /// @brief The sound played when the selected/cursor moves. - static inline sdl::SharedSound sm_cursor{}; + static inline sdl2::SharedSound sm_cursor{}; /// @brief Calculates the alignment variables. void calculate_alignments() noexcept; @@ -152,6 +153,6 @@ namespace ui void update_scrolling(); /// @brief Handles the menu's input routine. - void handle_input(); + void handle_input(const sdl2::Input &input); }; } // namespace ui diff --git a/include/ui/PopMessage.hpp b/include/ui/PopMessage.hpp index 9122f85..ddf38a8 100644 --- a/include/ui/PopMessage.hpp +++ b/include/ui/PopMessage.hpp @@ -7,7 +7,7 @@ namespace ui { - class PopMessage + class PopMessage final { public: /// @brief PopMessage constructor. @@ -20,7 +20,7 @@ namespace ui void update(double targetY); /// @brief Renders the dialog and text. - void render(); + void render(sdl2::Renderer &renderer); /// @brief Returns whether or not the message can be purged. bool finished() const noexcept; @@ -65,7 +65,10 @@ namespace ui sys::Timer m_typeTimer{}; /// @brief This is the texture used for the ends of the messages. - static inline sdl::SharedTexture sm_endCaps{}; + static inline sdl2::SharedTexture sm_endCaps{}; + + /// @brief Font used to render text. + static inline sdl2::SharedFont sm_font{}; /// @brief Ensures ^ is loaded and ready to go. void initialize_static_members(); @@ -83,6 +86,6 @@ namespace ui void update_display_timer() noexcept; /// @brief Renders the container around the message. - void render_container() noexcept; + void render_container(sdl2::Renderer &renderer) noexcept; }; } diff --git a/include/ui/PopMessageManager.hpp b/include/ui/PopMessageManager.hpp index 3e93bc2..c44b4c5 100644 --- a/include/ui/PopMessageManager.hpp +++ b/include/ui/PopMessageManager.hpp @@ -23,7 +23,7 @@ namespace ui static void update(); /// @brief Renders messages to screen. - static void render(); + static void render(sdl2::Renderer &renderer); /// @brief Pushes a new message to the queue for processing. static void push_message(int displayTicks, std::string_view message); @@ -57,7 +57,7 @@ namespace ui std::mutex m_queueMutex{}; /// @brief The little chirp that's played when messages pop. - sdl::SharedSound m_popSound{}; + sdl2::SharedSound m_popSound{}; /// @brief Loads the pop message sound into memory. void initialize_pop_sound(); diff --git a/include/ui/SlideOutPanel.hpp b/include/ui/SlideOutPanel.hpp index 8b866f2..546a7e3 100644 --- a/include/ui/SlideOutPanel.hpp +++ b/include/ui/SlideOutPanel.hpp @@ -29,13 +29,11 @@ namespace ui /// @brief Creates and returns and new ui::SlideOutPanel instance. static inline std::shared_ptr create(int width, SlideOutPanel::Side side) - { - return std::make_shared(width, side); - } + { return std::make_shared(width, side); } /// @brief Runs the update routine. /// @param hasFocus Whether or not the calling state has focus. - void update(bool hasFocus) override; + void update(const sdl2::Input &input, bool hasFocus) override; /// @brief Sub update routine. Allows the panel to hide and unhide itself even when not in focus. void sub_update(); @@ -43,10 +41,10 @@ namespace ui /// @brief Runs the render routine. /// @param target Target to render to. /// @param hasFocus Whether or the the calling state has focus. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Clears the target to a semi-transparent black. To do: Maybe not hard coded color. - void clear_target(); + void clear_target(sdl2::Renderer &renderer); /// @brief Resets the panel back to its default state. void reset() noexcept; @@ -83,7 +81,7 @@ namespace ui /// @brief Returns a pointer to the render target of the panel. /// @return Raw SDL_Texture pointer to target. - sdl::SharedTexture &get_target() noexcept; + sdl2::SharedTexture &get_target() noexcept; private: /// @brief States the panel can be in. @@ -113,7 +111,7 @@ namespace ui SlideOutPanel::State m_state{}; /// @brief Render target if panel. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief Vector of elements. std::vector> m_elements{}; @@ -131,6 +129,6 @@ namespace ui void update_position_state() noexcept; /// @brief Updates sub-elements. - void update_sub_elements(bool hasFocus) noexcept; + void update_sub_elements(const sdl2::Input &input, bool hasFocus) noexcept; }; } // namespace ui diff --git a/include/ui/TextScroll.hpp b/include/ui/TextScroll.hpp index 1a7fbb5..506e011 100644 --- a/include/ui/TextScroll.hpp +++ b/include/ui/TextScroll.hpp @@ -28,8 +28,8 @@ namespace ui int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center = true); TextScroll(std::string &text, @@ -38,8 +38,8 @@ namespace ui int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center = true); /// @brief Creates and returns a new TextScroll. See constructor. @@ -49,12 +49,10 @@ namespace ui int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center = true) - { - return std::make_shared(text, x, y, width, height, fontSize, textColor, clearColor, center); - } + { return std::make_shared(text, x, y, width, height, fontSize, textColor, clearColor, center); } static inline std::shared_ptr create(std::string &text, int x, @@ -62,12 +60,10 @@ namespace ui int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center = true) - { - return std::make_shared(text, x, y, width, height, fontSize, textColor, clearColor, center); - } + { return std::make_shared(text, x, y, width, height, fontSize, textColor, clearColor, center); } /// @brief Creates/sets the text and parameters for TextScroll. /// @param text Text to display/scroll. @@ -82,8 +78,8 @@ namespace ui int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center = true); void initialize(std::string &text, @@ -92,18 +88,18 @@ namespace ui int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center = true); /// @brief Runs the update routine. /// @param hasFocus Whether or not the calling state has focus. - void update(bool hasFocus) override; + void update(const sdl2::Input &input, bool hasFocus) override; /// @brief Runs the render routine. /// @param target Target to render to. /// @param hasFocus Whether or not the calling state has focus. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Returns the current text being used for scrolling. std::string_view get_text() const noexcept; @@ -133,11 +129,14 @@ namespace ui /// @brief Font size used to calculate and render text. int m_fontSize{}; + /// @brief Font used to render text. + sdl2::SharedFont m_font{}; + /// @brief Color used to render the text. - sdl::Color m_textColor{}; + SDL_Color m_textColor{}; /// @brief Color used to clear the render target. - sdl::Color m_clearColor{}; + SDL_Color m_clearColor{}; /// @brief Width of text in pixels. int m_textWidth{}; @@ -155,9 +154,16 @@ namespace ui bool m_textScrollTriggered{}; /// @brief Render target for the text so it can't be rendered outside of it. - sdl::SharedTexture m_renderTarget{}; + sdl2::SharedTexture m_renderTarget{}; /// @brief Timer for scrolling text. sys::Timer m_scrollTimer; + + /// @brief Generates and returns the name of the render target for the text scroll. + std::string generate_target_name(); + + /// @brief Generates a font name using the font size passed. + /// @param fontSize Size of the font. + std::string generate_font_name(int fontSize); }; -} // namespace ui +} // namespace ui \ No newline at end of file diff --git a/include/ui/TitleTile.hpp b/include/ui/TitleTile.hpp index 5f30656..e6be6cd 100644 --- a/include/ui/TitleTile.hpp +++ b/include/ui/TitleTile.hpp @@ -11,17 +11,17 @@ namespace ui /// @brief Constructor. /// @param isFavorite Whether the title is a favorite and should have the little heart rendered. /// @param icon Shared texture pointer to the icon. - TitleTile(bool isFavorite, int index, sdl::SharedTexture icon); + TitleTile(bool isFavorite, int index, sdl2::SharedTexture &icon); /// @brief Runs the update routine. /// @param isSelected Whether or not the tile is selected and needs to expand. - void update(int selected); + void update(bool isSelected); /// @brief Runs the render routine. /// @param target Target to render to. /// @param x X coordinate to render to. /// @param y Y coordinate to render to. - void render(sdl::SharedTexture &target, int x, int y); + void render(int x, int y); /// @brief Resets the width and height of the tile. void reset() noexcept; @@ -41,10 +41,13 @@ namespace ui /// @brief Whether or not the title is a favorite. bool m_isFavorite{}; - - int m_index{}; - /// @brief Title's icon texture. - sdl::SharedTexture m_icon{}; + sdl2::SharedTexture m_icon{}; + + /// @brief Font used for rendering the heart over the icon. + static inline sdl2::SharedFont sm_heartFont{}; + + /// @brief Ensures the font is loaded., + void initialize_static_members(); }; } // namespace ui diff --git a/include/ui/TitleView.hpp b/include/ui/TitleView.hpp index af2164c..b578fc9 100644 --- a/include/ui/TitleView.hpp +++ b/include/ui/TitleView.hpp @@ -20,18 +20,16 @@ namespace ui TitleView(data::User *user); static inline std::shared_ptr create(data::User *user) - { - return std::make_shared(user); - } + { return std::make_shared(user); } /// @brief Runs the update routine. /// @param hasFocus Whether the calling state has focus. - void update(bool hasFocus) override; + void update(const sdl2::Input &input, bool hasFocus) override; /// @brief Runs the render routine. /// @param target Target to render to. /// @param hasFocus Whether or not the calling state has focus. - void render(sdl::SharedTexture &target, bool hasFocus) override; + void render(sdl2::Renderer &renderer, bool hasFocus) override; /// @brief Returns index of the currently selected tile. /// @return Index of currently selected tile. @@ -75,13 +73,13 @@ namespace ui std::shared_ptr m_bounding{}; /// @brief Sound that is played when the selected title changes. This is shared with the menu code. - static inline sdl::SharedSound sm_cursor{}; + static inline sdl2::SharedSound sm_cursor{}; /// @brief Ensures static members are initialized properly. void initialize_static_members(); /// @brief Performs the input routine. - void handle_input(); + void handle_input(const sdl2::Input &input); /// @brief Performs the "scrolling" routine. void handle_scrolling(); diff --git a/romfs/Text/DE.json.z b/romfs/Text/DE.json.z new file mode 100644 index 0000000..84dbc7b Binary files /dev/null and b/romfs/Text/DE.json.z differ diff --git a/romfs/Text/ENGB.json.z b/romfs/Text/ENGB.json.z new file mode 100644 index 0000000..3b4cc39 Binary files /dev/null and b/romfs/Text/ENGB.json.z differ diff --git a/romfs/Text/ENUS.json.z b/romfs/Text/ENUS.json.z new file mode 100644 index 0000000..e845992 Binary files /dev/null and b/romfs/Text/ENUS.json.z differ diff --git a/romfs/Text/ES.json.z b/romfs/Text/ES.json.z new file mode 100644 index 0000000..9f049ea Binary files /dev/null and b/romfs/Text/ES.json.z differ diff --git a/romfs/Text/ES419.json.z b/romfs/Text/ES419.json.z new file mode 100644 index 0000000..ec9973d Binary files /dev/null and b/romfs/Text/ES419.json.z differ diff --git a/romfs/Text/FR.json.z b/romfs/Text/FR.json.z new file mode 100644 index 0000000..f33d498 Binary files /dev/null and b/romfs/Text/FR.json.z differ diff --git a/romfs/Text/FRCA.json.z b/romfs/Text/FRCA.json.z new file mode 100644 index 0000000..59e05fb Binary files /dev/null and b/romfs/Text/FRCA.json.z differ diff --git a/romfs/Text/IT.json.z b/romfs/Text/IT.json.z new file mode 100644 index 0000000..337568b Binary files /dev/null and b/romfs/Text/IT.json.z differ diff --git a/romfs/Text/JA.json.z b/romfs/Text/JA.json.z new file mode 100644 index 0000000..e2c7aa4 Binary files /dev/null and b/romfs/Text/JA.json.z differ diff --git a/romfs/Text/KO.json.z b/romfs/Text/KO.json.z new file mode 100644 index 0000000..6ead221 Binary files /dev/null and b/romfs/Text/KO.json.z differ diff --git a/romfs/Text/NL.json.z b/romfs/Text/NL.json.z new file mode 100644 index 0000000..86797e9 Binary files /dev/null and b/romfs/Text/NL.json.z differ diff --git a/romfs/Text/PT.json.z b/romfs/Text/PT.json.z new file mode 100644 index 0000000..10a7506 Binary files /dev/null and b/romfs/Text/PT.json.z differ diff --git a/romfs/Text/PTBR.json.z b/romfs/Text/PTBR.json.z new file mode 100644 index 0000000..85988cb Binary files /dev/null and b/romfs/Text/PTBR.json.z differ diff --git a/romfs/Text/RU.json.z b/romfs/Text/RU.json.z new file mode 100644 index 0000000..ec6f702 Binary files /dev/null and b/romfs/Text/RU.json.z differ diff --git a/romfs/Text/ZHCN.json.z b/romfs/Text/ZHCN.json.z new file mode 100644 index 0000000..bfb2ab8 Binary files /dev/null and b/romfs/Text/ZHCN.json.z differ diff --git a/romfs/Text/ZHTW.json.z b/romfs/Text/ZHTW.json.z new file mode 100644 index 0000000..d8f884a Binary files /dev/null and b/romfs/Text/ZHTW.json.z differ diff --git a/source/JKSV.cpp b/source/JKSV.cpp index f025bbf..0081b23 100644 --- a/source/JKSV.cpp +++ b/source/JKSV.cpp @@ -11,8 +11,8 @@ #include "error.hpp" #include "fslib.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include "remote/remote.hpp" #include "sdl.hpp" @@ -46,9 +46,9 @@ namespace // This function allows any service init to be logged with its name without repeating the code. template -static bool initialize_service(Result (*function)(Args...), const char *serviceName, Args... args) +static bool initialize_service(Result (*function)(Args...), const char *serviceName, Args &&...args) { - Result error = (*function)(args...); + Result error = (*function)(std::forward(args)...); if (R_FAILED(error)) { logger::log("Error initializing %s: 0x%X.", serviceName, error); @@ -60,7 +60,15 @@ static bool initialize_service(Result (*function)(Args...), const char *serviceN // ---- Construction ---- JKSV::JKSV() + : m_sdl2() + , m_window(graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT) + , m_renderer(m_window) + , m_audio() + , m_input() { + // Ensure SDL initialized correctly. If not, return. + if (!m_sdl2.is_initialized() || !m_window.is_initialized() || !m_renderer.is_initialized()) { return; } + // Set boost mode first. JKSV::set_boost_mode(); @@ -78,7 +86,6 @@ JKSV::JKSV() ABORT_ON_FAILURE(curl::initialize()); // Config and input. - input::initialize(); config::initialize(); // These are the strings used in the UI. @@ -94,8 +101,8 @@ JKSV::JKSV() sys::threadpool::push_job(remote::initialize, nullptr); // Launch the loading init. Finish init is called afterwards. - auto init_finish = []() { MainMenuState::create_and_push(); }; // Lambda that's exec'd after state is finished. - data::launch_initialization(false, init_finish); + auto init_finish = [&]() { MainMenuState::create_and_push(m_renderer); }; // Lambda that's exec'd after state is finished. + data::launch_initialization(false, m_renderer, init_finish); // This isn't required, but why not? FadeState::create_and_push(colors::BLACK, 0xFF, 0x00, nullptr); @@ -115,8 +122,6 @@ JKSV::~JKSV() config::save(); curl::exit(); JKSV::exit_services(); - sdl::text::SystemFont::exit(); - sdl::exit(); appletSetCpuBoostMode(ApmCpuBoostMode_Normal); appletUnlockExit(); @@ -128,25 +133,35 @@ bool JKSV::is_running() const noexcept { return sm_isRunning && appletMainLoop() void JKSV::update() { - input::update(); + // Update the input instance. + m_input.update(); - const bool plusPressed = input::button_pressed(HidNpadButton_Plus); + // Check if we should close. + const bool plusPressed = m_input.button_pressed(HidNpadButton_Plus); const bool isClosable = StateManager::back_is_closable(); if (plusPressed && isClosable) { sm_isRunning = false; } - StateManager::update(); + // Update states and pop-ups. + StateManager::update(m_input); ui::PopMessageManager::update(); } void JKSV::render() { - sdl::frame_begin(colors::CLEAR_COLOR); + // Set target to frame buffer and clear. + m_renderer.frame_begin(colors::CLEAR_COLOR); + // Render the base. JKSV::render_base(); - StateManager::render(); - ui::PopMessageManager::render(); - sdl::frame_end(); + // Render the states. + StateManager::render(m_renderer); + + // Render pop-ups. + ui::PopMessageManager::render(m_renderer); + + // Present to screen. + m_renderer.frame_end(); } void JKSV::request_quit() noexcept { sm_isRunning = false; } @@ -188,18 +203,39 @@ bool JKSV::initialize_services() bool JKSV::initialize_sdl() { - // Initialize SDL, freetype and the system font. - bool sdlInit = sdl::initialize("JKSV", graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT); - sdlInit = sdlInit && sdl::text::SystemFont::initialize(); - if (!sdlInit) { return false; } + // These are for loading the header icon. + static constexpr std::string_view HEADER_PATH = "romfs:/Textures/HeaderIcon.png"; + + // These are our breakpoints for wrapping lines. + static constexpr std::array BREAKPOINTS = {L' ', L' ', L'/', L'_', L'-', L'。', L'、'}; + + // These are our color changing points. + static constexpr std::array, 7> COLOR_POINTS = {{{L'#', colors::BLUE}, + {L'*', colors::DARK_RED}, + {L'<', colors::YELLOW}, + {L'>', colors::GREEN}, + {L'`', colors::BLUE_GREEN}, + {L'^', colors::PINK}, + {L'$', colors::GOLD}}}; + + // Ensure SDL2 textures and sounds are ready to use. + sdl2::Texture::initialize(m_renderer); + sdl2::Sound::initialize(m_audio); + + // Register break and color points. + sdl2::Font::add_break_points(BREAKPOINTS); + sdl2::Font::add_color_points(COLOR_POINTS); + + // Load our fonts. + m_titleFont = sdl2::FontManager::create_load_resource(graphics::fonts::names::THIRTY_FOUR_PIXEL, + graphics::fonts::sizes::THIRTY_FOUR_PIXEL); + m_buildFont = sdl2::FontManager::create_load_resource(graphics::fonts::names::FOURTEEN_PIXEL, + graphics::fonts::sizes::FOURTEEN_PIXEL); // Load the icon in the top left. - m_headerIcon = sdl::TextureManager::load("headerIcon", "romfs:/Textures/HeaderIcon.png"); + m_headerIcon = sdl2::TextureManager::create_load_resource(HEADER_PATH, HEADER_PATH); if (!m_headerIcon) { return false; } - // Push the color changing characters. - JKSV::add_color_chars(); - return true; } @@ -220,17 +256,6 @@ bool JKSV::create_directories() return true; } -void JKSV::add_color_chars() -{ - sdl::text::add_color_character(L'#', colors::BLUE); - sdl::text::add_color_character(L'*', colors::DARK_RED); - sdl::text::add_color_character(L'<', colors::YELLOW); - sdl::text::add_color_character(L'>', colors::GREEN); - sdl::text::add_color_character(L'`', colors::BLUE_GREEN); - sdl::text::add_color_character(L'^', colors::PINK); - sdl::text::add_color_character(L'$', colors::GOLD); -} - void JKSV::setup_translation_info_strings() { const char *translationFormat = strings::get_by_name(strings::names::TRANSLATION, 0); @@ -269,43 +294,32 @@ void JKSV::render_base() static constexpr int HEADER_Y = 27; // Coordinates for the title text. - static constexpr int TITLE_X = 130; - static constexpr int TITLE_Y = 32; - static constexpr int TITLE_SIZE = 34; + static constexpr int TITLE_X = 130; + static constexpr int TITLE_Y = 32; // Coordinates for the translation info and build date. - static constexpr int BUILD_X = 8; - static constexpr int BUILD_Y = 700; - static constexpr int TRANS_Y = 680; - static constexpr int BUILD_SIZE = 14; + static constexpr int BUILD_X = 8; + static constexpr int BUILD_Y = 700; + static constexpr int TRANS_Y = 680; // This is just the JKSV string. static constexpr std::string_view TITLE_TEXT = "JKSV"; // Top and bottom framing lines. - sdl::render_line(sdl::Texture::Null, LINE_X_BEGIN, LINE_A_Y, LINE_X_END, LINE_A_Y, colors::WHITE); - sdl::render_line(sdl::Texture::Null, LINE_X_BEGIN, LINE_B_Y, LINE_X_END, LINE_B_Y, colors::WHITE); + m_renderer.render_line(LINE_X_BEGIN, LINE_A_Y, LINE_X_END, LINE_A_Y, colors::WHITE); + m_renderer.render_line(LINE_X_BEGIN, LINE_B_Y, LINE_X_END, LINE_B_Y, colors::WHITE); // Icon - m_headerIcon->render(sdl::Texture::Null, HEADER_X, HEADER_Y); + m_headerIcon->render(HEADER_X, HEADER_Y); // "JKSV" - sdl::text::render(sdl::Texture::Null, TITLE_X, TITLE_Y, TITLE_SIZE, sdl::text::NO_WRAP, colors::WHITE, TITLE_TEXT); + m_titleFont->render_text(TITLE_X, TITLE_Y, colors::WHITE, TITLE_TEXT); // Translation info in bottom left. - if (m_showTranslationInfo) - { - sdl::text::render(sdl::Texture::Null, - BUILD_X, - TRANS_Y, - BUILD_SIZE, - sdl::text::NO_WRAP, - colors::WHITE, - m_translationInfo); - } + if (m_showTranslationInfo) { m_buildFont->render_text(BUILD_X, TRANS_Y, colors::WHITE, m_translationInfo); } // Build date - sdl::text::render(sdl::Texture::Null, BUILD_X, BUILD_Y, BUILD_SIZE, sdl::text::NO_WRAP, colors::WHITE, m_buildString); + m_buildFont->render_text(BUILD_X, BUILD_Y, colors::WHITE, m_buildString); } void JKSV::exit_services() diff --git a/source/StateManager.cpp b/source/StateManager.cpp index 7060db9..9771ee6 100644 --- a/source/StateManager.cpp +++ b/source/StateManager.cpp @@ -1,6 +1,6 @@ #include "StateManager.hpp" -void StateManager::update() +void StateManager::update(const sdl2::Input &input) { // Grab the instance. StateManager &instance = StateManager::get_instance(); @@ -10,7 +10,7 @@ void StateManager::update() { auto &back = stateVector.back(); - if (back->is_active() && back->has_focus()) { back->update(); } + if (back->is_active() && back->has_focus()) { back->update(input); } } // Purge uneeded states. @@ -39,12 +39,12 @@ void StateManager::update() } } -void StateManager::render() noexcept +void StateManager::render(sdl2::Renderer &renderer) noexcept { StateManager &instance = StateManager::get_instance(); auto &stateVector = instance.m_stateVector; - for (std::shared_ptr &state : stateVector) { state->render(); } + for (std::shared_ptr &state : stateVector) { state->render(renderer); } } bool StateManager::back_is_closable() noexcept diff --git a/source/appstates/BackupMenuState.cpp b/source/appstates/BackupMenuState.cpp index c9b0cee..0998110 100644 --- a/source/appstates/BackupMenuState.cpp +++ b/source/appstates/BackupMenuState.cpp @@ -8,8 +8,9 @@ #include "error.hpp" #include "fs/fs.hpp" #include "fslib.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" +#include "graphics/fonts.hpp" #include "keyboard/keyboard.hpp" #include "sdl.hpp" #include "strings/strings.hpp" @@ -43,13 +44,13 @@ BackupMenuState::BackupMenuState(data::User *user, data::TitleInfo *titleInfo, c // ---- Public functions ---- -void BackupMenuState::update() +void BackupMenuState::update(const sdl2::Input &input) { // Grab focus once and only once. const bool hasFocus = BaseState::has_focus(); // Update the panel first. - sm_slidePanel->update(hasFocus); + sm_slidePanel->update(input, hasFocus); // Grab these. const bool isOpen = sm_slidePanel->is_open(); @@ -67,11 +68,11 @@ void BackupMenuState::update() } // Input bools. - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool xPressed = input::button_pressed(HidNpadButton_X); - const bool yPressed = input::button_pressed(HidNpadButton_Y); - const bool zrPressed = input::button_pressed(HidNpadButton_ZR); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); + const bool xPressed = input.button_pressed(HidNpadButton_B); + const bool yPressed = input.button_pressed(HidNpadButton_Y); + const bool zrPressed = input.button_pressed(HidNpadButton_ZR); // Conditions. const bool newSelected = selected == 0; @@ -82,7 +83,7 @@ void BackupMenuState::update() const bool uploadBackup = zrPressed && !newSelected; const bool popEmpty = aPressed && !m_saveHasData; - if (newBackup) { BackupMenuState::name_and_create_backup(); } + if (newBackup) { BackupMenuState::name_and_create_backup(input); } else if (overwriteBackup) { BackupMenuState::confirm_overwrite(); } else if (restoreBackup) { BackupMenuState::confirm_restore(); } else if (deleteBackup) { BackupMenuState::confirm_delete(); } @@ -93,10 +94,10 @@ void BackupMenuState::update() // Lock and update the menu. std::lock_guard menuGuard{sm_menuMutex}; - sm_backupMenu->update(hasFocus); + sm_backupMenu->update(input, hasFocus); } -void BackupMenuState::render() +void BackupMenuState::render(sdl2::Renderer &renderer) { // Line render coords. static constexpr int LINE_X = 10; @@ -106,32 +107,37 @@ void BackupMenuState::render() static constexpr int CONTROL_X = 32; static constexpr int CONTROL_Y = 673; - // Clear the render target. - sm_slidePanel->clear_target(); - // Grab whether or not the state has focus and the render target for the panel. - const bool hasFocus = BaseState::has_focus(); - sdl::SharedTexture &target = sm_slidePanel->get_target(); - - // Render the top, bottom lines. Control guide string. - sdl::render_line(target, LINE_X, LINE_A_Y, sm_panelWidth - LINE_X, LINE_A_Y, colors::WHITE); - sdl::render_line(target, LINE_X, LINE_B_Y, sm_panelWidth - LINE_X, LINE_B_Y, colors::WHITE); - sdl::text::render(target, CONTROL_X, CONTROL_Y, 22, sdl::text::NO_WRAP, colors::WHITE, m_controlGuide); + const bool hasFocus = BaseState::has_focus(); // This is the target for the menu so it can't render outside of the lines above. - sm_menuRenderTarget->clear(colors::TRANSPARENT); - - // Lock and render the menu. { - std::lock_guard menuGuard{sm_menuMutex}; - sm_backupMenu->render(sm_menuRenderTarget, hasFocus); + graphics::ScopedRender menuRender{renderer, sm_menuRenderTarget}; + renderer.frame_begin(colors::TRANSPARENT); + + // Lock and render the menu. + { + std::lock_guard menuGuard{sm_menuMutex}; + sm_backupMenu->render(renderer, hasFocus); + } + + // Get target and change target. + { + graphics::ScopedRender slideRender{renderer, sm_slidePanel->get_target()}; + renderer.frame_begin(colors::SLIDE_PANEL_CLEAR); + + // Render the top, bottom lines. Control guide string. + renderer.render_line(LINE_X, LINE_A_Y, sm_panelWidth - LINE_X, LINE_A_Y, colors::WHITE); + renderer.render_line(LINE_X, LINE_B_Y, sm_panelWidth - LINE_X, LINE_B_Y, colors::WHITE); + sm_font->render_text(CONTROL_X, CONTROL_Y, colors::WHITE, m_controlGuide); + + // Render the menu target to the slide panel target. + sm_menuRenderTarget->render(0, 43); + } } - // Render the menu target to the slide panel target. - sm_menuRenderTarget->render(target, 0, 43); - // Finally, render the target to the screen. - sm_slidePanel->render(sdl::Texture::Null, hasFocus); + sm_slidePanel->render(renderer, hasFocus); } void BackupMenuState::refresh() @@ -196,12 +202,18 @@ void BackupMenuState::save_data_written() void BackupMenuState::initialize_static_members() { - if (sm_backupMenu && sm_slidePanel && sm_menuRenderTarget && sm_panelWidth) { return; } + // Name of the render target for the backup menu. + static constexpr std::string_view BACKUP_TARGET = "BackupMenuTarget"; - sm_panelWidth = sdl::text::get_width(22, m_controlGuide) + 64; - sm_backupMenu = ui::Menu::create(8, 8, sm_panelWidth - 16, 22, 600); - sm_slidePanel = ui::SlideOutPanel::create(sm_panelWidth, ui::SlideOutPanel::Side::Right); - sm_menuRenderTarget = sdl::TextureManager::load("backupMenuTarget", sm_panelWidth, 600, SDL_TEXTUREACCESS_TARGET); + if (sm_backupMenu && sm_slidePanel && sm_menuRenderTarget && sm_panelWidth && sm_font) { return; } + + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_TWO_PIXEL, + graphics::fonts::sizes::TWENTY_TWO_PIXEL); + sm_panelWidth = sm_font->get_text_width(m_controlGuide) + 64; + sm_backupMenu = ui::Menu::create(8, 8, sm_panelWidth - 16, 22, 600); + sm_slidePanel = ui::SlideOutPanel::create(sm_panelWidth, ui::SlideOutPanel::Side::Right); + sm_menuRenderTarget = + sdl2::TextureManager::create_load_resource(BACKUP_TARGET, sm_panelWidth, 600, SDL_TEXTUREACCESS_TARGET); } void BackupMenuState::ensure_target_directory() @@ -262,7 +274,7 @@ void BackupMenuState::initialize_remote_storage() remote->change_directory(remoteDir); } -void BackupMenuState::name_and_create_backup() +void BackupMenuState::name_and_create_backup(const sdl2::Input &input) { // Size of the buffer for naming backups. static constexpr size_t SIZE_NAME_LENGTH = 0x80; @@ -279,7 +291,7 @@ void BackupMenuState::name_and_create_backup() const bool exportZip = autoUpload || config::get_by_key(config::keys::EXPORT_TO_ZIP); // Input. - const bool zrHeld = input::button_held(HidNpadButton_ZR); + const bool zrHeld = input.button_held(HidNpadButton_ZR); // Whether or not we should skip the keyboard. const bool autoNamed = (autoName || zrHeld); // This can be eval'd here. @@ -525,7 +537,10 @@ void BackupMenuState::upload_backup() // Push the confirmation. ConfirmProgress::create_push_fade(query, holdRequired, tasks::backup::patch_backup, nullptr, m_dataStruct); } - else { ProgressState::create_push_fade(tasks::backup::upload_backup, m_dataStruct); } + else + { + ProgressState::create_push_fade(tasks::backup::upload_backup, m_dataStruct); + } } void BackupMenuState::pop_save_empty() diff --git a/source/appstates/BaseTask.cpp b/source/appstates/BaseTask.cpp index 470ea39..671170c 100644 --- a/source/appstates/BaseTask.cpp +++ b/source/appstates/BaseTask.cpp @@ -1,7 +1,7 @@ #include "appstates/BaseTask.hpp" #include "graphics/colors.hpp" -#include "input.hpp" +#include "graphics/fonts.hpp" #include "strings/strings.hpp" #include "ui/PopMessageManager.hpp" @@ -15,7 +15,8 @@ namespace BaseTask::BaseTask() : BaseState(false) , m_frameTimer(TICKS_GLYPH_TRIGGER) - , m_popUnableExit(strings::get_by_name(strings::names::GENERAL_POPS, 0)) {}; + , m_popUnableExit(strings::get_by_name(strings::names::GENERAL_POPS, 0)) +{ BaseTask::initialize_static_members(); } // ---- Public functions ---- @@ -27,9 +28,9 @@ void BaseTask::update_loading_glyph() else if (++m_currentFrame % 8 == 0) { m_currentFrame = 0; } } -void BaseTask::pop_on_plus() +void BaseTask::pop_on_plus(const sdl2::Input &input) { - const bool plusPressed = input::button_pressed(HidNpadButton_Plus); + const bool plusPressed = input.button_pressed(HidNpadButton_Plus); if (plusPressed) { ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_TICKS, m_popUnableExit); } } @@ -37,15 +38,18 @@ void BaseTask::pop_on_plus() void BaseTask::render_loading_glyph() { // Render coords. - static constexpr int RENDER_X = 56; - static constexpr int RENDER_Y = 673; - static constexpr int RENDER_SIZE = 32; + static constexpr int RENDER_X = 56; + static constexpr int RENDER_Y = 673; - sdl::text::render(sdl::Texture::Null, - RENDER_X, - RENDER_Y, - RENDER_SIZE, - sdl::text::NO_WRAP, - m_colorMod, - sm_glyphArray[m_currentFrame]); + sm_font->render_text(RENDER_X, RENDER_Y, colors::WHITE, sm_glyphArray[m_currentFrame]); } + +// ---- Private Functions ---- + +void BaseTask::initialize_static_members() +{ + if (sm_font) { return; } + + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::THIRTY_TWO_PIXEL, + graphics::fonts::sizes::THIRTY_TWO_PIXEL); +} \ No newline at end of file diff --git a/source/appstates/BlacklistEditState.cpp b/source/appstates/BlacklistEditState.cpp index db1296a..dfa6066 100644 --- a/source/appstates/BlacklistEditState.cpp +++ b/source/appstates/BlacklistEditState.cpp @@ -6,7 +6,6 @@ #include "data/data.hpp" #include "error.hpp" #include "graphics/screen.hpp" -#include "input.hpp" // ---- Construction ---- @@ -21,24 +20,24 @@ BlacklistEditState::BlacklistEditState() // ---- Public functions ---- -void BlacklistEditState::update() +void BlacklistEditState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); - sm_slidePanel->update(hasFocus); + sm_slidePanel->update(input, hasFocus); if (aPressed) { BlacklistEditState::remove_from_blacklist(); } else if (sm_slidePanel->is_closed()) { BlacklistEditState::deactivate_state(); } else if (bPressed || m_blacklist.empty()) { sm_slidePanel->close(); } } -void BlacklistEditState::render() +void BlacklistEditState::render(sdl2::Renderer &renderer) { const bool hasFocus = BaseState::has_focus(); - sm_slidePanel->clear_target(); - sm_slidePanel->render(sdl::Texture::Null, hasFocus); + + sm_slidePanel->render(renderer, hasFocus); } // ---- Private functions ---- diff --git a/source/appstates/DataLoadingState.cpp b/source/appstates/DataLoadingState.cpp index d174531..9d7888a 100644 --- a/source/appstates/DataLoadingState.cpp +++ b/source/appstates/DataLoadingState.cpp @@ -1,22 +1,21 @@ #include "appstates/DataLoadingState.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" #include "logging/logger.hpp" - -namespace -{ - constexpr int SCREEN_CENTER = 640; -} +#include "mathutil.hpp" // ---- Construction ---- DataLoadingState::DataLoadingState(data::DataContext &context, + sdl2::Renderer &renderer, DestructFunction destructFunction, sys::threadpool::JobFunction function, sys::Task::TaskData taskData) : BaseTask() , m_context(context) + , m_renderer(renderer) , m_destructFunction(destructFunction) { DataLoadingState::initialize_static_members(); @@ -25,27 +24,34 @@ DataLoadingState::DataLoadingState(data::DataContext &context, // ---- Public functions ---- -void DataLoadingState::update() +void DataLoadingState::update(const sdl2::Input &input) { BaseTask::update_loading_glyph(); if (!m_task->is_running()) { DataLoadingState::deactivate_state(); } - m_context.process_icon_queue(); + m_context.process_icon_queue(m_renderer); } void DataLoadingState::sub_update() { BaseTask::update_loading_glyph(); } -void DataLoadingState::render() +void DataLoadingState::render(sdl2::Renderer &renderer) { - static constexpr int ICON_X_COORD = SCREEN_CENTER - 128; - static constexpr int ICON_Y_COORD = 226; - const std::string status = m_task->get_status(); + // These are the dimensions of the icon loaded. + static constexpr int ICON_WIDTH = 256; + static constexpr int ICON_HEIGHT = 259; + static constexpr int ICON_X = math::Util::center_within(graphics::SCREEN_WIDTH, ICON_WIDTH); + static constexpr int ICON_Y = math::Util::center_within(graphics::SCREEN_HEIGHT, ICON_HEIGHT); - const int statusWidth = sdl::text::get_width(BaseTask::FONT_SIZE, status); - m_statusX = SCREEN_CENTER - (statusWidth / 2); + // Grab the status from the task and center it. + const std::string status = m_task->get_status(); + const int statusWidth = sm_font->get_text_width(status); + m_statusX = math::Util::center_within(graphics::SCREEN_WIDTH, statusWidth); - sdl::render_rect_fill(sdl::Texture::Null, 0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::CLEAR_COLOR); - sm_jksvIcon->render(sdl::Texture::Null, ICON_X_COORD, ICON_Y_COORD); - sdl::text::render(sdl::Texture::Null, m_statusX, 673, BaseTask::FONT_SIZE, sdl::text::NO_WRAP, colors::WHITE, status); + // This is to cover up the base rendering. + renderer.render_rectangle(0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::CLEAR_COLOR); + + // Render the icon, status and loading glyph. + sm_jksvIcon->render(ICON_X, ICON_Y); + sm_font->render_text(m_statusX, 673, colors::WHITE, status); BaseTask::render_loading_glyph(); } @@ -53,15 +59,21 @@ void DataLoadingState::render() void DataLoadingState::initialize_static_members() { - if (sm_jksvIcon) { return; } + // Path for the loading icon. + static constexpr std::string_view ICON_PATH = "romfs:/Textures/LoadingIcon.png"; - sm_jksvIcon = sdl::TextureManager::load("LoadingIcon", "romfs:/Textures/LoadingIcon.png"); + if (sm_jksvIcon && sm_font) { return; } + + // Load icon and font. + sm_jksvIcon = sdl2::TextureManager::create_load_resource(ICON_PATH, ICON_PATH); + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_PIXEL, + graphics::fonts::sizes::TWENTY_PIXEL); } void DataLoadingState::deactivate_state() { // This is to catch any stragglers. - m_context.process_icon_queue(); + m_context.process_icon_queue(m_renderer); if (m_destructFunction) { m_destructFunction(); } BaseState::deactivate(); } diff --git a/source/appstates/ExtrasMenuState.cpp b/source/appstates/ExtrasMenuState.cpp index 9333a74..571e50b 100644 --- a/source/appstates/ExtrasMenuState.cpp +++ b/source/appstates/ExtrasMenuState.cpp @@ -4,8 +4,9 @@ #include "appstates/MainMenuState.hpp" #include "data/data.hpp" #include "error.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" +#include "graphics/targets.hpp" #include "keyboard/keyboard.hpp" #include "strings/strings.hpp" #include "ui/PopMessageManager.hpp" @@ -14,9 +15,6 @@ namespace { - // This target is shared be a lot of states. - constexpr std::string_view SECONDARY_TARGET = "SecondaryTarget"; - // Enum for switch case readability. enum { @@ -35,23 +33,25 @@ static void finish_reinitialization(); // ---- Construction ---- -ExtrasMenuState::ExtrasMenuState() - : m_renderTarget(sdl::TextureManager::load(SECONDARY_TARGET, 1080, 555, SDL_TEXTUREACCESS_TARGET)) +ExtrasMenuState::ExtrasMenuState(sdl2::Renderer &renderer) + : m_renderer(renderer) + , m_renderTarget(sdl2::TextureManager::create_load_resource(graphics::targets::names::SECONDARY, + graphics::targets::dims::SECONDARY_WIDTH, + graphics::targets::dims::SECONDARY_HEIGHT, + SDL_TEXTUREACCESS_TARGET)) , m_controlGuide(ui::ControlGuide::create(strings::get_by_name(strings::names::CONTROL_GUIDES, 5))) -{ - ExtrasMenuState::initialize_menu(); -} +{ ExtrasMenuState::initialize_menu(); } // ---- Public functions ---- -void ExtrasMenuState::update() +void ExtrasMenuState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); - m_extrasMenu->update(hasFocus); - m_controlGuide->update(hasFocus); + m_extrasMenu->update(input, hasFocus); + m_controlGuide->update(input, hasFocus); if (aPressed) { @@ -71,14 +71,20 @@ void ExtrasMenuState::update() void ExtrasMenuState::sub_update() { m_controlGuide->sub_update(); } -void ExtrasMenuState::render() +void ExtrasMenuState::render(sdl2::Renderer &renderer) { const bool hasFocus = BaseState::has_focus(); - m_renderTarget->clear(colors::TRANSPARENT); - m_extrasMenu->render(m_renderTarget, hasFocus); - m_renderTarget->render(sdl::Texture::Null, 201, 91); - m_controlGuide->render(sdl::Texture::Null, hasFocus); + { // Set the render target. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(colors::TRANSPARENT); + + // Render the menu to it. + m_extrasMenu->render(renderer, hasFocus); + } + + m_renderTarget->render(201, 91); + m_controlGuide->render(renderer, hasFocus); } // ---- Private functions ---- @@ -93,7 +99,7 @@ void ExtrasMenuState::initialize_menu() } } -void ExtrasMenuState::reinitialize_data() { data::launch_initialization(true, finish_reinitialization); } +void ExtrasMenuState::reinitialize_data() { data::launch_initialization(true, m_renderer, finish_reinitialization); } void ExtrasMenuState::sd_to_sd_browser() { FileModeState::create_and_push("sdmc", "sdmc", false); } diff --git a/source/appstates/FadeState.cpp b/source/appstates/FadeState.cpp index 9982c94..6f69bed 100644 --- a/source/appstates/FadeState.cpp +++ b/source/appstates/FadeState.cpp @@ -12,7 +12,7 @@ namespace } // ---- Construction ---- -FadeState::FadeState(sdl::Color baseColor, uint8_t startAlpha, uint8_t endAlpha, std::shared_ptr nextState) +FadeState::FadeState(SDL_Color baseColor, uint8_t startAlpha, uint8_t endAlpha, std::shared_ptr nextState) : m_baseColor(baseColor) , m_alpha(startAlpha) , m_endAlpha(endAlpha) @@ -23,7 +23,7 @@ FadeState::FadeState(sdl::Color baseColor, uint8_t startAlpha, uint8_t endAlpha, m_fadeTimer.start(TICKS_TIMER_TRIGGER); } -void FadeState::update() +void FadeState::update(const sdl2::Input &input) { if (m_alpha == m_endAlpha) { @@ -38,12 +38,13 @@ void FadeState::update() } } -void FadeState::render() +void FadeState::render(sdl2::Renderer &renderer) { - const uint32_t rawColor = (m_baseColor.raw & 0xFFFFFF00) | m_alpha; - const sdl::Color fadeColor{rawColor}; + // Update alpha. + m_baseColor.a = m_alpha; - sdl::render_rect_fill(sdl::Texture::Null, 0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, fadeColor); + // Render overtop of everything. + renderer.render_rectangle(0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, m_baseColor); } // ---- Private functions ---- diff --git a/source/appstates/FileModeState.cpp b/source/appstates/FileModeState.cpp index 15d877c..63c7636 100644 --- a/source/appstates/FileModeState.cpp +++ b/source/appstates/FileModeState.cpp @@ -2,9 +2,9 @@ #include "appstates/FileOptionState.hpp" #include "config/config.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" #include "graphics/screen.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include "mathutil.hpp" #include "strings/strings.hpp" @@ -39,17 +39,17 @@ FileModeState::FileModeState(std::string_view mountA, std::string_view mountB, i // ---- Public functions ---- -void FileModeState::update() +void FileModeState::update(const sdl2::Input &input) { switch (m_state) { case State::Rising: case State::Dropping: FileModeState::update_y(); break; - case State::Open: FileModeState::update_handle_input(); break; + case State::Open: FileModeState::update_handle_input(input); break; } } -void FileModeState::render() +void FileModeState::render(sdl2::Renderer &renderer) { // Coords for divider lines. static constexpr int LINE_A_X = 617; @@ -59,27 +59,25 @@ void FileModeState::render() static constexpr int LINE_Y_A = 0; static constexpr int LINE_Y_B = 538; + // Grab focus. const bool hasFocus = BaseState::has_focus(); - sm_renderTarget->clear(colors::TRANSPARENT); + // Switch targets, clear. + { + graphics::ScopedRender scopedRender{renderer, sm_renderTarget}; + renderer.frame_begin(colors::TRANSPARENT); - // This is here so it's rendered underneath the pop-up frame. - sm_controlGuide->render(sdl::Texture::Null, hasFocus); + // Divider lines & menus. + renderer.render_line(LINE_A_X, LINE_Y_A, LINE_A_X, LINE_Y_B, colors::WHITE); + renderer.render_line(LINE_B_X, LINE_Y_A, LINE_B_X, LINE_Y_B, colors::DIALOG_DARK); + m_dirMenuA->render(renderer, hasFocus && m_target == Target::MountA); + m_dirMenuB->render(renderer, hasFocus && m_target == Target::MountB); + } - // Center divider lines. - sdl::render_line(sm_renderTarget, LINE_A_X, LINE_Y_A, LINE_A_X, LINE_Y_B, colors::WHITE); - sdl::render_line(sm_renderTarget, LINE_B_X, LINE_Y_A, LINE_B_X, LINE_Y_B, colors::DIALOG_DARK); - - // Menus - m_dirMenuA->render(sm_renderTarget, hasFocus && m_target == Target::MountA); - m_dirMenuB->render(sm_renderTarget, hasFocus && m_target == Target::MountB); - - // Frame. - sm_frame->render(sdl::Texture::Null, true); - - // Main target. - const int y = m_transition.get_y(); - sm_renderTarget->render(sdl::Texture::Null, 23, y + 12); + // This needs to be in this specific order to look right. + sm_controlGuide->render(renderer, hasFocus); + sm_frame->render(renderer, hasFocus); + sm_renderTarget->render(23, m_transition.get_y() + 12); } // ---- Private functions ---- @@ -99,8 +97,9 @@ void FileModeState::initialize_static_members() if (sm_frame && sm_renderTarget && sm_controlGuide) { return; } - sm_frame = ui::Frame::create(PERMA_X, graphics::SCREEN_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT); - sm_renderTarget = sdl::TextureManager::load(RENDER_TARGET_NAME, INNER_WIDTH, INNER_HEIGHT, SDL_TEXTUREACCESS_TARGET); + sm_frame = ui::Frame::create(PERMA_X, graphics::SCREEN_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT); + sm_renderTarget = + sdl2::TextureManager::create_load_resource(RENDER_TARGET_NAME, INNER_WIDTH, INNER_HEIGHT, SDL_TEXTUREACCESS_TARGET); sm_controlGuide = ui::ControlGuide::create(strings::get_by_name(strings::names::CONTROL_GUIDES, 4)); } @@ -147,7 +146,10 @@ void FileModeState::initialize_directory_menu(const fslib::Path &path, fslib::Di { std::string option{}; if (entry.is_directory()) { option = DIR_PREFIX; } - else { option = FILE_PREFIX; } + else + { + option = FILE_PREFIX; + } option += entry.get_filename(); menu.add_option(option); @@ -170,7 +172,7 @@ void FileModeState::update_y() noexcept else if (finishedDropping) { FileModeState::deactivate_state(); } } -void FileModeState::update_handle_input() noexcept +void FileModeState::update_handle_input(const sdl2::Input &input) noexcept { // Get whether or not the state has focus. const bool hasFocus = BaseState::has_focus(); @@ -181,11 +183,11 @@ void FileModeState::update_handle_input() noexcept fslib::Directory &directory = FileModeState::get_source_directory(); // Input bools. - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool xPressed = input::button_pressed(HidNpadButton_X); - const bool zlZRPressed = input::button_pressed(HidNpadButton_ZL) || input::button_pressed(HidNpadButton_ZR); - const bool minusPressed = input::button_pressed(HidNpadButton_Minus); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); + const bool xPressed = input.button_pressed(HidNpadButton_X); + const bool zlZRPressed = input.button_pressed(HidNpadButton_ZL) || input.button_pressed(HidNpadButton_ZR); + const bool minusPressed = input.button_pressed(HidNpadButton_Minus); // Conditions if (aPressed) { FileModeState::enter_selected(path, directory, menu); } @@ -199,8 +201,8 @@ void FileModeState::update_handle_input() noexcept } // Update the menu and control guide. - menu.update(hasFocus); - sm_controlGuide->update(hasFocus); + menu.update(input, hasFocus); + sm_controlGuide->update(input, hasFocus); } void FileModeState::enter_selected(fslib::Path &path, fslib::Directory &directory, ui::Menu &menu) @@ -256,14 +258,10 @@ void FileModeState::enter_directory(fslib::Path &path, } ui::Menu &FileModeState::get_source_menu() noexcept -{ - return m_target == Target::MountA ? *m_dirMenuA.get() : *m_dirMenuB.get(); -} +{ return m_target == Target::MountA ? *m_dirMenuA.get() : *m_dirMenuB.get(); } ui::Menu &FileModeState::get_destination_menu() noexcept -{ - return m_target == Target::MountA ? *m_dirMenuB.get() : *m_dirMenuA.get(); -} +{ return m_target == Target::MountA ? *m_dirMenuB.get() : *m_dirMenuA.get(); } fslib::Path &FileModeState::get_source_path() noexcept { return m_target == Target::MountA ? m_pathA : m_pathB; } diff --git a/source/appstates/FileOptionState.cpp b/source/appstates/FileOptionState.cpp index de6ebd0..878cc26 100644 --- a/source/appstates/FileOptionState.cpp +++ b/source/appstates/FileOptionState.cpp @@ -8,7 +8,6 @@ #include "error.hpp" #include "fs/fs.hpp" #include "fslib.hpp" -#include "input.hpp" #include "keyboard/keyboard.hpp" #include "logging/logger.hpp" #include "mathutil.hpp" @@ -70,23 +69,26 @@ FileOptionState::FileOptionState(FileModeState *spawningState) // ---- Public functions ---- -void FileOptionState::update() +void FileOptionState::update(const sdl2::Input &input) { switch (m_state) { case State::Opening: case State::Closing: FileOptionState::update_dimensions(); break; - case State::Opened: FileOptionState::update_handle_input(); break; + case State::Opened: FileOptionState::update_handle_input(input); break; } } -void FileOptionState::render() +void FileOptionState::render(sdl2::Renderer &renderer) { + // Grab focus. const bool hasFocus = BaseState::has_focus(); - sm_dialog->render(sdl::Texture::Null, hasFocus); + + // Render the dialog and bail if we're not read for the menu. + sm_dialog->render(renderer, hasFocus); if (!m_transition.in_place()) { return; } - sm_copyMenu->render(sdl::Texture::Null, hasFocus); + sm_copyMenu->render(renderer, hasFocus); } void FileOptionState::update_source() { m_updateSource = true; } @@ -156,7 +158,7 @@ void FileOptionState::update_dimensions() noexcept else if (isClosed) { FileOptionState::deactivate_state(); } } -void FileOptionState::update_handle_input() noexcept +void FileOptionState::update_handle_input(const sdl2::Input &input) noexcept { // Grab whether or not the state has focus since this is actually used a lot. const bool hasFocus = BaseState::has_focus(); @@ -166,12 +168,12 @@ void FileOptionState::update_handle_input() noexcept else if (m_updateDest) { FileOptionState::update_filemode_dest(); } // Update the menu input. - sm_copyMenu->update(hasFocus); + sm_copyMenu->update(input, hasFocus); // Local input handling. const int selected = sm_copyMenu->get_selected(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); if (aPressed) { @@ -398,7 +400,10 @@ void FileOptionState::get_show_target_properties() const bool isDir = fslib::directory_exists(targetPath); if (isDir) { FileOptionState::get_show_directory_properties(targetPath); } - else { FileOptionState::get_show_file_properties(targetPath); } + else + { + FileOptionState::get_show_file_properties(targetPath); + } } void FileOptionState::close_dialog() noexcept @@ -460,11 +465,11 @@ void FileOptionState::get_show_file_properties(const fslib::Path &path) const std::string pathString = path.string(); const std::string sizeString = get_size_string(fileSize); const std::string message = stringutil::get_formatted_string(messageFormat, - pathString.c_str(), - sizeString.c_str(), - createdBuffer, - lastModified, - lastAccessed); + pathString.c_str(), + sizeString.c_str(), + createdBuffer, + lastModified, + lastAccessed); MessageState::create_and_push(message); } diff --git a/source/appstates/MainMenuState.cpp b/source/appstates/MainMenuState.cpp index 6338d07..84a31b9 100644 --- a/source/appstates/MainMenuState.cpp +++ b/source/appstates/MainMenuState.cpp @@ -9,8 +9,8 @@ #include "appstates/TitleSelectState.hpp" #include "appstates/UserOptionState.hpp" #include "config/config.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include "remote/remote.hpp" #include "sdl.hpp" @@ -21,18 +21,32 @@ #include "tasks/update.hpp" #include "ui/PopMessageManager.hpp" +namespace +{ + // Main menu render target. + constexpr std::string_view TARGET = "MainMenuTarget"; + constexpr int TARGET_WIDTH = 200; + constexpr int TARGET_HEIGHT = 555; + + // Background. + constexpr std::string_view MAIN_BG = "romfs:/Textures/MenuBackground.png"; + constexpr std::string_view SETT_ICON = "romfs:/Textures/SettingsIcon.png"; + constexpr std::string_view EXTRA_ICON = "romfs:/Textures/ExtrasIcon.png"; + +} + // ---- Construction ---- -MainMenuState::MainMenuState() - : m_renderTarget(sdl::TextureManager::load("mainMenuTarget", 200, 555, SDL_TEXTUREACCESS_TARGET)) - , m_background(sdl::TextureManager::load("mainBackground", "romfs:/Textures/MenuBackground.png")) - , m_settingsIcon(sdl::TextureManager::load("settingsIcon", "romfs:/Textures/SettingsIcon.png")) - , m_extrasIcon(sdl::TextureManager::load("extrasIcon", "romfs:/Textures/ExtrasIcon.png")) +MainMenuState::MainMenuState(sdl2::Renderer &renderer) + : m_renderTarget(sdl2::TextureManager::create_load_resource(TARGET, TARGET_WIDTH, TARGET_HEIGHT, SDL_TEXTUREACCESS_TARGET)) + , m_background(sdl2::TextureManager::create_load_resource(MAIN_BG, MAIN_BG)) + , m_settingsIcon(sdl2::TextureManager::create_load_resource(SETT_ICON, SETT_ICON)) + , m_extrasIcon(sdl2::TextureManager::create_load_resource(EXTRA_ICON, EXTRA_ICON)) , m_mainMenu(ui::IconMenu::create(50, 15, 555)) , m_controlGuide(ui::ControlGuide::create(strings::get_by_name(strings::names::CONTROL_GUIDES, 0))) , m_dataStruct(std::make_shared()) { - MainMenuState::initialize_settings_extras(); + MainMenuState::initialize_settings_extras(renderer); MainMenuState::initialize_menu(); MainMenuState::initialize_view_states(); MainMenuState::initialize_data_struct(); @@ -41,14 +55,17 @@ MainMenuState::MainMenuState() // ---- Public functions ---- -void MainMenuState::update() +void MainMenuState::update(const sdl2::Input &input) { const int selected = m_mainMenu->get_selected(); const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool xPressed = input::button_pressed(HidNpadButton_X); - const bool yPressed = input::button_pressed(HidNpadButton_Y); + // Input + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool xPressed = input.button_pressed(HidNpadButton_X); + const bool yPressed = input.button_pressed(HidNpadButton_Y); + + // Whether or not to open the options targeting users. const bool toUserOptions = xPressed && selected < sm_userCount; if (aPressed) { MainMenuState::push_target_state(); } @@ -60,28 +77,38 @@ void MainMenuState::update() MainMenuState::confirm_update(); } - m_mainMenu->update(hasFocus); - m_controlGuide->update(hasFocus); + m_mainMenu->update(input, hasFocus); + m_controlGuide->update(input, hasFocus); for (auto &state : sm_states) { state->sub_update(); } } void MainMenuState::sub_update() { m_controlGuide->sub_update(); } -void MainMenuState::render() +void MainMenuState::render(sdl2::Renderer &renderer) { const bool hasFocus = BaseState::has_focus(); const int selected = m_mainMenu->get_selected(); - m_background->render(m_renderTarget, 0, 0); - m_mainMenu->render(m_renderTarget, hasFocus); - m_renderTarget->render(sdl::Texture::Null, 0, 91); - m_controlGuide->render(sdl::Texture::Null, hasFocus); + { + // Switch target. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(colors::CLEAR_COLOR); + // Render background and menu. + m_background->render(0, 0); + m_mainMenu->render(renderer, hasFocus); + } + + // Main FB stuff. + m_renderTarget->render(0, 91); + m_controlGuide->render(renderer, hasFocus); + + // If we have focus, render the state we're hovering. if (hasFocus) { BaseState *target = sm_states[selected].get(); - target->render(); + target->render(renderer); } } @@ -97,7 +124,10 @@ void MainMenuState::initialize_view_states() std::shared_ptr state{}; if (jksmMode) { state = TextTitleSelectState::create(user); } - else { state = TitleSelectState::create(user); } + else + { + state = TitleSelectState::create(user); + } sm_states.push_back(state); } @@ -116,12 +146,12 @@ void MainMenuState::refresh_view_states() // ---- Private functions ---- -void MainMenuState::initialize_settings_extras() +void MainMenuState::initialize_settings_extras(sdl2::Renderer &renderer) { if (!sm_settingsState || !sm_extrasState) { sm_settingsState = SettingsState::create(); - sm_extrasState = ExtrasMenuState::create(); + sm_extrasState = ExtrasMenuState::create(renderer); } } @@ -186,7 +216,10 @@ void MainMenuState::backup_all_for_all() { ConfirmProgress::create_push_fade(query, true, tasks::mainmenu::backup_all_for_all_remote, nullptr, m_dataStruct); } - else { ConfirmProgress::create_push_fade(query, true, tasks::mainmenu::backup_all_for_all_local, nullptr, m_dataStruct); } + else + { + ConfirmProgress::create_push_fade(query, true, tasks::mainmenu::backup_all_for_all_local, nullptr, m_dataStruct); + } } void MainMenuState::confirm_update() diff --git a/source/appstates/MessageState.cpp b/source/appstates/MessageState.cpp index 96b3aee..85aa22d 100644 --- a/source/appstates/MessageState.cpp +++ b/source/appstates/MessageState.cpp @@ -3,8 +3,9 @@ #include "StateManager.hpp" #include "appstates/FadeState.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" -#include "input.hpp" +#include "mathutil.hpp" #include "strings/strings.hpp" namespace @@ -55,45 +56,57 @@ MessageState::MessageState(std::string &message) // ---- Public functions ---- -void MessageState::update() +void MessageState::update(const sdl2::Input &input) { switch (m_state) { case State::Opening: case State::Closing: MessageState::update_dimensions(); break; - case State::Displaying: MessageState::update_handle_input(); break; + case State::Displaying: MessageState::update_handle_input(input); break; } } -void MessageState::render() +void MessageState::render(sdl2::Renderer &renderer) { + // This is were to render static constexpr int y = 229; - const bool hasFocus = BaseState::has_focus(); - sdl::render_rect_fill(sdl::Texture::Null, 0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); - sm_dialog->render(sdl::Texture::Null, hasFocus); + const bool hasFocus = BaseState::has_focus(); + + // Dim the background and render the dialog. Only continue if we're fully open. + renderer.render_rectangle(0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); + sm_dialog->render(renderer, hasFocus); if (!m_transition.in_place()) { return; } - sdl::text::render(sdl::Texture::Null, 312, y + 24, 20, 656, colors::WHITE, m_message); - sdl::render_line(sdl::Texture::Null, 280, y + 192, 999, y + 192, colors::DIV_COLOR); - sdl::text::render(sdl::Texture::Null, sm_okX, y + 214, 22, sdl::text::NO_WRAP, colors::WHITE, sm_okText); + sm_textFont->render_text_wrapped(312, y + 24, colors::WHITE, 656, m_message); + renderer.render_line(280, y + 192, 999, y + 192, colors::DIV_COLOR); + sm_optionFont->render_text(sm_okX, y + 214, colors::WHITE, sm_okText); } // ---- Private functions ---- void MessageState::initialize_static_members() { - static constexpr int HALF_WIDTH = 640; - static constexpr std::string_view POP_SOUND = "ConfirmPop"; - static constexpr const char *POP_PATH = "romfs:/Sound/ConfirmPop.wav"; + static constexpr std::string_view CONFIRM_POP = "romfs:/Sound/ConfirmPop.wav"; - if (sm_okText && sm_dialog && sm_dialogPop) { return; } + if (sm_okText && sm_dialog && sm_dialogPop && sm_textFont && sm_optionFont) { return; } - sm_okText = strings::get_by_name(strings::names::YES_NO_OK, 2); - sm_okX = HALF_WIDTH - (sdl::text::get_width(22, sm_okText) / 2); - sm_dialog = ui::DialogBox::create(0, 0, 0, 0); - sm_dialogPop = sdl::SoundManager::load(POP_SOUND, POP_PATH); + // This is needed later, so it comes first. + sm_textFont = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_PIXEL, + graphics::fonts::sizes::TWENTY_PIXEL); + sm_optionFont = sdl2::FontManager::create_load_resource(graphics::fonts::names::THIRTY_TWO_PIXEL, + graphics::fonts::sizes::TWENTY_TWO_PIXEL); + + // OK text and X coord. + sm_okText = strings::get_by_name(strings::names::YES_NO_OK, 2); + sm_okX = math::Util::center_within(graphics::SCREEN_WIDTH, sm_textFont->get_text_width(sm_okText)); + + // Dialog. + sm_dialog = ui::DialogBox::create(0, 0, 0, 0); sm_dialog->set_from_transition(m_transition, true); + + // Sound to play. + sm_dialogPop = sdl2::SoundManager::create_load_resource(CONFIRM_POP, CONFIRM_POP); } void MessageState::update_dimensions() noexcept @@ -109,10 +122,10 @@ void MessageState::update_dimensions() noexcept else if (closed) { MessageState::deactivate_state(); } } -void MessageState::update_handle_input() noexcept +void MessageState::update_handle_input(const sdl2::Input &input) noexcept { // Input bools. - const bool aPressed = input::button_pressed(HidNpadButton_A); + const bool aPressed = input.button_pressed(HidNpadButton_A); // Handle the triggerguard. m_triggerGuard = m_triggerGuard || (aPressed && !m_triggerGuard); diff --git a/source/appstates/ProgressState.cpp b/source/appstates/ProgressState.cpp index 7586ea7..8f99422 100644 --- a/source/appstates/ProgressState.cpp +++ b/source/appstates/ProgressState.cpp @@ -2,8 +2,9 @@ #include "appstates/FadeState.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" -#include "input.hpp" +#include "mathutil.hpp" #include "sdl.hpp" #include "strings/strings.hpp" #include "stringutil.hpp" @@ -43,17 +44,15 @@ ProgressState::ProgressState(sys::threadpool::JobFunction function, sys::Task::T TARGET_WIDTH, TARGET_HEIGHT, ui::Transition::DEFAULT_THRESHOLD) -{ - initialize_static_members(); -} +{ initialize_static_members(); } // ---- Public functions ---- -void ProgressState::update() +void ProgressState::update(const sdl2::Input &input) { // These are always updated and aren't conditional. BaseTask::update_loading_glyph(); - BaseTask::pop_on_plus(); + BaseTask::pop_on_plus(input); switch (m_state) { @@ -63,7 +62,7 @@ void ProgressState::update() } } -void ProgressState::render() +void ProgressState::render(sdl2::Renderer &renderer) { static constexpr int RIGHT_EDGE_X = (COORD_BAR_X + SIZE_BAR_WIDTH) - 16; @@ -71,51 +70,44 @@ void ProgressState::render() const bool hasFocus = BaseState::has_focus(); // This will dim the background. - sdl::render_rect_fill(sdl::Texture::Null, 0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); + renderer.render_rectangle(0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); // Render the glyph and dialog. Don't render anything else unless the task is running. BaseTask::render_loading_glyph(); - sm_dialog->render(sdl::Texture::Null, hasFocus); + sm_dialog->render(renderer, hasFocus); if (m_state != State::Running) { return; } - // This just makes this easier to work with. - const int barWidth = static_cast(SIZE_BAR_WIDTH); - // Grab and render the status. const std::string status = m_task->get_status(); - sdl::text::render(sdl::Texture::Null, 312, 255, BaseTask::FONT_SIZE, 656, colors::WHITE, status); + sm_font->render_text(312, 255, colors::WHITE, status); // This is the divider line. - sdl::render_line(sdl::Texture::Null, 280, 421, 999, 421, colors::DIV_COLOR); + renderer.render_line(280, 421, 999, 421, colors::DIV_COLOR); // Progress showing bar. - sdl::render_rect_fill(sdl::Texture::Null, COORD_BAR_X, COORD_BAR_Y, barWidth, 32, colors::BLACK); - sdl::render_rect_fill(sdl::Texture::Null, COORD_BAR_X, COORD_BAR_Y, m_progressBarWidth, 32, colors::BAR_GREEN); + renderer.render_rectangle(COORD_BAR_X, COORD_BAR_Y, static_cast(SIZE_BAR_WIDTH), 32, colors::BLACK); + renderer.render_rectangle(COORD_BAR_X, COORD_BAR_Y, m_progressBarWidth, 32, colors::BAR_GREEN); // These are the "caps" to round the edges of the bar. - sm_barEdges->render_part(sdl::Texture::Null, COORD_BAR_X, COORD_BAR_Y, 0, 0, 16, 32); - sm_barEdges->render_part(sdl::Texture::Null, RIGHT_EDGE_X, COORD_BAR_Y, 16, 0, 16, 32); + sm_barEdges->render_part(COORD_BAR_X, COORD_BAR_Y, 0, 0, 16, 32); + sm_barEdges->render_part(RIGHT_EDGE_X, COORD_BAR_Y, 16, 0, 16, 32); // Progress string. - sdl::text::render(sdl::Texture::Null, - m_percentageX, - COORD_TEXT_Y, - BaseTask::FONT_SIZE, - sdl::text::NO_WRAP, - colors::WHITE, - m_percentageString); + sm_font->render_text(m_percentageX, COORD_TEXT_Y, colors::WHITE, m_percentageString); } // ---- Private functions ---- void ProgressState::initialize_static_members() { - static constexpr std::string_view BAR_EDGE_NAME = "BarEdges"; + static constexpr std::string_view BAR_EDGES = "romfs:/Textures/BarEdges/png"; - if (sm_dialog && sm_barEdges) { return; } + if (sm_dialog && sm_barEdges && sm_font) { return; } sm_dialog = ui::DialogBox::create(0, 0, 0, 0); - sm_barEdges = sdl::TextureManager::load(BAR_EDGE_NAME, "romfs:/Textures/BarEdges.png"); + sm_barEdges = sdl2::TextureManager::create_load_resource(BAR_EDGES, BAR_EDGES); + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_PIXEL, + graphics::fonts::sizes::TWENTY_PIXEL); sm_dialog->set_from_transition(m_transition, true); } @@ -152,9 +144,8 @@ void ProgressState::update_progress() noexcept // This is the actual string that's displayed. m_percentageString = stringutil::get_formatted_string("%u%%", m_progress); - const int stringWidth = sdl::text::get_width(BaseTask::FONT_SIZE, m_percentageString); - // Center the string above. - m_percentageX = COORD_DISPLAY_CENTER - (stringWidth / 2); + const int stringWidth = sm_font->get_text_width(m_percentageString); + m_percentageX = math::Util::center_within(graphics::SCREEN_WIDTH, stringWidth); // Handle closing and updating. const bool taskRunning = m_task->is_running(); diff --git a/source/appstates/SaveCreateState.cpp b/source/appstates/SaveCreateState.cpp index c2fc051..3e32093 100644 --- a/source/appstates/SaveCreateState.cpp +++ b/source/appstates/SaveCreateState.cpp @@ -5,7 +5,6 @@ #include "data/data.hpp" #include "error.hpp" #include "fs/fs.hpp" -#include "input.hpp" #include "keyboard/keyboard.hpp" #include "logging/logger.hpp" #include "strings/strings.hpp" @@ -38,14 +37,14 @@ SaveCreateState::SaveCreateState(data::User *user, TitleSelectCommon *titleSelec // ---- Public functions ---- -void SaveCreateState::update() +void SaveCreateState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - sm_slidePanel->update(hasFocus); + sm_slidePanel->update(input, hasFocus); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); const bool panelClosed = sm_slidePanel->is_closed(); if (m_refreshRequired.load()) @@ -60,13 +59,12 @@ void SaveCreateState::update() else if (panelClosed) { SaveCreateState::deactivate_state(); } } -void SaveCreateState::render() +void SaveCreateState::render(sdl2::Renderer &renderer) { const bool hasFocus = BaseState::has_focus(); // Clear slide target, render menu, render slide to frame buffer. - sm_slidePanel->clear_target(); - sm_slidePanel->render(sdl::Texture::Null, hasFocus); + sm_slidePanel->render(renderer, hasFocus); } void SaveCreateState::refresh_required() { m_refreshRequired.store(true); } diff --git a/source/appstates/SaveImportState.cpp b/source/appstates/SaveImportState.cpp index 2842d4d..89d3ec6 100644 --- a/source/appstates/SaveImportState.cpp +++ b/source/appstates/SaveImportState.cpp @@ -3,7 +3,6 @@ #include "appstates/BackupMenuState.hpp" #include "appstates/ConfirmState.hpp" #include "config/config.hpp" -#include "input.hpp" #include "strings/strings.hpp" #include "stringutil.hpp" #include "tasks/saveimport.hpp" @@ -20,27 +19,26 @@ SaveImportState::SaveImportState(data::User *user) // ---- Public functions ---- -void SaveImportState::update() +void SaveImportState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - sm_slidePanel->update(hasFocus); + sm_slidePanel->update(input, hasFocus); if (!sm_slidePanel->is_open()) { return; } - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); if (aPressed) { SaveImportState::import_backup(); } else if (bPressed) { sm_slidePanel->close(); } else if (sm_slidePanel->is_closed()) { SaveImportState::deactivate_state(); } } -void SaveImportState::render() +void SaveImportState::render(sdl2::Renderer &renderer) { const bool hasFocus = BaseState::has_focus(); - sm_slidePanel->clear_target(); - sm_slidePanel->render(sdl::Texture::Null, hasFocus); + sm_slidePanel->render(renderer, hasFocus); } // ---- Private functions ---- diff --git a/source/appstates/SettingsState.cpp b/source/appstates/SettingsState.cpp index bd690ff..a96a9ea 100644 --- a/source/appstates/SettingsState.cpp +++ b/source/appstates/SettingsState.cpp @@ -8,8 +8,9 @@ #include "error.hpp" #include "fs/fs.hpp" #include "fslib.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" +#include "graphics/targets.hpp" #include "keyboard/keyboard.hpp" #include "logging/logger.hpp" #include "strings/strings.hpp" @@ -72,7 +73,10 @@ namespace SettingsState::SettingsState() : m_settingsMenu(ui::Menu::create(32, 10, 1000, 23, 555)) , m_controlGuide(ui::ControlGuide::create(strings::get_by_name(strings::names::CONTROL_GUIDES, 3))) - , m_renderTarget(sdl::TextureManager::load(SECONDARY_TARGET, 1080, 555, SDL_TEXTUREACCESS_TARGET)) + , m_renderTarget(sdl2::TextureManager::create_load_resource(graphics::targets::names::SECONDARY, + graphics::targets::dims::SECONDARY_WIDTH, + graphics::targets::dims::SECONDARY_HEIGHT, + SDL_TEXTUREACCESS_TARGET)) { SettingsState::load_settings_menu(); SettingsState::load_extra_strings(); @@ -81,33 +85,44 @@ SettingsState::SettingsState() // ---- Public functions ---- -void SettingsState::update() +void SettingsState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool xPressed = input::button_pressed(HidNpadButton_X); - const bool minusPressed = input::button_pressed(HidNpadButton_Minus); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); + const bool xPressed = input.button_pressed(HidNpadButton_X); + const bool minusPressed = input.button_pressed(HidNpadButton_Minus); - m_settingsMenu->update(hasFocus); + m_settingsMenu->update(input, hasFocus); if (aPressed) { SettingsState::toggle_options(); } else if (xPressed) { SettingsState::reset_settings(); } else if (minusPressed) { SettingsState::create_push_description_message(); } else if (bPressed) { BaseState::deactivate(); } - m_controlGuide->update(hasFocus); + m_controlGuide->update(input, hasFocus); } void SettingsState::sub_update() { m_controlGuide->sub_update(); } -void SettingsState::render() +void SettingsState::render(sdl2::Renderer &renderer) { + // Grab focus state. const bool hasFocus = BaseState::has_focus(); - m_renderTarget->clear(colors::TRANSPARENT); - m_settingsMenu->render(m_renderTarget, hasFocus); - m_renderTarget->render(sdl::Texture::Null, 201, 91); - m_controlGuide->render(sdl::Texture::Null, hasFocus); + { + // Set target and clear. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(colors::TRANSPARENT); + + // Render what we need to. + m_settingsMenu->render(renderer, hasFocus); + } + + // Render the target to the screen. + m_renderTarget->render(201, 91); + + // Control guide. + m_controlGuide->render(renderer, hasFocus); } // ---- Private functions ---- @@ -194,7 +209,10 @@ void SettingsState::change_working_directory() moved = fs::move_directory_recursively(oldPath, newPath); error::fslib(fslib::delete_directory_recursively(oldPath)); } - else { moved = fslib::rename_directory(oldPath, newPath); } + else + { + moved = fslib::rename_directory(oldPath, newPath); + } if (!moved) { @@ -292,7 +310,10 @@ void SettingsState::toggle_trash_folder() config::toggle_by_key(config::keys::ENABLE_TRASH_BIN); if (trashEnabled) { error::fslib(fslib::delete_directory_recursively(trashPath)); } - else { error::fslib(fslib::create_directory(trashPath)); } + else + { + error::fslib(fslib::create_directory(trashPath)); + } } void SettingsState::cycle_anim_scaling() diff --git a/source/appstates/TaskState.cpp b/source/appstates/TaskState.cpp index 09bbc6c..17ce4c8 100644 --- a/source/appstates/TaskState.cpp +++ b/source/appstates/TaskState.cpp @@ -2,8 +2,9 @@ #include "appstates/FadeState.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" -#include "input.hpp" +#include "mathutil.hpp" #include "sdl.hpp" #include "strings/strings.hpp" #include "ui/PopMessageManager.hpp" @@ -11,26 +12,25 @@ // ---- Construction ---- TaskState::TaskState(sys::threadpool::JobFunction function, sys::Task::TaskData taskData) -{ - m_task = std::make_unique(function, taskData); -} +{ m_task = std::make_unique(function, taskData); } // ---- Public functions ---- -void TaskState::update() +void TaskState::update(const sdl2::Input &input) { - BaseTask::pop_on_plus(); + BaseTask::pop_on_plus(input); BaseTask::update_loading_glyph(); if (!m_task->is_running()) { TaskState::deactivate_state(); } } -void TaskState::render() +void TaskState::render(sdl2::Renderer &renderer) { const std::string status = m_task->get_status(); - const int statusX = 640 - (sdl::text::get_width(BaseTask::FONT_SIZE, status.c_str()) / 2); + const int statusWidth = sm_font->get_text_width(status); + const int statusX = math::Util::center_within(graphics::SCREEN_WIDTH, statusWidth); - sdl::render_rect_fill(sdl::Texture::Null, 0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); - sdl::text::render(sdl::Texture::Null, statusX, 351, BaseTask::FONT_SIZE, sdl::text::NO_WRAP, colors::WHITE, status); + renderer.render_rectangle(0, 0, graphics::SCREEN_WIDTH, graphics::SCREEN_HEIGHT, colors::DIM_BACKGROUND); + sm_font->render_text(statusX, 351, colors::WHITE, status); BaseTask::render_loading_glyph(); } @@ -40,3 +40,13 @@ void TaskState::deactivate_state() FadeState::create_and_push(colors::DIM_BACKGROUND, colors::ALPHA_FADE_END, colors::ALPHA_FADE_BEGIN, nullptr); BaseState::deactivate(); } + +// ---- Private Functions ---- + +void TaskState::initialize_static_members() +{ + if (sm_font) { return; } + + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_PIXEL, + graphics::fonts::sizes::TWENTY_PIXEL); +} \ No newline at end of file diff --git a/source/appstates/TextTitleSelectState.cpp b/source/appstates/TextTitleSelectState.cpp index 0516854..9e7b97a 100644 --- a/source/appstates/TextTitleSelectState.cpp +++ b/source/appstates/TextTitleSelectState.cpp @@ -7,58 +7,63 @@ #include "config/config.hpp" #include "fs/save_mount.hpp" #include "fslib.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" +#include "graphics/targets.hpp" #include "logging/logger.hpp" #include "sdl.hpp" #include -namespace -{ - // All of these states share this same target. - constexpr std::string_view SECONDARY_TARGET = "SecondaryTarget"; -} // namespace - // ---- Construction ---- TextTitleSelectState::TextTitleSelectState(data::User *user) : TitleSelectCommon() , m_user(user) , m_titleSelectMenu(ui::Menu::create(32, 10, 1000, 23, 555)) - , m_renderTarget(sdl::TextureManager::load(SECONDARY_TARGET, 1080, 555, SDL_TEXTUREACCESS_TARGET)) -{ - TextTitleSelectState::refresh(); -} + , m_renderTarget(sdl2::TextureManager::create_load_resource(graphics::targets::names::SECONDARY, + graphics::targets::dims::SECONDARY_WIDTH, + graphics::targets::dims::SECONDARY_HEIGHT, + SDL_TEXTUREACCESS_TARGET)) +{ TextTitleSelectState::refresh(); } // ---- Public functions ---- -void TextTitleSelectState::update() +void TextTitleSelectState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool xPressed = input::button_pressed(HidNpadButton_X); - const bool yPressed = input::button_pressed(HidNpadButton_Y); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); + const bool xPressed = input.button_pressed(HidNpadButton_X); + const bool yPressed = input.button_pressed(HidNpadButton_Y); - m_titleSelectMenu->update(hasFocus); + m_titleSelectMenu->update(input, hasFocus); if (aPressed) { TextTitleSelectState::create_backup_menu(); } else if (xPressed) { TextTitleSelectState::create_title_option_menu(); } else if (yPressed) { TextTitleSelectState::add_remove_favorite(); } else if (bPressed) { BaseState::deactivate(); } - sm_controlGuide->update(hasFocus); + sm_controlGuide->update(input, hasFocus); } -void TextTitleSelectState::render() +void TextTitleSelectState::render(sdl2::Renderer &renderer) { + // Grab focus. const bool hasFocus = BaseState::has_focus(); - m_renderTarget->clear(colors::TRANSPARENT); - m_titleSelectMenu->render(m_renderTarget, hasFocus); - sm_controlGuide->render(sdl::Texture::Null, hasFocus); - m_renderTarget->render(sdl::Texture::Null, 201, 91); + { + // Switch targets, clear. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(colors::TRANSPARENT); + + // Render menu to target. + m_titleSelectMenu->render(renderer, hasFocus); + } + + // Switch back, render target and guide. + m_renderTarget->render(201, 91); + sm_controlGuide->render(renderer, hasFocus); } void TextTitleSelectState::refresh() @@ -77,7 +82,10 @@ void TextTitleSelectState::refresh() std::string option{}; if (favorite) { option = std::string{STRING_HEART} + title; } - else { option = title; } + else + { + option = title; + } m_titleSelectMenu->add_option(option); } diff --git a/source/appstates/TitleInfoState.cpp b/source/appstates/TitleInfoState.cpp index 25fd9fc..90a6254 100644 --- a/source/appstates/TitleInfoState.cpp +++ b/source/appstates/TitleInfoState.cpp @@ -4,7 +4,6 @@ #include "error.hpp" #include "fs/save_data_functions.hpp" #include "graphics/colors.hpp" -#include "input.hpp" #include "sdl.hpp" #include "strings/strings.hpp" #include "stringutil.hpp" @@ -36,8 +35,8 @@ namespace } // namespace // Defined at bottom -static inline std::shared_ptr create_new_field(std::string_view text, int x, int y, sdl::Color clear); -static inline std::shared_ptr create_new_field(std::string &text, int x, int y, sdl::Color clear); +static inline std::shared_ptr create_new_field(std::string_view text, int x, int y, SDL_Color clear); +static inline std::shared_ptr create_new_field(std::string &text, int x, int y, SDL_Color clear); static bool is_a_best_game(uint64_t applicationID) noexcept; // ---- Construction ---- @@ -65,35 +64,34 @@ TitleInfoState::TitleInfoState(data::User *user, data::TitleInfo *titleInfo, con // ---- Public functions ---- -void TitleInfoState::update() +void TitleInfoState::update(const sdl2::Input &input) { switch (m_state) { case State::Opening: case State::Closing: TitleInfoState::update_dimensions(); break; - case State::Displaying: TitleInfoState::update_handle_input(); break; + case State::Displaying: TitleInfoState::update_handle_input(input); break; } } -void TitleInfoState::render() +void TitleInfoState::render(sdl2::Renderer &renderer) { // Grab cause needed everywhere. const bool hasFocus = BaseState::has_focus(); // Render the frame. Only continue further if we're currently displaying. - sm_frame->render(sdl::Texture::Null, hasFocus); + sm_frame->render(renderer, hasFocus); if (m_state != State::Displaying) { return; } // We only want to render what's been triggered so far for the tiling in effect. - for (int i = 0; i < m_fieldDisplayCount; i++) { m_infoFields[i]->render(sdl::Texture::Null, hasFocus); } + for (int i = 0; i < m_fieldDisplayCount; i++) { m_infoFields[i]->render(renderer, hasFocus); } } // ---- Private functions ---- void TitleInfoState::initialize_static_members() { - static constexpr std::string_view CHIME_NAME = "TitleInfoChime"; - static constexpr const char *CHIME_PATH = "romfs:/Sound/TitleInfo.wav"; + static constexpr std::string_view CHIME_PATH = "romfs:/Sound/TitleInfo.wav"; if (sm_frame && sm_openChime) { @@ -106,7 +104,7 @@ void TitleInfoState::initialize_static_members() const int width = m_transition.get_width(); const int height = m_transition.get_height(); sm_frame = ui::Frame::create(x, y, width, height); - sm_openChime = sdl::SoundManager::load(CHIME_NAME, CHIME_PATH); + sm_openChime = sdl2::SoundManager::create_load_resource(CHIME_PATH, CHIME_PATH); } void TitleInfoState::initialize_info_fields() @@ -166,13 +164,13 @@ void TitleInfoState::create_title(int y) { std::string bestTitle = BESTEST_STAR + " " + title + " " + BESTEST_STAR; field = ui::TextScroll::create(bestTitle, - X, - y, - WIDTH, - TITLE_FIELD_HEIGHT, - TITLE_FONT_SIZE, - colors::WHITE, - colors::TRANSPARENT); + X, + y, + WIDTH, + TITLE_FIELD_HEIGHT, + TITLE_FONT_SIZE, + colors::WHITE, + colors::TRANSPARENT); } else { @@ -377,7 +375,7 @@ void TitleInfoState::update_dimensions() noexcept else if (closed) { TitleInfoState::deactivate_state(); } } -void TitleInfoState::update_handle_input() noexcept +void TitleInfoState::update_handle_input(const sdl2::Input &input) noexcept { // Grab a cache this since it's needed a lot. const bool hasFocus = BaseState::has_focus(); @@ -387,14 +385,14 @@ void TitleInfoState::update_handle_input() noexcept if (m_fieldDisplayCount < currentCount && m_timer.is_triggered()) { ++m_fieldDisplayCount; } // Update the actually displayed fields so the text scrolls if need be. - for (int i = 0; i < m_fieldDisplayCount; i++) { m_infoFields[i]->update(hasFocus); } + for (int i = 0; i < m_fieldDisplayCount; i++) { m_infoFields[i]->update(input, hasFocus); } // Input bools. - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool bPressed = input.button_pressed(HidNpadButton_B); if (bPressed) { TitleInfoState::close(); } } -inline sdl::Color TitleInfoState::get_field_color() noexcept +inline SDL_Color TitleInfoState::get_field_color() noexcept { m_fieldClearSwitch = m_fieldClearSwitch ? false : true; return m_fieldClearSwitch ? colors::DIALOG_DARK : colors::CLEAR_COLOR; @@ -411,15 +409,11 @@ void TitleInfoState::deactivate_state() { BaseState::deactivate(); } // ---- Static functions ---- -static inline std::shared_ptr create_new_field(std::string_view text, int x, int y, sdl::Color clear) -{ - return ui::TextScroll::create(text, x, y, SIZE_FIELD_WIDTH, SIZE_FIELD_HEIGHT, SIZE_FONT, colors::WHITE, clear, false); -} +static inline std::shared_ptr create_new_field(std::string_view text, int x, int y, SDL_Color clear) +{ return ui::TextScroll::create(text, x, y, SIZE_FIELD_WIDTH, SIZE_FIELD_HEIGHT, SIZE_FONT, colors::WHITE, clear, false); } -static inline std::shared_ptr create_new_field(std::string &text, int x, int y, sdl::Color clear) -{ - return ui::TextScroll::create(text, x, y, SIZE_FIELD_WIDTH, SIZE_FIELD_HEIGHT, SIZE_FONT, colors::WHITE, clear, false); -} +static inline std::shared_ptr create_new_field(std::string &text, int x, int y, SDL_Color clear) +{ return ui::TextScroll::create(text, x, y, SIZE_FIELD_WIDTH, SIZE_FIELD_HEIGHT, SIZE_FONT, colors::WHITE, clear, false); } static bool is_a_best_game(uint64_t applicationID) noexcept { diff --git a/source/appstates/TitleOptionState.cpp b/source/appstates/TitleOptionState.cpp index dff39ef..683d88c 100644 --- a/source/appstates/TitleOptionState.cpp +++ b/source/appstates/TitleOptionState.cpp @@ -10,7 +10,6 @@ #include "fs/fs.hpp" #include "fslib.hpp" #include "graphics/colors.hpp" -#include "input.hpp" #include "keyboard/keyboard.hpp" #include "logging/logger.hpp" #include "remote/remote.hpp" @@ -58,16 +57,16 @@ TitleOptionState::TitleOptionState(data::User *user, // ---- Public functions ---- -void TitleOptionState::update() +void TitleOptionState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); sm_slidePanel->unhide_on_focus(hasFocus); - sm_slidePanel->update(hasFocus); + sm_slidePanel->update(input, hasFocus); const bool isOpen = sm_slidePanel->is_open(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); const int selected = sm_titleOptionMenu->get_selected(); if (m_refreshRequired) @@ -103,12 +102,13 @@ void TitleOptionState::update() void TitleOptionState::sub_update() { sm_slidePanel->sub_update(); } -void TitleOptionState::render() +void TitleOptionState::render(sdl2::Renderer &renderer) { + // Grab focus. const bool hasFocus = BaseState::has_focus(); - sm_slidePanel->clear_target(); - sm_slidePanel->render(sdl::Texture::Null, hasFocus); + // Render panel. + sm_slidePanel->render(renderer, hasFocus); } void TitleOptionState::close_on_update() { m_exitRequired = true; } diff --git a/source/appstates/TitleSelectState.cpp b/source/appstates/TitleSelectState.cpp index be7c8f5..65b36e0 100644 --- a/source/appstates/TitleSelectState.cpp +++ b/source/appstates/TitleSelectState.cpp @@ -5,8 +5,9 @@ #include "appstates/MainMenuState.hpp" #include "appstates/TitleOptionState.hpp" #include "config/config.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" +#include "graphics/targets.hpp" #include "logging/logger.hpp" #include "sdl.hpp" #include "strings/strings.hpp" @@ -24,21 +25,23 @@ namespace TitleSelectState::TitleSelectState(data::User *user) : TitleSelectCommon() , m_user(user) - , m_renderTarget(sdl::TextureManager::load(SECONDARY_TARGET, 1080, 555, SDL_TEXTUREACCESS_TARGET)) + , m_renderTarget(sdl2::TextureManager::create_load_resource(graphics::targets::names::SECONDARY, + graphics::targets::dims::SECONDARY_WIDTH, + graphics::targets::dims::SECONDARY_HEIGHT, + SDL_TEXTUREACCESS_TARGET)) , m_titleView(ui::TitleView::create(m_user)) {}; - // ---- Public functions ---- -void TitleSelectState::update() +void TitleSelectState::update(const sdl2::Input &input) { if (!TitleSelectState::title_count_check()) { return; } const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool xPressed = input::button_pressed(HidNpadButton_X); - const bool yPressed = input::button_pressed(HidNpadButton_Y); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); + const bool xPressed = input.button_pressed(HidNpadButton_X); + const bool yPressed = input.button_pressed(HidNpadButton_Y); if (aPressed) { TitleSelectState::create_backup_menu(); } else if (xPressed) { TitleSelectState::create_title_option_menu(); } @@ -49,18 +52,26 @@ void TitleSelectState::update() return; } - m_titleView->update(hasFocus); - sm_controlGuide->update(hasFocus); + m_titleView->update(input, hasFocus); + sm_controlGuide->update(input, hasFocus); } -void TitleSelectState::render() +void TitleSelectState::render(sdl2::Renderer &renderer) { + // Grab focus status. const bool hasFocus = BaseState::has_focus(); - m_renderTarget->clear(colors::TRANSPARENT); - m_titleView->render(m_renderTarget, hasFocus); - sm_controlGuide->render(sdl::Texture::Null, hasFocus); - m_renderTarget->render(sdl::Texture::Null, 201, 91); + { + // Set target, clear. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(colors::TRANSPARENT); + + // Render view. + m_titleView->render(renderer, hasFocus); + } + + m_renderTarget->render(201, 91); + sm_controlGuide->render(renderer, hasFocus); } void TitleSelectState::refresh() { m_titleView->refresh(); } diff --git a/source/appstates/UserOptionState.cpp b/source/appstates/UserOptionState.cpp index 36530e4..858eca9 100644 --- a/source/appstates/UserOptionState.cpp +++ b/source/appstates/UserOptionState.cpp @@ -12,7 +12,6 @@ #include "error.hpp" #include "fs/fs.hpp" #include "fslib.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include "remote/remote.hpp" #include "strings/strings.hpp" @@ -49,15 +48,15 @@ UserOptionState::UserOptionState(data::User *user, TitleSelectCommon *titleSelec // ---- Public functions ---- -void UserOptionState::update() +void UserOptionState::update(const sdl2::Input &input) { const bool hasFocus = BaseState::has_focus(); - const bool aPressed = input::button_pressed(HidNpadButton_A); - const bool bPressed = input::button_pressed(HidNpadButton_B); + const bool aPressed = input.button_pressed(HidNpadButton_A); + const bool bPressed = input.button_pressed(HidNpadButton_B); // Start by running the unhide check routine and then update the panel. sm_menuPanel->unhide_on_focus(hasFocus); - sm_menuPanel->update(hasFocus); + sm_menuPanel->update(input, hasFocus); // Refresh here if needed to avoid threading issues. if (m_refreshRequired) @@ -86,14 +85,13 @@ void UserOptionState::update() void UserOptionState::sub_update() { sm_menuPanel->sub_update(); } -void UserOptionState::render() +void UserOptionState::render(sdl2::Renderer &renderer) { // Render target user's title selection screen. - m_titleSelect->render(); + m_titleSelect->render(renderer); // Render panel. - sm_menuPanel->clear_target(); - sm_menuPanel->render(sdl::Texture::Null, BaseState::has_focus()); + sm_menuPanel->render(renderer, BaseState::has_focus()); } void UserOptionState::refresh_required() { m_refreshRequired = true; } diff --git a/source/data/DataContext.cpp b/source/data/DataContext.cpp index c35dfe5..3c67b77 100644 --- a/source/data/DataContext.cpp +++ b/source/data/DataContext.cpp @@ -104,7 +104,8 @@ void data::DataContext::load_application_records(sys::Task *task) NsApplicationRecord record{}; bool listError{}; - do { + do + { listError = error::libnx(nsListApplicationRecord(&record, 1, offset++, &count)) || count <= 0; if (listError) { break; } if (DataContext::title_is_loaded(record.application_id)) { continue; } @@ -217,7 +218,8 @@ bool data::DataContext::read_cache(sys::Task *task) task->set_status(statusLoadingCache); NsApplicationControlData controlData{}; - do { + do + { const bool dataRead = cacheZip.read(&controlData, SIZE_CTRL_DATA) == SIZE_CTRL_DATA; if (!dataRead) { continue; } @@ -270,9 +272,9 @@ bool data::DataContext::write_cache(sys::Task *task) return true; } -void data::DataContext::process_icon_queue() +void data::DataContext::process_icon_queue(sdl2::Renderer &renderer) { std::lock_guard multiGuard{m_iconQueueMutex}; - for (data::DataCommon *common : m_iconQueue) { common->load_icon(); } + for (data::DataCommon *common : m_iconQueue) { common->load_icon(renderer); } m_iconQueue.clear(); } diff --git a/source/data/TitleInfo.cpp b/source/data/TitleInfo.cpp index 17b55b1..d679e1b 100644 --- a/source/data/TitleInfo.cpp +++ b/source/data/TitleInfo.cpp @@ -21,10 +21,10 @@ data::TitleInfo::TitleInfo(uint64_t applicationID) noexcept // This will filter from even trying to fetch control data for system titles. const bool isSystem = applicationID & 0x8000000000000000; const bool getError = !isSystem && error::libnx(nsGetApplicationControlData(NsApplicationControlSource_Storage, - m_applicationID, - &m_data, - SIZE_CTRL_DATA, - &controlSize)); + m_applicationID, + &m_data, + SIZE_CTRL_DATA, + &controlSize)); const bool entryError = !getError && error::libnx(nacpGetLanguageEntry(&m_data.nacp, &m_entry)); if (isSystem || getError) { @@ -149,8 +149,6 @@ bool data::TitleInfo::has_save_data_type(uint8_t saveType) const noexcept return false; } -sdl::SharedTexture data::TitleInfo::get_icon() const noexcept { return m_icon; } - void data::TitleInfo::set_path_safe_title(const char *newPathSafe) noexcept { const size_t length = std::char_traits::length(newPathSafe); @@ -160,20 +158,16 @@ void data::TitleInfo::set_path_safe_title(const char *newPathSafe) noexcept std::memcpy(m_pathSafeTitle, newPathSafe, length); } -void data::TitleInfo::load_icon() +void data::TitleInfo::load_icon(sdl2::Renderer &renderer) { // This is taken from the NacpStruct. static constexpr size_t SIZE_ICON = 0x20000; + const std::string textureName = stringutil::get_formatted_string("%04X", m_applicationID & 0xFFFF); - if (m_hasData) - { - const std::string textureName = stringutil::get_formatted_string("%016llX", m_applicationID); - m_icon = sdl::TextureManager::load(textureName, m_data.icon, SIZE_ICON); - } + if (m_hasData) { m_icon = sdl2::TextureManager::create_load_resource(textureName, m_data.icon, SIZE_ICON); } else { - const std::string text = stringutil::get_formatted_string("%04X", m_applicationID & 0xFFFF); - m_icon = gfxutil::create_generic_icon(text, 48, colors::DIALOG_DARK, colors::WHITE); + m_icon = gfxutil::create_generic_icon(renderer, textureName, 48, colors::DIALOG_DARK, colors::WHITE); } } diff --git a/source/data/User.cpp b/source/data/User.cpp index 9839fa7..a90bfad 100644 --- a/source/data/User.cpp +++ b/source/data/User.cpp @@ -45,7 +45,10 @@ data::User::User(AccountUid accountID, FsSaveDataType saveType) noexcept const bool profileError = error::libnx(accountGetProfile(&profile, m_accountID)); const bool baseError = !profileError && error::libnx(accountProfileGet(&profile, nullptr, &profileBase)); if (profileError || baseError) { User::create_account(); } - else { User::load_account(profile, profileBase); } + else + { + User::load_account(profile, profileBase); + } accountProfileClose(&profile); } @@ -174,7 +177,10 @@ void data::User::load_user_data() { infoReader.open(SAVE_DATA_SPACE_ORDER[i], m_accountID, SIZE_SAVE_INFO_BUFFER); } - else { infoReader.open(SAVE_DATA_SPACE_ORDER[i], m_saveType, SIZE_SAVE_INFO_BUFFER); } + else + { + infoReader.open(SAVE_DATA_SPACE_ORDER[i], m_saveType, SIZE_SAVE_INFO_BUFFER); + } if (!infoReader.is_open()) { continue; } while (infoReader.read()) @@ -218,7 +224,7 @@ void data::User::load_user_data() User::sort_data(); } -void data::User::load_icon() +void data::User::load_icon(sdl2::Renderer &renderer) { const std::string iconName = stringutil::get_formatted_string("%016llX%016llX", m_nickname, m_accountID.uid[0], m_accountID.uid[1]); @@ -230,7 +236,7 @@ void data::User::load_icon() const bool sizeError = !profileError && error::libnx(accountProfileGetImageSize(&profile, &iconSize)); if (profileError || sizeError) { - m_icon = gfxutil::create_generic_icon(m_nickname, SIZE_ICON_FONT, colors::DIALOG_DARK, colors::WHITE); + m_icon = gfxutil::create_generic_icon(renderer, m_nickname, SIZE_ICON_FONT, colors::DIALOG_DARK, colors::WHITE); return; } @@ -239,9 +245,12 @@ void data::User::load_icon() if (loadError) { return; } accountProfileClose(&profile); - m_icon = sdl::TextureManager::load(iconName, iconBuffer.get(), iconSize); + m_icon = sdl2::TextureManager::create_load_resource(iconName, iconBuffer.get(), iconSize); + } + else + { + m_icon = gfxutil::create_generic_icon(renderer, m_nickname, SIZE_ICON_FONT, colors::DIALOG_DARK, colors::WHITE); } - else { m_icon = gfxutil::create_generic_icon(m_nickname, SIZE_ICON_FONT, colors::DIALOG_DARK, colors::WHITE); } } void data::User::load_account(AccountProfile &profile, AccountProfileBase &profileBase) diff --git a/source/data/data.cpp b/source/data/data.cpp index e236707..2eb075f 100644 --- a/source/data/data.cpp +++ b/source/data/data.cpp @@ -25,21 +25,19 @@ namespace /// @brief The main routine for the task to load data. static void data_initialize_task(sys::threadpool::JobData taskData); -void data::launch_initialization(bool clearCache, std::function onDestruction) +void data::launch_initialization(bool clearCache, sdl2::Renderer &renderer, std::function onDestruction) { auto taskData = std::make_shared(); taskData->clearCache = clearCache; - auto loadingState = DataLoadingState::create(s_context, onDestruction, data_initialize_task, taskData); + auto loadingState = DataLoadingState::create(s_context, renderer, onDestruction, data_initialize_task, taskData); StateManager::push_state(loadingState); } void data::get_users(data::UserList &userList) { s_context.get_users(userList); } data::TitleInfo *data::get_title_info_by_id(uint64_t applicationID) noexcept -{ - return s_context.get_title_by_id(applicationID); -} +{ return s_context.get_title_by_id(applicationID); } void data::load_title_to_map(uint64_t applicationID) { s_context.load_title(applicationID); } @@ -48,9 +46,7 @@ bool data::title_exists_in_map(uint64_t applicationID) noexcept { return s_conte void data::get_title_info_list(data::TitleInfoList &listOut) { s_context.get_title_info_list(listOut); } void data::get_title_info_by_type(FsSaveDataType saveType, data::TitleInfoList &listOut) -{ - s_context.get_title_info_list_by_type(saveType, listOut); -} +{ s_context.get_title_info_list_by_type(saveType, listOut); } static void data_initialize_task(sys::threadpool::JobData taskData) { diff --git a/source/gfxutil.cpp b/source/gfxutil.cpp index 30da592..e30731e 100644 --- a/source/gfxutil.cpp +++ b/source/gfxutil.cpp @@ -1,28 +1,41 @@ #include "graphics/gfxutil.hpp" -namespace +#include "graphics/ScopedRender.hpp" +#include "mathutil.hpp" +#include "stringutil.hpp" + +sdl2::SharedTexture gfxutil::create_generic_icon(sdl2::Renderer &renderer, + std::string_view text, + int fontSize, + SDL_Color background, + SDL_Color textColor) { - /// @brief Width of generic icons in pixels. - constexpr int SIZE_ICON_WIDTH = 256; + // Icon dimensions. + static constexpr int ICON_WIDTH = 256; + static constexpr int ICON_HEIGHT = 256; - /// @brief Height of generic icons in pixels. - constexpr int SIZE_ICON_HEIGHT = 256; -} // namespace + // Create a new font to render with. + const std::string fontName = stringutil::get_formatted_string("IconFont%i", fontSize); + sdl2::SharedFont font = sdl2::FontManager::create_load_resource(fontName, fontSize); -sdl::SharedTexture gfxutil::create_generic_icon(std::string_view text, - int fontSize, - sdl::Color background, - sdl::Color foreground) -{ - // Create base icon texture. - sdl::SharedTexture icon = sdl::TextureManager::load(text, SIZE_ICON_WIDTH, SIZE_ICON_HEIGHT, SDL_TEXTUREACCESS_TARGET); + // Center our text. + const int textWidth = font->get_text_width(text); + const int textX = math::Util::center_within(ICON_WIDTH, textWidth); + const int textY = math::Util::center_within(ICON_HEIGHT, fontSize); - // Get the centered X and Y coordinates. - const int textX = (SIZE_ICON_WIDTH / 2) - (sdl::text::get_width(fontSize, text) / 2); - const int textY = (SIZE_ICON_HEIGHT / 2) - (fontSize / 2); + // Create the icon. + sdl2::SharedTexture icon = + sdl2::TextureManager::create_load_resource(text, ICON_WIDTH, ICON_HEIGHT, SDL_TEXTUREACCESS_TARGET); - icon->clear(background); - sdl::text::render(icon, textX, textY, fontSize, sdl::text::NO_WRAP, foreground, text); + { + // Target the icon we just created. + graphics::ScopedRender scopedRender(renderer, icon); + // Render. + renderer.frame_begin(background); + font->render_text(textX, textY, textColor, text); + } + + // Return. return icon; } diff --git a/source/input.cpp b/source/input.cpp deleted file mode 100644 index 8b6e6df..0000000 --- a/source/input.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "input.hpp" - -namespace -{ - PadState s_gamepad; -} - -void input::initialize() -{ - padConfigureInput(1, HidNpadStyleSet_NpadStandard); - padInitializeDefault(&s_gamepad); -} - -void input::update() noexcept { padUpdate(&s_gamepad); } - -bool input::button_pressed(HidNpadButton button) noexcept -{ - return (s_gamepad.buttons_cur & button) && !(s_gamepad.buttons_old & button); -} - -bool input::button_held(HidNpadButton button) noexcept -{ - return (s_gamepad.buttons_cur & button) && (s_gamepad.buttons_old & button); -} - -bool input::button_released(HidNpadButton button) noexcept -{ - return (s_gamepad.buttons_old & button) && !(s_gamepad.buttons_cur & button); -} diff --git a/source/remote/remote.cpp b/source/remote/remote.cpp index 708573c..8eee7b0 100644 --- a/source/remote/remote.cpp +++ b/source/remote/remote.cpp @@ -3,7 +3,6 @@ #include "StateManager.hpp" #include "appstates/TaskState.hpp" #include "error.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include "remote/GoogleDrive.hpp" #include "remote/WebDav.hpp" @@ -136,10 +135,6 @@ static void drive_sign_in(sys::threadpool::JobData taskData) while (std::time(NULL) < expiration && !drive->poll_sign_in(deviceCode)) { - const bool bPressed = input::button_pressed(HidNpadButton_B); - const bool bHeld = input::button_held(HidNpadButton_B); - if (bPressed || bHeld) { break; } - std::this_thread::sleep_for(std::chrono::seconds(pollingInterval)); } diff --git a/source/ui/BoundingBox.cpp b/source/ui/BoundingBox.cpp index ed4ec7b..e6b0319 100644 --- a/source/ui/BoundingBox.cpp +++ b/source/ui/BoundingBox.cpp @@ -16,21 +16,26 @@ ui::BoundingBox::BoundingBox(int x, int y, int width, int height) , m_y(y) , m_width(width) , m_height(height) -{ - BoundingBox::initialize_static_members(); -} +{ BoundingBox::initialize_static_members(); } // ---- Public functions ---- -void ui::BoundingBox::update(bool hasFocus) { m_colorMod.update(); } +void ui::BoundingBox::update(const sdl2::Input &input, bool hasFocus) { m_colorMod.update(); } -void ui::BoundingBox::render(sdl::SharedTexture &target, bool hasFocus) +void ui::BoundingBox::render(sdl2::Renderer &renderer, bool hasFocus) { + // Sizes of the pieces. + static constexpr int CORNER_WIDTH = 8; + static constexpr int CORNER_HEIGHT = 8; + static constexpr int RECT_WIDTH = 4; + static constexpr int RECT_HEIGHT = 4; + + // Set texture color modifier. sm_corners->set_color_mod(m_colorMod); - const int rightX = (m_x + m_width) - CORNER_WIDTH; - const int rightRectX = (m_x + m_width) - RECT_WIDTH; - + // Calculating this all here maker the rest easier. + const int rightX = (m_x + m_width) - CORNER_WIDTH; + const int rightRectX = (m_x + m_width) - RECT_WIDTH; const int midX = m_x + CORNER_WIDTH; const int midY = m_y + CORNER_HEIGHT; const int midWidth = m_width - (CORNER_WIDTH * 2); @@ -38,16 +43,19 @@ void ui::BoundingBox::render(sdl::SharedTexture &target, bool hasFocus) const int bottomY = (m_y + m_height) - CORNER_HEIGHT; const int bottomRectY = (m_y + m_height) - RECT_HEIGHT; - sm_corners->render_part(target, m_x, m_y, 0, 0, CORNER_WIDTH, CORNER_HEIGHT); - sdl::render_rect_fill(target, midX, m_y, midWidth, RECT_HEIGHT, m_colorMod); - sm_corners->render_part(target, rightX, m_y, CORNER_HEIGHT, 0, CORNER_WIDTH, CORNER_HEIGHT); + // Top + sm_corners->render_part(m_x, m_y, 0, 0, CORNER_WIDTH, CORNER_HEIGHT); + renderer.render_rectangle(midX, m_y, midWidth, RECT_HEIGHT, m_colorMod); + sm_corners->render_part(rightX, m_y, CORNER_HEIGHT, 0, CORNER_WIDTH, CORNER_HEIGHT); + // Middle - sdl::render_rect_fill(target, m_x, midY, RECT_WIDTH, midHeight, m_colorMod); - sdl::render_rect_fill(target, rightRectX, midY, RECT_WIDTH, midHeight, m_colorMod); + renderer.render_rectangle(m_x, midY, RECT_WIDTH, midHeight, m_colorMod); + renderer.render_rectangle(rightRectX, midY, RECT_WIDTH, midHeight, m_colorMod); + // Bottom - sm_corners->render_part(target, m_x, bottomY, 0, CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT); - sdl::render_rect_fill(target, midX, bottomRectY, midWidth, RECT_HEIGHT, m_colorMod); - sm_corners->render_part(target, rightX, bottomY, CORNER_WIDTH, CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT); + sm_corners->render_part(m_x, bottomY, 0, CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT); + renderer.render_rectangle(midX, bottomRectY, midWidth, RECT_HEIGHT, m_colorMod); + sm_corners->render_part(rightX, bottomY, CORNER_WIDTH, CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT); } void ui::BoundingBox::set_x(int x) noexcept { m_x = x; } @@ -62,6 +70,9 @@ void ui::BoundingBox::set_height(int height) noexcept { m_height = height; } void ui::BoundingBox::initialize_static_members() { + // Path to load the corner texture from? + static constexpr std::string_view CORNER_PATH = "romfs:/Textures/MenuBounding.png"; + if (sm_corners) { return; } - sm_corners = sdl::TextureManager::load("menuCorners", "romfs:/Textures/MenuBounding.png"); + sm_corners = sdl2::TextureManager::create_load_resource(CORNER_PATH, CORNER_PATH); } diff --git a/source/ui/ColorMod.cpp b/source/ui/ColorMod.cpp index 1da05b0..d364586 100644 --- a/source/ui/ColorMod.cpp +++ b/source/ui/ColorMod.cpp @@ -10,11 +10,5 @@ void ui::ColorMod::update() noexcept else if (changeUp) { m_direction = true; } } -ui::ColorMod::operator sdl::Color() const noexcept -{ - uint32_t color{}; - color |= static_cast((0x88 + m_colorMod) << 16); - color |= static_cast((0xC5 + (m_colorMod / 2)) << 8); - color |= 0xFF; - return sdl::Color{color}; -} +ui::ColorMod::operator SDL_Color() const noexcept +{ return SDL_Color{0x00, static_cast(0x88 + m_colorMod), static_cast(0xC5 + m_colorMod * 0.5), 0xFF}; } diff --git a/source/ui/ControlGuide.cpp b/source/ui/ControlGuide.cpp index 7f6fdb5..6259cba 100644 --- a/source/ui/ControlGuide.cpp +++ b/source/ui/ControlGuide.cpp @@ -1,6 +1,7 @@ #include "ui/ControlGuide.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" #include "logging/logger.hpp" @@ -23,18 +24,21 @@ namespace ui::ControlGuide::ControlGuide(const char *guide) : m_guide(guide) - , m_textWidth(sdl::text::get_width(GUIDE_TEXT_SIZE, m_guide)) , m_targetX(GUIDE_X_OFFSET - (m_textWidth + CONTAINER_PADDING)) , m_guideWidth(graphics::SCREEN_WIDTH - m_targetX) , m_transition(graphics::SCREEN_WIDTH, TRANS_Y, 0, 0, m_targetX, TRANS_Y, 0, 0, ui::Transition::DEFAULT_THRESHOLD) , m_state(State::Hidden) { + // Init static. ui::ControlGuide::initialize_static_members(); + + // Grab the width of the text. + m_textWidth = sm_font->get_text_width(m_guide); } // ---- Public functions ---- -void ui::ControlGuide::update(bool hasFocus) +void ui::ControlGuide::update(const sdl2::Input &input, bool hasFocus) { switch (m_state) { @@ -46,42 +50,35 @@ void ui::ControlGuide::update(bool hasFocus) void ui::ControlGuide::sub_update() { - // To do: Maybe this differently? - ControlGuide::update(false); + // I don't like repeating, but it's the easiest way to adapt this for my SDL2 changes. + switch (m_state) + { + case State::Opening: + case State::Hiding: ControlGuide::update_position_state(); break; + default: ControlGuide::update_state(false); break; + } } -void ui::ControlGuide::render(sdl::SharedTexture &target, bool hasFocus) +void ui::ControlGuide::render(sdl2::Renderer &renderer, bool hasFocus) { // These are for the rectangle that makes up the rest of the container. static constexpr int RECT_OFFSET_X = 16; static constexpr int RECT_HEIGHT = 48; // This is where the text is rendered and its size. - static constexpr int TEXT_OFFSET_X = 24; - static constexpr int TEXT_OFFSET_Y = 10; - static constexpr int TEXT_FONT_SIZE = 24; + static constexpr int TEXT_OFFSET_X = 24; + static constexpr int TEXT_OFFSET_Y = 10; // Grab the X and Y. const int guideX = m_transition.get_x(); const int guideY = m_transition.get_y(); // Render the cap and the rectangle. - sm_controlCap->render(sdl::Texture::Null, guideX, guideY); - sdl::render_rect_fill(sdl::Texture::Null, - guideX + RECT_OFFSET_X, - guideY, - m_guideWidth - RECT_OFFSET_X, - RECT_HEIGHT, - colors::GUIDE_COLOR); + sm_controlCap->render(guideX, guideY); + renderer.render_rectangle(guideX + RECT_OFFSET_X, guideY, m_guideWidth - RECT_OFFSET_X, RECT_HEIGHT, colors::GUIDE_COLOR); // Guide text. - sdl::text::render(sdl::Texture::Null, - guideX + TEXT_OFFSET_X, - guideY + TEXT_OFFSET_Y, - TEXT_FONT_SIZE, - sdl::text::NO_WRAP, - colors::WHITE, - m_guide); + sm_font->render_text(guideX + TEXT_OFFSET_X, guideY + TEXT_OFFSET_Y, colors::WHITE, m_guide); } // ---- Private functions ---- @@ -89,11 +86,14 @@ void ui::ControlGuide::render(sdl::SharedTexture &target, bool hasFocus) void ui::ControlGuide::initialize_static_members() { // Name for the texture manager. - static constexpr std::string_view NAME_CAP = "ControlGuideCap"; + static constexpr std::string_view CAP_PATH = "romfs:/Textures/GuideCap.png"; // If it's already loaded, return. - if (sm_controlCap) { return; } - sm_controlCap = sdl::TextureManager::load(NAME_CAP, "romfs:/Textures/GuideCap.png"); + if (sm_controlCap && sm_font) { return; } + + sm_controlCap = sdl2::TextureManager::create_load_resource(CAP_PATH, CAP_PATH); + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_FOUR_PIXEL, + graphics::fonts::sizes::TWENTY_FOUR_PIXEL); } void ui::ControlGuide::reset() noexcept diff --git a/source/ui/DialogBox.cpp b/source/ui/DialogBox.cpp index 7396623..f019de0 100644 --- a/source/ui/DialogBox.cpp +++ b/source/ui/DialogBox.cpp @@ -16,36 +16,32 @@ ui::DialogBox::DialogBox(int x, int y, int width, int height, ui::DialogBox::Typ , m_width(width) , m_height(height) , m_type(type) -{ - ui::DialogBox::initialize_static_members(); -} +{ ui::DialogBox::initialize_static_members(); } // ---- Public functions ---- -void ui::DialogBox::render(sdl::SharedTexture &target, bool hasFocus) +void ui::DialogBox::render(sdl2::Renderer &renderer, bool hasFocus) { - const bool darkDialog = m_type == DialogBox::Type::Dark; - sdl::SharedTexture &corners = darkDialog ? sm_darkCorners : sm_lightCorners; - const sdl::Color rectColor = darkDialog ? colors::DIALOG_DARK : colors::DIALOG_LIGHT; + const bool darkDialog = m_type == DialogBox::Type::Dark; + sdl2::SharedTexture &corners = darkDialog ? sm_darkCorners : sm_lightCorners; + const SDL_Color rectColor = darkDialog ? colors::DIALOG_DARK : colors::DIALOG_LIGHT; // Top - corners->render_part(target, m_x, m_y, 0, 0, CORNER_WIDTH, CORNER_HEIGHT); - sdl::render_rect_fill(target, m_x + CORNER_WIDTH, m_y, m_width - (CORNER_WIDTH * 2), CORNER_HEIGHT, rectColor); - corners->render_part(target, (m_x + m_width) - CORNER_WIDTH, m_y, CORNER_WIDTH, 0, CORNER_WIDTH, CORNER_HEIGHT); + corners->render_part(m_x, m_y, 0, 0, CORNER_WIDTH, CORNER_HEIGHT); + renderer.render_rectangle(m_x + CORNER_WIDTH, m_y, m_width - (CORNER_WIDTH * 2), CORNER_HEIGHT, rectColor); + corners->render_part((m_x + m_width) - CORNER_WIDTH, m_y, CORNER_WIDTH, 0, CORNER_WIDTH, CORNER_HEIGHT); // Middle - sdl::render_rect_fill(target, m_x, m_y + CORNER_HEIGHT, m_width, m_height - (CORNER_HEIGHT * 2), rectColor); + renderer.render_rectangle(m_x, m_y + CORNER_HEIGHT, m_width, m_height - (CORNER_HEIGHT * 2), rectColor); // Bottom - corners->render_part(target, m_x, (m_y + m_height) - CORNER_HEIGHT, 0, CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT); - sdl::render_rect_fill(target, - m_x + CORNER_WIDTH, - (m_y + m_height) - CORNER_HEIGHT, - m_width - (CORNER_WIDTH * 2), - CORNER_HEIGHT, - rectColor); - corners->render_part(target, - (m_x + m_width) - CORNER_WIDTH, + corners->render_part(m_x, (m_y + m_height) - CORNER_HEIGHT, 0, CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT); + renderer.render_rectangle(m_x + CORNER_WIDTH, + (m_y + m_height) - CORNER_HEIGHT, + m_width - (CORNER_WIDTH * 2), + CORNER_HEIGHT, + rectColor); + corners->render_part((m_x + m_width) - CORNER_WIDTH, (m_y + m_height) - CORNER_HEIGHT, CORNER_WIDTH, CORNER_HEIGHT, @@ -78,8 +74,11 @@ void ui::DialogBox::set_from_transition(ui::Transition &transition, bool centere void ui::DialogBox::initialize_static_members() { + static constexpr std::string_view DARK_PATH = "romfs:/Textures/DialogCornersDark.png"; + static constexpr std::string_view LIGHT_PATH = "romfs:/Textures/DialogCornersLight.png"; + if (sm_darkCorners && sm_lightCorners) { return; } - sm_darkCorners = sdl::TextureManager::load("darkCorners", "romfs:/Textures/DialogCornersDark.png"); - sm_lightCorners = sdl::TextureManager::load("lightCorners", "romfs:/Textures/DialogCornersLight.png"); + sm_darkCorners = sdl2::TextureManager::create_load_resource(DARK_PATH, DARK_PATH); + sm_lightCorners = sdl2::TextureManager::create_load_resource(LIGHT_PATH, LIGHT_PATH); } diff --git a/source/ui/Frame.cpp b/source/ui/Frame.cpp index 8a30954..8d3e7c4 100644 --- a/source/ui/Frame.cpp +++ b/source/ui/Frame.cpp @@ -9,13 +9,11 @@ ui::Frame::Frame(int x, int y, int width, int height) , m_y(y) , m_width(width) , m_height(height) -{ - Frame::initialize_static_members(); -} +{ Frame::initialize_static_members(); } // ---- Public functions ---- -void ui::Frame::render(sdl::SharedTexture &target, bool hasFocus) +void ui::Frame::render(sdl2::Renderer &renderer, bool hasFocus) { // This is the size of one of the "tiles" of the frame. static constexpr int TILE = 16; @@ -27,19 +25,19 @@ void ui::Frame::render(sdl::SharedTexture &target, bool hasFocus) const int textureCorner = TILE * 2; // Top. - sm_frameCorners->render_part(target, m_x, m_y, 0, 0, TILE, TILE); - sm_frameCorners->render_part_stretched(target, TILE, 0, TILE, TILE, m_x + TILE, m_y, midWidth, TILE); - sm_frameCorners->render_part(target, rightEdge, m_y, 32, 0, TILE, TILE); + sm_frameCorners->render_part(m_x, m_y, 0, 0, TILE, TILE); + sm_frameCorners->render_part_stretched(TILE, 0, TILE, TILE, m_x + TILE, m_y, midWidth, TILE); + sm_frameCorners->render_part(rightEdge, m_y, 32, 0, TILE, TILE); // Middle - sm_frameCorners->render_part_stretched(target, 0, TILE, TILE, TILE, m_x, m_y + TILE, TILE, midHeight); - sdl::render_rect_fill(target, m_x + TILE, m_y + TILE, midWidth, midHeight, colors::SLIDE_PANEL_CLEAR); - sm_frameCorners->render_part_stretched(target, textureCorner, TILE, TILE, TILE, rightEdge, m_y + TILE, TILE, midHeight); + sm_frameCorners->render_part_stretched(0, TILE, TILE, TILE, m_x, m_y + TILE, TILE, midHeight); + renderer.render_rectangle(m_x + TILE, m_y + TILE, midWidth, midHeight, colors::SLIDE_PANEL_CLEAR); + sm_frameCorners->render_part_stretched(textureCorner, TILE, TILE, TILE, rightEdge, m_y + TILE, TILE, midHeight); // Bottom - sm_frameCorners->render_part(target, m_x, bottomEdge, 0, textureCorner, TILE, TILE); - sm_frameCorners->render_part_stretched(target, TILE, textureCorner, TILE, TILE, m_x + TILE, bottomEdge, midWidth, TILE); - sm_frameCorners->render_part(target, rightEdge, bottomEdge, textureCorner, textureCorner, TILE, TILE); + sm_frameCorners->render_part(m_x, bottomEdge, 0, textureCorner, TILE, TILE); + sm_frameCorners->render_part_stretched(TILE, textureCorner, TILE, TILE, m_x + TILE, bottomEdge, midWidth, TILE); + sm_frameCorners->render_part(rightEdge, bottomEdge, textureCorner, textureCorner, TILE, TILE); } void ui::Frame::set_x(int x) noexcept { m_x = x; } @@ -67,9 +65,8 @@ void ui::Frame::set_from_transition(const ui::Transition &transition, bool cente void ui::Frame::initialize_static_members() { - static constexpr std::string_view FRAME_NAME = "FrameCorners"; - static constexpr const char *FRAME_PATH = "romfs:/Textures/Frame.png"; + static constexpr std::string_view FRAME = "romfs:/Textures/Frame.png"; if (sm_frameCorners) { return; } - sm_frameCorners = sdl::TextureManager::load(FRAME_NAME, FRAME_PATH); + sm_frameCorners = sdl2::TextureManager::create_load_resource(FRAME, FRAME); } diff --git a/source/ui/IconMenu.cpp b/source/ui/IconMenu.cpp index 4975c3d..ccb01c7 100644 --- a/source/ui/IconMenu.cpp +++ b/source/ui/IconMenu.cpp @@ -1,5 +1,6 @@ #include "ui/IconMenu.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" namespace @@ -24,33 +25,38 @@ ui::IconMenu::IconMenu(int x, int y, int renderTargetHeight) // ---- Public functions ---- -void ui::IconMenu::update(bool hasFocus) { Menu::update(hasFocus); } +void ui::IconMenu::update(const sdl2::Input &input, bool hasFocus) { Menu::update(input, hasFocus); } -void ui::IconMenu::render(sdl::SharedTexture &target, bool hasFocus) +void ui::IconMenu::render(sdl2::Renderer &renderer, bool hasFocus) { const int optionCount = m_options.size(); const int y = m_transition.get_y(); for (int i = 0, tempY = y; i < optionCount; i++, tempY += m_optionHeight) { - // Clear target. - m_optionTarget->clear(colors::TRANSPARENT); - if (i == m_selected) + // Set target, clear. { - if (hasFocus) + graphics::ScopedRender optionScope{renderer, m_optionTarget}; + renderer.frame_begin(colors::TRANSPARENT); + if (i == m_selected) { - m_boundingBox->set_x(m_x - 8); - m_boundingBox->set_y(tempY - 8); - m_boundingBox->render(target, hasFocus); + if (hasFocus) + { + m_boundingBox->set_x(m_x - 8); + m_boundingBox->set_y(tempY - 8); + m_boundingBox->render(renderer, hasFocus); + } + // This is always rendered. + renderer.render_rectangle(0, 0, 4, 130, colors::BLUE_GREEN); } - // This is always rendered. - sdl::render_rect_fill(m_optionTarget, 0, 0, 4, 130, colors::BLUE_GREEN); + + m_options[i]->render_stretched(8, 1, ICON_RENDER_WIDTH, ICON_RENDER_HEIGHT); } - m_options[i]->render_stretched(m_optionTarget, 8, 1, ICON_RENDER_WIDTH, ICON_RENDER_HEIGHT); - m_optionTarget->render(target, m_x, tempY); + + m_optionTarget->render(m_x, tempY); } } -void ui::IconMenu::add_option(sdl::SharedTexture newOption) +void ui::IconMenu::add_option(sdl2::SharedTexture newOption) { Menu::add_option("ICON"); // Parent class needs text for this to work correctly. m_options.push_back(newOption); diff --git a/source/ui/Menu.cpp b/source/ui/Menu.cpp index a642531..b98df65 100644 --- a/source/ui/Menu.cpp +++ b/source/ui/Menu.cpp @@ -1,8 +1,8 @@ #include "ui/Menu.hpp" #include "config/config.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" -#include "input.hpp" #include "mathutil.hpp" #include "ui/BoundingBox.hpp" @@ -27,20 +27,20 @@ ui::Menu::Menu(int x, int y, int width, int fontSize, int renderTargetHeight) // ---- Public functions ---- -void ui::Menu::update(bool hasFocus) +void ui::Menu::update(const sdl2::Input &input, bool hasFocus) { if (m_options.empty()) { return; } - m_boundingBox->update(hasFocus); - m_optionScroll->update(hasFocus); + m_boundingBox->update(input, hasFocus); + m_optionScroll->update(input, hasFocus); - Menu::handle_input(); + Menu::handle_input(input); Menu::update_scrolling(); Menu::update_scroll_text(); m_transition.update(); } -void ui::Menu::render(sdl::SharedTexture &target, bool hasFocus) +void ui::Menu::render(sdl2::Renderer &renderer, bool hasFocus) { if (m_options.empty()) { return; } @@ -52,22 +52,29 @@ void ui::Menu::render(sdl::SharedTexture &target, bool hasFocus) if (tempY < -m_fontSize) { continue; } else if (tempY > m_renderTargetHeight) { break; } - m_optionTarget->clear(colors::TRANSPARENT); - - if (i == m_selected) { - if (hasFocus) + graphics::ScopedRender optionScoped{renderer, m_optionTarget}; + renderer.frame_begin(colors::TRANSPARENT); + + if (i == m_selected) { - m_boundingBox->set_x(m_x - 4); - m_boundingBox->set_y(tempY - 4); - m_boundingBox->render(target, hasFocus); + if (hasFocus) + { + m_boundingBox->set_x(m_x - 4); + m_boundingBox->set_y(tempY - 4); + m_boundingBox->render(renderer, hasFocus); + } + renderer.render_rectangle(8, 8, 4, m_optionHeight - 12, colors::BLUE_GREEN); + m_optionScroll->render(renderer, hasFocus); + } + else + { + m_font->render_text(24, m_textY, colors::WHITE, m_options[i]); } - sdl::render_rect_fill(m_optionTarget, 8, 8, 4, m_optionHeight - 12, colors::BLUE_GREEN); - m_optionScroll->render(m_optionTarget, hasFocus); } - else { sdl::text::render(m_optionTarget, 24, m_textY, m_fontSize, sdl::text::NO_WRAP, colors::WHITE, m_options[i]); } + // render target to target - m_optionTarget->render(target, m_x, tempY); + m_optionTarget->render(m_x, tempY); } } @@ -144,7 +151,8 @@ void ui::Menu::initialize_option_target() static int MENU_ID{}; const std::string optionTargetName = "MENU_TARGET_" + std::to_string(MENU_ID++); - m_optionTarget = sdl::TextureManager::load(optionTargetName, m_width, m_optionHeight, SDL_TEXTUREACCESS_TARGET); + m_optionTarget = + sdl2::TextureManager::create_load_resource(optionTargetName, m_width, m_optionHeight, SDL_TEXTUREACCESS_TARGET); } void ui::Menu::initialize_ui_elements() @@ -166,11 +174,10 @@ void ui::Menu::initialize_ui_elements() void ui::Menu::initialize_sounds() { - static constexpr std::string_view CURSOR_NAME = "MenuCursor"; - static constexpr const char *CURSOR_PATH = "romfs:/Sound/MenuCursor.wav"; + static constexpr std::string_view CURSOR_PATH = "romfs:/Sound/MenuCursor.wav"; if (sm_cursor) { return; } - sm_cursor = sdl::SoundManager::load(CURSOR_NAME, CURSOR_PATH); + sm_cursor = sdl2::SoundManager::create_load_resource(CURSOR_PATH, CURSOR_PATH); } void ui::Menu::update_scroll_text() @@ -180,18 +187,18 @@ void ui::Menu::update_scroll_text() if (text != option) { m_optionScroll->set_text(std::string_view{option}, false); } } -void ui::Menu::handle_input() +void ui::Menu::handle_input(const sdl2::Input &input) { // Get the length of the menu. const int optionsSize = m_options.size(); // Control bools. - const bool upPressed = input::button_pressed(HidNpadButton_AnyUp); - const bool downPressed = input::button_pressed(HidNpadButton_AnyDown); - const bool leftPressed = input::button_pressed(HidNpadButton_AnyLeft); - const bool rightPressed = input::button_pressed(HidNpadButton_AnyRight); - const bool lShoulderPressed = input::button_pressed(HidNpadButton_L); - const bool rShoulderPressed = input::button_pressed(HidNpadButton_R); + const bool upPressed = input.button_pressed(HidNpadButton_AnyUp); + const bool downPressed = input.button_pressed(HidNpadButton_AnyDown); + const bool leftPressed = input.button_pressed(HidNpadButton_AnyLeft); + const bool rightPressed = input.button_pressed(HidNpadButton_AnyRight); + const bool lShoulderPressed = input.button_pressed(HidNpadButton_L); + const bool rShoulderPressed = input.button_pressed(HidNpadButton_R); // Wrapping conditions. const bool wrapEnd = upPressed && m_selected - 1 < 0; diff --git a/source/ui/PopMessage.cpp b/source/ui/PopMessage.cpp index a6eb72c..27b4dee 100644 --- a/source/ui/PopMessage.cpp +++ b/source/ui/PopMessage.cpp @@ -2,6 +2,7 @@ #include "config/config.hpp" #include "graphics/colors.hpp" +#include "graphics/fonts.hpp" #include "graphics/screen.hpp" #include "mathutil.hpp" #include "sdl.hpp" @@ -32,9 +33,7 @@ ui::PopMessage::PopMessage(int ticks, std::string &message) , m_ticks(ticks) , m_message(std::move(message)) , m_state(State::Rising) -{ - PopMessage::initialize_static_members(); -} +{ PopMessage::initialize_static_members(); } // ---- Public functions ---- @@ -62,9 +61,9 @@ void ui::PopMessage::update(double targetY) } } -void ui::PopMessage::render() +void ui::PopMessage::render(sdl2::Renderer &renderer) { - PopMessage::render_container(); + PopMessage::render_container(renderer); // Don't continue unless the message is in place or it's not in its closing state. if (m_state != State::Opening && m_state != State::Displaying) { return; } @@ -72,7 +71,7 @@ void ui::PopMessage::render() // This avoids allocating and returning another std::string. const int y = m_transition.get_y(); const std::string_view message{m_message.c_str(), static_cast(m_substrOffset)}; - sdl::text::render(sdl::Texture::Null, m_textX, y + 11, FONT_SIZE, sdl::text::NO_WRAP, colors::BLACK, message); + sm_font->render_text(m_textX, y + 11, colors::BLACK, message); } bool ui::PopMessage::finished() const noexcept { return m_state == State::Finished; } @@ -83,12 +82,13 @@ std::string_view ui::PopMessage::get_message() const noexcept { return m_message void ui::PopMessage::initialize_static_members() { - static constexpr std::string_view TEX_CAP_NAME = "PopCaps"; - static constexpr const char *TEX_CAP_PATH = "romfs:/Textures/PopMessage.png"; + static constexpr std::string_view CAP_PATH = "romfs:/Textures/PopMessage.png"; - if (sm_endCaps) { return; } + if (sm_endCaps && sm_font) { return; } - sm_endCaps = sdl::TextureManager::load(TEX_CAP_NAME, TEX_CAP_PATH); + sm_endCaps = sdl2::TextureManager::create_load_resource(CAP_PATH, CAP_PATH); + sm_font = sdl2::FontManager::create_load_resource(graphics::fonts::names::TWENTY_TWO_PIXEL, + graphics::fonts::sizes::TWENTY_TWO_PIXEL); } void ui::PopMessage::update_y() noexcept @@ -132,7 +132,7 @@ void ui::PopMessage::update_text_offset() // Get the substring and calculate the updated width and X of the message. const std::string_view subString{m_message.c_str(), static_cast(m_substrOffset)}; - const int subWidth = sdl::text::get_width(FONT_SIZE, subString); + const int subWidth = sm_font->get_text_width(subString); const int containerWidth = subWidth + CONTAINER_PADDING; m_textX = SCREEN_CENTER - (subWidth / 2); @@ -171,7 +171,7 @@ void ui::PopMessage::update_display_timer() noexcept } } -void ui::PopMessage::render_container() noexcept +void ui::PopMessage::render_container(sdl2::Renderer &renderer) noexcept { // The width of the CAP graphics. constexpr int CAP_WIDTH = 48; @@ -183,7 +183,7 @@ void ui::PopMessage::render_container() noexcept const int width = m_transition.get_width() + CAP_HALF; // Render the container. - sm_endCaps->render_part(sdl::Texture::Null, x, y, 0, 0, CAP_HALF, CAP_WIDTH); - sdl::render_rect_fill(sdl::Texture::Null, x + CAP_HALF, y, width - CAP_WIDTH, 48, colors::DIALOG_LIGHT); - sm_endCaps->render_part(sdl::Texture::Null, x + (width - CAP_HALF), y, CAP_HALF, 0, CAP_HALF, 48); + sm_endCaps->render_part(x, y, 0, 0, CAP_HALF, CAP_WIDTH); + renderer.render_rectangle(x + CAP_HALF, y, width - CAP_WIDTH, 48, colors::DIALOG_LIGHT); + sm_endCaps->render_part(x + (width - CAP_HALF), y, CAP_HALF, 0, CAP_HALF, 48); } diff --git a/source/ui/PopMessageManager.cpp b/source/ui/PopMessageManager.cpp index b9b2a69..5c6ace9 100644 --- a/source/ui/PopMessageManager.cpp +++ b/source/ui/PopMessageManager.cpp @@ -54,14 +54,14 @@ void ui::PopMessageManager::update() } } -void ui::PopMessageManager::render() +void ui::PopMessageManager::render(sdl2::Renderer &renderer) { PopMessageManager &manager = PopMessageManager::get_instance(); auto &messages = manager.m_messages; std::mutex &messageMutex = manager.m_messageMutex; std::lock_guard messageGuard{messageMutex}; - for (auto &message : messages) { message.render(); } + for (auto &message : messages) { message.render(renderer); } } void ui::PopMessageManager::push_message(int displayTicks, std::string_view message) @@ -118,10 +118,9 @@ void ui::PopMessageManager::push_message(int displayTicks, std::string &message) void ui::PopMessageManager::initialize_pop_sound() { - static constexpr std::string_view POP_NAME = "PopSound"; - static constexpr const char *POP_PATH = "romfs:/Sound/PopMessage.wav"; + static constexpr std::string_view POP_PATH = "romfs:/Sound/PopMessage.wav"; if (m_popSound) { return; } - m_popSound = sdl::SoundManager::load(POP_NAME, POP_PATH); + m_popSound = sdl2::SoundManager::create_load_resource(POP_PATH, POP_PATH); } \ No newline at end of file diff --git a/source/ui/SlideOutPanel.cpp b/source/ui/SlideOutPanel.cpp index 61a42cc..d4e7229 100644 --- a/source/ui/SlideOutPanel.cpp +++ b/source/ui/SlideOutPanel.cpp @@ -1,10 +1,12 @@ #include "ui/SlideOutPanel.hpp" #include "config/config.hpp" +#include "graphics/ScopedRender.hpp" #include "graphics/colors.hpp" #include "graphics/screen.hpp" #include "logging/logger.hpp" #include "mathutil.hpp" +#include "stringutil.hpp" #include #include @@ -25,38 +27,47 @@ ui::SlideOutPanel::SlideOutPanel(int width, Side side) 0, ui::Transition::DEFAULT_THRESHOLD) , m_state(State::Opening) - , m_renderTarget(sdl::TextureManager::load("PANEL_" + std::to_string(sm_targetID++), - m_width, - graphics::SCREEN_HEIGHT, - SDL_TEXTUREACCESS_TARGET)) {}; + , m_renderTarget(sdl2::TextureManager::create_load_resource(stringutil::get_formatted_string("PANEL_%i", sm_targetID++), + m_width, + graphics::SCREEN_HEIGHT, + SDL_TEXTUREACCESS_TARGET)) {}; // ---- Public functions ---- -void ui::SlideOutPanel::update(bool hasFocus) +void ui::SlideOutPanel::update(const sdl2::Input &input, bool hasFocus) { switch (m_state) { case State::Opening: case State::Closing: case State::Hiding: SlideOutPanel::update_position_state(); break; - case State::Opened: SlideOutPanel::update_sub_elements(hasFocus); break; + case State::Opened: SlideOutPanel::update_sub_elements(input, hasFocus); break; default: return; // Nothing should take place in any other state. } } void ui::SlideOutPanel::sub_update() { m_transition.update(); } -void ui::SlideOutPanel::render(sdl::SharedTexture &target, bool hasFocus) +void ui::SlideOutPanel::render(sdl2::Renderer &renderer, bool hasFocus) { - // Loop and render all sub-elements to the target. - for (auto ¤tElement : m_elements) { currentElement->render(m_renderTarget, hasFocus); } + { + // Set target. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + + // Loop and render all sub-elements to the target. + for (auto ¤tElement : m_elements) { currentElement->render(renderer, hasFocus); } + } // Render the main target to the target passed (most likely the screen); const int x = m_transition.get_x(); - m_renderTarget->render(target, x, 0); + m_renderTarget->render(x, 0); } -void ui::SlideOutPanel::clear_target() { m_renderTarget->clear(colors::SLIDE_PANEL_CLEAR); } +void ui::SlideOutPanel::clear_target(sdl2::Renderer &renderer) +{ + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(colors::SLIDE_PANEL_CLEAR); +} void ui::SlideOutPanel::reset() noexcept { @@ -132,7 +143,7 @@ void ui::SlideOutPanel::push_new_element(std::shared_ptr newElement void ui::SlideOutPanel::clear_elements() { m_elements.clear(); } -sdl::SharedTexture &ui::SlideOutPanel::get_target() noexcept { return m_renderTarget; } +sdl2::SharedTexture &ui::SlideOutPanel::get_target() noexcept { return m_renderTarget; } // ---- Private functions ---- @@ -150,7 +161,7 @@ void ui::SlideOutPanel::update_position_state() noexcept else if (hidden) { m_state = State::Hidden; } } -void ui::SlideOutPanel::update_sub_elements(bool hasFocus) noexcept +void ui::SlideOutPanel::update_sub_elements(const sdl2::Input &input, bool hasFocus) noexcept { - for (auto &element : m_elements) { element->update(hasFocus); } + for (auto &element : m_elements) { element->update(input, hasFocus); } } \ No newline at end of file diff --git a/source/ui/TextScroll.cpp b/source/ui/TextScroll.cpp index 65dca74..5ae5941 100644 --- a/source/ui/TextScroll.cpp +++ b/source/ui/TextScroll.cpp @@ -1,6 +1,8 @@ #include "ui/TextScroll.hpp" +#include "graphics/ScopedRender.hpp" #include "sdl.hpp" +#include "stringutil.hpp" namespace { @@ -9,8 +11,6 @@ namespace /// @brief This is the number of pixels between the two renderings of the text. constexpr int SIZE_TEXT_GAP = 0; - - int TARGET_ID{}; } // namespace // ---- Construction ---- @@ -21,12 +21,10 @@ ui::TextScroll::TextScroll(std::string_view text, int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center) -{ - TextScroll::initialize(text, x, y, width, height, fontSize, textColor, clearColor, center); -} +{ TextScroll::initialize(text, x, y, width, height, fontSize, textColor, clearColor, center); } ui::TextScroll::TextScroll(std::string &text, int x, @@ -34,12 +32,10 @@ ui::TextScroll::TextScroll(std::string &text, int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center) -{ - TextScroll::initialize(text, x, y, width, height, fontSize, textColor, clearColor, center); -} +{ TextScroll::initialize(text, x, y, width, height, fontSize, textColor, clearColor, center); } // ---- Public functions ---- @@ -49,24 +45,24 @@ void ui::TextScroll::initialize(std::string_view text, int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center) { - m_renderX = x; - m_renderY = y; - m_fontSize = fontSize; - m_textColor = textColor; + m_renderX = x; + m_renderY = y; + m_fontSize = fontSize; + m_font = sdl2::FontManager::create_load_resource(TextScroll::generate_font_name(fontSize), fontSize); + m_textColor = textColor; m_clearColor = clearColor; m_targetWidth = width; m_targetHeight = height; m_textY = (m_targetHeight / 2) - (m_fontSize / 2); m_scrollTimer.start(TICKS_SCROLL_TRIGGER); - - { - const std::string targetName = "textScroll_" + std::to_string(TARGET_ID++); - m_renderTarget = sdl::TextureManager::load(targetName, m_targetWidth, m_targetHeight, SDL_TEXTUREACCESS_TARGET); - } + m_renderTarget = sdl2::TextureManager::create_load_resource(TextScroll::generate_target_name(), + m_targetWidth, + m_targetHeight, + SDL_TEXTUREACCESS_TARGET); TextScroll::set_text(text, center); } @@ -77,24 +73,24 @@ void ui::TextScroll::initialize(std::string &text, int width, int height, int fontSize, - sdl::Color textColor, - sdl::Color clearColor, + SDL_Color textColor, + SDL_Color clearColor, bool center) { - m_renderX = x; - m_renderY = y; - m_fontSize = fontSize; - m_textColor = textColor; + m_renderX = x; + m_renderY = y; + m_fontSize = fontSize; + m_font = sdl2::FontManager::create_load_resource(TextScroll::generate_font_name(fontSize), fontSize); + m_textColor = textColor; m_clearColor = clearColor; m_targetWidth = width; m_targetHeight = height; m_textY = (m_targetHeight / 2) - (m_fontSize / 2); m_scrollTimer.start(TICKS_SCROLL_TRIGGER); - - { - const std::string targetName = "textScroll_" + std::to_string(TARGET_ID++); - m_renderTarget = sdl::TextureManager::load(targetName, m_targetWidth, m_targetHeight, SDL_TEXTUREACCESS_TARGET); - } + m_renderTarget = sdl2::TextureManager::create_load_resource(TextScroll::generate_target_name(), + m_targetWidth, + m_targetHeight, + SDL_TEXTUREACCESS_TARGET); TextScroll::set_text(text, center); } @@ -104,7 +100,7 @@ std::string_view ui::TextScroll::get_text() const noexcept { return m_text; } void ui::TextScroll::set_text(std::string_view text, bool center) { m_text = text; - m_textWidth = sdl::text::get_width(m_fontSize, m_text.c_str()); + m_textWidth = m_font->get_text_width(m_text); m_textScrollTriggered = false; if (m_textWidth > m_targetWidth - 16) @@ -128,7 +124,7 @@ void ui::TextScroll::set_text(std::string_view text, bool center) void ui::TextScroll::set_text(std::string &text, bool center) { m_text = std::move(text); - m_textWidth = sdl::text::get_width(m_fontSize, m_text.c_str()); + m_textWidth = m_font->get_text_width(m_text); m_textScrollTriggered = false; if (m_textWidth > m_targetWidth - 16) @@ -149,7 +145,7 @@ void ui::TextScroll::set_text(std::string &text, bool center) } } -void ui::TextScroll::update(bool hasFocus) +void ui::TextScroll::update(const sdl2::Input &input, bool hasFocus) { // I don't think this needs to care about having focus. const int invertedWidth = -(m_textWidth + SIZE_TEXT_GAP); @@ -172,25 +168,34 @@ void ui::TextScroll::update(bool hasFocus) } } -void ui::TextScroll::render(sdl::SharedTexture &target, bool hasFocus) +void ui::TextScroll::render(sdl2::Renderer &renderer, bool hasFocus) { - m_renderTarget->clear(m_clearColor); + // Gap between scrolling text rendering. + static constexpr int TEXT_GAP = 8; - if (!m_textScrolling) { - sdl::text::render(m_renderTarget, m_textX, m_textY, m_fontSize, sdl::text::NO_WRAP, m_textColor, m_text); + // Scoped render to the target. + graphics::ScopedRender scopedRender{renderer, m_renderTarget}; + renderer.frame_begin(m_clearColor); + + if (!m_textScrolling) { m_font->render_text(m_textX, m_textY, m_textColor, m_text); } + else + { + // We're going to render text twice so it looks like it's scrolling and doesn't end. Ever. + m_font->render_text(m_textX, m_textY, m_textColor, m_text); + m_font->render_text(m_textX + m_textWidth + TEXT_GAP, m_textY, m_textColor, m_text); + } } - else - { - // We're going to render text twice so it looks like it's scrolling and doesn't end. Ever. - sdl::text::render(m_renderTarget, m_textX, m_textY, m_fontSize, sdl::text::NO_WRAP, m_textColor, m_text); - sdl::text::render(m_renderTarget, - m_textX + m_textWidth + 8, - m_textY, - m_fontSize, - sdl::text::NO_WRAP, - m_textColor, - m_text); - } - m_renderTarget->render(target, m_renderX, m_renderY); + m_renderTarget->render(m_renderX, m_renderY); } + +// ---- Private Functions ---- + +std::string ui::TextScroll::generate_target_name() +{ + static int targetID{}; + return stringutil::get_formatted_string("TextScroll_%i", targetID++); +} + +std::string ui::TextScroll::generate_font_name(int fontSize) +{ return stringutil::get_formatted_string("TextScrollFont_%i", fontSize); } \ No newline at end of file diff --git a/source/ui/TitleTile.cpp b/source/ui/TitleTile.cpp index 6f10a05..977a5ec 100644 --- a/source/ui/TitleTile.cpp +++ b/source/ui/TitleTile.cpp @@ -11,7 +11,7 @@ namespace // ---- Construction ---- -ui::TitleTile::TitleTile(bool isFavorite, int index, sdl::SharedTexture icon) +ui::TitleTile::TitleTile(bool isFavorite, int index, sdl2::SharedTexture &icon) : m_transition(0, 0, UNSELECTED_WIDTH_HEIGHT, @@ -22,15 +22,12 @@ ui::TitleTile::TitleTile(bool isFavorite, int index, sdl::SharedTexture icon) UNSELECTED_WIDTH_HEIGHT, m_transition.DEFAULT_THRESHOLD) , m_isFavorite(isFavorite) - , m_index(index) , m_icon(icon) {}; // ---- Public functions ---- -void ui::TitleTile::update(int selected) +void ui::TitleTile::update(bool isSelected) { - const bool isSelected = selected == m_index; - if (isSelected) { m_transition.set_target_width(SELECTED_WIDTH_HEIGHT); @@ -45,7 +42,7 @@ void ui::TitleTile::update(int selected) m_transition.update_width_height(); } -void ui::TitleTile::render(sdl::SharedTexture &target, int x, int y) +void ui::TitleTile::render(int x, int y) { static constexpr std::string_view HEART_CHAR = "\uE017"; @@ -54,8 +51,9 @@ void ui::TitleTile::render(sdl::SharedTexture &target, int x, int y) const int renderX = x - ((width - 128) / 2); const int renderY = y - ((width - 128) / 2); - m_icon->render_stretched(target, renderX, renderY, width, height); - if (m_isFavorite) { sdl::text::render(target, renderX + 2, renderY + 2, 28, sdl::text::NO_WRAP, colors::PINK, HEART_CHAR); } + m_icon->render_stretched(renderX, renderY, width, height); + + if (m_isFavorite) { sm_heartFont->render_text(renderX + 2, renderY + 2, colors::PINK, HEART_CHAR); } } void ui::TitleTile::reset() noexcept @@ -69,3 +67,16 @@ void ui::TitleTile::reset() noexcept int ui::TitleTile::get_width() const noexcept { return m_transition.get_width(); } int ui::TitleTile::get_height() const noexcept { return m_transition.get_height(); } + +// ---- Private Functions ---- + +void ui::TitleTile::initialize_static_members() +{ + // Font name for the manager. + static constexpr std::string_view FONT_NAME = "HeartFont"; + static constexpr int FONT_SIZE = 28; + + if (sm_heartFont) { return; } + + sm_heartFont = sdl2::FontManager::create_load_resource(FONT_NAME, FONT_SIZE); +} \ No newline at end of file diff --git a/source/ui/TitleView.cpp b/source/ui/TitleView.cpp index f453868..de12110 100644 --- a/source/ui/TitleView.cpp +++ b/source/ui/TitleView.cpp @@ -3,7 +3,6 @@ #include "config/config.hpp" #include "error.hpp" #include "graphics/colors.hpp" -#include "input.hpp" #include "logging/logger.hpp" #include @@ -26,19 +25,19 @@ ui::TitleView::TitleView(data::User *user) TitleView::refresh(); } -void ui::TitleView::update(bool hasFocus) +void ui::TitleView::update(const sdl2::Input &input, bool hasFocus) { if (m_titleTiles.empty()) { return; } - m_bounding->update(hasFocus); - TitleView::handle_input(); + m_bounding->update(input, hasFocus); + TitleView::handle_input(input); TitleView::handle_scrolling(); TitleView::update_tiles(); m_transition.update(); } -void ui::TitleView::render(sdl::SharedTexture &target, bool hasFocus) +void ui::TitleView::render(sdl2::Renderer &renderer, bool hasFocus) { static constexpr int TILE_SPACE_VERT = 144; static constexpr int TILE_SPACE_HOR = 144; @@ -59,8 +58,7 @@ void ui::TitleView::render(sdl::SharedTexture &target, bool hasFocus) continue; } - ui::TitleTile &tile = m_titleTiles[j]; - tile.render(target, tempX, tempY); + m_titleTiles[i].render(tempX, tempY); } } @@ -69,12 +67,11 @@ void ui::TitleView::render(sdl::SharedTexture &target, bool hasFocus) m_bounding->set_x(m_selectedX - 30); m_bounding->set_y(m_selectedY - 30); - sdl::render_rect_fill(target, m_selectedX - 28, m_selectedY - 28, 184, 184, colors::CLEAR_COLOR); - m_bounding->render(target, hasFocus); + renderer.render_rectangle(m_selectedX - 28, m_selectedY - 28, 184, 184, colors::CLEAR_COLOR); + m_bounding->render(renderer, hasFocus); } - ui::TitleTile &selectedTile = m_titleTiles[m_selected]; - selectedTile.render(target, m_selectedX, m_selectedY); + m_titleTiles[m_selected].render(m_selectedX, m_selectedY); } int ui::TitleView::get_selected() const noexcept { return m_selected; } @@ -83,6 +80,7 @@ void ui::TitleView::set_selected(int selected) noexcept { const int tilesCount = m_titleTiles.size(); if (selected < 0 || selected >= tilesCount) { return; } + m_selected = selected; } @@ -98,8 +96,8 @@ void ui::TitleView::refresh() data::TitleInfo *titleInfo = data::get_title_info_by_id(applicationID); if (error::is_null(titleInfo)) { continue; } - const bool isFavorite = config::is_favorite(applicationID); - sdl::SharedTexture icon = titleInfo->get_icon(); // I don't like this but w/e. + const bool isFavorite = config::is_favorite(applicationID); + sdl2::SharedTexture &icon = titleInfo->get_icon(); m_titleTiles.emplace_back(isFavorite, i, icon); } @@ -120,23 +118,22 @@ void ui::TitleView::play_sound() noexcept { sm_cursor->play(); } void ui::TitleView::initialize_static_members() { - static constexpr std::string_view CURSOR_NAME = "MenuCursor"; - static constexpr const char *CURSOR_PATH = "romfs:/Sound/MenuCursor.wav"; + static constexpr std::string_view CURSOR_PATH = "romfs:/Sound/MenuCursor.wav"; if (sm_cursor) { return; } - sm_cursor = sdl::SoundManager::load(CURSOR_NAME, CURSOR_PATH); + sm_cursor = sdl2::SoundManager::create_load_resource(CURSOR_PATH, CURSOR_PATH); } -void ui::TitleView::handle_input() +void ui::TitleView::handle_input(const sdl2::Input &input) { const int totalTiles = m_titleTiles.size() - 1; - const bool upPressed = input::button_pressed(HidNpadButton_AnyUp); - const bool downPressed = input::button_pressed(HidNpadButton_AnyDown); - const bool leftPressed = input::button_pressed(HidNpadButton_AnyLeft); - const bool rightPressed = input::button_pressed(HidNpadButton_AnyRight); - const bool lShoulderPressed = input::button_pressed(HidNpadButton_L); - const bool rShoulderPressed = input::button_pressed(HidNpadButton_R); + const bool upPressed = input.button_pressed(HidNpadButton_AnyUp); + const bool downPressed = input.button_pressed(HidNpadButton_AnyDown); + const bool leftPressed = input.button_pressed(HidNpadButton_AnyLeft); + const bool rightPressed = input.button_pressed(HidNpadButton_AnyRight); + const bool lShoulderPressed = input.button_pressed(HidNpadButton_L); + const bool rShoulderPressed = input.button_pressed(HidNpadButton_R); const int previousSelected = m_selected;