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

📄 load.c

📁 著名物理引擎Hawk的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* load.c, HAWK game engine
 *
 * Copyright 1997-1998 by Phil Frisbie, Jr.
 * for Hawk Software
 *
 */

#include <string.h>
#include "hawk.h"
#include "hardware.h"
#include "internal.h"
#include "endian.h"
#include "load.h"
#include "pcx.h"
#include "bmp.h"
#include "tga.h"
#include "hawklogo.h"
#include "jpeglib.h"

int			globalpal[256];
int			globalpal33[256];
int			globalpal66[256];
int			palLoaded = FALSE;
byte		*palette;
byte		gammatable[256];

LOADEDFILE	*texfile = NULL;
int			ntexfiles;


int makeWARPS(TEXTURE *t, unsigned char *image_in)
{
	int			j, x, y;
	int			w = t->width;
	int			h = t->height;
	int			*image, *temp, *temp2;
	int			texture = 0;
	float		fxScale = M_PI / w * 2.0f;
	float		fyScale = M_PI / h * 2.0f;
	
	image = (int *)image_in;
	temp = TempMalloc(w * h * 4);
	temp2 = TempMalloc(w * h * 4);
	for(j=0;j<ANIM_TEX_FRAMES;++j)
	{
		float	fPhase = j * 2 * M_PI / ANIM_TEX_FRAMES;
		
		memcpy(temp2, image, (w * h * 4));
		/* shear in x */
		for (y = 0; y < h; ++y)
		{
			float	fScramble = (w * ANIM_TEX_DISP) 
				* cos( M_PI_2 * (sin( fPhase )
				+ cos( y * fyScale )) );
			float	fFloor = floor( fScramble );
			float	f2 = fScramble - fFloor;
			float	f1 = 1 - f2;
			int		sx = ((int)fFloor) % w;
			
			if (sx < 0) sx += w;
			for (x = 0; x < w; ++x)
			{
				unsigned int	ul1 = temp2[(x + sx) % w + y * w];
				unsigned int	ul2 = temp2[(x + sx + 1) % w + y * w];
				
				temp[x + y * w] = 
					((int)((ul1 & 0xff000000) * f1 + (ul2 & 0xff000000) * f2) & 0xff000000) |
					((int)((ul1 & 0x00ff0000) * f1 + (ul2 & 0x00ff0000) * f2) & 0x00ff0000) |
					((int)((ul1 & 0x0000ff00) * f1 + (ul2 & 0x0000ff00) * f2) & 0x0000ff00) |
					((int)((ul1 & 0x000000ff) * f1 + (ul2 & 0x000000ff) * f2) & 0x000000ff);
			}
		}
		
		/* shear in y */
		for (x = 0; x < w; ++x)
		{
			float	fScramble = (h * ANIM_TEX_DISP)
				* cos( M_PI_2 * (cos( fPhase )
				+ sin( x * fxScale )) );
			float	fFloor = floor( fScramble );
			float	f2 = fScramble - fFloor;
			float	f1 = 1 - f2;
			int		sy = ((int)fFloor) % h;
			
			if (sy < 0) sy += h;
			for (y = 0; y < h; ++y)
			{
				unsigned int	ul1 = temp[x + ((y + sy) % h) * w];
				unsigned int	ul2 = temp[x + ((y + sy + 1) % h) * w];
				
				temp2[x + y * w] =
					((int)((ul1 & 0xff000000) * f1 + (ul2 & 0xff000000) * f2) & 0xff000000) |
					((int)((ul1 & 0x00ff0000) * f1 + (ul2 & 0x00ff0000) * f2) & 0x00ff0000) |
					((int)((ul1 & 0x0000ff00) * f1 + (ul2 & 0x0000ff00) * f2) & 0x0000ff00) |
					((int)((ul1 & 0x000000ff) * f1 + (ul2 & 0x000000ff) * f2) & 0x000000ff);
			}
		}
		if(!texture)
			texture = texBuildMipmaps(RGBA_TEXTURE, w, h, (unsigned char *)temp2);
		else
			texBuildMipmaps(RGBA_TEXTURE, w, h, (unsigned char *)temp2);

	}
	t->ntextures = ANIM_TEX_FRAMES;
	TempFree(temp);
	TempFree(temp2);
	
	return texture;
}

int loadPCX(char *filename, TEXTURE *t)
{
	unsigned char c, x, *pixelbuffer, *filebuffer, *pal, *temp;
	int i, j, width, height, filepointer, pixelpointer, size;
	int texnumber;
	PCX_HEADER *header;
	
	size = fileSize(filename);
	if(size <= 0)
		return (0);
	filebuffer = TempMalloc(size);
	loadFile(filename, 0, size, filebuffer);
	header = (PCX_HEADER *)filebuffer;
	width = (header->xmax - header->xmin) + 1;
	height = (header->ymax - header->ymin) + 1;
	pixelbuffer = (unsigned char *) TempMalloc(width * height +256);
	filepointer = sizeof(PCX_HEADER) - 1;
	pixelpointer = 0;
	
	for (i=0;i<(width * height);++i)
	{
		c = filebuffer[filepointer++];
		if((c & 0xc0) == 0xc0) 
		{
			x = c & 0x3f;
			c = filebuffer[filepointer++];
			while(x--)
			{
				pixelbuffer[pixelpointer++] = c;
				i++;
			}
			i--;
		}
		else
		{
			pixelbuffer[pixelpointer++] = c;
		}
	}
	
	pal = TempMalloc(768);
	loadFile(filename, size - 768 , 768, pal);
    t->width = width;
    t->height = height;
	t->ntextures = 0;
	
	size = width * height * 4;
	temp = TempMalloc(size);
	for(i=0,j=0;i<size;i+=4,j++)
	{
		unsigned int k=pixelbuffer[j] * 3;
		
		temp[i] = gammatable[pal[k]];
		temp[i+1] = gammatable[pal[k+1]];
		temp[i+2] = gammatable[pal[k+2]];
		if(pixelbuffer[j])
			temp[i+3] = 255;
		else
			temp[i+3] = 0;
	}
	if(t->type&IS_FONT)
		texnumber = texBuildLightmap(RGBA_TEXTURE, width, height, temp);
	else if(t->type&IS_SKY)
		texnumber = texBuildLightmap(RGB_TEXTURE, width, height, temp);
	else if(t->type&SURF_WARP)
		texnumber = makeWARPS(t, temp);
	else
		texnumber = texBuildMipmaps(RGB_TEXTURE, width, height, temp);
	TempFree(filebuffer);
	TempFree(pixelbuffer);
	TempFree(pal);
	TempFree(temp);
	return (texnumber);
}

int loadBMP(char *filename, TEXTURE *t)
{
	BMP_HEADER	*header;
	unsigned char *filebuffer, *temp, *pixelbuffer;
	int i, j, size, texnumber;

	size = fileSize(filename);
	if(size <= 0)
		return (0);
	filebuffer = TempMalloc(size);
	loadFile(filename, 0, size, filebuffer);
	header = (BMP_HEADER *)filebuffer;

	if(header->bitCount != 24)
		return (0);

	t->width = header->width;
	t->height = header->height;
	t->ntextures = 0;

	pixelbuffer = header->data;
	temp = TempMalloc(t->width * t->height * 4);

	for(i=0, j=0;j<(t->width * t->height * 3);j+=3)
	{
		unsigned int alpha;

		alpha = temp[i++] = pixelbuffer[j+2];
		alpha += temp[i++] = pixelbuffer[j+1];
		alpha += temp[i++] = pixelbuffer[j];
		/* adjust this for sprites */
		if(alpha == 0)/* full black */
			temp[i++] = 0;
		else
			temp[i++] = 255;
	}
/*	saveFile("hawk.bin", (t->width * t->height * 4), temp);	 */
	if(t->type&IS_FONT)
		texnumber = texBuildLightmap(RGBA_TEXTURE, t->width, t->height, temp);
	else if(t->type&IS_SKY)
		texnumber = texBuildLightmap(RGB_TEXTURE, t->width, t->height, temp);
	else if(t->type&SURF_WARP)
		texnumber = makeWARPS(t, temp);
	else
		texnumber = texBuildMipmaps(RGB_TEXTURE, t->width, t->height, temp);

	TempFree(filebuffer);
	TempFree(temp);

	return (texnumber);
}

int loadJPG(char *filename, TEXTURE *t)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	FILE * infile;
	JSAMPARRAY buffer;
	int row_stride, pixelpointer = 0;
	unsigned char *pixelbuffer;
	int texnumber;
	
	if ((infile = fopen(filename, "rb")) == NULL) {
		fprintf(stderr, "can't open %s\n", filename);
		return 0;
	}
	
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);

	/* this call needs to be rewritten for PAK files */
	jpeg_stdio_src(&cinfo, infile);
	
	jpeg_read_header(&cinfo, TRUE);
	
	jpeg_start_decompress(&cinfo);

	t->width = cinfo.output_width;
	t->height = cinfo.output_height;
	t->ntextures = 0;
	
	row_stride = cinfo.output_width * cinfo.output_components;
	
	buffer = (*cinfo.mem->alloc_sarray)
		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
	pixelbuffer = (unsigned char *) TempMalloc(cinfo.output_width *
		cinfo.output_height * 3);
	while (cinfo.output_scanline < cinfo.output_height) {
		
		jpeg_read_scanlines(&cinfo, buffer, 1);
		memcpy(pixelbuffer + pixelpointer, buffer[0], row_stride);
		pixelpointer += row_stride;
	}
	
	jpeg_finish_decompress(&cinfo);
	
	jpeg_destroy_decompress(&cinfo);

    t->width = cinfo.output_width;
    t->height = cinfo.output_height;
	t->ntextures = 0;

	if(t->type&SURF_WARP)
		texnumber = makeWARPS(t, pixelbuffer);
	else
		texnumber = texBuildMipmaps(RGB_TEXTURE, cinfo.output_width,
				cinfo.output_height, pixelbuffer);
	TempFree(pixelbuffer);
	
	fclose(infile);
	
	return (texnumber);
}

int loadWAL(char *filename, TEXTURE *t)
{
    byte		*source;
    int			*dest;
	int			*pal = globalpal;
    int			width, height, i, count, size;
	miptex_t	*qtex;
	int			texnumber;
	int			alpha = FALSE;
	
	size = fileSize(filename);
	if(size <= 0)
		return (0);
	qtex = TempMalloc(size);
	loadFile(filename, 0, size, qtex);

	if(t->type&SURF_TRANS33)
	{
		pal = globalpal33;
		alpha = TRUE;
	}
	if(t->type&SURF_TRANS66)
	{
		pal = globalpal66;
		alpha = TRUE;
	}
	
    width = littleLong(qtex->width);
    height = littleLong(qtex->height);
	
    t->width = width;
    t->height = height;
	t->ntextures = 0;
	
	dest = TempMalloc(width * height * 4);
	
    count = width * height;
	size = count * 4;
	source = (byte *)qtex + littleLong(qtex->offsets[0]);
	
	for(i=0;i<count;i++)
	{
		unsigned int k=source[i];
		
		dest[i] = pal[k];
	}

	if(t->type&SURF_WARP)
	{
		texnumber = makeWARPS(t, (unsigned char *)dest);
	}
	else
	{
		if(alpha)
			texnumber = texBuildMipmaps(RGBA_TEXTURE, width, height, (unsigned char *)dest);
		else
			texnumber = texBuildMipmaps(RGB_TEXTURE, width, height, (unsigned char *)dest);
	}

	t->number = texnumber;

	TempFree(qtex);
	TempFree(dest);
	
    return (texnumber);
}

int loadTGA (char *filename, TEXTURE *t)
{
	int				columns, rows, numPixels, size;
	byte			*pixbuf, *filebuffer, *inbuf;
	int				row, column;
	byte			*targa_rgba;
	TGA_HEADER		*header;
	int				texnumber;

	size = fileSize(filename);
	if(size <= 0)
		return (0);
	filebuffer = TempMalloc(size);
	loadFile(filename, 0, size, filebuffer);
	header = (TGA_HEADER *)filebuffer;

	header->colormap_index = littleShort(header->colormap_index);
	header->colormap_length = littleShort(header->colormap_length);
	header->x_origin = littleShort(header->x_origin);
	header->y_origin = littleShort(header->y_origin);
	header->width = littleShort(header->width);
	header->height = littleShort(header->height);

	t->width = header->width;
	t->height = header->height;
	t->ntextures = 0;
	inbuf = header->data;

	if(header->image_type!=2 && header->image_type!=10)

⌨️ 快捷键说明

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