From 600ca64ae567f67912f015a161585412d71ca57a Mon Sep 17 00:00:00 2001 From: Thomas Edvalson Date: Mon, 18 Jan 2016 22:24:27 -0500 Subject: [PATCH] Add basic console, remove console method from Game --- include/cpp3ds/Graphics/Console.hpp | 33 ++++++------ include/cpp3ds/Window/Game.hpp | 6 --- src/cpp3ds/Graphics/Console.cpp | 78 +++++++++++++++++++++-------- src/cpp3ds/Window/Game.cpp | 53 +++++++------------- src/emu3ds/Window/Game.cpp | 7 --- 5 files changed, 90 insertions(+), 87 deletions(-) diff --git a/include/cpp3ds/Graphics/Console.hpp b/include/cpp3ds/Graphics/Console.hpp index 8f6792c..51eb313 100644 --- a/include/cpp3ds/Graphics/Console.hpp +++ b/include/cpp3ds/Graphics/Console.hpp @@ -23,26 +23,14 @@ class Console : public Drawable { public: - //////////////////////////////////////////////////////////// - /// \brief Default constructor - /// - /// The constructor creates and activates the context - /// - //////////////////////////////////////////////////////////// - Console(); - //////////////////////////////////////////////////////////// /// \brief Destructor /// - /// The destructor deactivates and destroys the context + /// The destructor deactivates and destroys the console /// //////////////////////////////////////////////////////////// ~Console(); -public: - - void create(Screen screen = BottomScreen); - void update(float delta); void write(String text); @@ -51,10 +39,14 @@ public: void setVisible(bool visible); - static void initialize(); + static Console& getInstance(); + + static void enable(Screen screen, Color color = Color::White); + static void enableBasic(Screen screen); + static bool isEnabled(); + static bool isEnabledBasic(); void setScreen(Screen screen); - Screen getScreen(); void setColor(const Color& color); @@ -62,6 +54,14 @@ public: private: + //////////////////////////////////////////////////////////// + /// \brief Default constructor + /// + /// The constructor creates and activates the context + /// + //////////////////////////////////////////////////////////// + Console(); + //////////////////////////////////////////////////////////// /// \brief Draw the console to a render target /// @@ -79,7 +79,8 @@ private: std::vector m_lines; Text m_memoryText; unsigned int m_limit; - static bool m_initialized; + static bool m_enabled; + static bool m_enabledBasic; bool m_visible; Screen m_screen; }; diff --git a/include/cpp3ds/Window/Game.hpp b/include/cpp3ds/Window/Game.hpp index df8c3a0..cf199e2 100644 --- a/include/cpp3ds/Window/Game.hpp +++ b/include/cpp3ds/Window/Game.hpp @@ -21,8 +21,6 @@ public: virtual void processEvent(Event& event) = 0; virtual void renderTopScreen(Window& window) = 0; virtual void renderBottomScreen(Window& window) = 0; - void console(Screen screen = BottomScreen, Color color = Color::White); - void consoleBasic(Screen screen = BottomScreen); void render(); void run(); void exit(); @@ -31,11 +29,7 @@ public: protected: Window windowTop, windowBottom; private: - bool m_consoleEnabled; - bool m_consoleBasicEnabled; bool m_triggerExit; - Console m_console; - Screen m_consoleScreen; #ifdef EMULATION sf::RenderTexture m_frameTextureTop, m_frameTextureBottom; sf::Sprite m_frameSpriteTop, m_frameSpriteBottom; diff --git a/src/cpp3ds/Graphics/Console.cpp b/src/cpp3ds/Graphics/Console.cpp index 6774fda..4df1775 100644 --- a/src/cpp3ds/Graphics/Console.cpp +++ b/src/cpp3ds/Graphics/Console.cpp @@ -43,52 +43,86 @@ static const devoptab_t dotab_stdout = { namespace cpp3ds { -bool Console::m_initialized = false; +bool Console::m_enabled = false; +bool Console::m_enabledBasic = false; //////////////////////////////////////////////////////////// Console::Console() +: m_visible(true) { - // } //////////////////////////////////////////////////////////// Console::~Console() { - if (m_initialized) - { - // +} + + +//////////////////////////////////////////////////////////// +Console& Console::getInstance() +{ + static Console console; + return console; +} + + +//////////////////////////////////////////////////////////// +void Console::enable(Screen screen, Color color) +{ + if (m_enabledBasic) + return; + if (!m_enabled) { + m_enabled = true; +#ifndef EMULATION + gfxInitDefault(); + devoptab_list[STD_OUT] = &dotab_stdout; + devoptab_list[STD_ERR] = &dotab_stdout; + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); +#endif + Console& console = getInstance(); + priv::ResourceInfo font = priv::core_resources["opensans.ttf"]; + console.m_font.loadFromMemory(font.data, font.size); + + console.m_memoryText.setFont(console.m_font); + console.m_memoryText.setCharacterSize(12); + + console.m_screen = screen; + console.m_color = color; + console.m_limit = 1000; } } //////////////////////////////////////////////////////////// -void Console::initialize() +void Console::enableBasic(Screen screen) { - if (!m_initialized) { - m_initialized = true; + if (m_enabled) + return; + if (!m_enabledBasic) { + m_enabledBasic = true; + Console& console = getInstance(); + console.m_screen = screen; #ifndef EMULATION - devoptab_list[STD_OUT] = &dotab_stdout; - devoptab_list[STD_ERR] = &dotab_stdout; - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); + gfxInitDefault(); + consoleInit(screen == TopScreen ? GFX_TOP : GFX_BOTTOM, nullptr); #endif } } //////////////////////////////////////////////////////////// -void Console::create(Screen screen) +bool Console::isEnabled() { - initialize(); - priv::ResourceInfo font = priv::core_resources["opensans.ttf"]; - m_font.loadFromMemory(font.data, font.size); + return m_enabled; +} - m_memoryText.setFont(m_font); - m_memoryText.setCharacterSize(12); - m_screen = screen; - m_limit = 1000; +//////////////////////////////////////////////////////////// +bool Console::isEnabledBasic() +{ + return m_enabledBasic; } @@ -102,12 +136,12 @@ void Console::update(float delta) if (m_lines.size() > m_limit) m_lines.erase(m_lines.begin(), m_lines.end() - m_limit); - #ifndef EMULATION +#ifndef EMULATION std::ostringstream ss; ss << (__linear_heap_size - linearSpaceFree()) / 1024 << "kb / " << __linear_heap_size / 1024 << "kb"; m_memoryText.setString(ss.str()); m_memoryText.setPosition((m_screen == TopScreen ? 395 : 315) - m_memoryText.getGlobalBounds().width, 5); - #endif +#endif int h = 240; int i = m_lines.size(); diff --git a/src/cpp3ds/Window/Game.cpp b/src/cpp3ds/Window/Game.cpp index a9d549d..982e2a8 100644 --- a/src/cpp3ds/Window/Game.cpp +++ b/src/cpp3ds/Window/Game.cpp @@ -23,10 +23,8 @@ static void apt_clock_hook(APT_HookType hook, void* param) Game::Game() : m_triggerExit(false) -, m_consoleEnabled(false) -, m_consoleBasicEnabled(false) { - if (!Console::isInitialized()) + if (!Console::isEnabled() && !Console::isEnabledBasic()) gfxInitDefault(); CitroInit(); Service::enable(RomFS); @@ -51,49 +49,30 @@ Game::~Game() } -void Game::console(Screen screen, Color color) -{ - if (m_consoleEnabled || m_consoleBasicEnabled) - return; - Console::initialize(); - m_consoleScreen = screen; - m_consoleEnabled = true; - m_console.setVisible(true); - m_console.create(screen); - m_console.setColor(color); -} - - -void Game::consoleBasic(Screen screen) -{ - if (m_consoleEnabled || m_consoleBasicEnabled) - return; - Console::initializeBasic(screen); - m_consoleScreen = screen; - m_consoleBasicEnabled = true; -} - - void Game::render() { - if (!m_consoleBasicEnabled || m_consoleScreen != TopScreen) { + Console& console = Console::getInstance(); + + if (!console.isEnabledBasic() || console.getScreen() != TopScreen) { C3D_FrameBegin(C3D_FRAME_SYNCDRAW); C3D_FrameDrawOn(windowTop.getCitroTarget()); + windowTop.setView(windowTop.getDefaultView()); renderTopScreen(windowTop); - if (m_consoleEnabled && m_consoleScreen == TopScreen) { + if (console.isEnabled() && console.getScreen() == TopScreen) { windowTop.setView(windowTop.getDefaultView()); - windowTop.draw(m_console); + windowTop.draw(Console::getInstance()); } C3D_FrameEnd(0); } - if (!m_consoleBasicEnabled || m_consoleScreen != BottomScreen) { + if (!console.isEnabledBasic() || console.getScreen() != BottomScreen) { C3D_FrameBegin(C3D_FRAME_SYNCDRAW); C3D_FrameDrawOn(windowBottom.getCitroTarget()); + windowBottom.setView(windowBottom.getDefaultView()); renderBottomScreen(windowBottom); - if (m_consoleEnabled && m_consoleScreen == BottomScreen) { + if (console.isEnabled() && console.getScreen() == BottomScreen) { windowBottom.setView(windowBottom.getDefaultView()); - windowBottom.draw(m_console); + windowBottom.draw(Console::getInstance()); } C3D_FrameEnd(0); } @@ -113,6 +92,8 @@ void Game::run() Clock clock; Time deltaTime; + Console& console = Console::getInstance(); + // Hook for clock aptHook(&apt_hook_cookie, apt_clock_hook, &clock); @@ -122,8 +103,8 @@ void Game::run() Sensor::update(); while (eventmanager.pollEvent(event)) { - if (m_consoleEnabled) { - if (!m_console.processEvent(event)) + if (console.isEnabled()) { + if (!console.processEvent(event)) continue; } processEvent(event); @@ -133,8 +114,8 @@ void Game::run() if (m_triggerExit) break; - if (m_consoleEnabled) - m_console.update(deltaTime.asSeconds()); + if (console.isEnabled()) + console.update(deltaTime.asSeconds()); update(deltaTime.asSeconds()); render(); diff --git a/src/emu3ds/Window/Game.cpp b/src/emu3ds/Window/Game.cpp index bd10514..d687764 100644 --- a/src/emu3ds/Window/Game.cpp +++ b/src/emu3ds/Window/Game.cpp @@ -11,7 +11,6 @@ namespace cpp3ds { Game::Game() : m_triggerExit(false) -, m_consoleEnabled(false) { priv::ensureExtensionsInit(); @@ -32,12 +31,6 @@ Game::~Game() } -void Game::console(Screen screen, Color color) -{ - // -} - - void Game::exit() { m_triggerExit = true;