OnScreenDisplay.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2009 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <list>
  6. #include <map>
  7. #include <string>
  8. #include "Common/CommonTypes.h"
  9. #include "Common/Timer.h"
  10. #include "Core/ConfigManager.h"
  11. #include "VideoCommon/OnScreenDisplay.h"
  12. #include "VideoCommon/RenderBase.h"
  13. namespace OSD
  14. {
  15. struct Message
  16. {
  17. Message() {}
  18. Message(const std::string& s, u32 ts, u32 rgba) : m_str(s), m_timestamp(ts), m_rgba(rgba)
  19. {
  20. }
  21. std::string m_str;
  22. u32 m_timestamp;
  23. u32 m_rgba;
  24. };
  25. static std::multimap<CallbackType, Callback> s_callbacks;
  26. static std::list<Message> s_msgList;
  27. void AddMessage(const std::string& str, u32 ms, u32 rgba)
  28. {
  29. s_msgList.emplace_back(str, Common::Timer::GetTimeMs() + ms, rgba);
  30. }
  31. void DrawMessages()
  32. {
  33. if (!SConfig::GetInstance().m_LocalCoreStartupParameter.bOnScreenDisplayMessages)
  34. return;
  35. int left = 25, top = 15;
  36. auto it = s_msgList.begin();
  37. while (it != s_msgList.end())
  38. {
  39. int time_left = (int)(it->m_timestamp - Common::Timer::GetTimeMs());
  40. float alpha = std::max(1.0f, std::min(0.0f, time_left / 1024.0f));
  41. u32 color = (it->m_rgba & 0xFFFFFF) | ((u32)((it->m_rgba >> 24) * alpha) << 24);
  42. g_renderer->RenderText(it->m_str, left, top, color);
  43. top += 15;
  44. if (time_left <= 0)
  45. it = s_msgList.erase(it);
  46. else
  47. ++it;
  48. }
  49. }
  50. void ClearMessages()
  51. {
  52. s_msgList.clear();
  53. }
  54. // On-Screen Display Callbacks
  55. void AddCallback(CallbackType type, Callback cb)
  56. {
  57. s_callbacks.insert(std::pair<CallbackType, Callback>(type, cb));
  58. }
  59. void DoCallbacks(CallbackType type)
  60. {
  61. auto it_bounds = s_callbacks.equal_range(type);
  62. for (auto it = it_bounds.first; it != it_bounds.second; ++it)
  63. {
  64. it->second();
  65. }
  66. // Wipe all callbacks on shutdown
  67. if (type == OSD_SHUTDOWN)
  68. s_callbacks.clear();
  69. }
  70. } // namespace