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

📄 load.c

📁 著名物理引擎Hawk的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	{
		TempFree(filebuffer);
		return 0;
	}

	if(header->colormap_type !=0 || (header->pixel_size!=32 && header->pixel_size!=24))
	{
		TempFree(filebuffer);
		return 0;
	}

	columns = header->width;
	rows = header->height;
	numPixels = columns * rows;

	targa_rgba = TempMalloc(numPixels*4);

	if(header->id_length != 0)
		*inbuf += header->id_length;  /* skip TARGA image comment */
	
	if(header->image_type==2) {  /* Uncompressed, RGB images */
		for(row=rows-1; row>=0; row--) {
			pixbuf = targa_rgba + row*columns*4;
			for(column=0; column<columns; column++) {
				unsigned char red,green,blue,alphabyte;
				switch (header->pixel_size) {
					case 24:
							
							blue = *inbuf++;
							green = *inbuf++;
							red = *inbuf++;
							*pixbuf++ = red;
							*pixbuf++ = green;
							*pixbuf++ = blue;
							*pixbuf++ = 255;
							break;
					case 32:
							blue = *inbuf++;
							green = *inbuf++;
							red = *inbuf++;
							alphabyte = *inbuf++;
							*pixbuf++ = red;
							*pixbuf++ = green;
							*pixbuf++ = blue;
							*pixbuf++ = alphabyte;
							break;
				}
			}
		}
	}
	else if (header->image_type==10) {   /* Runlength encoded RGB images */
		unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
		for(row=rows-1; row>=0; row--) {
			pixbuf = targa_rgba + row*columns*4;
			for(column=0; column<columns; ) {
				packetHeader=*inbuf++;
				packetSize = 1 + (packetHeader & 0x7f);
				if (packetHeader & 0x80) {        /* run-length packet */
					switch (header->pixel_size) {
						case 24:
								blue = *inbuf++;
								green = *inbuf++;
								red = *inbuf++;
								alphabyte = 255;
								break;
						case 32:
								blue = *inbuf++;
								green = *inbuf++;
								red = *inbuf++;
								alphabyte = *inbuf++;
								break;
					}
	
					for(j=0;j<packetSize;j++) {
						*pixbuf++=red;
						*pixbuf++=green;
						*pixbuf++=blue;
						*pixbuf++=alphabyte;
						column++;
						if (column==columns) { /* run spans across rows */
							column=0;
							if (row>0)
								row--;
							else
								goto breakOut;
							pixbuf = targa_rgba + row*columns*4;
						}
					}
				}
				else {                            /* non run-length packet */
					for(j=0;j<packetSize;j++) {
						switch (header->pixel_size) {
							case 24:
									blue = *inbuf++;
									green = *inbuf++;
									red = *inbuf++;
									*pixbuf++ = red;
									*pixbuf++ = green;
									*pixbuf++ = blue;
									*pixbuf++ = 255;
									break;
							case 32:
									blue = *inbuf++;
									green = *inbuf++;
									red = *inbuf++;
									alphabyte = *inbuf++;
									*pixbuf++ = red;
									*pixbuf++ = green;
									*pixbuf++ = blue;
									*pixbuf++ = alphabyte;
									break;
						}
						column++;
						if (column==columns) { /* pixel packet run spans across rows */
							column=0;
							if (row>0)
								row--;
							else
								goto breakOut;
							pixbuf = targa_rgba + row*columns*4;
						}						
					}
				}
			}
			breakOut:;
		}
	}

	if(t->type&IS_FONT)
		texnumber = texBuildLightmap(RGBA_TEXTURE, t->width, t->height, targa_rgba);
	else if(t->type&IS_SKY)
		texnumber = texBuildLightmap(RGB_TEXTURE, t->width, t->height, targa_rgba);
	else if(t->type&SURF_WARP)
		texnumber = makeWARPS(t, targa_rgba);
	else
		texnumber = texBuildMipmaps(RGB_TEXTURE, t->width, t->height, targa_rgba);

	TempFree(filebuffer);
	TempFree(targa_rgba);
	return (texnumber);
}

#define MAX_TEX		2048

void initTexfile(void)
{
	texfile = (LOADEDFILE *)TagMalloc(sizeof(LOADEDFILE) * MAX_TEX, TAG_LEVEL);
	memset(texfile, 0, sizeof(LOADEDFILE) * MAX_TEX);
	ntexfiles = 0;

}

int texCheckFile(char *texname)
{
	int	i;
	
	for(i=1;i<ntexfiles;i++)
	{
		if(!_stricmp(texname, texfile[i].name))/* found a match */
			return texfile[i].number;
	}
	return -1;/* not found */
}

void texSetFile(char *texname, int texnumber)
{
	strcpy(texfile[ntexfiles].name, texname);
	texfile[ntexfiles].number = texnumber;
	ntexfiles++;
}

int realLoadTexture(char *filename, TEXTURE *t)
{
	if(strstr(filename, ".PCX"))
		return (loadPCX(filename, t));
	if(strstr(filename, ".BMP"))
		return (loadBMP(filename, t));
	if(strstr(filename, ".JPG"))
		return (loadJPG(filename, t));
	if(strstr(filename, ".WAL"))
		return (loadWAL(filename, t));
	if(strstr(filename, ".TGA"))
		return (loadTGA(filename, t));
	
	return (0);
}

/* returns texture number, or 0 if not successful */
int loadTexture(char *filename, TEXTURE *t)
{
	int texnumber;

	_strupr(filename);
	
	if(!texfile)
		initTexfile();

	texnumber = texCheckFile(filename);
	if(texnumber < 0)
	{
		texnumber = realLoadTexture(filename, t);
		texSetFile(filename, texnumber);
	}
	return texnumber;
}

void loadSetGamma(byte *pal)
{
	int				r, g, b, v;
	int				i, j = 0;
	int				inf;
	float			gamma;
	
	gamma = Engine.gamma;
	
	if (gamma == 1.0)
	{
		for (i=0 ; i<256 ; i++)
			gammatable[i] = i;
	}
	else
	{
		for (i=0 ; i<256 ; i++)
		{
			inf = 255 * pow ( (i+0.5)/255.5 , gamma ) + 0.5;
			if (inf < 0)
				inf = 0;
			if (inf > 255)
				inf = 255;
			gammatable[i] = inf;
		}
	}
	
    for (i=0 ; i<256 ; i++)
    {
		r = gammatable[pal[0]];
		g = gammatable[pal[1]];
		b = gammatable[pal[2]];
		
		v = (r<<24) + (g<<16) + (b<<8) + 255;
		v = bigLong (v);
		
		globalpal[i] = v;

		v = (r<<24) + (g<<16) + (b<<8) + 169;
		v = bigLong (v);
		
		globalpal33[i] = v;

		v = (r<<24) + (g<<16) + (b<<8) + 84;
		v = bigLong (v);
		
		globalpal66[i] = v;

		pal += 3;
    }
}

#define MAX_PER_FRAME	100

void loadLightmap(int s)
{
	dface_t		*face = &BSP.dfaces[s];
	SURFACE		*surf = &Level.surf[s];
	unsigned char *lightmap;
	unsigned char *dest;
	int			j, k, l = 0, size, texnumber;
	int			h = Level.lighttex[s].height;
	int			w = Level.lighttex[s].width;
	int			type = surf->textype;
	static		frame = 0;
	static		nloaded = 0;
	
	if(frame != Engine.gameframes) /* reset the counter */
	{
		nloaded = 0;
		frame = Engine.gameframes;
	}
	if(nloaded > MAX_PER_FRAME)
		return;
	if(!(Engine.texture&&Engine.light)) /* do not load if not texturing and lighting */
		return;
	if(type&SURF_SKY) /* skip sky */
		return;
	if(type&SURF_WARP) /* skip warping (liquid) textures */
		return;
	if(type&SURF_NODRAW) /* skip if not to draw */
		return;
	if((type&(SURF_TRANS33||SURF_TRANS66)) /* skip translucent textures */
		&!(type&SURF_LIGHT))
		return;

	/* create the lightmap textures */
	lightmap = (unsigned char *)&BSP.dlightdata[face->lightofs];
	size = w * h * 4;
	dest = TempMalloc(size);
	
	for(j=0,k=0;j<size;j+=4,k+=3)
	{
		dest[j] = gammatable[lightmap[k]];
		dest[j+1] = gammatable[lightmap[k+1]];
		dest[j+2] = gammatable[lightmap[k+2]];
		dest[j+3] = 0;
	}
	
	texnumber = texBuildLightmap(RGB_TEXTURE, w, h, dest);
	Level.lighttex[s].number = texnumber;

	TempFree(dest);
	nloaded++;
}

void loadtest(void)
{
	Level.background = 0;
	GLevel.force = 0.8;
	GLevel.gravity = 4.0;
	reshape(Engine.w, Engine.h);
}

void loadLogo(void)
{
	Engine.logotex = texBuildTex(RGBA_TEXTURE, HAWKLOGO_WIDTH, HAWKLOGO_HEIGHT, hawklogo);
}

BOOL loadPalette(void)
{
	int size;
	
	size = fileSize("pics/colormap.pcx");
	if(size <= 0)
		return FALSE;
	palette = TagMalloc(768, TAG_LEVEL);
	loadFile("pics/colormap.pcx", size - 768 , 768, palette);
	return TRUE;
}

void loadSky(char *basename)
{
	TEXTURE t;
	char	temp[256];

	t.type = IS_SKY;

	strcpy(temp, basename);
	strcat(temp, "ft.tga");
	Level.background = loadTexture(temp, &t);
	strcpy(temp, basename);
	strcat(temp, "rt.tga");
	loadTexture(temp, &t);
	strcpy(temp, basename);
	strcat(temp, "bk.tga");
	loadTexture(temp, &t);
	strcpy(temp, basename);
	strcat(temp, "lf.tga");
	loadTexture(temp, &t);
	strcpy(temp, basename);
	strcat(temp, "up.tga");
	loadTexture(temp, &t);
	strcpy(temp, basename);
	strcat(temp, "dn.tga");
	loadTexture(temp, &t);
}

/* calculate BBoxes for some BSP model objects*/
void fixupBBoxes(void)
{
}

BOOL HAWK_LoadMap(char *filename)
{
	int			e, nentity;

	loadLogo();
	loadtest();
	if(!loadPalette())
		return FALSE;
	loadSetGamma(palette);
	loadMap(filename);

	nentity = GAMEDLL->ParseEntities(BSP.dentdata, BSP.entdatasize);
	for(e=0;e<nentity;e++)
	{
		GAMEDLL->SpawnObject(e);
	}

	fixupBBoxes();

	loadSky(GLevel.sky);
	
	return TRUE;
}

⌨️ 快捷键说明

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