win32SplashScreen.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #define _WIN32_WINNT 0x0500
  23. #include <windows.h>
  24. #include <tchar.h>
  25. #include "platform/platform.h"
  26. #include "console/console.h"
  27. // from Torque.rc
  28. #define IDI_ICON1 103
  29. // Window Class name
  30. static const TCHAR* c_szSplashClass = L"Torque3DSplashWindow";
  31. static HWND gSplashWndOwner = NULL;
  32. static HWND gSplashWnd = NULL;
  33. static HBITMAP gSplashImage = NULL;
  34. // Registers a window class for the splash and splash owner windows.
  35. static void RegisterWindowClass(HINSTANCE hinst)
  36. {
  37. WNDCLASS wc = { 0 };
  38. wc.lpfnWndProc = DefWindowProc;
  39. wc.hInstance = hinst;
  40. wc.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_ICON1));
  41. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  42. wc.lpszClassName = c_szSplashClass;
  43. RegisterClass(&wc);
  44. }
  45. static void UnregisterSplashWindowClass(HINSTANCE hinst)
  46. {
  47. WNDCLASSEX classInfo;
  48. if (GetClassInfoEx(hinst,c_szSplashClass,&classInfo))
  49. UnregisterClass(c_szSplashClass,hinst);
  50. }
  51. // Creates the splash owner window and the splash window.
  52. static HWND CreateSplashWindow(HINSTANCE hinst)
  53. {
  54. RegisterWindowClass(hinst);
  55. gSplashWndOwner = CreateWindow(c_szSplashClass, NULL, WS_POPUP,
  56. 0, 0, 0, 0, NULL, NULL, hinst, NULL);
  57. return CreateWindowEx(WS_EX_LAYERED, c_szSplashClass, NULL, WS_POPUP | WS_VISIBLE,
  58. 0, 0, 0, 0, gSplashWndOwner, NULL, hinst, NULL);
  59. }
  60. // Calls UpdateLayeredWindow to set a bitmap (with alpha) as the content of the splash window.
  61. static void SetSplashImage(HWND hwndSplash, HBITMAP hbmpSplash)
  62. {
  63. // get the size of the bitmap
  64. BITMAP bm;
  65. GetObject(hbmpSplash, sizeof(bm), &bm);
  66. SIZE sizeSplash = { bm.bmWidth, bm.bmHeight };
  67. // get the primary monitor's info
  68. POINT ptZero = { 0 };
  69. HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  70. MONITORINFO monitorinfo = { 0 };
  71. monitorinfo.cbSize = sizeof(monitorinfo);
  72. GetMonitorInfo(hmonPrimary, &monitorinfo);
  73. // center the splash screen in the middle of the primary work area
  74. const RECT & rcWork = monitorinfo.rcWork;
  75. POINT ptOrigin;
  76. ptOrigin.x = rcWork.left + (rcWork.right - rcWork.left - sizeSplash.cx) / 2;
  77. ptOrigin.y = rcWork.top + (rcWork.bottom - rcWork.top - sizeSplash.cy) / 2;
  78. // create a memory DC holding the splash bitmap
  79. HDC hdcScreen = GetDC(NULL);
  80. HDC hdcMem = CreateCompatibleDC(hdcScreen);
  81. HBITMAP hbmpOld = (HBITMAP) SelectObject(hdcMem, hbmpSplash);
  82. // paint the window (in the right location) with the alpha-blended bitmap
  83. UpdateLayeredWindow(hwndSplash, hdcScreen, &ptOrigin, &sizeSplash,
  84. hdcMem, &ptZero, RGB(0, 0, 0), NULL, ULW_OPAQUE);
  85. // delete temporary objects
  86. SelectObject(hdcMem, hbmpOld);
  87. DeleteDC(hdcMem);
  88. ReleaseDC(NULL, hdcScreen);
  89. }
  90. void CloseSplashWindow(HINSTANCE hinst)
  91. {
  92. if (gSplashWndOwner)
  93. {
  94. //ShowWindow(gSplashWnd, 0);
  95. DestroyWindow(gSplashWndOwner);
  96. UnregisterSplashWindowClass(hinst);
  97. }
  98. gSplashWndOwner = NULL;
  99. gSplashWnd = NULL;
  100. }
  101. #ifndef TORQUE_SDL
  102. bool Platform::closeSplashWindow()
  103. {
  104. CloseSplashWindow(GetModuleHandle(NULL));
  105. return true;
  106. }
  107. bool Platform::displaySplashWindow( String path )
  108. {
  109. if(path.isEmpty())
  110. return false;
  111. #ifdef UNICODE
  112. const UTF16 *lFileName = path.utf16();
  113. #else
  114. const UTF8 *lFileName = path.c_str();
  115. #endif
  116. gSplashImage = (HBITMAP) ::LoadImage(0, lFileName,
  117. IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  118. if (!gSplashImage)
  119. return false;
  120. gSplashWnd = CreateSplashWindow(GetModuleHandle(NULL));
  121. if (!gSplashWnd)
  122. return false;
  123. SetSplashImage(gSplashWnd, gSplashImage);
  124. return true;
  125. }
  126. #endif