win32SplashScreen.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "platform/platform.h"
  25. #include "console/console.h"
  26. // from Torque.rc
  27. #define IDI_ICON1 103
  28. // Window Class name
  29. static const TCHAR* c_szSplashClass = L"Torque3DSplashWindow";
  30. static HWND gSplashWndOwner = NULL;
  31. static HWND gSplashWnd = NULL;
  32. static HBITMAP gSplashImage = NULL;
  33. // Registers a window class for the splash and splash owner windows.
  34. static void RegisterWindowClass(HINSTANCE hinst)
  35. {
  36. WNDCLASS wc = { 0 };
  37. wc.lpfnWndProc = DefWindowProc;
  38. wc.hInstance = hinst;
  39. wc.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_ICON1));
  40. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  41. wc.lpszClassName = c_szSplashClass;
  42. RegisterClass(&wc);
  43. }
  44. static void UnregisterSplashWindowClass(HINSTANCE hinst)
  45. {
  46. WNDCLASSEX classInfo;
  47. if (GetClassInfoEx(hinst,c_szSplashClass,&classInfo))
  48. UnregisterClass(c_szSplashClass,hinst);
  49. }
  50. // Creates the splash owner window and the splash window.
  51. static HWND CreateSplashWindow(HINSTANCE hinst)
  52. {
  53. RegisterWindowClass(hinst);
  54. gSplashWndOwner = CreateWindow(c_szSplashClass, NULL, WS_POPUP,
  55. 0, 0, 0, 0, NULL, NULL, hinst, NULL);
  56. return CreateWindowEx(WS_EX_LAYERED, c_szSplashClass, NULL, WS_POPUP | WS_VISIBLE,
  57. 0, 0, 0, 0, gSplashWndOwner, NULL, hinst, NULL);
  58. }
  59. // Calls UpdateLayeredWindow to set a bitmap (with alpha) as the content of the splash window.
  60. static void SetSplashImage(HWND hwndSplash, HBITMAP hbmpSplash)
  61. {
  62. // get the size of the bitmap
  63. BITMAP bm;
  64. GetObject(hbmpSplash, sizeof(bm), &bm);
  65. SIZE sizeSplash = { bm.bmWidth, bm.bmHeight };
  66. // get the primary monitor's info
  67. POINT ptZero = { 0 };
  68. HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  69. MONITORINFO monitorinfo = { 0 };
  70. monitorinfo.cbSize = sizeof(monitorinfo);
  71. GetMonitorInfo(hmonPrimary, &monitorinfo);
  72. // center the splash screen in the middle of the primary work area
  73. const RECT & rcWork = monitorinfo.rcWork;
  74. POINT ptOrigin;
  75. ptOrigin.x = rcWork.left + (rcWork.right - rcWork.left - sizeSplash.cx) / 2;
  76. ptOrigin.y = rcWork.top + (rcWork.bottom - rcWork.top - sizeSplash.cy) / 2;
  77. // create a memory DC holding the splash bitmap
  78. HDC hdcScreen = GetDC(NULL);
  79. HDC hdcMem = CreateCompatibleDC(hdcScreen);
  80. HBITMAP hbmpOld = (HBITMAP) SelectObject(hdcMem, hbmpSplash);
  81. // paint the window (in the right location) with the alpha-blended bitmap
  82. UpdateLayeredWindow(hwndSplash, hdcScreen, &ptOrigin, &sizeSplash,
  83. hdcMem, &ptZero, RGB(0, 0, 0), NULL, ULW_OPAQUE);
  84. // delete temporary objects
  85. SelectObject(hdcMem, hbmpOld);
  86. DeleteDC(hdcMem);
  87. ReleaseDC(NULL, hdcScreen);
  88. }
  89. void CloseSplashWindow(HINSTANCE hinst)
  90. {
  91. if (gSplashWndOwner)
  92. {
  93. //ShowWindow(gSplashWnd, 0);
  94. DestroyWindow(gSplashWndOwner);
  95. UnregisterSplashWindowClass(hinst);
  96. }
  97. gSplashWndOwner = NULL;
  98. gSplashWnd = NULL;
  99. }
  100. bool Platform::displaySplashWindow()
  101. {
  102. gSplashImage = (HBITMAP) ::LoadImage(0, L"art\\gui\\splash.bmp",
  103. IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  104. if (!gSplashImage)
  105. return false;
  106. gSplashWnd = CreateSplashWindow(GetModuleHandle(NULL));
  107. if (!gSplashWnd)
  108. return false;
  109. SetSplashImage(gSplashWnd, gSplashImage);
  110. return true;
  111. }