VC++游戏编程----游戏画面特效制作2

本文介绍了如何在VC++游戏中实现半透明和透明效果。半透明效果通过前景和背景颜色的混合公式实现,包括获取位图结构、建立暂存数组、合成像素颜色等步骤。透明半透明效果则利用内存DC和位图对象,先完成透明处理再进行半透明合成,以消除矩形边框。文中提供详细的代码示例。
摘要由CSDN通过智能技术生成

一、半透明效果

半透明效果在游戏世界里是用的很频繁的,比如鬼魂,隐形人物。

简单的说,半透明是前景图案和背景图案像素颜色的混合。可以简单的把半透明处理,整理为一个公式:

半透明图色彩 = 前景图色彩× 不透明度 + 背景图色彩 × (1 -不透明度)


半透明处理的步骤:

1.      获取位图结构。
GetObject()获取位图的结构后,可得到位图信息。

2.      建立暂存数组
获得了位图结构后,接下来需要先建立一个暂时存储位图像素的颜色值。这个暂存数组的大小有前一步获得位图信息的bmHeight, bmWidthBytes决定。

这里有必要提一下BITMAP结构,因为在之后将会用到。

typedef struct tagBITMAP {
  LONG   bmType; //位图类型
  LONG   bmWidth; //位图宽度
  LONG   bmHeight; //长度
  LONG   bmWidthBytes; //每一列像素所占Byte数
  WORD   bmPlanes; //颜色平面数
  WORD   bmBitsPixel; //像素位数
  LPVOID bmBits; //位图内存指针
} BITMAP, *PBITMAP; 

3.      取得位图位值。
建立数组后,可以用GetBitmapBits()来获得位图位值。

4.      合成像素颜色值
获得位图的所有像素颜色值后,按照公式完成半透明颜色值的合成。

5.      重设位图颜色。
根据数组的内容来重新设位图的颜色。使用SetBitmapBits() 。

完成后,最后就是贴图了。

 

范例:

首先创建win32项目。让系统生成基本框架代码。

在程序开始处添加全局变量。

HBITMAP bg, girl;
HDC mdc;
//常量
const int xstart= 50;
const int ystart= 20;

然后在InitInstance()函数中添加代码

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   HDC hdc;

   hInst = hInstance; // 将实例句柄存储在全局变量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFA
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "Snake.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers //DEL void CMainFrame::OnGameStart() //DEL { //DEL // TODO: Add your command handler code here //DEL IniGame(); //DEL m_nGameStatus = 1; //DEL SetTimer(1,100,NULL); //DEL Invalidate(); //DEL }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值