From 9303b57db1ad3bfe978296b765a1d0767d7a6f1c Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sat, 24 Aug 2013 02:13:54 +0200 Subject: [PATCH] Refactor VideoCommon/OnScreenDisplay. Use strings internally, use a multimap and std::function for callbacks (instead of a flat vector + loop over the vector to find the right callback type), fix coding style issues. Simplify MainAndroid code a bit. --- Source/Core/DolphinWX/Src/MainAndroid.cpp | 25 +--- .../Core/VideoCommon/Src/OnScreenDisplay.cpp | 115 ++++++------------ Source/Core/VideoCommon/Src/OnScreenDisplay.h | 13 +- 3 files changed, 47 insertions(+), 106 deletions(-) diff --git a/Source/Core/DolphinWX/Src/MainAndroid.cpp b/Source/Core/DolphinWX/Src/MainAndroid.cpp index 5fc616d5ac..d5ff986841 100644 --- a/Source/Core/DolphinWX/Src/MainAndroid.cpp +++ b/Source/Core/DolphinWX/Src/MainAndroid.cpp @@ -132,25 +132,6 @@ void Host_SysMessage(const char *fmt, ...) void Host_SetWiiMoteConnectionState(int _State) {} -void OSDCallbacks(u32 UserData) -{ - switch(UserData) - { - case 0: // Init - ButtonManager::Init(); - break; - case 1: // Draw - ButtonManager::DrawButtons(); - break; - case 2: // Shutdown - ButtonManager::Shutdown(); - break; - default: - WARN_LOG(COMMON, "Error, wrong OSD type"); - break; - } -} - #define DVD_BANNER_WIDTH 96 #define DVD_BANNER_HEIGHT 32 std::vector m_volume_names; @@ -321,8 +302,8 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Run(JNIEnv * { surf = ANativeWindow_fromSurface(env, _surf); // Install our callbacks - OSD::AddCallback(OSD::OSD_INIT, OSDCallbacks, 0); - OSD::AddCallback(OSD::OSD_SHUTDOWN, OSDCallbacks, 2); + OSD::AddCallback(OSD::OSD_INIT, ButtonManager::Init); + OSD::AddCallback(OSD::OSD_SHUTDOWN, ButtonManager::Shutdown); LogManager::Init(); SConfig::Init(); @@ -337,7 +318,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Run(JNIEnv * ini.Get("Android", "ScreenControls", &onscreencontrols, true); if (onscreencontrols) - OSD::AddCallback(OSD::OSD_ONFRAME, OSDCallbacks, 1); + OSD::AddCallback(OSD::OSD_ONFRAME, ButtonManager::DrawButtons); // No use running the loop when booting fails if ( BootManager::BootCore( g_filename.c_str() ) ) diff --git a/Source/Core/VideoCommon/Src/OnScreenDisplay.cpp b/Source/Core/VideoCommon/Src/OnScreenDisplay.cpp index 72abcd5752..b66c2019c9 100644 --- a/Source/Core/VideoCommon/Src/OnScreenDisplay.cpp +++ b/Source/Core/VideoCommon/Src/OnScreenDisplay.cpp @@ -11,56 +11,27 @@ #include "RenderBase.h" #include "Timer.h" -#include +#include +#include namespace OSD { -struct MESSAGE +struct Message { - MESSAGE() {} - MESSAGE(const char* p, u32 dw) - { - strncpy(str, p, 255); - str[255] = '\0'; - dwTimeStamp = dw; - } - char str[256]; - u32 dwTimeStamp; + Message() {} + Message(const std::string& s, u32 ts) : str(s), timestamp(ts) {} + + std::string str; + u32 timestamp; }; -class OSDCALLBACK -{ -private: - CallbackPtr m_functionptr; - CallbackType m_type; - u32 m_data; -public: - OSDCALLBACK(CallbackType OnType, CallbackPtr FuncPtr, u32 UserData) - { - m_type = OnType; - m_functionptr = FuncPtr; - m_data = UserData; - } - void Call() - { - m_functionptr(m_data); - } - - CallbackType Type() { return m_type; } -}; - -std::vector m_callbacks; -static std::list s_listMsgs; - -void AddMessage(const char* pstr, u32 ms) -{ - s_listMsgs.push_back(MESSAGE(pstr, Common::Timer::GetTimeMs() + ms)); -} +static std::multimap s_callbacks; +static std::list s_msgList; void AddMessage(const std::string& str, u32 ms) { - AddMessage(str.c_str(), ms); + s_msgList.push_back(Message(str, Common::Timer::GetTimeMs() + ms)); } void DrawMessages() @@ -68,58 +39,50 @@ void DrawMessages() if(!SConfig::GetInstance().m_LocalCoreStartupParameter.bOnScreenDisplayMessages) return; - if (s_listMsgs.size() > 0) + int left = 25, top = 15; + auto it = s_msgList.begin(); + while (it != s_msgList.end()) { - int left = 25, top = 15; - std::list::iterator it = s_listMsgs.begin(); - while (it != s_listMsgs.end()) + int time_left = (int)(it->timestamp - Common::Timer::GetTimeMs()); + u32 alpha = 255; + + if (time_left < 1024) { - int time_left = (int)(it->dwTimeStamp - Common::Timer::GetTimeMs()); - int alpha = 255; - - if (time_left < 1024) - { - alpha = time_left >> 2; - if (time_left < 0) - alpha = 0; - } - - alpha <<= 24; - - g_renderer->RenderText(it->str, left+1, top+1, 0x000000|alpha); - g_renderer->RenderText(it->str, left, top, 0xffff30|alpha); - top += 15; - - if (time_left <= 0) - it = s_listMsgs.erase(it); - else - ++it; + alpha = time_left >> 2; + if (time_left < 0) + alpha = 0; } + + alpha <<= 24; + + g_renderer->RenderText(it->str.c_str(), left + 1, top + 1, 0x000000 | alpha); + g_renderer->RenderText(it->str.c_str(), left, top, 0xffff30 | alpha); + top += 15; + + if (time_left <= 0) + it = s_msgList.erase(it); + else + ++it; } } void ClearMessages() { - std::list::iterator it = s_listMsgs.begin(); - - while (it != s_listMsgs.end()) - { - it = s_listMsgs.erase(it); - } + s_msgList.clear(); } // On-Screen Display Callbacks -void AddCallback(CallbackType OnType, CallbackPtr FuncPtr, u32 UserData) +void AddCallback(CallbackType type, Callback cb) { - m_callbacks.push_back(OSDCALLBACK(OnType, FuncPtr, UserData)); + s_callbacks.insert(std::pair(type, cb)); } -void DoCallbacks(CallbackType OnType) +void DoCallbacks(CallbackType type) { - for (auto it = m_callbacks.begin(); it != m_callbacks.end(); ++it) + auto it_bounds = s_callbacks.equal_range(type); + for (auto it = it_bounds.first; it != it_bounds.second; ++it) { - if (it->Type() == OnType) - it->Call(); + it->second(); } } diff --git a/Source/Core/VideoCommon/Src/OnScreenDisplay.h b/Source/Core/VideoCommon/Src/OnScreenDisplay.h index 6db53f0ead..a4a377b5d7 100644 --- a/Source/Core/VideoCommon/Src/OnScreenDisplay.h +++ b/Source/Core/VideoCommon/Src/OnScreenDisplay.h @@ -5,13 +5,12 @@ #ifndef _OSD_H_ #define _OSD_H_ +#include #include namespace OSD { - // On-screen message display -void AddMessage(const char* str, u32 ms = 2000); void AddMessage(const std::string& str, u32 ms = 2000); void DrawMessages(); // draw the current messages on the screen. Only call once per frame. void ClearMessages(); @@ -23,12 +22,10 @@ enum CallbackType OSD_ONFRAME, OSD_SHUTDOWN }; -typedef void(*CallbackPtr)(u32); +typedef std::function Callback; -void AddCallback(CallbackType OnType, CallbackPtr FuncPtr, u32 UserData); - -void DoCallbacks(CallbackType OnType); -} // namespace +void AddCallback(CallbackType type, Callback cb); +void DoCallbacks(CallbackType type); +} // namespace OSD #endif // _OSD_H_ -