CCDevice-win32.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
  23. #include "platform/CCDevice.h"
  24. #include "platform/CCFileUtils.h"
  25. #include "platform/CCStdC.h"
  26. NS_CC_BEGIN
  27. int Device::getDPI()
  28. {
  29. static int dpi = -1;
  30. if (dpi == -1)
  31. {
  32. HDC hScreenDC = GetDC( nullptr );
  33. int PixelsX = GetDeviceCaps( hScreenDC, HORZRES );
  34. int MMX = GetDeviceCaps( hScreenDC, HORZSIZE );
  35. ReleaseDC( nullptr, hScreenDC );
  36. dpi = 254.0f*PixelsX/MMX/10;
  37. }
  38. return dpi;
  39. }
  40. void Device::setAccelerometerEnabled(bool isEnabled)
  41. {}
  42. void Device::setAccelerometerInterval(float interval)
  43. {}
  44. class BitmapDC
  45. {
  46. public:
  47. BitmapDC(HWND hWnd = nullptr)
  48. : _DC(nullptr)
  49. , _bmp(nullptr)
  50. , _font((HFONT)GetStockObject(DEFAULT_GUI_FONT))
  51. , _wnd(nullptr)
  52. {
  53. _wnd = hWnd;
  54. HDC hdc = GetDC(hWnd);
  55. _DC = CreateCompatibleDC(hdc);
  56. ReleaseDC(hWnd, hdc);
  57. }
  58. ~BitmapDC()
  59. {
  60. prepareBitmap(0, 0);
  61. if (_DC)
  62. {
  63. DeleteDC(_DC);
  64. }
  65. removeCustomFont();
  66. }
  67. wchar_t * utf8ToUtf16(const std::string& str)
  68. {
  69. wchar_t * pwszBuffer = nullptr;
  70. do
  71. {
  72. if (str.empty())
  73. {
  74. break;
  75. }
  76. // utf-8 to utf-16
  77. int nLen = str.size();
  78. int nBufLen = nLen + 1;
  79. pwszBuffer = new wchar_t[nBufLen];
  80. CC_BREAK_IF(! pwszBuffer);
  81. memset(pwszBuffer,0,nBufLen);
  82. nLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), nLen, pwszBuffer, nBufLen);
  83. pwszBuffer[nLen] = '\0';
  84. } while (0);
  85. return pwszBuffer;
  86. }
  87. bool setFont(const char * pFontName = "", int nSize = 0)
  88. {
  89. bool bRet = false;
  90. do
  91. {
  92. std::string fontName = pFontName;
  93. std::string fontPath;
  94. HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  95. LOGFONTA tNewFont = {0};
  96. LOGFONTA tOldFont = {0};
  97. GetObjectA(hDefFont, sizeof(tNewFont), &tNewFont);
  98. if (!fontName.empty())
  99. {
  100. // create font from ttf file
  101. if (FileUtils::getInstance()->getFileExtension(fontName) == ".ttf")
  102. {
  103. fontPath = FileUtils::getInstance()->fullPathForFilename(fontName.c_str());
  104. int nFindPos = fontName.rfind("/");
  105. fontName = &fontName[nFindPos+1];
  106. nFindPos = fontName.rfind(".");
  107. fontName = fontName.substr(0,nFindPos);
  108. }
  109. else
  110. {
  111. auto nFindPos = fontName.rfind("/");
  112. if (nFindPos != fontName.npos)
  113. {
  114. if (fontName.length() == nFindPos + 1)
  115. {
  116. fontName = "";
  117. }
  118. else
  119. {
  120. fontName = &fontName[nFindPos+1];
  121. }
  122. }
  123. }
  124. tNewFont.lfCharSet = DEFAULT_CHARSET;
  125. strcpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str());
  126. }
  127. if (nSize)
  128. {
  129. tNewFont.lfHeight = -nSize;
  130. }
  131. GetObjectA(_font, sizeof(tOldFont), &tOldFont);
  132. if (tOldFont.lfHeight == tNewFont.lfHeight
  133. && 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
  134. {
  135. // already has the font
  136. bRet = true;
  137. break;
  138. }
  139. // delete old font
  140. removeCustomFont();
  141. if (fontPath.size() > 0)
  142. {
  143. _curFontPath = fontPath;
  144. wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
  145. if (pwszBuffer)
  146. {
  147. if(AddFontResource(pwszBuffer))
  148. {
  149. SendMessage( _wnd, WM_FONTCHANGE, 0, 0);
  150. }
  151. delete [] pwszBuffer;
  152. pwszBuffer = nullptr;
  153. }
  154. }
  155. _font = nullptr;
  156. // disable Cleartype
  157. tNewFont.lfQuality = ANTIALIASED_QUALITY;
  158. // create new font
  159. _font = CreateFontIndirectA(&tNewFont);
  160. if (! _font)
  161. {
  162. // create failed, use default font
  163. _font = hDefFont;
  164. break;
  165. }
  166. bRet = true;
  167. } while (0);
  168. return bRet;
  169. }
  170. SIZE sizeWithText(const wchar_t * pszText, int nLen, DWORD dwFmt, LONG nWidthLimit)
  171. {
  172. SIZE tRet = {0};
  173. do
  174. {
  175. CC_BREAK_IF(! pszText || nLen <= 0);
  176. RECT rc = {0, 0, 0, 0};
  177. DWORD dwCalcFmt = DT_CALCRECT;
  178. if (nWidthLimit > 0)
  179. {
  180. rc.right = nWidthLimit;
  181. dwCalcFmt |= DT_WORDBREAK
  182. | (dwFmt & DT_CENTER)
  183. | (dwFmt & DT_RIGHT);
  184. }
  185. // use current font to measure text extent
  186. HGDIOBJ hOld = SelectObject(_DC, _font);
  187. // measure text size
  188. DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
  189. SelectObject(_DC, hOld);
  190. tRet.cx = rc.right;
  191. tRet.cy = rc.bottom;
  192. } while (0);
  193. return tRet;
  194. }
  195. bool prepareBitmap(int nWidth, int nHeight)
  196. {
  197. // release bitmap
  198. if (_bmp)
  199. {
  200. DeleteObject(_bmp);
  201. _bmp = nullptr;
  202. }
  203. if (nWidth > 0 && nHeight > 0)
  204. {
  205. _bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
  206. if (! _bmp)
  207. {
  208. return false;
  209. }
  210. }
  211. return true;
  212. }
  213. int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign)
  214. {
  215. int nRet = 0;
  216. wchar_t * pwszBuffer = nullptr;
  217. wchar_t* fixedText = nullptr;
  218. do
  219. {
  220. CC_BREAK_IF(! pszText);
  221. DWORD dwFmt = DT_WORDBREAK;
  222. DWORD dwHoriFlag = (int)eAlign & 0x0f;
  223. DWORD dwVertFlag = ((int)eAlign & 0xf0) >> 4;
  224. switch (dwHoriFlag)
  225. {
  226. case 1: // left
  227. dwFmt |= DT_LEFT;
  228. break;
  229. case 2: // right
  230. dwFmt |= DT_RIGHT;
  231. break;
  232. case 3: // center
  233. dwFmt |= DT_CENTER;
  234. break;
  235. }
  236. int nLen = strlen(pszText);
  237. // utf-8 to utf-16
  238. int nBufLen = nLen + 1;
  239. pwszBuffer = new wchar_t[nBufLen];
  240. CC_BREAK_IF(! pwszBuffer);
  241. memset(pwszBuffer, 0, sizeof(wchar_t)*nBufLen);
  242. nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
  243. if (strchr(pszText, '&'))
  244. {
  245. fixedText = new wchar_t[nLen * 2 + 1];
  246. int fixedIndex = 0;
  247. for (int index = 0; index < nLen; ++index)
  248. {
  249. if (pwszBuffer[index] == '&')
  250. {
  251. fixedText[fixedIndex] = '&';
  252. fixedText[fixedIndex + 1] = '&';
  253. fixedIndex += 2;
  254. }
  255. else
  256. {
  257. fixedText[fixedIndex] = pwszBuffer[index];
  258. fixedIndex += 1;
  259. }
  260. }
  261. fixedText[fixedIndex] = '\0';
  262. nLen = fixedIndex;
  263. }
  264. SIZE newSize;
  265. if (fixedText)
  266. {
  267. newSize = sizeWithText(fixedText, nLen, dwFmt, tSize.cx);
  268. }
  269. else
  270. {
  271. newSize = sizeWithText(pwszBuffer, nLen, dwFmt, tSize.cx);
  272. }
  273. RECT rcText = {0};
  274. // if content width is 0, use text size as content size
  275. if (tSize.cx <= 0)
  276. {
  277. tSize = newSize;
  278. rcText.right = newSize.cx;
  279. rcText.bottom = newSize.cy;
  280. }
  281. else
  282. {
  283. LONG offsetX = 0;
  284. LONG offsetY = 0;
  285. rcText.right = newSize.cx; // store the text width to rectangle
  286. // calculate text horizontal offset
  287. if (1 != dwHoriFlag // and text isn't align to left
  288. && newSize.cx < tSize.cx) // and text's width less then content width,
  289. { // then need adjust offset of X.
  290. offsetX = (2 == dwHoriFlag) ? tSize.cx - newSize.cx // align to right
  291. : (tSize.cx - newSize.cx) / 2; // align to center
  292. }
  293. // if content height is 0, use text height as content height
  294. // else if content height less than text height, use content height to draw text
  295. if (tSize.cy <= 0)
  296. {
  297. tSize.cy = newSize.cy;
  298. dwFmt |= DT_NOCLIP;
  299. rcText.bottom = newSize.cy; // store the text height to rectangle
  300. }
  301. else if (tSize.cy < newSize.cy)
  302. {
  303. // content height larger than text height need, clip text to rect
  304. rcText.bottom = tSize.cy;
  305. }
  306. else
  307. {
  308. rcText.bottom = newSize.cy; // store the text height to rectangle
  309. // content larger than text, need adjust vertical position
  310. dwFmt |= DT_NOCLIP;
  311. // calculate text vertical offset
  312. offsetY = (2 == dwVertFlag) ? tSize.cy - newSize.cy // align to bottom
  313. : (3 == dwVertFlag) ? (tSize.cy - newSize.cy) / 2 // align to middle
  314. : 0; // align to top
  315. }
  316. if (offsetX || offsetY)
  317. {
  318. OffsetRect(&rcText, offsetX, offsetY);
  319. }
  320. }
  321. CC_BREAK_IF(! prepareBitmap(tSize.cx, tSize.cy));
  322. // draw text
  323. HGDIOBJ hOldFont = SelectObject(_DC, _font);
  324. HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
  325. SetBkMode(_DC, TRANSPARENT);
  326. SetTextColor(_DC, RGB(255, 255, 255)); // white color
  327. // draw text
  328. if (fixedText)
  329. {
  330. nRet = DrawTextW(_DC, fixedText, nLen, &rcText, dwFmt);
  331. }
  332. else
  333. {
  334. nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
  335. }
  336. //DrawTextA(_DC, pszText, nLen, &rcText, dwFmt);
  337. SelectObject(_DC, hOldBmp);
  338. SelectObject(_DC, hOldFont);
  339. } while (0);
  340. CC_SAFE_DELETE_ARRAY(pwszBuffer);
  341. delete[] fixedText;
  342. return nRet;
  343. }
  344. CC_SYNTHESIZE_READONLY(HDC, _DC, DC);
  345. CC_SYNTHESIZE_READONLY(HBITMAP, _bmp, Bitmap);
  346. private:
  347. friend class Image;
  348. HFONT _font;
  349. HWND _wnd;
  350. std::string _curFontPath;
  351. void removeCustomFont()
  352. {
  353. HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  354. if (hDefFont != _font)
  355. {
  356. DeleteObject(_font);
  357. _font = hDefFont;
  358. }
  359. // release temp font resource
  360. if (_curFontPath.size() > 0)
  361. {
  362. wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
  363. if (pwszBuffer)
  364. {
  365. RemoveFontResource(pwszBuffer);
  366. SendMessage( _wnd, WM_FONTCHANGE, 0, 0);
  367. delete [] pwszBuffer;
  368. pwszBuffer = nullptr;
  369. }
  370. _curFontPath.clear();
  371. }
  372. }
  373. };
  374. static BitmapDC& sharedBitmapDC()
  375. {
  376. static BitmapDC s_BmpDC;
  377. return s_BmpDC;
  378. }
  379. Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
  380. {
  381. Data ret;
  382. do
  383. {
  384. BitmapDC& dc = sharedBitmapDC();
  385. if (! dc.setFont(textDefinition._fontName.c_str(), textDefinition._fontSize))
  386. {
  387. log("Can't found font(%s), use system default", textDefinition._fontName.c_str());
  388. }
  389. // draw text
  390. // does changing to SIZE here affects the font size by rounding from float?
  391. SIZE size = {(LONG) textDefinition._dimensions.width,(LONG) textDefinition._dimensions.height};
  392. CC_BREAK_IF(! dc.drawText(text, size, align));
  393. int dataLen = size.cx * size.cy * 4;
  394. unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen);
  395. CC_BREAK_IF(! dataBuf);
  396. struct
  397. {
  398. BITMAPINFOHEADER bmiHeader;
  399. int mask[4];
  400. } bi = {0};
  401. bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
  402. CC_BREAK_IF(! GetDIBits(dc.getDC(), dc.getBitmap(), 0, 0,
  403. nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
  404. width = (short)size.cx;
  405. height = (short)size.cy;
  406. // copy pixel data
  407. bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0)
  408. ? - bi.bmiHeader.biHeight : bi.bmiHeader.biHeight;
  409. GetDIBits(dc.getDC(), dc.getBitmap(), 0, height, dataBuf,
  410. (LPBITMAPINFO)&bi, DIB_RGB_COLORS);
  411. COLORREF textColor = (textDefinition._fontFillColor.b << 16 | textDefinition._fontFillColor.g << 8 | textDefinition._fontFillColor.r) & 0x00ffffff;
  412. float alpha = textDefinition._fontAlpha / 255.0f;
  413. COLORREF * pPixel = nullptr;
  414. for (int y = 0; y < height; ++y)
  415. {
  416. pPixel = (COLORREF *)dataBuf + y * width;
  417. for (int x = 0; x < width; ++x)
  418. {
  419. COLORREF& clr = *pPixel;
  420. clr = ((BYTE)(GetRValue(clr) * alpha) << 24) | textColor;
  421. ++pPixel;
  422. }
  423. }
  424. ret.fastSet(dataBuf,dataLen);
  425. hasPremultipliedAlpha = false;
  426. } while (0);
  427. return ret;
  428. }
  429. void Device::setKeepScreenOn(bool /*value*/)
  430. {
  431. }
  432. void Device::vibrate(float /*duration*/)
  433. {
  434. }
  435. NS_CC_END
  436. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32