游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4959|回复: 11

跪求请问有没有写2D透明贴图的函数?

[复制链接]

4

主题

66

帖子

66

积分

注册会员

Rank: 2

积分
66
发表于 2006-11-3 11:39:00 | 显示全部楼层 |阅读模式
  API里有没有能够提供透明贴图的函数?


没有的话,有没有大大能给个透明贴图的函数.~

例如我背景色是黑色,可以帮我去掉背景色,留下人物的.

4

主题

66

帖子

66

积分

注册会员

Rank: 2

积分
66
 楼主| 发表于 2006-11-3 11:40:00 | 显示全部楼层

Re: 跪求请问有没有写2D透明贴图的函数?

尽量能空间换时间.~

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-3 12:49:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

BOOL AlphaBlend(
  HDC hdcDest,                 // handle to destination DC
  int nXOriginDest,            // x-coord of upper-left corner
  int nYOriginDest,            // y-coord of upper-left corner
  int nWidthDest,              // destination width
  int nHeightDest,             // destination height
  HDC hdcSrc,                  // handle to source DC
  int nXOriginSrc,             // x-coord of upper-left corner
  int nYOriginSrc,             // y-coord of upper-left corner
  int nWidthSrc,               // source width
  int nHeightSrc,              // source height
  BLENDFUNCTION blendFunction  // alpha-blending function
);
Parameter
hdcDest
[in] Handle to the destination device context.
nXOriginDest
[in] Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nYOriginDest
[in] Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nWidthDest
[in] Specifies the width, in logical units, of the destination rectangle.
nHeightDest
[in] Specifies the height, in logical units, of the destination rectangle.
hdcSrc
[in] Handle to the source device context.
nXOriginSrc
[in] Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.
nYOriginSrc
[in] Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.
nWidthSrc
[in] Specifies the width, in logical units, of the source rectangle.
nHeightSrc
[in] Specifies the height, in logical units, of the source rectangle.
blendFunction
[in] Specifies the alpha-blending function for source and destination bitmaps, a global alpha value to be applied to the entire source bitmap, and format information for the source bitmap. The source and destination blend functions are currently limited to AC_SRC_OVER. See the BLENDFUNCTION and EMRALPHABLEND structures.
Return Values
If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE.

Windows NT/2000/XP: To get extended error information, call GetLastError. This can return the following value.

Value Meaning
ERROR_INVALID_PARAMETER One or more of the input parameters is invalid.


Remarks
If the source rectangle and destination rectangle are not the same size, the source bitmap is stretched to match the destination rectangle. If the SetStretchBltMode function is used, the iStretchMode value is automatically converted to COLORONCOLOR for this function (that is, BLACKONWHITE, WHITEONBLACK, and HALFTONE are changed to COLORONCOLOR).

The destination coordinates are transformed by using the transformation currently specified for the destination device context. The source coordinates are transformed by using the transformation currently specified for the source device context.

An error occurs (and the function returns FALSE) if the source device context identifies an enhanced metafile device context.

If destination and source bitmaps do not have the same color format, AlphaBlend converts the source bitmap to match the destination bitmap.

AlphaBlend does not support mirroring. If either the width or height of the source or destination is negative, this call will fail.

When rendering to a printer, first call GetDeviceCaps with SHADEBLENDCAPS to determine if the printer supports blending with AlphaBlend. Note that, for a display DC, all blending operations are supported and these flags represent whether the operations are accelerated.

If the source and destination are the same surfacethat is, they are both the screen or the same memory bitmapand the source and destination rectangles overlap, an error occurs and the function returns FALSE.

The source rectangle must lie completely within the source surface, otherwise an error occurs and the function returns FALSE.

AlphaBlend fails if the width or height of the source or destination is negative.

Note  The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If SourceConstantAlpha is 0, it is assumed that the image is transparent. Set the SourceConstantAlpha value to 255 (which indicates that the image is opaque) when you only want to use per-pixel alpha values.

Requirements
  Windows NT/2000/XP: Included in Windows 2000 and later.
  Windows 95/98/Me: Included in Windows 98 and later.
  Header: Declared in Wingdi.h; include Windows.h.
  Library: Included as a resource in Msimg32.dll.

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-3 12:52:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

透明显示位图
这是一个有关位图的类中的一个函数,它能显示带有透明区域的位图。请注意其中的三
个显示函数的用法。(有些复杂的,不是用一个光珊掩码就能解决的:-)

/*************************************************************************
*
* DrawTransparentInRect()
*
* 参数说明:
*
* CDC& dc - 显示位图的设备描述表
* COLORREF crColour - 图像中要被滤掉的颜色(缺省值为白色)
* CRect* crDest - 目标矩形坐标数据(缺省值是NULL)
* CRect* crSou - 源图像待显示部分的矩形坐标(缺省值是NULL)
* int mode - 如果是0则将不显示指定的颜色,如果是1则保留指定
* 的颜色(缺省值为0)
*
* 返回值:
*
* BOOL - 如果成功返回TRUE,否则返回FALSE
*
* 描述:
*
* 将类中的图像透明的绘入目标设备描述表指定区域,如有必要可以产生拉伸或压缩
* 动作
*
* 如果类中没有图像,它什么也不作,直接返回
* 如果待绘制的图像不在当前剪贴区域内,则直接返回
* 如果入口参数crDest和crSou都是NULL,则函数将在设备描述表的0,0位置不变形的
* 绘制类中位图的整个内容
* 如果入口参数crDest和crSou的宽度或高度值不同,则绘制出来的图像会变形(被
* 拉伸或被压缩)
*
************************************************************************/


BOOL CBmpProc:rawTransparentInRect(CDC& dc,
COLORREF crColour,
CRect* crDest,
CRect* crSou,
int mode)
{
// 如果类中没有图像,直接返回
if (!IsValid())
return TRUE;

ASSERT(m_hObject);

// 缺省矩形等于图像尺寸
CRect DCRect(Rect()); // 目标位置
CRect SORect(Rect()); // 源位置

if (crDest)
DCRect = *crDest;
if (crSou)
SORect = *crSou;

// 如果待绘制的图像不在当前剪贴区域内,则直接返回
if (!dc.RectVisible(&DCRect))
return TRUE;

COLORREF crOldBack;
COLORREF crOldText;

if (mode==0)
{ // 去除指定的颜色
crOldBack = dc.SetBkColor(RGB(255,255,255));
crOldText = dc.SetTextColor(RGB(0,0,0));
}
else
{ // 保留指定的颜色
crOldBack = dc.SetBkColor(RGB(0,0,0));
crOldText = dc.SetTextColor(RGB(255,255,255));
}

CDC dcImage, dcTrans;

// 创建内存设备描述表
dcImage.CreateCompatibleDC(&dc);
dcTrans.CreateCompatibleDC(&dc);

// 选择原始图像到DC中
CBitmap* pOldBitmapImage = dcImage.SelectObject(this);

// 创建掩码位图
CBitmap bitmapTrans;
int nWidth = SORect.Width();
int nHeight = SORect.Height();
bitmapTrans.CreateBitmap(nWidth, nHeight, 1, 1, NULL);

// 选择掩码位图到DC中
CBitmap* pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans);

// 创建掩码图像(基于指定的颜色)
dcImage.SetBkColor(crColour);
dcTrans.BitBlt(0, 0, nWidth, nHeight, &dcImage, SORect.left, SORect.top,
SRCCOPY);

// 显示透明位图

/* 设置目标DC的拉伸模式为STRETCH_DELETESCANS,也就是不显示拉伸掉的图像 */
int srlold = dc.SetStretchBltMode(STRETCH_DELETESCANS);

// 显示位图
dc.StretchBlt(DCRect.left,DCRect.top,DCRect.Width(),DCRect.Height(),
&dcImage,SORect.left,SORect.top,SORect.Width(),SORect.Height(),SRCINVERT);
dc.StretchBlt(DCRect.left,DCRect.top,DCRect.Width(),DCRect.Height(),
&dcTrans,SORect.left,SORect.top,SORect.Width(),SORect.Height(),SRCAND);
dc.StretchBlt(DCRect.left,DCRect.top,DCRect.Width(),DCRect.Height(),
&dcImage,SORect.left,SORect.top,SORect.Width(),SORect.Height(),SRCINVERT);

// 恢复设备描述表原来的设置
dc.SetStretchBltMode(srlold);

// 恢复设置
dcImage.SelectObject(pOldBitmapImage);
dcTrans.SelectObject(pOldBitmapTrans);
dc.SetBkColor(crOldBack);
dc.SetTextColor(crOldText);

return TRUE;
}

8

主题

716

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2006-11-3 12:53:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

http://www.yesky.com/399/1749899.shtml

4

主题

66

帖子

66

积分

注册会员

Rank: 2

积分
66
 楼主| 发表于 2006-11-3 16:35:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

挖哈哈哈..爽了...谢谢个位大大了.不知道好用不好用...CODE先CTRL_V下.

180

主题

3511

帖子

3520

积分

论坛元老

Rank: 8Rank: 8

积分
3520
发表于 2006-11-4 03:08:00 | 显示全部楼层

Re: 跪求请问有没有写2D透明贴图的函数?

halei: 跪求请问有没有写2D透明贴图的函数?

中国人怎么动不动就跪求,有没有自尊啊? [em22]

35

主题

370

帖子

376

积分

中级会员

Rank: 3Rank: 3

积分
376
发表于 2006-11-4 20:13:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

呵呵 ,即要自尊,又要自谦`
不要以下给人一堆代码哦~抓住精要说~然后再给代码。。

30

主题

398

帖子

403

积分

中级会员

Rank: 3Rank: 3

积分
403
QQ
发表于 2006-11-5 10:14:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

其实很多东西都可以搜索到的……为什么不先搜索一下呢?

0

主题

17

帖子

17

积分

新手上路

Rank: 1

积分
17
发表于 2007-5-1 21:34:00 | 显示全部楼层

Re:跪求请问有没有写2D透明贴图的函数?

C++的爸爸都60多了吧...
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-22 06:27

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表