Drawbmp.cpp
资源名称:wx_demo.zip [点击查看]
上传用户:s2588lin
上传日期:2007-01-07
资源大小:306k
文件大小:64k
源码类别:
其他智力游戏
开发平台:
Visual C++
- #include<afxwin.h>
- #include"main.h"
- #include<ddraw.h>
- #include<dinput.h>
- #include<math.h>
- #define Surface LPDIRECTDRAWSURFACE7
- #define SurfaceDesc DDSURFACEDESC2
- double dir[6]={ 0, 0.1 ,0.3 ,0.52 ,0.78 ,1.11};
- // Define Global Data //
- LPDIRECTDRAW7 lpDD;
- LPDIRECTINPUT7 DInput;
- LPDIRECTINPUTDEVICE2 Keyboard;
- LPDIRECTINPUTDEVICE2 Mouse;
- DDSURFACEDESC2 ddsd;
- Surface Primary;
- Surface PrimaryBack;
- Surface BackGround;
- Surface BackUp;
- Surface Land;
- Surface Circle;
- //DDOVERLAYFX OverlayFX; // DirectDraw overlay effects struct
- //DDCAPS DDCaps; // DirectDraw hardware capabilities struct
- //DWORD OverlayFlags;
- //HRESULT hRet;
- //HBITMAP hbitmap;
- //BITMAP bmp;
- //RECT rs,rd;
- //POINT p={0,0};
- #define INIT_DIRECTDRAW_STRUCT(x) (ZeroMemory(&x, sizeof(x)), x.dwSize=sizeof(x))
- // Unit Directions Define //
- #define UP 0
- #define UR 1
- #define RIGHT 2
- #define DR 3
- #define DOWN 4
- #define DL 5
- #define LEFT 6
- #define UL 7
- // Building Type Define //
- #define PYRAMID 0
- #define LAKE 1
- #define WONDER 2
- #define TOWER 3
- #define HOUSE 4
- // Unit Order Define //
- #define UMOVE 0
- #define UATTACK 1
- #define UHOLDPLACE 2
- // UnitGroup Shape //
- #define CONCENTRATE 0
- #define SURROUND 1
- #define squaresize 20
- #define SpaceStartx 0
- #define SpaceStarty 0
- #define SpaceEndx 799
- #define SpaceEndy 479
- #define ScrollStep 20
- #define BmpWidth 150
- #define BmpHeight 100
- #define BaseTime 20
- #define MapWidth 60
- #define MapHeight 60
- #define MAXH 13
- #define MAXL 13
- int SeekCount=0;
- int MapPixelWidth=squaresize*MapWidth;
- int MapPixelHeight=squaresize*MapHeight;
- int map[MapHeight][MapWidth];
- int xstart=0,ystart=0;
- int CoverSign,UpdateFrame;
- int BuildingCount=0;
- int ChooseSx,ChooseSy,ChooseEx,ChooseEy,OldCx,OldCy;
- int xscroll,yscroll;
- int Mousex=398,Mousey=298;
- int xlimit,ylimit;
- bool TimeIsUp=FALSE;
- bool ChooseGroup=FALSE;
- bool BeginChoose=FALSE;
- //int pathmapMode;
- HWND mWnd;
- void DrawLittleMap()
- {
- int i,j;
- CBrush cr1,cr2,cr3,cr4;
- cr1.CreateSolidBrush(RGB(50,120,70));
- cr2.CreateSolidBrush(RGB(210,10,50));
- cr3.CreateSolidBrush(RGB(50,10,250));
- cr4.CreateSolidBrush(RGB(0,0,20));
- int si,sj;
- si=680;
- sj=479;
- HDC hdc;
- CRect re(si,sj,si+120,sj+120);
- int x1,y1,x2,y2;
- PrimaryBack->GetDC(&hdc);
- FillRect(hdc,re,cr1);
- for(j=0;j<=59;j++)
- for(i=0;i<=59;i++)
- {
- x1=si+i*2;
- x2=si+i*2+2;
- y1=sj+j*2;
- y2=sj+j*2+2;
- if(map[j][i]==1||map[j][i]==2) FillRect(hdc,CRect(x1,y1,x2,y2),cr2);
- if(map[j][i]==3) FillRect(hdc,CRect(x1,y1,x2,y2),cr3);
- if(map[j][i]==4) FillRect(hdc,CRect(x1,y1,x2,y2),cr4);
- }
- PrimaryBack->ReleaseDC(hdc);
- DeleteObject(cr1);
- DeleteObject(cr2);
- DeleteObject(cr3);
- }
- void DrawRect(HDC hdc,CRect re)
- {
- CBrush cr;
- cr.CreateSolidBrush(RGB(0,0,0));
- FillRect(hdc,re,cr);
- cr.DeleteObject();
- }
- void ShowInt(int x,int y,int num)
- {
- HDC hdc;
- char bb[4];
- itoa(num,bb,10);
- Primary->GetDC(&hdc);
- SetBkMode(hdc,TRANSPARENT);
- SetTextColor(hdc,RGB(200,200,100));
- DrawRect(hdc,CRect(x,y,x+50,y+20));
- TextOut(hdc,x,y,bb,strlen(bb));
- Primary->ReleaseDC(hdc);
- PrimaryBack->GetDC(&hdc);
- SetBkMode(hdc,TRANSPARENT);
- DrawRect(hdc,CRect(x,y,x+50,y+20));
- SetTextColor(hdc,RGB(200,200,100));
- TextOut(hdc,x,y,bb,strlen(bb));
- PrimaryBack->ReleaseDC(hdc);
- }
- BOOL ImageToSurface(Surface lpdds,int ResID)
- {
- HBITMAP hbm;
- HDC hdcImage= NULL;
- HDC hdcSurf = NULL;
- HRESULT ddrval;
- SurfaceDesc ddsd;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
- ddrval = lpdds->GetSurfaceDesc(&ddsd);
- if(ddrval!=DD_OK) { MessageBox(NULL,"GetSurface size failed!","error",MB_OK); return FALSE;}
- hbm = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ResID), IMAGE_BITMAP, ddsd.dwWidth, ddsd.dwHeight, LR_DEFAULTCOLOR|LR_DEFAULTSIZE);//LR_CREATEDIBSECTION);
- hdcImage = CreateCompatibleDC(NULL);
- SelectObject(hdcImage, hbm);
- ddrval = lpdds->GetDC(&hdcSurf);
- if(BitBlt(hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, 0, 0, SRCCOPY)==FALSE)
- DeleteDC(hdcImage); DeleteObject(hbm); lpdds->ReleaseDC(hdcSurf);
- return TRUE;
- }
- void DrawRect(CRect re)
- {
- CBrush cr;
- cr.CreateSolidBrush(RGB(0,0,0));
- HDC hdc;
- Primary->GetDC(&hdc);
- FillRect(hdc,re,cr);
- Primary->ReleaseDC(hdc);
- PrimaryBack->GetDC(&hdc);
- FillRect(hdc,re,cr);
- PrimaryBack->ReleaseDC(hdc);
- }
- typedef struct tagPathx
- {
- int cx;
- int cy;
- struct tagPathx *Next;
- struct tagPathx *Last;
- }PATHX;
- class Object
- {
- public:
- Surface surface;
- CString name;
- int width,height;
- int PixelWidth,PixelHeight;
- int x,y;
- int RIDnormal;
- virtual bool Init();
- virtual void Show() {};
- virtual void Destroy();
- };
- bool Object::Init()
- {
- SurfaceDesc ddsd;
- PixelWidth=width*squaresize;
- PixelHeight=height*squaresize;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=PixelWidth;
- ddsd.dwHeight=PixelHeight;
- HRESULT hRet=lpDD->CreateSurface(&ddsd,&surface,NULL);
- if(hRet!=DD_OK) return FALSE;
- DDCOLORKEY KColor;
- KColor.dwColorSpaceLowValue=RGB(0,0,0);
- KColor.dwColorSpaceHighValue=RGB(0,0,0);
- surface->SetColorKey(DDCKEY_SRCBLT,&KColor);
- return TRUE;
- }
- void Object::Destroy()
- {
- if(surface) { surface->Release(); surface=NULL; }
- }
- //------------------------------------------------------------------------
- class BuildingType:public Object
- {
- public:
- int takeup[MAXH][MAXL];
- int RIDchoose;
- int RIDanimation[5];
- void Init(int arraytemp[MAXH][MAXL]);
- };
- void BuildingType::Init(int arraytemp[MAXH][MAXL])
- {
- Object::Init();
- ImageToSurface(surface,RIDnormal);
- for(int i=0;i<=width-1;i++)
- for(int j=0;j<=height-1;j++)
- { takeup[j][i]=arraytemp[j][i]; }
- }
- class UnitType:public Object
- {
- public:
- int RIDanimation[5];
- int RIDDirection[8];
- int TakeUp[8][16];
- int TimeCount;
- int Time;
- int steplen,stepx,stepy;
- Surface DirectionSurf[8];
- bool Init();
- };
- bool UnitType::Init()
- {
- HRESULT hRet;
- SurfaceDesc ddsd;
- PixelWidth=width*squaresize;
- PixelHeight=height*squaresize;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=PixelWidth;
- ddsd.dwHeight=PixelHeight;
- TimeCount=0;
- for(int i=0;i<=7;i++)
- {
- hRet=lpDD->CreateSurface(&ddsd,&DirectionSurf[i],NULL);
- if(hRet!=DD_OK) return FALSE;
- ImageToSurface(DirectionSurf[i],RIDDirection[i]);
- }
- return TRUE;
- }
- class Building:public Object
- {
- public:
- int Breakage;
- int Type;
- BuildingType *m_BType;
- virtual bool Init(BuildingType *BType);
- virtual void Show(int x,int y);
- };
- bool Building::Init(BuildingType *BType)
- {
- m_BType=BType;
- RIDnormal=m_BType->RIDnormal;
- width=m_BType->width;
- height=m_BType->height;
- Object::Init();
- surface->Blt(CRect(0,0,PixelWidth,PixelHeight),BType->surface,CRect(0,0,PixelWidth,PixelHeight),0,NULL);
- return TRUE;
- }
- void Building::Show(int tx,int ty)
- {
- x=tx,y=ty;
- for(int i=0;i<=width-1;i++)
- for(int j=0;j<=height-1;j++)
- { map[y+j][x+i]=m_BType->takeup[j][i]; }
- int x1=xstart+squaresize*x;
- int y1=xstart+squaresize*y;
- BackGround->Blt(CRect(x1,y1,x1+PixelWidth,y1+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- }
- //----------------------------------------------------------------------------------
- class BuildingGroup
- {
- public:
- CPtrList BuildingList;
- void Recover();
- void Destroy();
- };
- void BuildingGroup::Destroy()
- {
- POSITION pos;
- for(pos=BuildingList.GetHeadPosition();pos!=NULL;)
- {
- Building *ttBuild;
- ttBuild=(Building*)BuildingList.GetNext(pos);
- ttBuild->Destroy();
- }
- BuildingList.RemoveAll();
- }
- BuildingGroup AllBuildings;
- //----------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------
- class Unit:public Object
- {
- public:
- int Breakage;
- int Direction;
- int RIDchoose;
- int lastx,lasty,mtx,mty,oldmtx,oldmty,endpx,endpy,xfsign,yfsign;
- int stepx,stepy,steplen;
- int Targetx,Targety;
- int Attackx,Attacky;
- int MoveSign,StepBegin;
- int NodeCount;
- int pathmap[MapHeight][MapWidth];
- PATHX *Pathx,*PathHead;
- PATHX *PathRecord[3600];
- UnitType *m_UType;
- int Time,TimeCount,SeekInterval,SeekTime;
- int FireInterval,FireTimeCount;
- int OrderList[5];
- int OrderCount;
- bool BeChosed;
- bool IsBlock;
- bool AllowShow;
- bool Seek(int Tarx,int Tary);
- void Recover();
- void Redraw();
- inline int GetOrder() { return OrderList[OrderCount]; }
- virtual bool Init(UnitType *UType);
- virtual void Show();
- };
- bool Unit::Init(UnitType *UType)
- {
- m_UType=UType; // Set Type
- Time=m_UType->Time;
- steplen=m_UType->steplen;
- stepx=m_UType->stepx;
- stepy=m_UType->stepy;
- width=m_UType->width;
- height=m_UType->height;
- PixelWidth=m_UType->PixelWidth;
- PixelHeight=m_UType->PixelHeight;
- HRESULT hRet; // Set Surface
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=PixelWidth;
- ddsd.dwHeight=PixelHeight;
- hRet=lpDD->CreateSurface(&ddsd,&surface,NULL);
- if(hRet!=DD_OK) return FALSE;
- DDCOLORKEY KColor;
- KColor.dwColorSpaceLowValue=RGB(0,0,0);
- KColor.dwColorSpaceHighValue=RGB(0,0,0);
- surface->SetColorKey(DDCKEY_SRCBLT,&KColor);
- surface->Blt(CRect(0,0,PixelWidth,PixelHeight),m_UType->DirectionSurf[Direction],CRect(0,0,PixelWidth,PixelHeight),0,NULL);
- if(hRet!=DD_OK) return FALSE;
- MoveSign=0;Pathx=NULL;PathHead=NULL; // Set Other Information
- xfsign=0;yfsign=0;
- lastx=x;lasty=y;
- mtx=xstart+x*squaresize;
- mty=ystart+y*squaresize;
- OrderCount=0;
- OrderList[0]=UHOLDPLACE;
- SeekInterval=8;
- SeekTime=0;
- BeChosed=FALSE;
- IsBlock=FALSE;
- FireTimeCount=0;
- FireInterval=10;
- for(int j=y;j<=y+height-1;j++)
- for(int i=x;i<=x+width-1 ;i++) map[j][i]=3;
- return TRUE;
- }
- void Unit::Redraw()
- {
- if(BeChosed)BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),Circle,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- }
- bool Unit::Seek(int tarx,int tary)
- {
- int i,j; // Get the path //
- for( i=0;i<=NodeCount-1;i++)
- {
- if(PathRecord[i]!=NULL) delete PathRecord[i];
- }
- NodeCount=0;
- PATHX *NodeChain,*Rnode;
- PathHead=new PATHX;
- PathHead->cx=tarx;
- PathHead->cy=tary;
- PathHead->Next=NULL;
- PathHead->Last=NULL;
- Pathx=PathHead;
- Rnode=PathHead;
- NodeChain=PathHead;
- PathRecord[NodeCount]=PathHead;
- NodeCount++;
- int tx,ty;
- int oldtx,oldty;
- int stx=x;
- int sty=y;
- int n=0;
- int dire;
- for(tx=0;tx<=MapWidth-1;tx++) for(ty=0;ty<=MapHeight-1;ty++) pathmap[ty][tx]=map[ty][tx];
- int FindSign=0;
- while((NodeChain->cx!=stx||NodeChain->cy!=sty))
- {
- n++;
- if(n>=MapWidth*MapHeight)
- {
- //ShowInt(300,560,n/10);
- FindSign=0;
- break;
- }
- oldtx=NodeChain->cx;
- oldty=NodeChain->cy;
- for(dire=0;dire<=7;dire++)
- {
- switch(dire)
- {
- case 0: {tx=oldtx-1; ty=oldty ; break;} // go left ?
- case 1: {tx=oldtx+1; ty=oldty ; break;} // go right ?
- case 2: {tx=oldtx; ty=oldty-1; break;} // go up ?
- case 3: {tx=oldtx; ty=oldty+1; break;} // go down ?*/
- case 4: {tx=oldtx-1; ty=oldty-1 ;
- if((map[oldty][tx]==1||map[ty][oldtx]==1))
- { tx=oldtx-1; ty=oldty ; }
- break;
- } // go left ?
- case 5: {tx=oldtx+1; ty=oldty+1 ;
- if((map[oldty][tx]==1||map[ty][oldtx]==1))
- { tx=oldtx+1; ty=oldty ; }
- break;
- } // go right ?
- case 6: {tx=oldtx+1; ty=oldty-1;
- if((map[oldty][tx]==1||map[ty][oldtx]==1))
- { tx=oldtx+1; ty=oldty ;}
- break;
- }
- case 7: {tx=oldtx-1;ty=oldty+1;
- if((map[oldty][tx]==1||map[ty][oldtx]==1))
- { tx=oldtx-1; ty=oldty ; }
- break;
- }
- };
- if(tx>=0&&(tx+width-1)<=(MapWidth-1)&&ty>=0&&(ty+height-1)<=(MapHeight-1))
- {
- bool MapCannotMove=FALSE;
- bool FindDest=FALSE;
- for(j=ty;j<=ty+height-1;j++)
- for(i=tx;i<=tx+width-1;i++)
- {
- if(map[j][i]==1||map[j][i]==3||map[j][i]==4) MapCannotMove=TRUE;
- }
- if( (!MapCannotMove) && (pathmap[ty][tx]!=1) )
- {
- pathmap[ty][tx]=1;
- PATHX *NewNode; NewNode=new PATHX;
- NewNode->cx=tx; NewNode->cy=ty;
- NewNode->Last=NodeChain;
- NewNode->Next=NULL;
- Rnode->Next=NewNode;
- Rnode=Rnode->Next;
- PathRecord[NodeCount]=NewNode;
- NodeCount++;
- if(tx==x&&ty==y)
- {
- Pathx=NewNode; MoveSign=1;
- lastx=stx;
- lasty=sty;
- FindSign=1;
- Targetx=tarx;Targety=tary;
- IsBlock=FALSE;
- break;
- }
- }
- }
- }
- if(NodeChain->Next!=NULL) NodeChain=NodeChain->Next;
- }
- if(FindSign==0)
- {
- for(i=0;i<=NodeCount-1;i++) { if(PathRecord[i]!=NULL) delete PathRecord[i]; }
- NodeCount=0;
- PathHead=new PATHX;
- PathHead->cx=x;
- PathHead->cy=y;
- PathHead->Last=NULL;
- PathHead->Next=NULL;
- Pathx=PathHead;
- PathRecord[NodeCount]=PathHead;
- NodeCount++;
- StepBegin=0;
- MoveSign=1;
- Targetx=tarx;Targety=tary;
- IsBlock=TRUE;
- return FALSE;
- }
- return TRUE;
- }
- class UnitGroup
- {
- public:
- int MemberCount;
- CPtrList UnitList;
- bool CanStop(int xt,int yt,int uw,int uh,POSITION pos);
- void MoveTo(int tarx,int tary,int shape);
- void Recover();
- void Show();
- void Destroy();
- };
- UnitGroup AllUnits,ChosedGroup;
- void UnitGroup::Show()
- {
- POSITION posx;
- for(posx=UnitList.GetHeadPosition();posx!=NULL;)
- {
- Unit *ttUnit;
- ttUnit=(Unit*)UnitList.GetNext(posx);
- ttUnit->Show();
- }
- }
- void UnitGroup::Recover()
- {
- POSITION pos;
- for(pos=UnitList.GetHeadPosition();pos!=NULL;)
- {
- Unit *ttUnit;
- ttUnit=(Unit*)UnitList.GetNext(pos);
- ttUnit->Recover();
- }
- }
- bool UnitGroup::CanStop(int xt,int yt,int uw,int uh,POSITION pos)
- {
- if(xt<0||xt>MapWidth-uw||yt<0||yt>MapHeight-uh) return FALSE;
- int j,i,uy,ux;
- bool CannotStop=FALSE;
- for(j=yt;j<=yt+uh-1;j++)
- for(i=xt;i<=xt+uw-1;i++)
- if(!map[j][i]==0) CannotStop=TRUE;
- Unit *ttUnit;
- if(!CannotStop)
- {
- ttUnit=(Unit*)UnitList.GetNext(pos);
- uy=ttUnit->y;ux=ttUnit->x;
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- ttUnit->Seek(xt,yt);
- ttUnit->OrderCount=0;
- ttUnit->OrderList[0]=UMOVE;
- ttUnit->OrderList[1]=UHOLDPLACE;
- return TRUE;
- }
- return FALSE;
- }
- void UnitGroup::MoveTo(int tarx1,int tary1,int shape)
- {
- POSITION pos;
- pos = UnitList.GetHeadPosition();
- int i=0,j=0;
- int radio=0;
- int uw,uh;
- int xt=tarx1,yt=tary1,n;
- int ux,uy;
- bool FindSpace=FALSE;
- bool FindSign=FALSE;
- Unit *ttUnit;
- switch(shape)
- {
- case CONCENTRATE:
- {
- ttUnit=(Unit*)UnitList.GetAt(pos);
- uw=ttUnit->width; uh=ttUnit->height;
- if(CanStop(xt,yt,uw,uh,pos)) ttUnit=(Unit*)UnitList.GetNext(pos);
- ux=ttUnit->x; uy=ttUnit->y;
- while(pos!=NULL)
- {
- for(n=0;n<=radio;n++)
- {
- xt+=uw;
- ttUnit=(Unit*)UnitList.GetAt(pos);
- uw=ttUnit->width; uh=ttUnit->height;
- if(CanStop(xt,yt,uw,uh,pos)) ttUnit=(Unit*)UnitList.GetNext(pos);
- if(pos==NULL) return;
- }
- for(n=0;n<=radio;n++)
- {
- yt+=uh;
- ttUnit=(Unit*)UnitList.GetAt(pos);
- uw=ttUnit->width; uh=ttUnit->height;
- if(CanStop(xt,yt,uw,uh,pos)) ttUnit=(Unit*)UnitList.GetNext(pos);
- if(pos==NULL) return;
- }
- for(n=0;n<=radio+1;n++)
- {
- xt-=uw;
- ttUnit=(Unit*)UnitList.GetAt(pos);
- uw=ttUnit->width; uh=ttUnit->height;
- if(CanStop(xt,yt,uw,uh,pos)) ttUnit=(Unit*)UnitList.GetNext(pos);
- if(pos==NULL) return;
- }
- for(n=0;n<=radio+1;n++)
- {
- yt-=uw;
- ttUnit=(Unit*)UnitList.GetAt(pos);
- uw=ttUnit->width; uh=ttUnit->height;
- if(CanStop(xt,yt,uw,uh,pos)) ttUnit=(Unit*)UnitList.GetNext(pos);
- if(pos==NULL) return;
- }
- radio+=2;
- }
- break;
- }
- case SURROUND:
- {
- int r;
- int targetx,targety;
- while(pos!=NULL)
- {
- ttUnit=(Unit*)UnitList.GetNext(pos);
- ux=ttUnit->x; uy=ttUnit->y;
- uw=ttUnit->width; uh=ttUnit->height;
- r=(int)sqrt((ux-xt)*(ux-xt)+(uy-yt)*(uy-yt));
- targetx=xt-(int)(9*(xt-ux)/r);
- targety=yt-(int)(9*(yt-uy)/r);
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- if(!ttUnit->Seek(targetx,targety))
- {
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==0) map[j][i]=3; }
- }
- ttUnit->Attackx=xt;
- ttUnit->Attacky=yt;
- ttUnit->OrderCount=0;
- ttUnit->OrderList[0]=UMOVE;
- ttUnit->OrderList[1]=UATTACK;
- ttUnit->OrderList[2]=UHOLDPLACE;
- }
- break;
- }
- };
- }
- void UnitGroup::Destroy()
- {
- POSITION pos;
- for(pos=UnitList.GetHeadPosition();pos!=NULL;)
- {
- Unit *ttUnit;
- ttUnit=(Unit*)UnitList.GetNext(pos);
- ttUnit->Destroy();
- }
- UnitList.RemoveAll();
- }
- void Unit::Recover()
- {
- if(MoveSign==1)
- {
- oldmtx=mtx;oldmty=mty;
- BackGround->Blt(CRect(oldmtx,oldmty,oldmtx+PixelWidth,oldmty+PixelHeight),BackUp,CRect(oldmtx,oldmty,oldmtx+PixelWidth,oldmty+PixelHeight),0,NULL);
- int mx=oldmtx/squaresize;
- int my=oldmty/squaresize;
- int xt,yt;
- Unit *ttUnit;
- POSITION pos;
- CRect rectsrc(mx,my,mx+width+1,my+height+1);
- for(pos=AllUnits.UnitList.GetHeadPosition();pos!=NULL;)
- {
- ttUnit=(Unit*)AllUnits.UnitList.GetNext(pos);
- xt=ttUnit->x; yt=ttUnit->y;
- if((!(xt==x&&yt==y))&&ttUnit->MoveSign==0&&map[yt][xt]!=2)
- {
- CRect temprect;
- if(IntersectRect(temprect,rectsrc,CRect(xt,yt,xt+width+1,yt+height+1)))
- ttUnit->Redraw();
- }
- }
- }
- }
- //-----------------------------------------------------//
- // Signal Define And Functions //
- //-----------------------------------------------------//
- class Signal:public Object
- {
- public:
- int RIDanimation[3];
- int CurrentFrame;
- int Time;
- int TimeCount;
- bool ShowBegin;
- Surface MoveSignal[3];
- void Recover();
- virtual bool Init();
- virtual void Show();
- virtual void Destroy();
- };
- Signal MSignal;
- bool Signal::Init()
- {
- PixelWidth=width*squaresize;
- PixelHeight=height*squaresize;
- DDCOLORKEY KColor;
- KColor.dwColorSpaceLowValue=RGB(0,0,0);
- KColor.dwColorSpaceHighValue=RGB(0,0,0);
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=PixelWidth;
- ddsd.dwHeight=PixelHeight;
- HRESULT hRet;
- hRet=lpDD->CreateSurface(&ddsd,&surface,NULL);
- surface->SetColorKey(DDCKEY_SRCBLT,&KColor);
- for(int i=0;i<=2;i++)
- {
- hRet=lpDD->CreateSurface(&ddsd,&MoveSignal[i],NULL);
- ImageToSurface(MoveSignal[i],RIDanimation[i]);
- }
- if(hRet!=DD_OK) return FALSE;
- ShowBegin=FALSE;
- CurrentFrame=0;
- return TRUE;
- }
- void Signal::Recover()
- {
- if(CurrentFrame==3)
- {
- int x1,y1,x2,y2;
- int uw,uh;
- x1=xstart+x;y1=ystart+y;
- x2=x1+PixelWidth;y2=y1+PixelHeight;
- CurrentFrame=0;ShowBegin=FALSE;
- BackGround->Blt(CRect(x1,y1,x2,y2),BackUp,CRect(x1,y1,x2,y2),0,NULL);
- int xt,yt;
- Unit *ttUnit;
- POSITION posU;
- int xr1,yr1,xr2,yr2;
- xr1=x1/squaresize;
- xr2=x2/squaresize;
- yr1=y1/squaresize;
- yr2=y2/squaresize;
- CRect temprect;
- for(posU=AllUnits.UnitList.GetHeadPosition();posU!=NULL;)
- {
- ttUnit=(Unit*)AllUnits.UnitList.GetNext(posU);
- uw=ttUnit->width;
- uh=ttUnit->height;
- xt=ttUnit->x;
- yt=ttUnit->y;
- if(IntersectRect(temprect,CRect(xr1-1,yr1-1,xr2+1,yr2+1),CRect(xt,yt,xt+uw,yt+uh)))
- {
- bool CannotRedraw=FALSE;
- for(int j=yr1;j<=yr2;j++)
- for(int i=xr1;i<=xr2;i++)
- { if(map[j][i]==2) CannotRedraw=TRUE; }
- if(!CannotRedraw) ttUnit->Redraw();
- }
- }
- }
- }
- void Signal::Show()
- {
- TimeCount++;
- if(TimeCount==Time)
- {
- TimeCount=0;
- int x1,y1,x2,y2,w,h;
- w=width*squaresize;h=height*squaresize;
- x1=xstart+x;y1=ystart+y;
- x2=x1+PixelWidth;y2=y1+PixelHeight;
- if(ShowBegin)
- {
- surface->Blt(CRect(0,0,w,h),MoveSignal[CurrentFrame],CRect(0,0,w,h),0,NULL);
- BackGround->Blt(CRect(x1,y1,x2,y2),surface,CRect(0,0,w,h),DDBLT_KEYSRC,NULL);
- CurrentFrame++;
- }
- }
- }
- void Signal::Destroy()
- {
- if(surface) surface->Release(); surface=NULL;
- for(int i=0;i<=2;i++)
- {
- if(MoveSignal[i]) {MoveSignal[i]->Release();MoveSignal[i]=NULL; }
- }
- }
- void BuildingGroup::Recover()
- {
- POSITION posB;
- POSITION posU;
- Building *tempB;
- Unit *tempU;
- bool FindCover;
- int x1,y1,x2,y2,xtl,ytl,xtn,ytn;
- int i,j,uw,uh;
- for(posB=BuildingList.GetHeadPosition();posB!=NULL;)
- {
- FindCover=FALSE;
- tempB=(Building*)BuildingList.GetNext(posB);
- x1=tempB->x;y1=tempB->y;x2=x1+tempB->width;y2=y1+tempB->height;
- CRect rectsrc(x1,y1,x2,y2);
- CRect temprect;
- for(posU=AllUnits.UnitList.GetHeadPosition();posU!=NULL;)
- {
- tempU=(Unit*)AllUnits.UnitList.GetNext(posU);
- xtl=tempU->lastx; ytl=tempU->lasty;
- xtn=tempU->x ; ytn=tempU->y;
- if(tempU->MoveSign==1)
- {
- uw=tempU->width;
- uh=tempU->height;
- if(IntersectRect(temprect,rectsrc,CRect(xtl,ytl,xtl+uw,ytl+uh)))
- {
- for(j=temprect.top;j<=temprect.bottom;j++)
- for(i=temprect.left;i<=temprect.right;i++)
- if(map[j][i]==2) {FindCover=TRUE; break; }
- }
- if(IntersectRect(temprect,rectsrc,CRect(xtn,ytn,xtn+uw,ytn+uh)))
- {
- for(j=temprect.top;j<=temprect.bottom;j++)
- for(i=temprect.left;i<=temprect.right;i++)
- if(map[j][i]==2) {FindCover=TRUE; break; }
- }
- }
- }
- if(FindCover) tempB->Show(x1,y1);
- }
- }
- class WeaponType:public Object
- {
- public:
- int RIDanimation[5];
- int RIDDirection[20];
- int RIDExplosion[14];
- int Time;
- int DirectionCount;
- float steplen;
- Surface DirectionSurf[20];
- Surface Explosion[14];
- bool Init();
- };
- bool WeaponType::Init()
- {
- HRESULT hRet;
- SurfaceDesc ddsd;
- PixelWidth=width*squaresize;
- PixelHeight=height*squaresize;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=PixelWidth;
- ddsd.dwHeight=PixelHeight;
- DDCOLORKEY KColor;
- KColor.dwColorSpaceLowValue=RGB(0,0,0);
- KColor.dwColorSpaceHighValue=RGB(0,0,0);
- int i;
- for(i=0;i<=DirectionCount-1;i++)
- {
- hRet=lpDD->CreateSurface(&ddsd,&DirectionSurf[i],NULL);
- DirectionSurf[i]->SetColorKey(DDCKEY_SRCBLT,&KColor);
- if(hRet!=DD_OK) return FALSE;
- ImageToSurface(DirectionSurf[i],RIDDirection[i]);
- }
- for(i=0;i<=13;i++)
- {
- hRet=lpDD->CreateSurface(&ddsd,&Explosion[i],NULL);
- Explosion[i]->SetColorKey(DDCKEY_SRCBLT,&KColor);
- if(hRet!=DD_OK) return FALSE;
- ImageToSurface(Explosion[i],RIDExplosion[i]);
- }
- return TRUE;
- }
- class Weapon:public Object
- {
- public:
- int Direction;
- int xfsign,yfsign;
- int Targetx,Targety;
- int Time,TimeCount;
- float oldx,oldy,newx,newy,endx,endy;
- float stepx,stepy,steplen;
- bool AllowShow;
- bool MoveSign;
- bool ExplosionBegin;
- void Recover();
- void Redraw();
- int Explo_FrameCount;
- WeaponType *m_WType;
- virtual bool Init(WeaponType *WType,int sx,int sy,int Ax,int Ay);
- virtual void Show();
- };
- bool Weapon::Init(WeaponType *WType,int sx,int sy,int Ax,int Ay)
- {
- m_WType=WType;
- Time=m_WType->Time;
- TimeCount=0;
- width=m_WType->width;
- height=m_WType->height;
- steplen=m_WType->steplen;
- PixelWidth=m_WType->PixelWidth;
- PixelHeight=m_WType->PixelHeight;
- HRESULT hRet;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=PixelWidth;
- ddsd.dwHeight=PixelHeight;
- hRet=lpDD->CreateSurface(&ddsd,&surface,NULL);
- if(hRet!=DD_OK) return FALSE;
- DDCOLORKEY KColor;
- KColor.dwColorSpaceLowValue=RGB(0,0,0);
- KColor.dwColorSpaceHighValue=RGB(0,0,0);
- surface->SetColorKey(DDCKEY_SRCBLT,&KColor);
- newx=(float)sx*squaresize;
- newy=(float)sy*squaresize;
- endx=(float)Ax*squaresize;
- endy=(float)Ay*squaresize;
- float disx=fabs(endx-newx);
- float disy=fabs(endy-newy);
- float dis=_hypot(disx,disy);
- stepx=0;stepy=0;
- stepx=steplen*disx/dis; stepy=steplen*disy/dis;
- if(endx<newx) { stepx=-stepx; }
- if(endy<newy) { stepy=-stepy; }
- xfsign=0;yfsign=0;
- // Count Directions
- float rx=8;
- float dd=fabs((Ay-sy)/rx);
- for(int i=0;i<=5;i++)
- {
- if(dd>=dir[i]&&dd<=dir[i+1])
- {
- if(Ax>=sx&&Ay>sy) { Direction=5+i; break; }
- if(Ax>=sx&&Ay<sy) { Direction=6-i; break; }
- if(Ax<=sx&&Ay<sy) { Direction=14+i; break; }
- if(Ax<=sx&&Ay>sy) { Direction=15-i; break; }
- //if(Ax<=sx&&Ay<sy) { Direction=5-i; break; }
- }
- }
- if(Ax==sx)
- {
- if(stepy<0) Direction=0;
- if(stepy>0) Direction=10;
- }
- if(Ay==sy)
- {
- if(stepx<0) Direction=15;
- if(stepx>0) Direction=5;
- }
- if(Direction<0||Direction>19) Direction=0;
- //-------------------------------------------//
- surface->Blt(CRect(0,0,PixelWidth,PixelHeight),m_WType->DirectionSurf[Direction],CRect(0,0,PixelWidth,PixelHeight),0,NULL);
- if(hRet!=DD_OK) return FALSE;
- MoveSign=TRUE;
- oldx=newx;oldy=newy;
- Explo_FrameCount=0;
- ExplosionBegin=FALSE;
- return TRUE;
- }
- void Weapon::Show()
- {
- if(ExplosionBegin)
- {
- BackGround->Blt(CRect(newx,newy,newx+PixelWidth,newy+PixelHeight),m_WType->Explosion[Explo_FrameCount],CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- Explo_FrameCount++;
- if(Explo_FrameCount==13) ExplosionBegin=FALSE;
- }
- if(MoveSign) // It is Moving..
- {
- if(xfsign==0) newx=newx+stepx;
- if(yfsign==0) newy=newy+stepy;
- if( ( (stepx>=0) && (newx>=endx)) || ( (stepx<=0) && (newx<=endx) )) xfsign=1;
- if( ( (stepy>=0) &&( newy>=endy)) || ( (stepy<=0) && (newy<=endy) )) yfsign=1;
- if(xfsign==1&&yfsign==1)
- {
- xfsign=0;yfsign=0;
- ExplosionBegin=TRUE;
- MoveSign=FALSE;
- }
- else
- {
- BackGround->Blt(CRect(newx,newy,newx+PixelWidth,newy+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- }
- }
- }
- void Weapon::Recover()
- {
- if(MoveSign||ExplosionBegin)
- {
- oldx=newx;oldy=newy;
- BackGround->Blt(CRect(oldx,oldy,oldx+PixelWidth,oldy+PixelHeight),BackUp,CRect(oldx,oldy,oldx+PixelWidth,oldy+PixelHeight),0,NULL);
- }
- int mx=oldx/squaresize;
- int my=oldy/squaresize;
- int xt,yt;
- int uw,uh;
- Unit *ttUnit;
- POSITION pos;
- CRect rectsrc(mx,my,mx+width+1,my+height+1);
- for(pos=AllUnits.UnitList.GetHeadPosition();pos!=NULL;)
- {
- ttUnit=(Unit*)AllUnits.UnitList.GetNext(pos);
- xt=ttUnit->x; yt=ttUnit->y;
- uw=ttUnit->width;uh=ttUnit->height;
- if((!(xt==x&&yt==y))&&ttUnit->MoveSign==0&&map[yt][xt]!=2)
- {
- CRect temprect;
- if(IntersectRect(temprect,rectsrc,CRect(xt,yt,xt+uw+1,yt+uh+1)))
- ttUnit->Redraw();
- }
- }
- }
- class WeaponGroup
- {
- public:
- int MemberCount;
- CPtrList WeaponList;
- void Recover();
- void Show();
- void Destroy();
- };
- void WeaponGroup::Show()
- {
- POSITION posW,pos2;
- Weapon *ttWeapon;
- for(posW=WeaponList.GetHeadPosition();(pos2=posW)!=NULL;)
- {
- ttWeapon=(Weapon*)WeaponList.GetNext(posW);
- ttWeapon->Show();
- if( (!ttWeapon->MoveSign) && (!ttWeapon->ExplosionBegin))
- {
- ttWeapon->Recover();
- WeaponList.RemoveAt(pos2);
- delete ttWeapon;
- }
- }
- }
- void WeaponGroup::Recover()
- {
- POSITION posW;
- Weapon *ttWeapon;
- for(posW=WeaponList.GetHeadPosition();posW!=NULL;)
- {
- ttWeapon=(Weapon*)WeaponList.GetNext(posW);
- ttWeapon->Recover();
- }
- }
- WeaponGroup AllWeapons;
- WeaponType *Missle;
- void Unit::Show()
- {
- int CurrentOrder=GetOrder();
- switch(CurrentOrder)
- {
- case UHOLDPLACE:
- {
- break;
- }
- case UMOVE:
- {
- int i,j;
- if(StepBegin==1) // from one square to another
- {
- if(xfsign==0) mtx=mtx+stepx;
- if(yfsign==0) mty=mty+stepy;
- if((stepx>0&&mtx>=endpx)||(stepx<0&&mtx<=endpx)) {oldmtx=endpx;mtx=endpx;xfsign=1; }
- if((stepy>0&&mty>=endpy)||(stepy<0&&mty<=endpy)) {oldmty=endpy;mty=endpy;yfsign=1; }
- if(xfsign==1&&yfsign==1) { xfsign=0;yfsign=0;StepBegin=0;oldmtx=-1; }
- else
- {
- if(BeChosed)BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),Circle,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- }
- }
- if(Pathx!=NULL&&MoveSign==1&&StepBegin==0)
- {
- lastx=x;lasty=y;
- if(Pathx->cx!=Targetx||Pathx->cy!=Targety)
- {
- for(j=lasty;j<=lasty+height-1;j++)
- for(i=lastx;i<=lastx+width-1;i++)
- if(map[j][i]==3) map[j][i]=0;
- PATHX *ttPath;
- ttPath=Pathx->Last;
- bool SeekAgain=FALSE;
- if(ttPath!=NULL)
- {
- int xt=ttPath->cx;
- int yt=ttPath->cy;
- for(j=yt;j<=yt+height-1;j++)
- for(i=xt;i<=xt+width-1;i++)
- { if(map[j][i]==3||map[j][i]==1) { SeekAgain=TRUE; break; } }
- }
- if(IsBlock||SeekAgain)
- {
- if(!IsBlock) IsBlock=TRUE;
- StepBegin=0;
- SeekCount++;
- if(SeekCount==SeekInterval)
- {
- SeekCount=0;
- SeekTime++;
- for(j=y;j<=y+height-1;j++)
- for(i=x;i<=x+width-1;i++)
- if(map[j][i]==3) map[j][i]=0;
- if(Seek(Targetx,Targety)) IsBlock=FALSE;
- if(OrderList[OrderCount+1]==UATTACK)
- {
- MoveSign=0; SeekTime=0;
- int r=(int)sqrt((x-Attackx)*(x-Attackx)+(y-Attacky)*(y-Attacky));
- if(r<=9) OrderCount++;
- }
- if(SeekTime==12)
- {
- MoveSign=0; SeekTime=0;
- }
- }
- for(j=y;j<=y+height-1;j++)
- for(i=x;i<=x+width-1;i++)
- if(map[j][i]==0) map[j][i]=3;
- if(BeChosed)BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),Circle,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- return;
- }
- Pathx=Pathx->Last;
- x=Pathx->cx;
- y=Pathx->cy;
- for(j=y;j<=y+height-1;j++)
- for(i=x;i<=x+width-1;i++)
- if(map[j][i]==0) map[j][i]=3;
- endpx =xstart+x*squaresize;
- endpy =ystart+y*squaresize;
- mtx=xstart+lastx*squaresize;
- mty=ystart+lasty*squaresize;
- if(endpx>mtx) {stepx=steplen; Direction=RIGHT;}
- if(endpx<mtx) {stepx=-steplen; Direction=LEFT; }
- if(endpy>mty) {stepy=steplen ; Direction=DOWN; }
- if(endpy<mty) {stepy=-steplen; Direction=UP; }
- if(endpx>mtx&&endpy>mty) Direction=DR;
- if(endpx>mtx&&endpy<mty) Direction=UR;
- if(endpx<mtx&&endpy<mty) Direction=UL;
- if(endpx<mtx&&endpy>mty) Direction=DL;
- surface->Blt(CRect(0,0,PixelWidth,PixelHeight),m_UType->DirectionSurf[Direction],CRect(0,0,PixelWidth,PixelHeight),0,NULL);
- if(BeChosed)BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),Circle,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- StepBegin=1;
- for(j=y;j<=y+height-1;j++)
- for(i=x;i<=x+width-1;i++)
- if(map[j][i]==0) map[j][i]=3;
- }
- else
- {
- if(BeChosed)BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),Circle,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- BackGround->Blt(CRect(mtx,mty,mtx+PixelWidth,mty+PixelHeight),surface,CRect(0,0,PixelWidth,PixelHeight),DDBLT_KEYSRC,NULL);
- AllBuildings.Recover();
- OrderCount++;
- StepBegin=0;
- MoveSign=0;
- for(j=y;j<=y+height-1;j++)
- for(i=x;i<=x+width-1;i++)
- if(map[j][i]==0) map[j][i]=3;
- }
- }
- break;
- } // UMOVE END
- case UATTACK:
- {
- if(FireTimeCount==FireInterval)
- {
- Weapon *ttWeapon;
- ttWeapon=new Weapon;
- ttWeapon->Init(Missle,x+1,y+1,Attackx,Attacky);
- AllWeapons.WeaponList.AddTail(ttWeapon);
- FireTimeCount=0;
- }
- FireTimeCount++;
- break;
- } // UATTACK END
- }; // SWITCH END
- }
- //------------------Signal End-----------------------//
- //------------------------------------------------------------------------//
- BEGIN_MESSAGE_MAP(CMydrawApp, CWinApp)
- //{{AFX_MSG_MAP(MydrawApp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void DestroyOverlay()
- {
- if (Land) { Land->Release(); Land = NULL; }
- if (BackGround) { BackGround->Release(); BackGround=NULL;}
- AllUnits.Destroy();
- AllBuildings.Destroy();
- MSignal.Destroy();
- //for(int i=0;i<=7;i++) { DirectionSurf[i]->Release();DirectionSurf[i]=NULL;}
- // buildingtype release()
- }
- void DestroyPrimary()
- {
- if(PrimaryBack) {PrimaryBack->Release(); PrimaryBack = NULL;}
- if(Primary) {Primary->Release(); Primary = NULL;}
- }
- void InitDirectInput()
- {
- HRESULT hRet;
- hRet=DirectInputCreateEx( (HINSTANCE)GetWindowLong(mWnd, GWL_HINSTANCE ),
- DIRECTINPUT_VERSION, IID_IDirectInput7, (LPVOID*)&DInput, NULL);
- if(hRet!=DI_OK) MessageBox(NULL,"Create InputDevice Failed!","Error",MB_OK);
- hRet=DInput->CreateDeviceEx(GUID_SysMouse,IID_IDirectInputDevice2,
- (VOID**)&Mouse, NULL );
- if(hRet!=DI_OK) TRACE("Mouseinput create failed!n");
- hRet=Mouse->SetDataFormat(&c_dfDIMouse);
- if(hRet!=DI_OK) TRACE("Mouseinput set dataformat failed!n");
- hRet=Mouse->SetCooperativeLevel(mWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
- if(hRet!=DI_OK) TRACE("mouse SetCooperativeLevel Failed!n");
- DIPROPDWORD Property;
- Property.diph.dwSize=sizeof(DIPROPDWORD);
- Property.diph.dwHeaderSize=sizeof(DIPROPHEADER);
- Property.diph.dwObj=0;
- Property.diph.dwHow=DIPH_DEVICE;
- Property.dwData=128;
- Mouse->SetProperty(DIPROP_BUFFERSIZE,&Property.diph);
- }
- LPCSTR InitDDraw()
- {
- InitDirectInput();
- HRESULT Result;
- Result = DirectDrawCreateEx(NULL, (VOID**)&lpDD, IID_IDirectDraw7, NULL);
- if(Result!=DD_OK) return "Create DirectDrawObject Failed!";
- Result = lpDD->SetCooperativeLevel(mWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN);
- if(Result!=DD_OK) return "SetCooperativeLevel Failed!";
- Result = lpDD->SetDisplayMode(800,600,16,0,0);
- if(Result!=DD_OK) return "SetDisplayMode Failed!";
- return "OK";
- }
- LPCSTR InitPrimarySurface()
- {
- HRESULT hRet;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags = DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
- ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
- ddsd.dwBackBufferCount = 1;
- hRet=lpDD->CreateSurface(&ddsd, &Primary, NULL);
- if(hRet!=DD_OK) return "Create Primary Surface Failed!";
- DDSCAPS2 ddsCaps;
- ZeroMemory(&ddsCaps,sizeof(ddsCaps));
- ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
- hRet = Primary->GetAttachedSurface(&ddsCaps, &PrimaryBack);
- if(hRet!=DD_OK)return "Create BackBuffer Action Failed";
- return "OK";
- }
- LPCSTR InitOverlaySurface()
- {
- HRESULT hRet;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=1200;
- ddsd.dwHeight=1200;
- hRet=lpDD->CreateSurface(&ddsd,&BackGround,NULL);
- hRet=lpDD->CreateSurface(&ddsd,&BackUp,NULL);
- if(hRet!=DD_OK) return "Create BackGround Surface Action Failed";
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=400;
- ddsd.dwHeight=400;
- hRet=lpDD->CreateSurface(&ddsd,&Land,NULL);
- if(hRet!=DD_OK) return "Create Land Surface Action Failed";
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
- ddsd.ddsCaps.dwCaps=DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth=60;
- ddsd.dwHeight=60;
- hRet=lpDD->CreateSurface(&ddsd,&Circle,NULL);
- DDCOLORKEY KColor;
- KColor.dwColorSpaceLowValue=RGB(0,0,0);
- KColor.dwColorSpaceHighValue=RGB(0,0,0);
- Circle->SetColorKey(DDCKEY_SRCBLT,&KColor);
- if(hRet!=DD_OK) return "Create Circle Surface Action Failed";
- return "OK";
- }
- BOOL LoadImageOntoSurface(Surface lpdds,LPSTR lpstrResID,int Bmpx,int Bmpy)
- {
- HBITMAP hbm;
- HDC hdcImage= NULL;
- HDC hdcSurf = NULL;
- BOOL bRetVal = FALSE;
- HRESULT ddrval;
- DDSURFACEDESC2 ddsd;
- INIT_DIRECTDRAW_STRUCT(ddsd);
- ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
- ddrval = lpdds->GetSurfaceDesc(&ddsd);
- if(ddrval!=DD_OK)
- {
- MessageBox(NULL,"HATE YOU","error",MB_OK);
- }
- hbm = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(lpstrResID), IMAGE_BITMAP, Bmpx, Bmpy, LR_DEFAULTCOLOR);//LR_CREATEDIBSECTION);
- hdcImage = CreateCompatibleDC(NULL);
- SelectObject(hdcImage, hbm);
- ddrval = lpdds->GetDC(&hdcSurf);
- if(BitBlt(hdcSurf, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, 0, 0, SRCCOPY)==FALSE)
- { MessageBox(NULL,"HATE YOU","error",MB_OK); }
- DeleteDC(hdcImage);
- DeleteObject(hbm);
- lpdds->ReleaseDC(hdcSurf);
- return TRUE;
- }
- void PrepareMap(int randseed)
- {
- int i,j;
- for(i=0;i<=2;i++)
- for(j=0;j<=2;j++)
- BackGround->Blt(CRect(xstart+i*400,ystart+j*400,xstart+squaresize*20+i*400,ystart+squaresize*20+j*400),Land,CRect(0,0,400,400),0,NULL);
- i=2;j=4;
- POSITION posB;
- Building *NewBuild;
- for(posB=AllBuildings.BuildingList.GetHeadPosition();posB!=NULL;)
- {
- j=j+3;
- NewBuild=(Building*)AllBuildings.BuildingList.GetNext(posB);
- NewBuild->Show(i,j);
- i=i+15;
- if(i>=50) i=2;
- }
- BackUp->Blt(CRect(0,0,1200,1200),BackGround,CRect(0,0,1200,1200),0,NULL);
- POSITION posU;
- Unit *ttUnit;
- for(posU=AllUnits.UnitList.GetHeadPosition();posU!=NULL;)
- {
- ttUnit=(Unit*)AllUnits.UnitList.GetNext(posU);
- ttUnit->Redraw();
- }
- }
- void InitGraph()
- {
- CString Result;
- Result=InitDDraw();
- if(Result!="OK"){ MessageBox(mWnd,Result,"error",MB_OK); return ;}
- Result=InitPrimarySurface();
- if(Result!="OK"){ MessageBox(mWnd,Result,"error",MB_OK); return ;}
- Result=InitOverlaySurface();
- if(Result!="OK"){ MessageBox(mWnd,Result,"error",MB_OK); return ;}
- int i;
- Missle=new WeaponType;
- Missle->width=1;
- Missle->height=1;
- Missle->Time=1;
- Missle->steplen=8;
- Missle->DirectionCount=20;
- for(i=0;i<=Missle->DirectionCount-1;i++) Missle->RIDDirection[i]=297+i;
- for(i=0;i<=13;i++) Missle->RIDExplosion[i]=256+i;
- Missle->Init();
- UnitType *Tank,*SpaceCar;
- Tank=new UnitType;
- Tank->width=3;
- Tank->height=3;
- Tank->RIDDirection[0]=227;
- Tank->Time=2;
- Tank->steplen=8;
- Tank->stepx=Tank->steplen;
- Tank->stepy=Tank->steplen;
- Tank->RIDDirection[1]=226;
- Tank->RIDDirection[2]=225;
- Tank->RIDDirection[3]=224;
- Tank->RIDDirection[4]=223;
- Tank->RIDDirection[5]=222;
- Tank->RIDDirection[6]=228;
- Tank->RIDDirection[7]=221;
- Tank->Init();
- SpaceCar=new UnitType;
- SpaceCar->Time=2;
- SpaceCar->steplen=5;
- SpaceCar->stepx=SpaceCar->steplen;
- SpaceCar->stepy=SpaceCar->steplen;
- SpaceCar->width=3;
- SpaceCar->height=3;
- SpaceCar->RIDDirection[0]=235;
- SpaceCar->RIDDirection[1]=234;
- SpaceCar->RIDDirection[2]=233;
- SpaceCar->RIDDirection[3]=232;
- SpaceCar->RIDDirection[4]=231;
- SpaceCar->RIDDirection[5]=230;
- SpaceCar->RIDDirection[6]=236;
- SpaceCar->RIDDirection[7]=229;
- SpaceCar->Init();
- LoadImageOntoSurface(Land,MAKEINTRESOURCE(193),400,400);
- LoadImageOntoSurface(Circle,MAKEINTRESOURCE(270),60,60);
- int j=0;
- for(i=0;i<=7;i++)
- {
- Unit *car;
- car=new Unit;
- car->x=i*4+3;
- car->y=3;
- if(j>=8) j=0;
- car->Direction=j;
- j++;
- car->Init(Tank);
- AllUnits.UnitList.AddTail(car);
- }
- for(i=0;i<=2;i++)
- {
- Unit *car;
- car=new Unit;
- car->x=i*4+40;
- car->y=3;
- if(j>=8) j=0;
- car->Direction=j;
- j++;
- car->Init(Tank);
- AllUnits.UnitList.AddTail(car);
- }
- MSignal.RIDanimation[0]=194;
- MSignal.RIDanimation[1]=196;
- MSignal.RIDanimation[2]=195;
- MSignal.CurrentFrame=0;
- MSignal.width=2;
- MSignal.height=2;
- MSignal.Time=2;
- MSignal.TimeCount=0;
- MSignal.Init();
- BuildingType *m_pyramid,*m_tower,*m_wonder,*m_crackship;
- m_pyramid= new BuildingType;
- m_tower = new BuildingType;
- m_wonder = new BuildingType;
- m_crackship = new BuildingType;
- m_pyramid->width=11;
- m_pyramid->height=7;
- m_pyramid->name="pyramid";
- m_pyramid->RIDnormal=216;
- int arraytemp0[MAXH][MAXL]={0,0,0,2,2,2,2,2,0,0,0,0,0,
- 0,0,2,2,2,2,2,2,2,2,2,0,0,
- 0,0,2,1,1,1,1,1,2,2,2,0,0,
- 1,1,1,1,1,1,1,1,1,1,1,0,0,
- 0,1,1,1,1,1,1,1,1,1,1,0,0,
- 0,0,0,1,1,1,1,1,1,1,0,0,0,
- 0,0,0,0,1,1,1,1,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- m_pyramid->Init(arraytemp0);
- m_tower->width=3;
- m_tower->height=6;
- m_tower->name="Tower";
- m_tower->RIDnormal=217;
- int arraytemp1[MAXH][MAXL]={4,4,4,0,0,0,0,0,0,0,0,0,0,
- 4,4,4,0,0,0,0,0,0,0,0,0,0,
- 4,4,4,0,0,0,0,0,0,0,0,0,0,
- 4,4,4,0,0,0,0,0,0,0,0,0,0,
- 4,4,4,0,0,0,0,0,0,0,0,0,0,
- 4,4,4,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- /*int arraytemp1[MAXH][MAXL]={2,2,2,0,0,0,0,0,0,0,0,0,0,
- 2,2,2,0,0,0,0,0,0,0,0,0,0,
- 2,2,2,0,0,0,0,0,0,0,0,0,0,
- 2,1,2,0,0,0,0,0,0,0,0,0,0,
- 1,1,1,0,0,0,0,0,0,0,0,0,0,
- 1,1,1,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 }; */
- m_tower->Init(arraytemp1);
- m_wonder->width=8;
- m_wonder->height=11;
- m_wonder->name="Wonder";
- m_wonder->RIDnormal=219;
- int arraytemp2[MAXH][MAXL]={0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,2,2,2,2,0,0,0,0,0,0,0,0,
- 0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,0,0,2,2,2,0,0,0,0,0,0,0,
- 0,0,0,2,2,2,0,0,0,0,0,0,0,
- 0,0,0,1,1,0,0,0,0,0,0,0,0,
- 0,1,1,1,1,1,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,1,1,0,0,0,0,0,
- 0,1,1,1,1,1,1,0,0,0,0,0,0,
- 0,0,1,1,1,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- m_wonder->Init(arraytemp2);
- m_crackship->width=6;
- m_crackship->height=6;
- m_crackship->name="CrackShip";
- m_crackship->RIDnormal=220;
- /*int arraytemp3[MAXH][MAXL]={0,0,0,2,2,0,0,0,0,0,0,0,0,
- 0,0,2,2,2,0,0,0,0,0,0,0,0,
- 0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,1,1,1,2,2,0,0,0,0,0,0,0,
- 0,1,1,1,1,0,0,0,0,0,0,0,0,
- 0,0,1,1,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 }; */
- int arraytemp3[MAXH][MAXL]={0,0,0,4,4,0,0,0,0,0,0,0,0,
- 0,0,4,4,4,0,0,0,0,0,0,0,0,
- 0,4,4,4,4,4,0,0,0,0,0,0,0,
- 0,4,4,4,4,4,0,0,0,0,0,0,0,
- 0,4,4,4,4,0,0,0,0,0,0,0,0,
- 0,0,4,4,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- m_crackship->Init(arraytemp3);
- for(i=0;i<=2;i++)
- {
- Building *NewBuild;
- NewBuild=new Building;
- NewBuild->Init(m_pyramid);
- AllBuildings.BuildingList.AddTail(NewBuild);
- BuildingCount++;
- }
- Building *NewBuild;
- NewBuild=new Building;
- NewBuild->Init(m_wonder);
- AllBuildings.BuildingList.AddTail(NewBuild);
- BuildingCount++;
- for(i=0;i<=3;i++)
- {
- Building *NewBuild;
- NewBuild=new Building;
- NewBuild->Init(m_tower);
- AllBuildings.BuildingList.AddTail(NewBuild);
- BuildingCount++;
- }
- Building *NewBuild1;
- NewBuild1=new Building;
- NewBuild1->Init(m_crackship);
- AllBuildings.BuildingList.AddTail(NewBuild1);
- BuildingCount++;
- CBrush br(RGB(0,0,0));
- CRect rc;
- HDC hdc;
- Primary->GetDC(&hdc);
- GetWindowRect(mWnd,&rc);
- FillRect(hdc,&rc, (HBRUSH)br);
- Primary->ReleaseDC(hdc);
- PrimaryBack->GetDC(&hdc);
- GetWindowRect(mWnd,&rc);
- FillRect(hdc,&rc, (HBRUSH)br);
- PrimaryBack->ReleaseDC(hdc);
- BackGround->GetDC(&hdc);
- GetWindowRect(mWnd,&rc);
- FillRect(hdc,&rc, (HBRUSH)br);
- BackGround->ReleaseDC(hdc);
- PrepareMap(0);
- }
- void ShowString(int x,int y,char *str)
- {
- HDC hdc;
- Primary->GetDC(&hdc);
- TextOut(hdc,x,y,"####",4);
- TextOut(hdc,x,y,str,strlen(str));
- Primary->ReleaseDC(hdc);
- PrimaryBack->GetDC(&hdc);
- TextOut(hdc,x,y,"####",4);
- TextOut(hdc,x,y,str,strlen(str));
- PrimaryBack->ReleaseDC(hdc);
- }
- int GetRand(int s,int e)
- {
- int nRand;
- float tempnum,tempnum1;
- nRand=rand();
- tempnum=(float)e/RAND_MAX;
- tempnum1=(float)nRand*tempnum+0.5F;
- return int(tempnum1);
- }
- void ShowChooseRect()
- {
- HDC hdc;
- POINT cp;
- BackGround->GetDC(&hdc);
- CPen cpen;
- cpen.CreatePen(PS_SOLID,1,RGB(255,255,255));
- SelectObject(hdc,cpen);
- MoveToEx(hdc,ChooseSx,ChooseSy,&cp);
- int x1,y1;
- x1=Mousex+xscroll-xstart;
- y1=Mousey+yscroll-ystart;
- if(x1<=2) x1=1;
- if(x1>MapPixelWidth-1) x1=MapPixelWidth-1;
- if(y1<1) y1=1;
- if(y1>MapPixelHeight-1) y1=MapPixelHeight-1;
- LineTo(hdc,x1,ChooseSy);
- LineTo(hdc,x1,y1);
- LineTo(hdc,ChooseSx,y1);
- LineTo(hdc,ChooseSx,ChooseSy);
- BackGround->ReleaseDC(hdc);
- cpen.DeleteObject();
- OldCx=x1;
- OldCy=y1;
- }
- void ShowChooseRecover()
- {
- int x1,y1,x2,y2,xt,yt,uw,uh;
- if(ChooseSx<=OldCx) { x1=ChooseSx;x2=OldCx;}
- else { x2=ChooseSx;x1=OldCx;}
- if(ChooseSy<=OldCy) { y1=ChooseSy;y2=OldCy;}
- else { y2=ChooseSy;y1=OldCy;}
- int range=1;
- BackGround->Blt(CRect(x1-range,y1-range,x2+range,y1+range),BackUp,CRect(x1-range,y1-range,x2+range,y1+range),0,NULL);
- BackGround->Blt(CRect(x2-range,y1-range,x2+range,y2+range),BackUp,CRect(x2-range,y1-range,x2+range,y2+range),0,NULL);
- BackGround->Blt(CRect(x1-range,y2-range,x2+range,y2+range),BackUp,CRect(x1-range,y2-range,x2+range,y2+range),0,NULL);
- BackGround->Blt(CRect(x1-range,y1-range,x1+range,y2+range),BackUp,CRect(x1-range,y1-range,x1+range,y2+range),0,NULL);
- POSITION posU;
- Unit *ttUnit;
- int RectStartx=x1/squaresize;
- int RectEndx=x2/squaresize;
- int RectStarty=y1/squaresize;
- int RectEndy=y2/squaresize;
- for(posU=AllUnits.UnitList.GetHeadPosition();posU!=NULL;)
- {
- ttUnit=(Unit*)AllUnits.UnitList.GetNext(posU);
- xt=ttUnit->x;yt=ttUnit->y;
- uw=ttUnit->width;uh=ttUnit->height;
- CRect *temprect=new CRect;
- IntersectRect(temprect,CRect(RectStartx,RectStarty,RectEndx,RectStarty+1),CRect(xt,yt,xt+uw,yt+uh));
- if(!temprect->IsRectNull())
- { ttUnit->Redraw(); }
- IntersectRect(temprect,CRect(RectStartx,RectEndy,RectEndx,RectEndy+1),CRect(xt,yt,xt+uw,yt+uh));
- if(!temprect->IsRectNull())
- { ttUnit->Redraw(); }
- IntersectRect(temprect,CRect(RectStartx,RectStarty,RectStartx+1,RectEndy),CRect(xt,yt,xt+uw,yt+uh));
- if(!temprect->IsRectNull())
- { ttUnit->Redraw(); }
- IntersectRect(temprect,CRect(RectEndx,RectStarty,RectEndx+1,RectEndy),CRect(xt,yt,xt+uw,yt+uh));
- if(!temprect->IsRectNull())
- { ttUnit->Redraw(); }
- delete temprect;
- }
- }
- //------------------------- Begin------------------------------------------------------------//
- CMydrawApp theApp;
- CMydrawApp::CMydrawApp()
- {
- }
- BOOL CMydrawApp::InitInstance()
- {
- m_pMainWnd=new CMydrawFrame();
- InitGraph();
- m_pMainWnd->ShowWindow(SW_NORMAL);
- m_pMainWnd->UpdateWindow();
- xlimit=1200-800-1;
- ylimit=1200-480-1;
- xscroll=0;
- yscroll=0;
- DrawLittleMap();
- PrimaryBack->Blt(CRect(SpaceStartx,SpaceStarty,SpaceEndx,SpaceEndy)
- ,BackGround,CRect(xscroll,yscroll,xscroll+SpaceEndx,yscroll+SpaceEndy),0,NULL);
- Primary->Flip(NULL,0);
- return TRUE;
- }
- BOOL CMydrawApp::OnIdle(LONG)
- {
- int OldMx=Mousex;
- int OldMy=Mousey;
- POINT mpp;
- GetCursorPos(&mpp);
- Mousex=mpp.x;
- Mousey=mpp.y;
- bool Scroll=FALSE;
- if(Mousex!=OldMx||Mousey!=OldMy)
- {
- if(Mousex>=SpaceEndx)
- {
- Mousex=SpaceEndx-1;
- if(xscroll<xlimit) {xscroll+=ScrollStep; Scroll=TRUE;}
- }
- if(Mousex<=SpaceStartx)
- {
- Mousex=SpaceStartx+1;
- if(xscroll>1) {xscroll-=ScrollStep; Scroll=TRUE;}
- }
- if(Mousey>=599)
- {
- Mousey=598;
- if(yscroll<ylimit) {yscroll+=ScrollStep; Scroll=TRUE;}
- }
- if(Mousey<=0)
- {
- Mousey=1;
- if(yscroll>1) {yscroll-=ScrollStep; Scroll=TRUE;}
- }
- if(Scroll)
- {
- UpdateFrame=TRUE;
- Scroll=FALSE;
- }
- if(BeginChoose) UpdateFrame=TRUE;
- }
- if(TimeIsUp||UpdateFrame)
- {
- if(BeginChoose) ShowChooseRecover();
- if(TimeIsUp)
- {
- if(MSignal.ShowBegin) MSignal.Recover();
- AllWeapons.Recover();
- AllUnits.Recover();
- AllUnits.Show();
- AllBuildings.Recover();
- AllWeapons.Show();
- if(MSignal.ShowBegin) MSignal.Show();
- TimeIsUp=FALSE;
- DrawLittleMap();
- }
- if(BeginChoose) { ShowChooseRect(); }
- PrimaryBack->Blt(CRect(SpaceStartx,SpaceStarty,SpaceEndx,SpaceEndy),BackGround,CRect(xscroll,yscroll,xscroll+SpaceEndx,yscroll+SpaceEndy),0,NULL);
- Primary->Flip(NULL,0);
- UpdateFrame=FALSE;
- }
- return TRUE;
- }
- BEGIN_MESSAGE_MAP(CMydrawFrame,CFrameWnd)
- ON_WM_ACTIVATE()
- ON_WM_TIMER()
- ON_WM_KEYDOWN()
- ON_WM_LBUTTONDOWN()
- ON_WM_RBUTTONDOWN()
- ON_WM_LBUTTONUP()
- END_MESSAGE_MAP()
- CMydrawFrame::CMydrawFrame()
- {
- CString sClassName;
- sClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,LoadCursor(0, IDC_ARROW),
- (HBRUSH )GetStockObject(BLACK_BRUSH), LoadIcon(AfxGetInstanceHandle(), NULL));
- CreateEx( WS_EX_TOPMOST, sClassName, "asap", WS_POPUP, 0,0,100,100,NULL,NULL);
- mWnd=m_hWnd;
- SetTimer(1,BaseTime,NULL);
- }
- void CMydrawFrame::OnActivate(UINT nState,CWnd *pWndOther,BOOL bMinimized)
- {
- if(nState!=WA_INACTIVE)
- {
- if(Mouse) Mouse->Acquire();
- }
- }
- void CMydrawFrame::OnRButtonDown(UINT fl,CPoint mp)
- {
- ChooseGroup=FALSE;
- POSITION posU;
- Unit *ttUnit;
- int sign=0;
- for(posU=ChosedGroup.UnitList.GetHeadPosition();posU!=NULL;)
- {
- ttUnit=(Unit*)ChosedGroup.UnitList.GetNext(posU);
- ttUnit->BeChosed=FALSE;
- sign=0;
- if(ttUnit->MoveSign==0){ ttUnit->MoveSign=1; sign=1;}
- ttUnit->Recover();
- if(sign==1) ttUnit->MoveSign=0;
- ttUnit->Redraw();
- }
- }
- void CMydrawFrame::OnLButtonUp(UINT fl,CPoint mp)
- {
- if(BeginChoose)
- {
- ShowChooseRecover();
- BeginChoose=FALSE;
- POSITION posU;
- int x1,y1,x2,y2,xt,yt;
- if(ChooseSx<=OldCx) { x1=ChooseSx;x2=OldCx;}
- else { x2=ChooseSx;x1=OldCx;}
- if(ChooseSy<=OldCy) { y1=ChooseSy;y2=OldCy;}
- else { y2=ChooseSy;y1=OldCy;}
- int RectStartx=x1/squaresize;
- int RectEndx=x2/squaresize;
- int RectStarty=y1/squaresize;
- int RectEndy=y2/squaresize;
- ChosedGroup.UnitList.RemoveAll();
- ChosedGroup.MemberCount=0;
- Unit *ttUnit;
- for(posU=AllUnits.UnitList.GetHeadPosition();posU!=NULL;)
- {
- ttUnit=(Unit*)AllUnits.UnitList.GetNext(posU);
- ttUnit->BeChosed=FALSE;
- xt=ttUnit->x;yt=ttUnit->y;
- if(xt>=RectStartx&&xt<=RectEndx&&yt>=RectStarty&&yt<=RectEndy)
- {
- ttUnit->BeChosed=TRUE;
- ttUnit->Redraw();
- ChosedGroup.UnitList.AddTail(ttUnit);
- ChosedGroup.MemberCount++;
- }
- if(ChosedGroup.MemberCount>0) ChooseGroup=TRUE; else ChooseGroup=FALSE;
- // ShowInt(20,500,ChosedGroup.MemberCount);
- }
- }
- }
- void CMydrawFrame::OnLButtonDown(UINT fl,CPoint mp)
- {
- SetCapture();
- Mousex=mp.x;
- Mousey=mp.y;
- int x1,y1,x,y;
- x=(Mousex+xscroll-xstart)/squaresize;
- y=(Mousey+yscroll-ystart)/squaresize;
- if(!ChooseGroup)
- {
- BeginChoose=TRUE;
- ChooseSx=Mousex+xscroll-xstart;
- ChooseSy=Mousey+yscroll-ystart;
- }
- //------------------ Group have been chosed ----------------------------//
- if(ChooseGroup)
- {
- int uu1=x,uu2=y;
- if(x>=0&&y>=0&&x<=(MapWidth-1)&&y<=(MapHeight-1))
- {
- if(map[y][x]==0)
- {
- if(MSignal.ShowBegin==FALSE)
- { MSignal.ShowBegin=TRUE;
- MSignal.x=Mousex-20+xscroll+xstart;
- MSignal.y=Mousey-20+yscroll+ystart;
- }
- x1=xstart+x*squaresize-xscroll;
- y1=ystart+y*squaresize-yscroll;
- //SignalSign=1;
- ChosedGroup.MoveTo(uu1,uu2,CONCENTRATE);
- }
- if(map[y][x]==4)
- {
- ChosedGroup.MoveTo(uu1,uu2,SURROUND);
- }
- }
- }
- }
- //----------------------------------------------------------------------//
- void CMydrawFrame::OnTimer(UINT nIDEvent)
- {
- TimeIsUp=TRUE;
- }
- void CMydrawFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if(nChar==VK_ESCAPE)
- {
- DestroyOverlay();
- DestroyPrimary();
- if(DInput) {DInput->Release(); DInput=NULL;}
- delete lpDD;
- PostMessage(WM_DESTROY,0,0);
- }
- }
- /*int step=2;
- ttUnit=(Unit*)ChosedGroup.UnitList.GetNext(pos);
- uy=ttUnit->y;
- ux=ttUnit->x;
- uw=ttUnit->width;
- uh=ttUnit->height;
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- if(!ttUnit->Seek(uu1,uu2))
- { for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]!=2) map[j][i]=3; }}
- while(pos!=NULL)
- {
- radio+=step;
- xt=uu1-radio;
- for(yt=uu2-radio;yt<=uu2+radio;yt+=step)
- if(map[yt][xt]==0)
- {
- if(xt>=0&&yt>=0&&xt<=(MapWidth-1)&&yt<=(MapHeight-1))
- {
- if(pos==NULL) return;
- ttUnit=(Unit*)ChosedGroup.UnitList.GetNext(pos);
- uy=ttUnit->y;
- ux=ttUnit->x;
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- if(!ttUnit->Seek(xt,yt))
- { for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]!=2) map[j][i]=3; }}
- }
- }
- xt=uu1+radio;
- for(yt=uu2-radio;yt<=uu2+radio;yt+=step)
- if(map[yt][xt]==0)
- {
- if(xt>=0&&yt>=0&&xt<=(MapWidth-1)&&yt<=(MapHeight-1))
- {
- if(pos==NULL) return;
- ttUnit=(Unit*)ChosedGroup.UnitList.GetNext(pos);
- uy=ttUnit->y;ux=ttUnit->x;
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- if(!ttUnit->Seek(xt,yt))
- {for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]!=2) map[j][i]=3; }}
- }
- }
- yt=uu2-radio;
- for(xt=uu1-radio+1;xt<=uu1+radio-1;xt+=step)
- if(map[yt][xt]==0)
- {
- if(pos==NULL) return;
- if(xt>=0&&yt>=0&&xt<=(MapWidth-1)&&yt<=(MapHeight-1))
- {
- ttUnit=(Unit*)ChosedGroup.UnitList.GetNext(pos);
- uy=ttUnit->y;ux=ttUnit->x;
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- if(!ttUnit->Seek(xt,yt))
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]!=2) map[j][i]=3; }
- }
- }
- yt=uu2+radio;
- for(xt=uu1-radio+1;xt<=uu1+radio-1;xt+=step)
- if(map[yt][xt]==0)
- {
- if(xt>=0&&yt>=0&&xt<=(MapWidth-1)&&yt<=(MapHeight-1))
- {
- if(pos==NULL) return;
- ttUnit=(Unit*)ChosedGroup.UnitList.GetNext(pos);
- uy=ttUnit->y;ux=ttUnit->x;
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]==3) map[j][i]=0; }
- if(!ttUnit->Seek(xt,yt))
- for(j=uy;j<=uy+uh-1;j++) for(i=ux;i<=ux+uw-1;i++) {if(map[j][i]!=2) map[j][i]=3; }
- }
- }
- }
- /*n=2;
- Building[n].width=8;
- Building[n].height=5;
- Building[n].name="Water";
- Building[n].RIDnormal=218;
- int arraytemp2[MAXH][MAXL]={0,0,1,1,1,0,0,0,0,0,0,0,0,
- 0,1,1,1,1,1,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,0,1,0,0,0,0,0,
- 0,1,1,1,1,1,1,1,0,0,0,0,0,
- 0,0,0,1,1,1,1,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0};
- Building[n].Init(arraytemp2);
- n=3;
- Building[n].width=3;
- Building[n].height=6;
- Building[n].name="Tower2";
- Building[n].RIDnormal=217;
- int arraytemp3[MAXH][MAXL]={0,2,0,0,0,0,0,0,0,0,0,0,0,
- 2,2,2,0,0,0,0,0,0,0,0,0,0,
- 2,2,2,0,0,0,0,0,0,0,0,0,0,
- 2,2,2,0,0,0,0,0,0,0,0,0,0,
- 1,1,1,0,0,0,0,0,0,0,0,0,0,
- 0,1,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- Building[n].Init(arraytemp3);
- n=4;
- Building[n].width=5;
- Building[n].height=5;
- Building[n].name="House1";
- Building[n].RIDnormal=186;
- int arraytemp4[MAXH][MAXL]={2,2,2,2,2,0,0,0,0,0,0,0,0,
- 2,2,1,1,1,0,0,0,0,0,0,0,0,
- 1,1,1,1,1,0,0,0,0,0,0,0,0,
- 1,1,1,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- Building[n].Init(arraytemp4);
- n=5;
- Building[n].width=5;
- Building[n].height=5;
- Building[n].name="House2";
- Building[n].RIDnormal=186;
- int arraytemp5[MAXH][MAXL]={2,2,2,2,2,0,0,0,0,0,0,0,0,
- 2,2,1,1,1,0,0,0,0,0,0,0,0,
- 1,1,1,1,1,0,0,0,0,0,0,0,0,
- 1,1,1,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- Building[n].Init(arraytemp5);
- n=6;
- Building[n].width=8;
- Building[n].height=11;
- Building[n].name="House2";
- Building[n].RIDnormal=219;
- int arraytemp6[MAXH][MAXL]={0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,2,2,2,2,0,0,0,0,0,0,0,0,
- 0,2,2,2,2,2,0,0,0,0,0,0,0,
- 0,0,0,2,2,2,0,0,0,0,0,0,0,
- 0,0,0,2,2,2,0,0,0,0,0,0,0,
- 0,0,0,1,1,0,0,0,0,0,0,0,0,
- 0,1,1,1,1,1,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,1,1,0,0,0,0,0,
- 0,1,1,1,1,1,1,0,0,0,0,0,0,
- 0,0,1,1,1,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0 };
- Building[n].Init(arraytemp6);
- if(abs(xt-ux)<=13&&abs(yt-uy)<=13)
- {
- ShowInt(30,530,777);
- Weapon *ttWeapon;
- ttWeapon=new Weapon;
- ttWeapon->newx=ux*squaresize;
- ttWeapon->newy=uy*squaresize;
- ttWeapon->endx=xt*squaresize;
- ttWeapon->endy=yt*squaresize;
- ttWeapon->Init(ux,uy,xt,yt,Missle);
- ttWeapon->MoveSign=TRUE;
- AllWeapons.WeaponList.AddTail(ttWeapon);
- }*/