BsWin32PlatformUtility.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPrerequisitesUtil.h"
  4. #include "Win32/BsWin32PlatformUtility.h"
  5. #include "BsColor.h"
  6. #include <windows.h>
  7. #include <iphlpapi.h>
  8. namespace BansheeEngine
  9. {
  10. void PlatformUtility::terminate(bool force)
  11. {
  12. if (!force)
  13. PostQuitMessage(0);
  14. else
  15. TerminateProcess(GetCurrentProcess(), 0);
  16. }
  17. double PlatformUtility::queryPerformanceTimerMs()
  18. {
  19. LARGE_INTEGER counterValue;
  20. QueryPerformanceCounter(&counterValue);
  21. LARGE_INTEGER counterFreq;
  22. QueryPerformanceFrequency(&counterFreq);
  23. return (double)counterValue.QuadPart / (counterFreq.QuadPart * 0.001);
  24. }
  25. void PlatformUtility::copyToClipboard(const WString& string)
  26. {
  27. HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (string.size() + 1) * sizeof(WString::value_type));
  28. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  29. string.copy(buffer, string.size());
  30. buffer[string.size()] = '\0';
  31. GlobalUnlock(hData);
  32. if (OpenClipboard(NULL))
  33. {
  34. EmptyClipboard();
  35. SetClipboardData(CF_UNICODETEXT, hData);
  36. CloseClipboard();
  37. }
  38. else
  39. {
  40. GlobalFree(hData);
  41. }
  42. }
  43. WString PlatformUtility::copyFromClipboard()
  44. {
  45. if (OpenClipboard(NULL))
  46. {
  47. HANDLE hData = GetClipboardData(CF_UNICODETEXT);
  48. if (hData != NULL)
  49. {
  50. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  51. WString string(buffer);
  52. GlobalUnlock(hData);
  53. CloseClipboard();
  54. return string;
  55. }
  56. else
  57. {
  58. CloseClipboard();
  59. return L"";
  60. }
  61. }
  62. return L"";
  63. }
  64. WString PlatformUtility::keyCodeToUnicode(UINT32 keyCode)
  65. {
  66. static HKL keyboardLayout = GetKeyboardLayout(0);
  67. static UINT8 keyboarState[256];
  68. if (GetKeyboardState(keyboarState) == FALSE)
  69. return 0;
  70. UINT virtualKey = MapVirtualKeyExW(keyCode, 1, keyboardLayout);
  71. wchar_t output[2];
  72. int count = ToUnicodeEx(virtualKey, keyCode, keyboarState, output, 2, 0, keyboardLayout);
  73. if (count > 0)
  74. return WString(output, count);
  75. return StringUtil::WBLANK;
  76. }
  77. bool PlatformUtility::getMACAddress(MACAddress& address)
  78. {
  79. std::memset(&address, 0, sizeof(address));
  80. PIP_ADAPTER_INFO adapterInfo = bs_alloc<IP_ADAPTER_INFO>();
  81. ULONG len = sizeof(IP_ADAPTER_INFO);
  82. DWORD rc = GetAdaptersInfo(adapterInfo, &len);
  83. if (rc == ERROR_BUFFER_OVERFLOW)
  84. {
  85. bs_free(adapterInfo);
  86. adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(bs_alloc(len));
  87. }
  88. else if (rc != ERROR_SUCCESS)
  89. {
  90. bs_free(adapterInfo);
  91. return false;
  92. }
  93. if (GetAdaptersInfo(adapterInfo, &len) == NO_ERROR)
  94. {
  95. PIP_ADAPTER_INFO curAdapter = nullptr;
  96. curAdapter = adapterInfo;
  97. while (curAdapter)
  98. {
  99. if (curAdapter->Type == MIB_IF_TYPE_ETHERNET && curAdapter->AddressLength == sizeof(address))
  100. {
  101. std::memcpy(&address, curAdapter->Address, curAdapter->AddressLength);
  102. return true;
  103. }
  104. curAdapter = curAdapter->Next;
  105. }
  106. }
  107. bs_free(adapterInfo);
  108. return false;
  109. }
  110. String PlatformUtility::generateUUID()
  111. {
  112. UUID uuid;
  113. UuidCreate(&uuid);
  114. UINT8* uuidStr;
  115. UuidToStringA(&uuid, &uuidStr);
  116. String output((char*)uuidStr);
  117. RpcStringFreeA(&uuidStr);
  118. return output;
  119. }
  120. void PlatformUtility::open(const Path& path)
  121. {
  122. ShellExecute(nullptr, "open", path.toString().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
  123. }
  124. HBITMAP Win32PlatformUtility::createBitmap(const Color* pixels, UINT32 width, UINT32 height, bool premultiplyAlpha)
  125. {
  126. BITMAPINFO bi;
  127. ZeroMemory(&bi, sizeof(BITMAPINFO));
  128. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  129. bi.bmiHeader.biWidth = width;
  130. bi.bmiHeader.biHeight = height;
  131. bi.bmiHeader.biPlanes = 1;
  132. bi.bmiHeader.biBitCount = 32;
  133. bi.bmiHeader.biCompression = BI_RGB;
  134. HDC hDC = GetDC(nullptr);
  135. void* data = nullptr;
  136. HBITMAP hBitmap = CreateDIBSection(hDC, &bi, DIB_RGB_COLORS, (void**)&data, nullptr, 0);
  137. HDC hBitmapDC = CreateCompatibleDC(hDC);
  138. ReleaseDC(nullptr, hDC);
  139. //Select the bitmaps to DC
  140. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap);
  141. //Scan each pixel of the source bitmap and create the masks
  142. Color pixel;
  143. DWORD *dst = (DWORD*)data;
  144. for (UINT32 y = 0; y < height; ++y)
  145. {
  146. for (UINT32 x = 0; x < width; ++x)
  147. {
  148. UINT32 revY = height - y - 1;
  149. pixel = pixels[revY * width + x];
  150. if (premultiplyAlpha)
  151. {
  152. pixel.r *= pixel.a;
  153. pixel.g *= pixel.a;
  154. pixel.b *= pixel.a;
  155. }
  156. *dst = pixel.getAsBGRA();
  157. dst++;
  158. }
  159. }
  160. SelectObject(hBitmapDC, hOldBitmap);
  161. DeleteDC(hBitmapDC);
  162. return hBitmap;
  163. }
  164. }