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

📄 setup.cpp

📁 一个VC写A*寻路的程序库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			
			int count=Header.Width*Header.Height;
			
			Header.ImagePixelSize=READBYTE(s);
			Header.ImageDescriptorByte=READBYTE(s);
			
			// 			int AlphaChannelBits=Header.ImageDescriptorByte & 15;
			//			int ImageOrigin=(Header.ImageDescriptorByte>>4)&3;
			// 0= bottom left
			// 1=bottom right
			// 2=top left (default)
			// 3=top right
			bool toptobottom=(Header.ImageDescriptorByte&32)?1:0;
			//			bool lefttoright=(Header.ImageDescriptorByte&64)?1:0;
			
			//
			if(Header.IDLength>0)
			{
				memcpy(CIF,s,Header.IDLength);
				CIF[Header.IDLength]='\0';
				s+=(int)Header.IDLength;
			}
			
			//
			if(Header.Width!=WIDTH && Header.Height!=HEIGHT)
				; //printf("TGA is not %d by %d\n",width,height);
			else
			{
				//
				if(Header.ColorMapType==1 && Header.ColorMapLength>0)
				{
					memset(ColorMap,0,sizeof(ColorMap));
					
					if(Header.ColorMapEntrySize==16)
					{
						for(int i=0;i<Header.ColorMapLength;i++)
						{
							WORD w=READWORD(s);
							
							BYTE r,g,b;
							b=(BYTE)((w&31)<<3);
							g=(BYTE)(((w>>5)&31)<<3);
							r=(BYTE)(((w>>10)&31)<<3);
							
							ColorMap[i].red=r;
							ColorMap[i].green=g;
							ColorMap[i].blue=b;
						}
					}
					else if(Header.ColorMapEntrySize==24)
					{
						for(int i=0;i<Header.ColorMapLength;i++)
						{
							ColorMap[i].blue=READBYTE(s);
							ColorMap[i].green=READBYTE(s);
							ColorMap[i].red=READBYTE(s);
						}
					}
					else if(Header.ColorMapEntrySize==32)
					{
						for(int i=0;i<Header.ColorMapLength;i++)
						{
							ColorMap[i].blue=READBYTE(s);
							ColorMap[i].green=READBYTE(s);
							ColorMap[i].red=READBYTE(s);
							*s++;
						}
					}
				}
				
				
				///////////////////////////////////////////////////////////
				if(toptobottom)
				{
					switch(Header.ImageType)
					{
					case TGA_UncompressedColorMapped:
						if(Header.ImagePixelSize==8)
						{
							for(int y=0;y<Header.Height;y++)
							{
								for(int x=0;x<Header.Width;x++)
								{
									BYTE b=READBYTE(s);
									world[y][x].terrain_cost=15-(b>>4);
								}
							}
						}
						break;
						
						//
					case TGA_RLEColorMapped:
						{
							int x=0,y=0;
							
							int dcount=WIDTH;
							
							BYTE Packet;
							bool PacketType;
							BYTE PixelCount;
							
							while(count>0) // (s<ends) && (d<endd) )
							{
								Packet=READBYTE(s);
								PacketType=(Packet&128)==128;
								PixelCount=(BYTE)((Packet&127)+1);
								
								count-=PixelCount;
								
								if(PacketType)
								{
									// run-length
									BYTE b=READBYTE(s);
									
									while(PixelCount--)
									{
										world[y][x].terrain_cost=15-(b>>4);
										x++; if(x>=WIDTH) { x=0; y++; }
										
										//skip overdraw
										if(--dcount<=0) dcount=WIDTH;
									}
								}
								else
								{
									// raw data
									while(PixelCount--)
									{
										BYTE b=READBYTE(s);
										world[y][x].terrain_cost=15-(b>>4);
										x++; if(x>=WIDTH) { x=0; y++; }
										
										//skip overdraw
										if(--dcount<=0)	dcount=WIDTH;
									}
								}
							}
							//
							break;
						}
					}
				}
				///////////////////////////////////////////////////////////
				
				///////////////////////////////////////////////////////////
				if(!toptobottom)
				{
					switch(Header.ImageType)
					{
					case TGA_UncompressedColorMapped:
						if(Header.ImagePixelSize==8)
						{
							for(int y=Header.Height-1;y>=0;y--)
							{
								for(int x=0;x<Header.Width;x++)
								{
									BYTE b=READBYTE(s);
									world[y][x].terrain_cost=15-(b>>4);
								}
							}
						}
						break;
						
						//
					case TGA_RLEColorMapped:
						{
							int x=0,y=Header.Height-1;
							
							int dcount=WIDTH;
							
							BYTE Packet;
							bool PacketType;
							BYTE PixelCount;
							
							while(count>0) // (s<ends) && (d<endd) )
							{
								Packet=READBYTE(s);
								PacketType=(Packet&128)==128;
								PixelCount=(BYTE)((Packet&127)+1);
								
								count-=PixelCount;
								
								if(PacketType)
								{
									// run-length
									BYTE b=READBYTE(s);
									
									while(PixelCount--)
									{
										world[y][x].terrain_cost=15-(b>>4);
										x++; if(x>=WIDTH) { x=0; y--; }
										
										//skip overdraw
										if(--dcount<=0) dcount=WIDTH;
									}
								}
								else
								{
									// raw data
									while(PixelCount--)
									{
										BYTE b=READBYTE(s);
										world[y][x].terrain_cost=15-(b>>4);
										x++; if(x>=WIDTH) { x=0; y--; }
										
										//skip overdraw
										if(--dcount<=0)	dcount=WIDTH;
									}
								}
							}
							//
							break;
						}
					}
					///////////////////////////////////////////////////////////
					
				}
			}
		}
		delete [] infile;
	}
	
	finish_load();
}



//
void Setup::Save(char *filename)
{
	//
	const int outsize=256*3+sizeof(Header)+WIDTH*HEIGHT;
	int length=0;
	BYTE *outfile=new BYTE[outsize];
	if(outfile)
	{
		//
		BYTE *d=outfile;
		
		//
		WRITEBYTE(d,0); //idlength
		WRITEBYTE(d,1); //m->ColorMaptype
		WRITEBYTE(d,TGA_UncompressedColorMapped); //imagetype
		WRITEWORD(d,1); //m->ColorMapstart
		WRITEWORD(d,256); //m->ColorMaplength
		WRITEBYTE(d,24); //m->ColorMapdepth
		WRITEWORD(d,0); //xoffset
		WRITEWORD(d,0); //yoffset
		WRITEWORD(d,WIDTH); //width
		WRITEWORD(d,HEIGHT); //height
		WRITEBYTE(d,8); //pixeldepth
		WRITEBYTE(d,32); //imagedescriptor
		
		//
		for(int i=0;i<256;i++)
		{
			WRITEBYTE(d,(BYTE)i);
			WRITEBYTE(d,(BYTE)i);
			WRITEBYTE(d,(BYTE)i);
		}
		
		//
		for(int y=0;y<HEIGHT;y++)
		{
			for(int x=0;x<WIDTH;x++)
			{
				WRITEBYTE(d,(BYTE)((15-world[y][x].terrain_cost)<<4));
			}
		}
		
		//
		length=(d-outfile);
		int stream=_open(
			(const char *)filename,
			_O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, _S_IREAD | _S_IWRITE
			);
		if(stream!=-1)
		{
			_write( stream, outfile, length);
			_close(stream);
		}
		
		delete [] outfile;
	}
}

//
void Setup::median()
{
	int accum=0;
	
	for(int y=0;y<HEIGHT;y++)
	{
		for(int x=0;x<WIDTH;x++)
		{
			accum+=world[y][x].terrain_cost;
		}
	}
	
	median_terrain_cost_auto=(float)accum/(float)(WIDTH*HEIGHT);
}

/*
int Setup::random_range(int min, int max)
{
	return (rand()%(max-min))+min;
}
*/
//
int Setup::get_startx()
{
	return startx;
}
int Setup::get_starty()
{
	return starty;
}
int Setup::get_endx()
{
	return endx;
}
int Setup::get_endy()
{
	return endy;
}

//
void Setup::set_start(int y, int x)
{
	startx=x;
	starty=y;
}
void Setup::set_end(int y, int x)
{
	endx=x;
	endy=y;
}


// saves settings to registry
void Setup::save_settings()
{
	//TODO
}

// loads last used settings from registry
void Setup::load_settings()
{
	//TODO
}


//
void Setup::get_colorscheme_colors(COLORREF &background, COLORREF &foreground)
{
	switch(colorscheme)
	{
	case COLORSCHEME_WHITE:	background=0x00ffffff; foreground=0x00000000; break;
	case COLORSCHEME_BLACK:	background=0x00000000; foreground=0x00ffffff; break;
	case COLORSCHEME_GRAY:	background=0x00C8D0D4; foreground=0x00000000; break;
	case COLORSCHEME_PAPER:	background=0x00c0ffff; foreground=0x00303030; break;
	default: background=0x00808080; foreground=0x00000000; break;
	}
}






//////////////////////////////////////////////////////////////////////
// presearch
//////////////////////////////////////////////////////////////////////

//
void Setup::Presearch()
{
	//
	Presearch_Impassables();

	//
	int group=MIN_GROUP;
	int y;
	int x;
	Presearch_FindUngrouped(y,x);
	while(y!=-1 && x!=-1)
	{
		nodes[1].y=y;
		nodes[1].x=x;
		world[y][x].group=group;
        Presearch_FloodFill(group);
		Presearch_FindUngrouped(y,x);
		if(++group>=MAX_GROUP) break; //hmm...
	}

	presearch_maxgroup=group;
}

//
void Setup::Presearch_FindUngrouped(int& y, int& x)
{
	y=-1;
	x=-1;

	for(int ly=0;ly<HEIGHT;ly++)
	{
		for(int lx=0;lx<WIDTH;lx++)
		{
			if(world[ly][lx].group==NO_GROUP)
			{
				y=ly;
				x=lx;
				return;
			}
		}
	}
}

//
void Setup::Presearch_Impassables()
{
	for(int y=0;y<HEIGHT;y++)
	{
		for(int x=0;x<WIDTH;x++)
		{
			world[y][x].group=(BYTE)(world[y][x].terrain_cost==IMPASSABLE_TERRAIN_COST)?IMPASSABLE_GROUP:NO_GROUP;
		}
	}
}

//
void Setup::Presearch_FloodFill(const int group)
{
	int last_node=1;
	do
	{
		int parenty=nodes[last_node].y;
		int parentx=nodes[last_node].x;
		last_node--;
		
		for(int d=0;d<8;d++)
		{
			int y=parenty+DXY[d].y;
			int x=parentx+DXY[d].x;
			
			if(world[y][x].group==NO_GROUP)
			{
				world[y][x].group=group;
				last_node++;
				nodes[last_node].y=y;
				nodes[last_node].x=x;
			}
		}
	} while(last_node>0 && last_node<MAX_PIXELS);
}

⌨️ 快捷键说明

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