CCDevice-win32.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 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 = nullptr, int nSize = 0, bool enableBold = false)
  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. if (enableBold)
  132. {
  133. tNewFont.lfWeight = FW_BOLD;
  134. }
  135. else
  136. {
  137. tNewFont.lfWeight = FW_NORMAL;
  138. }
  139. GetObjectA(_font, sizeof(tOldFont), &tOldFont);
  140. if (tOldFont.lfHeight == tNewFont.lfHeight
  141. && tOldFont.lfWeight == tNewFont.lfWeight
  142. && 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
  143. {
  144. bRet = true;
  145. break;
  146. }
  147. // delete old font
  148. removeCustomFont();
  149. if (fontPath.size() > 0)
  150. {
  151. _curFontPath = fontPath;
  152. wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
  153. if (pwszBuffer)
  154. {
  155. if (AddFontResource(pwszBuffer))
  156. {
  157. SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
  158. }
  159. delete[] pwszBuffer;
  160. pwszBuffer = nullptr;
  161. }
  162. }
  163. _font = nullptr;
  164. // disable Cleartype
  165. tNewFont.lfQuality = ANTIALIASED_QUALITY;
  166. // create new font
  167. _font = CreateFontIndirectA(&tNewFont);
  168. if (!_font)
  169. {
  170. // create failed, use default font
  171. _font = hDefFont;
  172. break;
  173. }
  174. bRet = true;
  175. } while (0);
  176. return bRet;
  177. }
  178. SIZE sizeWithText(const wchar_t * pszText,
  179. int nLen,
  180. DWORD dwFmt,
  181. const char* fontName,
  182. int textSize,
  183. LONG nWidthLimit,
  184. LONG nHeightLimit,
  185. bool enableWrap,
  186. int overflow)
  187. {
  188. SIZE tRet = { 0 };
  189. do
  190. {
  191. CC_BREAK_IF(!pszText || nLen <= 0);
  192. RECT rc = { 0, 0, 0, 0 };
  193. DWORD dwCalcFmt = DT_CALCRECT;
  194. if (!enableWrap)
  195. {
  196. dwCalcFmt |= DT_SINGLELINE;
  197. }
  198. if (nWidthLimit > 0)
  199. {
  200. rc.right = nWidthLimit;
  201. dwCalcFmt |= DT_WORDBREAK
  202. | (dwFmt & DT_CENTER)
  203. | (dwFmt & DT_RIGHT);
  204. }
  205. if (overflow == 2)
  206. {
  207. LONG actualWidth = nWidthLimit + 1;
  208. LONG actualHeight = nHeightLimit + 1;
  209. int newFontSize = textSize + 1;
  210. while (actualWidth > nWidthLimit || actualHeight > nHeightLimit)
  211. {
  212. if (newFontSize <= 0)
  213. {
  214. break;
  215. }
  216. this->setFont(fontName, newFontSize);
  217. // use current font to measure text extent
  218. HGDIOBJ hOld = SelectObject(_DC, _font);
  219. rc.right = nWidthLimit;
  220. // measure text size
  221. DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
  222. SelectObject(_DC, hOld);
  223. actualWidth = rc.right;
  224. actualHeight = rc.bottom;
  225. newFontSize = newFontSize - 1;
  226. }
  227. }
  228. else
  229. {
  230. // use current font to measure text extent
  231. HGDIOBJ hOld = SelectObject(_DC, _font);
  232. // measure text size
  233. DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
  234. SelectObject(_DC, hOld);
  235. }
  236. tRet.cx = rc.right;
  237. tRet.cy = rc.bottom;
  238. } while (0);
  239. return tRet;
  240. }
  241. bool prepareBitmap(int nWidth, int nHeight)
  242. {
  243. // release bitmap
  244. if (_bmp)
  245. {
  246. DeleteObject(_bmp);
  247. _bmp = nullptr;
  248. }
  249. if (nWidth > 0 && nHeight > 0)
  250. {
  251. _bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
  252. if (!_bmp)
  253. {
  254. return false;
  255. }
  256. }
  257. return true;
  258. }
  259. int drawText(const char * pszText, SIZE& tSize, Device::TextAlign eAlign, const char * fontName, int textSize,
  260. bool enableWrap, int overflow)
  261. {
  262. int nRet = 0;
  263. wchar_t * pwszBuffer = nullptr;
  264. wchar_t* fixedText = nullptr;
  265. do
  266. {
  267. CC_BREAK_IF(!pszText);
  268. DWORD dwFmt = DT_WORDBREAK;
  269. if (!enableWrap) {
  270. dwFmt |= DT_SINGLELINE;
  271. }
  272. DWORD dwHoriFlag = (int)eAlign & 0x0f;
  273. DWORD dwVertFlag = ((int)eAlign & 0xf0) >> 4;
  274. switch (dwHoriFlag)
  275. {
  276. case 1: // left
  277. dwFmt |= DT_LEFT;
  278. break;
  279. case 2: // right
  280. dwFmt |= DT_RIGHT;
  281. break;
  282. case 3: // center
  283. dwFmt |= DT_CENTER;
  284. break;
  285. }
  286. int nLen = strlen(pszText);
  287. // utf-8 to utf-16
  288. int nBufLen = nLen + 1;
  289. pwszBuffer = new wchar_t[nBufLen];
  290. CC_BREAK_IF(!pwszBuffer);
  291. memset(pwszBuffer, 0, sizeof(wchar_t)*nBufLen);
  292. nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
  293. if (strchr(pszText, '&'))
  294. {
  295. fixedText = new wchar_t[nLen * 2 + 1];
  296. int fixedIndex = 0;
  297. for (int index = 0; index < nLen; ++index)
  298. {
  299. if (pwszBuffer[index] == '&')
  300. {
  301. fixedText[fixedIndex] = '&';
  302. fixedText[fixedIndex + 1] = '&';
  303. fixedIndex += 2;
  304. }
  305. else
  306. {
  307. fixedText[fixedIndex] = pwszBuffer[index];
  308. fixedIndex += 1;
  309. }
  310. }
  311. fixedText[fixedIndex] = '\0';
  312. nLen = fixedIndex;
  313. }
  314. SIZE newSize;
  315. if (fixedText)
  316. {
  317. newSize = sizeWithText(fixedText, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
  318. }
  319. else
  320. {
  321. newSize = sizeWithText(pwszBuffer, nLen, dwFmt, fontName, textSize, tSize.cx, tSize.cy, enableWrap, overflow);
  322. }
  323. RECT rcText = { 0 };
  324. // if content width is 0, use text size as content size
  325. if (tSize.cx <= 0)
  326. {
  327. tSize = newSize;
  328. rcText.right = newSize.cx;
  329. rcText.bottom = newSize.cy;
  330. }
  331. else
  332. {
  333. LONG offsetX = 0;
  334. LONG offsetY = 0;
  335. rcText.right = newSize.cx; // store the text width to rectangle
  336. // calculate text horizontal offset
  337. if (1 != dwHoriFlag // and text isn't align to left
  338. && newSize.cx < tSize.cx) // and text's width less then content width,
  339. { // then need adjust offset of X.
  340. offsetX = (2 == dwHoriFlag) ? tSize.cx - newSize.cx // align to right
  341. : (tSize.cx - newSize.cx) / 2; // align to center
  342. }
  343. // if content height is 0, use text height as content height
  344. // else if content height less than text height, use content height to draw text
  345. if (tSize.cy <= 0)
  346. {
  347. tSize.cy = newSize.cy;
  348. dwFmt |= DT_NOCLIP;
  349. rcText.bottom = newSize.cy; // store the text height to rectangle
  350. }
  351. else if (tSize.cy < newSize.cy)
  352. {
  353. // content height larger than text height need, clip text to rect
  354. rcText.bottom = tSize.cy;
  355. }
  356. else
  357. {
  358. rcText.bottom = newSize.cy; // store the text height to rectangle
  359. // content larger than text, need adjust vertical position
  360. dwFmt |= DT_NOCLIP;
  361. // calculate text vertical offset
  362. offsetY = (2 == dwVertFlag) ? tSize.cy - newSize.cy // align to bottom
  363. : (3 == dwVertFlag) ? (tSize.cy - newSize.cy) / 2 // align to middle
  364. : 0; // align to top
  365. }
  366. if (offsetX || offsetY)
  367. {
  368. OffsetRect(&rcText, offsetX, offsetY);
  369. }
  370. }
  371. CC_BREAK_IF(!prepareBitmap(tSize.cx, tSize.cy));
  372. // draw text
  373. HGDIOBJ hOldFont = SelectObject(_DC, _font);
  374. HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
  375. SetBkMode(_DC, TRANSPARENT);
  376. SetTextColor(_DC, RGB(255, 255, 255)); // white color
  377. // draw text
  378. if (fixedText)
  379. {
  380. nRet = DrawTextW(_DC, fixedText, nLen, &rcText, dwFmt);
  381. }
  382. else
  383. {
  384. nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
  385. }
  386. SelectObject(_DC, hOldBmp);
  387. SelectObject(_DC, hOldFont);
  388. } while (0);
  389. CC_SAFE_DELETE_ARRAY(pwszBuffer);
  390. delete[] fixedText;
  391. return nRet;
  392. }
  393. CC_SYNTHESIZE_READONLY(HDC, _DC, DC);
  394. CC_SYNTHESIZE_READONLY(HBITMAP, _bmp, Bitmap);
  395. private:
  396. friend class Image;
  397. HFONT _font;
  398. HWND _wnd;
  399. std::string _curFontPath;
  400. void removeCustomFont()
  401. {
  402. HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  403. if (hDefFont != _font)
  404. {
  405. DeleteObject(_font);
  406. _font = hDefFont;
  407. }
  408. // release temp font resource
  409. if (_curFontPath.size() > 0)
  410. {
  411. wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
  412. if (pwszBuffer)
  413. {
  414. RemoveFontResource(pwszBuffer);
  415. SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
  416. delete[] pwszBuffer;
  417. pwszBuffer = nullptr;
  418. }
  419. _curFontPath.clear();
  420. }
  421. }
  422. };
  423. static BitmapDC& sharedBitmapDC()
  424. {
  425. static BitmapDC s_BmpDC;
  426. return s_BmpDC;
  427. }
  428. Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
  429. {
  430. Data ret;
  431. do
  432. {
  433. BitmapDC& dc = sharedBitmapDC();
  434. if (!dc.setFont(textDefinition._fontName.c_str(), textDefinition._fontSize,false))
  435. {
  436. log("Can't found font(%s), use system default", textDefinition._fontName.c_str());
  437. }
  438. // draw text
  439. // does changing to SIZE here affects the font size by rounding from float?
  440. SIZE size = { (LONG)textDefinition._dimensions.width,(LONG)textDefinition._dimensions.height };
  441. CC_BREAK_IF(!dc.drawText(text, size, align, textDefinition._fontName.c_str(), textDefinition._fontSize, textDefinition._enableWrap, textDefinition._overflow));
  442. int dataLen = size.cx * size.cy * 4;
  443. unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen);
  444. CC_BREAK_IF(!dataBuf);
  445. struct
  446. {
  447. BITMAPINFOHEADER bmiHeader;
  448. int mask[4];
  449. } bi = { 0 };
  450. bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
  451. CC_BREAK_IF(!GetDIBits(dc.getDC(), dc.getBitmap(), 0, 0,
  452. nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
  453. width = (short)size.cx;
  454. height = (short)size.cy;
  455. // copy pixel data
  456. bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0)
  457. ? -bi.bmiHeader.biHeight : bi.bmiHeader.biHeight;
  458. GetDIBits(dc.getDC(), dc.getBitmap(), 0, height, dataBuf,
  459. (LPBITMAPINFO)&bi, DIB_RGB_COLORS);
  460. COLORREF textColor = (textDefinition._fontFillColor.b << 16 | textDefinition._fontFillColor.g << 8 | textDefinition._fontFillColor.r) & 0x00ffffff;
  461. float alpha = textDefinition._fontAlpha / 255.0f;
  462. COLORREF * pPixel = nullptr;
  463. for (int y = 0; y < height; ++y)
  464. {
  465. pPixel = (COLORREF *)dataBuf + y * width;
  466. for (int x = 0; x < width; ++x)
  467. {
  468. COLORREF& clr = *pPixel;
  469. clr = ((BYTE)(GetRValue(clr) * alpha) << 24) | textColor;
  470. ++pPixel;
  471. }
  472. }
  473. ret.fastSet(dataBuf, dataLen);
  474. hasPremultipliedAlpha = false;
  475. } while (0);
  476. return ret;
  477. }
  478. void Device::setKeepScreenOn(bool value)
  479. {
  480. CC_UNUSED_PARAM(value);
  481. }
  482. void Device::vibrate(float duration)
  483. {
  484. CC_UNUSED_PARAM(duration);
  485. }
  486. NS_CC_END
  487. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32