⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pathfinder2d.cpp

📁 一个VC写A*寻路的程序库
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				*(d+1)=color;
				*(d+clientwidth)=color;
				*(d+clientwidth+1)=color;
				d-=2;
				s--;
			}
			spush-=clientwidth;
			s=spush;
			dpush-=clientwidth<<1;
			d=dpush;
		}
	}
}

//
void Presearch_View(LPBYTE pwBits, HDC hdc,HWND hWnd)
{
	if(pwBits)
	{
		//
		RECT rt;
		GetClientRect(hWnd, &rt);
		int clientwidth = (rt.right-rt.left);
		int clientheight = (rt.bottom-rt.top);
		
		COLORREF background;
		COLORREF foreground;
		vSetup->get_colorscheme_colors(background,foreground);
		HBRUSH hbrBkGnd = CreateSolidBrush(background);
		FillRect(hdc, &rt, hbrBkGnd);
		DeleteObject(hbrBkGnd);
		SetBkColor(hdc,background);
		SetTextColor(hdc,foreground);
		
		//
		TCHAR szStatusLine[1024];
		GetClientRect(hWnd, &rt);
		sprintf(szStatusLine,"%d groups of areas unpathable to each other",vSetup->presearch_maxgroup);
		DrawText(hdc, szStatusLine, strlen(szStatusLine), &rt, DT_CENTER);
		
		// draw world map
		_RGBA *pbegin=(_RGBA *)pwBits+(clientwidth>>1)-(WIDTH)+ ((clientheight>>1)-(HEIGHT))*clientwidth;
		DWORD *tmp=(DWORD *)pbegin;
		_RGBA *ppush=pbegin;
		for(int y=0;y<HEIGHT;y++)
		{
			for(int x=0;x<WIDTH;x++)
			{
				BYTE group=vSetup->world[y][x].group;
				DWORD d;
				if(group==NO_GROUP)
					d=0x0000ff00;
				else if(group==IMPASSABLE_GROUP)
					d=0x00ff0000;
				else
				{// hmm. this right?
					BYTE r=((group&1)<<7) + ((group& 8)<<2);
					BYTE g=((group&2)<<6) + ((group&16)<<1) + ((group&64)>>6);
					BYTE b=((group&4)<<5) + ((group&32)<<0) + ((group&128)>>8);
					d=(r<<16) | (g<<8) | b;
				}
				*tmp=d;
				*(tmp+1)=d;
				*(tmp+clientwidth)=d;
				*(tmp+clientwidth+1)=d;
				tmp+=2;
			}
			ppush+=(clientwidth<<1);
			tmp=(DWORD *)ppush;
		}
	}
}



//
void DrawMode(LPBYTE pwBits, HDC hdc,HWND hWnd)
{
	if(pwBits)
	{
		//
		RECT rt;
		GetClientRect(hWnd, &rt);
		int clientwidth = (rt.right-rt.left);
		
		COLORREF background;
		COLORREF foreground;
		vSetup->get_colorscheme_colors(background,foreground);
		HBRUSH hbrBkGnd = CreateSolidBrush(background);
		FillRect(hdc, &rt, hbrBkGnd);
		DeleteObject(hbrBkGnd);
		SetBkColor(hdc,background);
		SetTextColor(hdc,foreground);
		
		//
		_RGBA *p,*ppush,*pbegin;
		pbegin=(_RGBA *)pwBits;
		int y,x;
		int n;
		
		// draw world map
		p=ppush=pbegin;
		for(y=0;y<HEIGHT;y++)
		{
			for(x=0;x<WIDTH;x++)
			{
				_RGBA color;
				BYTE b=(BYTE)(255-(vSetup->world[y][x].terrain_cost<<4));
				color.red=b;
				color.green=b;
				color.blue=b;
				*(p+0)=color;
				*(p+1)=color;
				*(p+clientwidth)=color;
				*(p+clientwidth+1)=color;
				p+=2;
			}
			ppush+=clientwidth<<1;
			p=ppush;
		}
		
		// draw elevations for selection
		p=ppush=pbegin+(WIDTH<<1);
		for(n=15;n>=0;n--)
		{
			BYTE b=(BYTE)((n<<4)+15);
			for(y=0;y<20;y++)
			{
				for(x=0;x<40;x++)
				{
					(p+x)->red=b;
					(p+x)->green=b;
					(p+x)->blue=b;
				}
				ppush+=clientwidth;
				p=ppush;
			}
		}
		
		// draw current elevation
		p=ppush=pbegin+(WIDTH<<1)+60+HEIGHT*clientwidth;
		BYTE b=(BYTE)((15-elevation)<<4);
		for(y=0;y<60;y++)
		{
			for(x=0;x<60;x++)
			{
				(p+x)->red=b;
				(p+x)->green=b;
				(p+x)->blue=b;
			}
			ppush+=clientwidth;
			p=ppush;
		}
		
		// draw line mode
		p=ppush=pbegin+(WIDTH<<1)+60+10*clientwidth;;
		for(y=0;y<1;y++)
		{
			for(x=0;x<60;x++)
			{
				(p+x)->red=0;
				(p+x)->green=255;
				(p+x)->blue=0;
			}
			ppush+=clientwidth;
			p=ppush;
		}
		// draw brush mode
		p=ppush=pbegin+(WIDTH<<1)+60+20*clientwidth;;
		for(y=0;y<20;y++)
		{
			for(x=0;x<60;x++)
			{
				(p+x)->red=0;
				(p+x)->green=255;
				(p+x)->blue=0;
			}
			ppush+=clientwidth;
			p=ppush;
		}
		
		//
		p=ppush=pbegin+(WIDTH<<1)+60+(brushmode*20)*clientwidth;;
		for(x=0;x<60;x++)
		{
			(p+x)->red=255;
			(p+x)->green=0;
			(p+x)->blue=0;
			(p+x+clientwidth*20)->red=255;
			(p+x+clientwidth*20)->green=0;
			(p+x+clientwidth*20)->blue=0;
		}
		for(y=0;y<20;y++)
		{
			(p+y*clientwidth)->red=255;
			(p+y*clientwidth)->green=0;
			(p+y*clientwidth)->blue=0;
			(p+60+y*clientwidth)->red=255;
			(p+60+y*clientwidth)->green=0;
			(p+60+y*clientwidth)->blue=0;
		}
	}
}



//
void PaintFunc_TooSmall(HDC hdc,HWND hWnd, int w, int h)
{
	if(pwBits)
	{
		//
		RECT rt;
		GetClientRect(hWnd, &rt);
		
		COLORREF background;
		COLORREF foreground;
		vSetup->get_colorscheme_colors(background,foreground);
		HBRUSH hbrBkGnd = CreateSolidBrush(background);
		FillRect(hdc, &rt, hbrBkGnd);
		DeleteObject(hbrBkGnd);
		SetBkColor(hdc,background);
		SetTextColor(hdc,foreground);
		
		//
		TCHAR szStatusLine[1024];
		
		sprintf(szStatusLine,"Window has been resized too small.\n\n");
		DrawText(hdc, szStatusLine, strlen(szStatusLine), &rt, DT_CENTER);
		
		if(w>0)
		{
			rt.top+=16;
			sprintf(szStatusLine,"<-- width short by %d pixels -->",w);
			DrawText(hdc, szStatusLine, strlen(szStatusLine), &rt, DT_CENTER);
		}
		
		if(h>0)
		{
			rt.top+=16;
			sprintf(szStatusLine,">-- height short by %d pixels --<",h);
			DrawText(hdc, szStatusLine, strlen(szStatusLine), &rt, DT_CENTER);
		}
	}
}


//
void pixel(int x, int y)
{
	if(x>=0 && x<WIDTH && y>=0 && y<HEIGHT)
	{
		vSetup->world[y][x].terrain_cost=elevation;
	}
}


//
void line(
		  int x1,
		  int y1,
		  int x2,
		  int y2
		  )
{
	int x, y;
	int dx, dy;
	int incx, incy;
	int balance;
	
	if (x2 >= x1)
	{
		dx = x2 - x1;
		incx = 1;
	}
	else
	{
		dx = x1 - x2;
		incx = -1;
	}
	
	if (y2 >= y1)
	{
		dy = y2 - y1;
		incy = 1;
	}
	else
	{
		dy = y1 - y2;
		incy = -1;
	}
	
	x = x1;
	y = y1;
	
	if (dx >= dy)
	{
		dy <<= 1;
		balance = dy - dx;
		dx <<= 1;
		
		while (x != x2)
		{
			pixel(x,y);
			if (balance >= 0)
			{
				y += incy;
				balance -= dx;
			}
			balance += dy;
			x += incx;
		} 
		pixel(x,y);
	}
	else
	{
		dx <<= 1;
		balance = dx - dy;
		dy <<= 1;
		
		while (y != y2)
		{
			pixel(x,y);
			if (balance >= 0)
			{
				x += incx;
				balance -= dy;
			}
			balance += dx;
			y += incy;
		}
		pixel(x,y);
	}
}


//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	
	switch (message) 
	{
		
	case WM_MBUTTONDOWN:
		{
			//			WORD fwKeys = wParam;        // key flags 
			WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
			WORD yPos = HIWORD(lParam);  // vertical position of cursor 
			
			drawmode=!drawmode;
			runflag=!drawmode;
			vSetup->blocking_walls();
			
			lastx=xPos>>1;
			lasty=yPos>>1;
			Redraw(hWnd);
		}
		break;
	case WM_MBUTTONUP:
		{
		}
		break;
		/*
		#ifndef WM_MOUSEWHEEL
		#error No WM_MOUSEWHEEL. Include latest Platform SDK if using VC++ 6.
		#else
		case WM_MOUSEWHEEL:
		{
		WORD fwKeys = LOWORD(wParam);    // key flags
		short zDelta = (short) HIWORD(wParam);    // wheel rotation
		WORD xPos = (short) LOWORD(lParam);    // horizontal position of pointer
		WORD yPos = (short) HIWORD(lParam);    // vertical position of pointer
		
		  vGeneric->ai+=(((zDelta)/WHEEL_DELTA)>0)?-1:1;
		  if(vGeneric->ai<0) vGeneric->ai=0;
		  if(vGeneric->ai>=MAX_AIS) vGeneric->ai=MAX_AIS-1;
		  
			Redraw(hWnd);
			}
			break;
			#endif
		*/
	case WM_LBUTTONDOWN:
		{
			if(!drawmode)
			{
				//			WORD fwKeys = wParam;        // key flags 
				WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
				WORD yPos = HIWORD(lParam);  // vertical position of cursor 
				
				if(zoom)
				{ lastx=xPos>>1; lasty=yPos>>1; }
				else
				{ lastx=xPos; lasty=yPos; }
				
				if(zoom) {xPos=(WORD)(xPos>>1); yPos=(WORD)(yPos>>1); }
				
				if(xPos<WIDTH && yPos<HEIGHT && xPos>0 && yPos>0)
				{
					if(vSetup->world[yPos][xPos].terrain_cost<15)
					{
						vSetup->starty=(BYTE)yPos;
						vSetup->startx=(BYTE)xPos;
						vSetup->okToPath=false;
						vGeneric->UpdateSettings();
						Redraw(hWnd);
					}
				}
			}
			else
			{
				//			WORD fwKeys = wParam;        // key flags 
				WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
				WORD yPos = HIWORD(lParam);  // vertical position of cursor 
				
				if(xPos>(WIDTH<<1) && xPos<(WIDTH<<1)+40)
				{
					elevation=(yPos/20);
					Redraw(hWnd);
				}
				else if(xPos>(WIDTH<<1)+60 && xPos<(WIDTH<<1)+60+60)
				{
					if(yPos>=0 && yPos<20)
					{
						brushmode=false;
						Redraw(hWnd);
					}
					else if(yPos>=20 && yPos<40)
					{
						brushmode=true;
						Redraw(hWnd);
					}
				}
				else if(xPos<(WIDTH<<1) && yPos<(HEIGHT<<1) && xPos>0 && yPos>0)
				{
					pendown=true;
					lastx=xPos>>1;
					lasty=yPos>>1;
				}
			}
		}
		break;
	case WM_LBUTTONUP:
		{
			pendown=false;
		}
		break;
		
	case WM_RBUTTONDOWN:
		{
			if(!drawmode)
			{
				//			WORD fwKeys = wParam;        // key flags 
				WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
				WORD yPos = HIWORD(lParam);  // vertical position of cursor 
				
				if(zoom)
				{ lastx=xPos>>1; lasty=yPos>>1; }
				else
				{ lastx=xPos; lasty=yPos; }
				
				if(zoom) {xPos=(WORD)(xPos>>1); yPos=(WORD)(yPos>>1); }
				
				if(xPos<WIDTH && yPos<HEIGHT && xPos>0 && yPos>0)
				{
					if(vSetup->world[yPos][xPos].terrain_cost<15)
					{
						vSetup->endy=(BYTE)yPos;
						vSetup->endx=(BYTE)xPos;
						vSetup->okToPath=true;
						vGeneric->UpdateSettings();
						vGeneric->Reset();
						Redraw(hWnd);
					}
				}
			}
		}
		break;
	case WM_RBUTTONUP:
		{
			pendown=false;
		}
		break;
		
	case WM_MOUSEMOVE:
		{
			if(drawmode)
			{
				//			WORD fwKeys = wParam;        // key flags 
				WORD xPos = LOWORD(lParam);  // horizontal position of cursor 
				WORD yPos = HIWORD(lParam);  // vertical position of cursor 
				
				xPos=(WORD)(xPos>>1); yPos=(WORD)(yPos>>1);
				
				if(xPos<WIDTH-1 && yPos<HEIGHT-1 && xPos>1 && yPos>1)
				{
					if(pendown)
					{
						if(!brushmode)
							line(lastx,lasty,xPos,yPos);
						else
						{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -