2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-04-25 16:18:04 +08:00
|
|
|
#define __CC_PLATFORM_IMAGE_CPP__
|
2013-12-05 11:53:25 +08:00
|
|
|
#if _MSC_VER
|
|
|
|
#include <cctype>
|
|
|
|
#endif
|
2012-06-19 17:22:55 +08:00
|
|
|
#include "platform/CCImageCommon_cpp.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief A memory DC which uses to draw text on bitmap.
|
|
|
|
*/
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BitmapDC(HWND hWnd = NULL)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _DC(NULL)
|
|
|
|
, _bmp(NULL)
|
|
|
|
, _font((HFONT)GetStockObject(DEFAULT_GUI_FONT))
|
|
|
|
, _wnd(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_wnd = hWnd;
|
2012-04-19 14:35:52 +08:00
|
|
|
HDC hdc = GetDC(hWnd);
|
2013-06-15 14:03:30 +08:00
|
|
|
_DC = CreateCompatibleDC(hdc);
|
2012-04-19 14:35:52 +08:00
|
|
|
ReleaseDC(hWnd, hdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapDC()
|
|
|
|
{
|
|
|
|
prepareBitmap(0, 0);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_DC)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DeleteDC(_DC);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (hDefFont != _font)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DeleteObject(_font);
|
|
|
|
_font = hDefFont;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-06-01 13:44:28 +08:00
|
|
|
// release temp font resource
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_curFontPath.size() > 0)
|
2012-06-01 13:44:28 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
|
2012-06-01 13:44:28 +08:00
|
|
|
if (pwszBuffer)
|
|
|
|
{
|
|
|
|
RemoveFontResource(pwszBuffer);
|
2013-06-15 14:03:30 +08:00
|
|
|
SendMessage( _wnd, WM_FONTCHANGE, 0, 0);
|
2012-06-01 13:44:28 +08:00
|
|
|
delete [] pwszBuffer;
|
|
|
|
pwszBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-06-01 13:44:28 +08:00
|
|
|
wchar_t * utf8ToUtf16(std::string nString)
|
|
|
|
{
|
|
|
|
wchar_t * pwszBuffer = NULL;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (nString.size() < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// utf-8 to utf-16
|
|
|
|
int nLen = nString.size();
|
|
|
|
int nBufLen = nLen + 1;
|
|
|
|
pwszBuffer = new wchar_t[nBufLen];
|
|
|
|
CC_BREAK_IF(! pwszBuffer);
|
|
|
|
memset(pwszBuffer,0,nBufLen);
|
|
|
|
nLen = MultiByteToWideChar(CP_UTF8, 0, nString.c_str(), nLen, pwszBuffer, nBufLen);
|
|
|
|
pwszBuffer[nLen] = '\0';
|
|
|
|
} while (0);
|
|
|
|
return pwszBuffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool setFont(const char * pFontName = NULL, int nSize = 0)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::string fontName = pFontName;
|
|
|
|
std::string fontPath;
|
|
|
|
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
|
|
|
|
LOGFONTA tNewFont = {0};
|
|
|
|
LOGFONTA tOldFont = {0};
|
|
|
|
GetObjectA(hDefFont, sizeof(tNewFont), &tNewFont);
|
|
|
|
if (fontName.c_str())
|
|
|
|
{
|
|
|
|
// create font from ttf file
|
|
|
|
int nFindttf = fontName.find(".ttf");
|
|
|
|
int nFindTTF = fontName.find(".TTF");
|
|
|
|
if (nFindttf >= 0 || nFindTTF >= 0)
|
|
|
|
{
|
2013-07-12 12:03:39 +08:00
|
|
|
fontPath = FileUtils::getInstance()->fullPathForFilename(fontName.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
int nFindPos = fontName.rfind("/");
|
|
|
|
fontName = &fontName[nFindPos+1];
|
|
|
|
nFindPos = fontName.rfind(".");
|
|
|
|
fontName = fontName.substr(0,nFindPos);
|
|
|
|
}
|
|
|
|
tNewFont.lfCharSet = DEFAULT_CHARSET;
|
|
|
|
strcpy_s(tNewFont.lfFaceName, LF_FACESIZE, fontName.c_str());
|
|
|
|
}
|
|
|
|
if (nSize)
|
|
|
|
{
|
|
|
|
tNewFont.lfHeight = -nSize;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
GetObjectA(_font, sizeof(tOldFont), &tOldFont);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (tOldFont.lfHeight == tNewFont.lfHeight
|
2012-10-08 14:52:08 +08:00
|
|
|
&& 0 == strcmp(tOldFont.lfFaceName, tNewFont.lfFaceName))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// already has the font
|
|
|
|
bRet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete old font
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_font != hDefFont)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DeleteObject(_font);
|
2012-06-01 13:44:28 +08:00
|
|
|
// release old font register
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_curFontPath.size() > 0)
|
2012-06-01 13:44:28 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
|
2012-06-01 13:44:28 +08:00
|
|
|
if (pwszBuffer)
|
|
|
|
{
|
|
|
|
if(RemoveFontResource(pwszBuffer))
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
SendMessage( _wnd, WM_FONTCHANGE, 0, 0);
|
2012-06-01 13:44:28 +08:00
|
|
|
}
|
|
|
|
delete [] pwszBuffer;
|
|
|
|
pwszBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-09-18 17:15:05 +08:00
|
|
|
if (fontPath.size() > 0)
|
2013-06-15 14:03:30 +08:00
|
|
|
_curFontPath = fontPath;
|
2012-09-18 17:15:05 +08:00
|
|
|
else
|
2013-06-15 14:03:30 +08:00
|
|
|
_curFontPath.clear();
|
2012-06-01 13:44:28 +08:00
|
|
|
// register temp font
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_curFontPath.size() > 0)
|
2012-06-01 13:44:28 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
wchar_t * pwszBuffer = utf8ToUtf16(_curFontPath);
|
2012-06-01 13:44:28 +08:00
|
|
|
if (pwszBuffer)
|
|
|
|
{
|
|
|
|
if(AddFontResource(pwszBuffer))
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
SendMessage( _wnd, WM_FONTCHANGE, 0, 0);
|
2012-06-01 13:44:28 +08:00
|
|
|
}
|
|
|
|
delete [] pwszBuffer;
|
|
|
|
pwszBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_font = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-01 13:44:28 +08:00
|
|
|
// disable Cleartype
|
|
|
|
tNewFont.lfQuality = ANTIALIASED_QUALITY;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// create new font
|
2013-06-15 14:03:30 +08:00
|
|
|
_font = CreateFontIndirectA(&tNewFont);
|
|
|
|
if (! _font)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// create failed, use default font
|
2013-06-15 14:03:30 +08:00
|
|
|
_font = hDefFont;
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:27:32 +08:00
|
|
|
SIZE sizeWithText(const wchar_t * pszText, int nLen, DWORD dwFmt, LONG nWidthLimit)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
SIZE tRet = {0};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! pszText || nLen <= 0);
|
|
|
|
|
|
|
|
RECT rc = {0, 0, 0, 0};
|
|
|
|
DWORD dwCalcFmt = DT_CALCRECT;
|
|
|
|
|
|
|
|
if (nWidthLimit > 0)
|
|
|
|
{
|
|
|
|
rc.right = nWidthLimit;
|
|
|
|
dwCalcFmt |= DT_WORDBREAK
|
|
|
|
| (dwFmt & DT_CENTER)
|
|
|
|
| (dwFmt & DT_RIGHT);
|
|
|
|
}
|
|
|
|
// use current font to measure text extent
|
2013-06-15 14:03:30 +08:00
|
|
|
HGDIOBJ hOld = SelectObject(_DC, _font);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// measure text size
|
2013-06-15 14:03:30 +08:00
|
|
|
DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
|
|
|
|
SelectObject(_DC, hOld);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
tRet.cx = rc.right;
|
|
|
|
tRet.cy = rc.bottom;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return tRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool prepareBitmap(int nWidth, int nHeight)
|
|
|
|
{
|
|
|
|
// release bitmap
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_bmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DeleteObject(_bmp);
|
|
|
|
_bmp = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
if (nWidth > 0 && nHeight > 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_bmp = CreateBitmap(nWidth, nHeight, 1, 32, NULL);
|
|
|
|
if (! _bmp)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-19 15:37:54 +08:00
|
|
|
int drawText(const char * pszText, SIZE& tSize, Image::TextAlign eAlign)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int nRet = 0;
|
|
|
|
wchar_t * pwszBuffer = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! pszText);
|
|
|
|
|
|
|
|
DWORD dwFmt = DT_WORDBREAK;
|
2013-07-26 15:22:45 +08:00
|
|
|
DWORD dwHoriFlag = (int)eAlign & 0x0f;
|
|
|
|
DWORD dwVertFlag = ((int)eAlign & 0xf0) >> 4;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
switch (dwHoriFlag)
|
|
|
|
{
|
|
|
|
case 1: // left
|
|
|
|
dwFmt |= DT_LEFT;
|
|
|
|
break;
|
|
|
|
case 2: // right
|
|
|
|
dwFmt |= DT_RIGHT;
|
|
|
|
break;
|
|
|
|
case 3: // center
|
|
|
|
dwFmt |= DT_CENTER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nLen = strlen(pszText);
|
2012-05-28 14:27:32 +08:00
|
|
|
// utf-8 to utf-16
|
|
|
|
int nBufLen = nLen + 1;
|
|
|
|
pwszBuffer = new wchar_t[nBufLen];
|
|
|
|
CC_BREAK_IF(! pwszBuffer);
|
|
|
|
|
|
|
|
memset(pwszBuffer, 0, sizeof(wchar_t)*nBufLen);
|
|
|
|
nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);
|
|
|
|
|
|
|
|
SIZE newSize = sizeWithText(pwszBuffer, nLen, dwFmt, tSize.cx);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
RECT rcText = {0};
|
|
|
|
// if content width is 0, use text size as content size
|
|
|
|
if (tSize.cx <= 0)
|
|
|
|
{
|
|
|
|
tSize = newSize;
|
|
|
|
rcText.right = newSize.cx;
|
|
|
|
rcText.bottom = newSize.cy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
LONG offsetX = 0;
|
|
|
|
LONG offsetY = 0;
|
|
|
|
rcText.right = newSize.cx; // store the text width to rectangle
|
|
|
|
|
|
|
|
// calculate text horizontal offset
|
|
|
|
if (1 != dwHoriFlag // and text isn't align to left
|
|
|
|
&& newSize.cx < tSize.cx) // and text's width less then content width,
|
|
|
|
{ // then need adjust offset of X.
|
|
|
|
offsetX = (2 == dwHoriFlag) ? tSize.cx - newSize.cx // align to right
|
|
|
|
: (tSize.cx - newSize.cx) / 2; // align to center
|
|
|
|
}
|
|
|
|
|
|
|
|
// if content height is 0, use text height as content height
|
|
|
|
// else if content height less than text height, use content height to draw text
|
|
|
|
if (tSize.cy <= 0)
|
|
|
|
{
|
|
|
|
tSize.cy = newSize.cy;
|
|
|
|
dwFmt |= DT_NOCLIP;
|
|
|
|
rcText.bottom = newSize.cy; // store the text height to rectangle
|
|
|
|
}
|
|
|
|
else if (tSize.cy < newSize.cy)
|
|
|
|
{
|
|
|
|
// content height larger than text height need, clip text to rect
|
|
|
|
rcText.bottom = tSize.cy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rcText.bottom = newSize.cy; // store the text height to rectangle
|
|
|
|
|
|
|
|
// content larger than text, need adjust vertical position
|
|
|
|
dwFmt |= DT_NOCLIP;
|
|
|
|
|
|
|
|
// calculate text vertical offset
|
|
|
|
offsetY = (2 == dwVertFlag) ? tSize.cy - newSize.cy // align to bottom
|
|
|
|
: (3 == dwVertFlag) ? (tSize.cy - newSize.cy) / 2 // align to middle
|
|
|
|
: 0; // align to top
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offsetX || offsetY)
|
|
|
|
{
|
|
|
|
OffsetRect(&rcText, offsetX, offsetY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_BREAK_IF(! prepareBitmap(tSize.cx, tSize.cy));
|
|
|
|
|
|
|
|
// draw text
|
2013-06-15 14:03:30 +08:00
|
|
|
HGDIOBJ hOldFont = SelectObject(_DC, _font);
|
|
|
|
HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
SetBkMode(_DC, TRANSPARENT);
|
|
|
|
SetTextColor(_DC, RGB(255, 255, 255)); // white color
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// draw text
|
2013-06-15 14:03:30 +08:00
|
|
|
nRet = DrawTextW(_DC, pwszBuffer, nLen, &rcText, dwFmt);
|
|
|
|
//DrawTextA(_DC, pszText, nLen, &rcText, dwFmt);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
SelectObject(_DC, hOldBmp);
|
|
|
|
SelectObject(_DC, hOldFont);
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
|
|
|
CC_SAFE_DELETE_ARRAY(pwszBuffer);
|
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SYNTHESIZE_READONLY(HDC, _DC, DC);
|
|
|
|
CC_SYNTHESIZE_READONLY(HBITMAP, _bmp, Bitmap);
|
2012-04-19 14:35:52 +08:00
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
friend class Image;
|
2013-06-15 14:03:30 +08:00
|
|
|
HFONT _font;
|
|
|
|
HWND _wnd;
|
|
|
|
std::string _curFontPath;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Image::initWithString(
|
2012-04-19 14:35:52 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth/* = 0*/,
|
|
|
|
int nHeight/* = 0*/,
|
2013-07-26 15:55:12 +08:00
|
|
|
TextAlign eAlignMask/* = kAlignCenter*/,
|
2012-04-19 14:35:52 +08:00
|
|
|
const char * pFontName/* = nil*/,
|
|
|
|
int nSize/* = 0*/)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! pText);
|
|
|
|
|
|
|
|
BitmapDC& dc = sharedBitmapDC();
|
|
|
|
|
|
|
|
if (! dc.setFont(pFontName, nSize))
|
|
|
|
{
|
2013-07-24 10:46:13 +08:00
|
|
|
log("Can't found font(%s), use system default", pFontName);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw text
|
|
|
|
SIZE size = {nWidth, nHeight};
|
|
|
|
CC_BREAK_IF(! dc.drawText(pText, size, eAlignMask));
|
|
|
|
|
2013-07-25 21:35:00 +08:00
|
|
|
_dataLen = size.cx * size.cy * 4;
|
|
|
|
_data = new unsigned char[_dataLen];
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _data);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
BITMAPINFOHEADER bmiHeader;
|
|
|
|
int mask[4];
|
|
|
|
} bi = {0};
|
|
|
|
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
|
|
|
|
CC_BREAK_IF(! GetDIBits(dc.getDC(), dc.getBitmap(), 0, 0,
|
|
|
|
NULL, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_width = (short)size.cx;
|
|
|
|
_height = (short)size.cy;
|
|
|
|
_preMulti = false;
|
2013-07-27 22:06:30 +08:00
|
|
|
_renderFormat = Texture2D::PixelFormat::RGBA8888;
|
2012-04-19 14:35:52 +08:00
|
|
|
// copy pixed data
|
|
|
|
bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0)
|
|
|
|
? - bi.bmiHeader.biHeight : bi.bmiHeader.biHeight;
|
2013-06-15 14:03:30 +08:00
|
|
|
GetDIBits(dc.getDC(), dc.getBitmap(), 0, _height, _data,
|
2012-04-19 14:35:52 +08:00
|
|
|
(LPBITMAPINFO)&bi, DIB_RGB_COLORS);
|
|
|
|
|
|
|
|
// change pixel's alpha value to 255, when it's RGB != 0
|
|
|
|
COLORREF * pPixel = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (int y = 0; y < _height; ++y)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
pPixel = (COLORREF *)_data + y * _width;
|
|
|
|
for (int x = 0; x < _width; ++x)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
COLORREF& clr = *pPixel;
|
|
|
|
if (GetRValue(clr) || GetGValue(clr) || GetBValue(clr))
|
|
|
|
{
|
|
|
|
clr |= 0xff000000;
|
|
|
|
}
|
|
|
|
++pPixel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|