OutScreen.cpp
资源名称:LwGame2.0.rar [点击查看]
上传用户:semy2100
上传日期:2013-01-22
资源大小:3633k
文件大小:9k
源码类别:
射击游戏
开发平台:
Visual C++
- /********************************************************************
- * 空运爆发 *
- * Version 2.0 *
- * 本游戏全部代码由: *
- * 冲天实验室 梁锋 编写 *
- * 邮箱:lenwine@126.com *
- * bbs: http://free2.e-168.cn/lenwind (正在开发中...) *
- **********************************************************************/
- #include "stdafx.h"
- static int bglong=::GetSystemMetrics(SM_CXSCREEN);
- static int bgtall=::GetSystemMetrics(SM_CYSCREEN);
- #define ID_TIMER 0
- #define bsupan 168
- static int forewidth, foreheight;//前台页面的宽度和高度
- static RECT backrect; //后台页面映射矩形
- static int backwidth, backheight;//后台页面的宽度和高度
- //===========================================
- LPDIRECTDRAW lpDD; //DirectDraw对象
- LPDIRECTDRAWCLIPPER lpClipper; //DirectDrawClipper对象
- LPDIRECTDRAWSURFACE lpDDSFront; //以下均为DirectDrawSurface对象
- LPDIRECTDRAWSURFACE lpDDSBack;
- LPDIRECTDRAWSURFACE lpDDSBall;
- LPDIRECTDRAWSURFACE lpDDSBom[2];
- LPDIRECTDRAWSURFACE lpDDSMap;
- LPDIRECTDRAWSURFACE lpDDSBulet[2];
- LPDIRECTDRAWSURFACE lpDDSFlame;
- //===========================================
- OutScreen::OutScreen(Manager *mgr):mManager(mgr)
- {
- DesignCS(mManager->MgrFace,mManager->Imglong,mManager->Imgtall);//客户区大小设置
- // InitDDraw(mManager->MgrFace,mManager->Imglong,mManager->Imgtall);
- bk=(HBITMAP)LoadImage(NULL,"diimg.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
- bMap=new Showmap("Fire.bmp",0,true,RGB(255,0,255));
- SetTimer(mManager->MgrFace,ID_TIMER,1000,NULL);
- OSdc=GetDC(mManager->MgrFace);//得到句柄
- bkdc=CreateCompatibleDC(OSdc);
- Mdc=CreateCompatibleDC(OSdc);
- memDC=CreateCompatibleDC(OSdc);
- hBitmap=CreateCompatibleBitmap(memDC,mManager->Imglong,mManager->Imgtall);//创建兼容位图
- Mbit=CreateCompatibleBitmap(OSdc,mManager->Imglong,mManager->Imgtall);
- SelectObject(bkdc,bk);
- SelectObject(Mdc,hBitmap);
- SelectObject(Mdc,Mbit);
- }
- OutScreen::~OutScreen()//析构函数
- {
- ReleaseDC(mManager->MgrFace,OSdc);
- DeleteDC(Mdc);
- DeleteDC(memDC);
- DeleteObject(Mbit);
- DeleteObject(hBitmap);
- DeleteDC(bkdc);
- DeleteObject(bk);
- }
- void OutScreen::PaintOutPut(int x,int y,int Bitlong,int Bittall,int bx,int by,bool bHStyles)
- {
- StretchBlt(Mdc,0,y,32,mManager->Imgtall,bkdc,0,y,480,32,SRCCOPY);
- bMap->DImage(Mdc,x,y,Bitlong,Bittall,bx,by);//角色显示
- }
- //把画面输出到客户屏幕上
- void OutScreen::PmOutPut()
- {
- BitBlt(Mdc,0,0,mManager->Imglong,mManager->Imgtall,memDC,0,0,SRCCOPY);
- StretchBlt(Mdc,0,0,mManager->Imglong,64,bkdc,0,0,64,480,SRCCOPY);
- //StretchBlt(OSdc,0,0,mManager->Imglong,mManager->Imgtall,bksdc,0,0,608,480,SRCCOPY);
- // paintsp();
- BitBlt(OSdc,0,0,mManager->Imglong,mManager->Imgtall,Mdc,0,0,SRCCOPY);
- }
- void OutScreen::DesignCS(HWND dhwnd,int Imglong,int Imgtall)
- {//窗口全屏
- RECT rt={0,0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN)};
- MoveWindow(dhwnd,rt.left,rt.top,rt.right-rt.left,rt.bottom-rt.top,TRUE);
- }
- void OutScreen::Refresh()
- {
- SelectObject(Mdc,GetStockObject(BLACK_BRUSH));
- Rectangle(Mdc,0,0,mManager->Imglong,mManager->Imgtall);
- }
- HDC OutScreen::GetCloseDDC()
- {
- return Mdc;
- }
- void OutScreen::beginsp(void)
- {
- int numsp;
- sp = (supan*)malloc(sizeof(supan)*bsupan);
- for(numsp=0; numsp<bsupan;numsp++)
- {
- sp[numsp].x = (rand() % bglong);//碎片宽
- sp[numsp].y = (rand() % bgtall);//碎片高
- sp[numsp].t = (rand() % 1);//间隔
- }
- }
- void OutScreen::freesp(void)
- {
- free(sp);
- }
- void OutScreen::paintsp(void)
- {
- int numsp, lightsp;
- for(numsp=0; numsp<bsupan; numsp++)
- {
- lightsp = 256 - sp[numsp].t;
- SetPixel(OSdc,sp[numsp].x,sp[numsp].y,RGB(200,255,255));
- SetPixel(OSdc,sp[numsp].x,sp[numsp].y+1,RGB(200,255,255));
- SetPixel(OSdc,sp[numsp].x+1,sp[numsp].y,RGB(200,255,255));
- SetPixel(OSdc,sp[numsp].x+1,sp[numsp].y+1,RGB(200,255,255));
- SetPixel(OSdc,sp[numsp].x+1,sp[numsp].y+2,RGB(200,255,255));
- SetPixel(OSdc,sp[numsp].x+2,sp[numsp].y+3,RGB(200,255,255));
- SetPixel(OSdc,sp[numsp].x+2,sp[numsp].y+4,RGB(200,255,255));
- sp[numsp].y+=2;
- if(sp[numsp].y >mManager->Imglong)
- {
- sp[numsp].x = (rand() % bglong);
- sp[numsp].y = (rand() % bglong);
- sp[numsp].t = (rand() % bgtall);
- }
- }
- }
- void OutScreen::leftsp()
- {
- int numsp, lightsp;
- for(numsp=0; numsp<bsupan; numsp++)
- {
- lightsp = 256 - sp[numsp].t;
- //碎片块X,Y用来放大像素
- SetPixel(OSdc,sp[numsp].x,sp[numsp].y,RGB((rand()%255),255,lightsp));
- SetPixel(OSdc,sp[numsp].x,sp[numsp].y+1,RGB((rand()%255),255,lightsp));
- SetPixel(OSdc,sp[numsp].x+1,sp[numsp].y,RGB(255,255,255));
- SetPixel(OSdc,sp[numsp].x+1,sp[numsp].y+1,RGB((rand()%255),255,lightsp));
- SetPixel(OSdc,sp[numsp].x+1,sp[numsp].y+2,RGB((rand()%255),255,255));
- sp[numsp].x-=3;
- if(sp[numsp].x < -200)
- {
- sp[numsp].x = (rand() % bglong);
- sp[numsp].y = (rand() % bgtall);
- sp[numsp].t = (rand() % bgtall);
- }
- }
- }
- void OutScreen::rightsp()
- {
- int numsp, lightsp;
- for(numsp=0; numsp<bsupan; numsp++)
- {
- lightsp = 256 - sp[numsp].t;
- sp[numsp].x+=7;
- SetPixel(OSdc,sp[numsp].x,sp[numsp].y,RGB((rand()%250),lightsp,lightsp));
- if(sp[numsp].x >bglong)
- {
- sp[numsp].x = (rand() % bgtall);
- sp[numsp].y = (rand() % bglong);
- sp[numsp].t =(rand() * (bglong));
- }
- }
- }
- //定义DOWN反方向
- void OutScreen::downsp()
- {
- int numsp, lightsp;
- for(numsp=0; numsp<bsupan; numsp++)
- {
- lightsp = 256 - sp[numsp].t;
- sp[numsp].y+=9;
- SetPixel(OSdc,sp[numsp].x,sp[numsp].y,RGB((rand()%250),lightsp,lightsp));
- if(sp[numsp].y >(mManager->Imgtall))
- {
- sp[numsp].x = (rand() % bglong);
- sp[numsp].y = (rand() % bgtall);
- sp[numsp].t = (rand() % bglong);
- }
- }
- }
- //================================================================
- //初始化DirectDraw环境
- BOOL OutScreen::InitDDraw()
- {
- /*
- ddsd是指向DDSRFACEDESC结构的一个指针。该结构存储了DirectDraw表面(Surface)的
- 当前描述。在这种情况下,我们需要注意的是:DDSURFACEDESC的成员描述这个表面
- (Surface)的高度和宽度,分别表示为:DDSD_HEIGHT和 DDSD_WIDTH。调用
- IDirectDrawSurface::GetSurfaceDesc方法,把适当的值来载入到这个结构。
- 对于DDEX2来说,
- DDCopyBitmap函数锁定表面(Surface)并将位图文件复制到后台缓冲区,
- 使用StretchBit函数延伸或压缩后台缓冲区到可适用的大小。
- */
- DWORD dwFlags;
- DDSURFACEDESC ddsd;//这个结构描述"页"的特徵.
- //创建DirectDraw对象
- DirectDrawCreate(NULL, &lpDD, NULL);
- //设置协作级别为窗口模式
- dwFlags = DDSCL_NORMAL;
- lpDD->SetCooperativeLevel(mManager->MgrFace, dwFlags);
- //创建主页面
- ZeroMemory(&ddsd, sizeof(ddsd));
- ddsd.dwSize = sizeof(ddsd);
- ddsd.dwFlags = DDSD_CAPS;
- ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;//指定我们用的是前页.
- lpDD->CreateSurface(&ddsd, &lpDDSFront, NULL);
- //创建离屏页面
- ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
- RECT rect;
- GetClientRect(mManager->MgrFace, &rect);
- forewidth=mManager->Imglong;
- foreheight=mManager->Imgtall;
- GetRect(&backrect, 65, 65, forewidth, foreheight);
- ddsd.dwWidth = backwidth=forewidth+130;
- ddsd.dwHeight = backheight=foreheight+130;
- lpDD->CreateSurface( &ddsd, &lpDDSBack, NULL );
- //创建地图页面
- ddsd.dwWidth = 64;
- ddsd.dwHeight = 64;
- lpDD->CreateSurface( &ddsd, &lpDDSMap, NULL );
- DDReLoadBitmap(lpDDSMap, "wmap.bmp");
- return TRUE;
- }
- //*****************************************************
- //释放所有的DirectDraw对象。
- void OutScreen::FreeDDraw()
- {
- if( lpDD != NULL )
- {
- lpDD->Release();
- lpDD = NULL;
- }
- }
- //*****************************************************
- //换页
- void OutScreen::Flip()
- {
- RECT Window;
- POINT pt;
- GetClientRect(mManager->MgrFace, &Window);
- pt.x=pt.y=0;
- ClientToScreen(mManager->MgrFace, &pt);
- OffsetRect(&Window, pt.x, pt.y);
- HRESULT rval=lpDDSFront->Blt(&Window, lpDDSBack, &backrect, DDBLT_WAIT, NULL);
- if(rval == DDERR_SURFACELOST) lpDDSFront->Restore();
- return ;
- }
- //***************************************
- //绘制背景地图
- void OutScreen::DrawMap()
- {
- //背景贴图的偏移坐标
- static int mapposX=64, mapposY=64;
- mapposX--;
- if (mapposX<=0 ) mapposX=64;
- for (int x=0; x<=(int)backwidth/64; x++)
- for (int y=0; y<=(int)backheight/64; y++)
- {
- RECT rect;
- GetRect(&rect, 64*x-mapposY, 64*y-mapposX, 64, 64);
- HRESULT rval=lpDDSBack->Blt( &rect, lpDDSMap, NULL, DDBLT_WAIT, NULL);
- if(rval == DDERR_SURFACELOST) lpDDSBack->Restore();
- }
- }
- //更新屏幕
- void OutScreen::UpdateFrame()
- {
- DrawMap();
- Flip();
- }
- //填充一个RECT结构
- void OutScreen::GetRect(RECT* rect, long left, long top, long width, long height)
- {
- rect->left=left;
- rect->top=top;
- rect->right=left+width;
- rect->bottom=top+height;
- return;
- }