Beispiel #1
0
bool LoadRTLights( void )
{
    FILE	*	fp;
    int8_t		Filename[ 256 ];
    char	*	NewExt = ".RTL";
    u_int32_t		MagicNumber;
    u_int32_t		VersionNumber;
    int			j;
    RT_LIGHT *	light;
    u_int16_t		type;
    XLIGHT	*	xlight;
    RT_FIXED_LIGHT		*fixed;
    RT_PULSING_LIGHT	*pulse;
    RT_FLICKERING_LIGHT	*flicker;
    RT_SPOT_LIGHT		*spot;

    Change_Ext( &LevelNames[ LevelNum ][ 0 ], &Filename[ 0 ], NewExt );


    fp = file_open( &Filename[ 0 ], "rb" );

    if( fp )
    {
        fread_var( fp, MagicNumber );
        fread_var( fp, VersionNumber );

        if( ( MagicNumber != MAGIC_NUMBER ) || ( VersionNumber != RTL_VERSION_NUMBER  ) )
        {
            fclose( fp );
            Msg( "LoadRTLights() Incompatible Real-Time Lights (.RTL) file %s", &Filename[ 0 ] );
            return( false );
        }

        fread_var( fp, rt_lights );

        if ( rt_lights )
        {
            rt_light = (RT_LIGHT *) calloc( rt_lights, sizeof( RT_LIGHT ) );
            if ( !rt_light )
            {
                fclose( fp );
                Msg( "LoadRTLights() malloc failed for %d Real-Time Lights ", rt_lights );
                rt_lights = 0;
                return false ;
            }

            for ( j = 0; j < rt_lights; j++ )
            {
                rt_light[ j ].xlight = (u_int16_t) -1;
                rt_light[ j ].enabled = false;
            }

            for ( j = 0; j < rt_lights; j++ )
            {
                light = &rt_light[ j ];
                fread_var( fp, type );
                light->type = (LIGHTTYPE) type;
                fread_var( fp, light->group );
                fread_var( fp, light->pos.x );
                fread_var( fp, light->pos.y );
                fread_var( fp, light->pos.z );
                fread_var( fp, light->range );
                fread_var( fp, light->r );
                fread_var( fp, light->g );
                fread_var( fp, light->b );
                light->r *= 255.0F;
                light->g *= 255.0F;
                light->b *= 255.0F;
                fread_var( fp, type );
                light->generation_type = (GENTYPE) type;
                fread_var( fp, light->generation_delay );
                switch ( light->type )
                {
                case LIGHT_FIXED:
                    fixed = &light->fixed;
                    fread_var( fp, type );
                    fixed->on_type = (PULSETYPE) type;
                    fread_var( fp, type );
                    fixed->off_type = (PULSETYPE) type;
                    fread_var( fp, fixed->on_time );
                    fread_var( fp, fixed->off_time );
                    fixed->on_time *= ANIM_SECOND;
                    fixed->off_time *= ANIM_SECOND;
                    break;
                case LIGHT_PULSING:
                    pulse = &light->pulse;
                    fread_var( fp, type );
                    pulse->type = (PULSETYPE) type;
                    fread_var( fp, pulse->on_time );
                    fread_var( fp, pulse->stay_on_time );
                    fread_var( fp, pulse->off_time );
                    fread_var( fp, pulse->stay_off_time );
                    pulse->on_time *= ANIM_SECOND;
                    pulse->stay_on_time *= ANIM_SECOND;
                    pulse->off_time *= ANIM_SECOND;
                    pulse->stay_off_time *= ANIM_SECOND;
                    pulse->stay_on_point = pulse->on_time + pulse->stay_on_time;
                    pulse->off_point = pulse->stay_on_point + pulse->off_time;
                    pulse->total_time = pulse->off_point + pulse->stay_off_time;
                    break;
                case LIGHT_FLICKERING:
                    flicker = &light->flicker;
                    fread_var( fp, flicker->stay_on_chance );
                    fread_var( fp, flicker->stay_off_chance );
                    fread_var( fp, flicker->stay_on_time );
                    fread_var( fp, flicker->stay_off_time );
                    flicker->stay_on_time *= ANIM_SECOND;
                    flicker->stay_off_time *= ANIM_SECOND;
                    break;
                case LIGHT_SPOT:
                    spot = &light->spot;
                    fread_var( fp, spot->dir.x );
                    fread_var( fp, spot->dir.y );
                    fread_var( fp, spot->dir.z );
                    fread_var( fp, spot->up.x );
                    fread_var( fp, spot->up.y );
                    fread_var( fp, spot->up.z );
                    fread_var( fp, spot->cone );
                    fread_var( fp, spot->rotation_period );
                    if ( spot->rotation_period )
                        spot->rotation_speed = 2 * PI / ( spot->rotation_period * ANIM_SECOND );
                    else
                        spot->rotation_speed = 0.0F;
                    spot->angle = 0.0F;
                    break;
                }
                light->now_time = 0.0F;
                switch ( light->generation_type )
                {
                case GENTYPE_Initialised:
                    light->enabled = true;
                    light->state = STATE_TURNING_ON;
                    light->delay = 0.0F;
                    break;
                case GENTYPE_Time:
                    light->enabled = false;
                    light->state = STATE_OFF;
                    light->delay = light->generation_delay * ANIM_SECOND;
                    break;
                case GENTYPE_Trigger:
                    light->enabled = false;
                    light->state = STATE_OFF;
                    light->delay = 0.0F;
                    break;
                }
                light->xlight = FindFreeXLight();
                if ( light->xlight != (u_int16_t) -1 )
                {
                    xlight = &XLights[ light->xlight ];
                    xlight->Pos = light->pos;
                    xlight->Size = light->range;
                    xlight->SizeCount = 0.0F;
                    xlight->r = light->r;
                    xlight->g = light->g;
                    xlight->b = light->b;
                    xlight->Group = light->group;
                    xlight->Visible = false;
                    if ( light->type == LIGHT_SPOT )
                    {
                        xlight->Type = SPOT_LIGHT;
                        xlight->CosArc = (float) cos( D2R( light->spot.cone * 0.5F ) );
                    }
                }
            }
        }
        fclose( fp );
    }

    return true;
}
Beispiel #2
0
int create_texture(LPTEXTURE *t, const char *path, uint16 *width, uint16 *height, int numMips, BOOL * colorkey, D3DPOOL pool)
{
#ifdef TEXTURE_PNG
	HRESULT hr;
	texture_image_t image;

	Change_Ext( path, image.path, ".PNG" );
	if( ! File_Exists( (char*) image.path ) )
	{
		DebugPrintf("Could not find texture file: %s\n",path);
		return TRUE;
	}

	// at this point i have mips turned off for the commit
	// cause it appears that mip levels after 0 are not present
	// i think we need to copy each successive mipmap in at a time
	numMips = 1;

	// load the image
	if(load_image( &image, numMips )!=0)
	{
		DebugPrintf("couldn't load image\n");
		return S_FALSE;
	}

	// create d3d texture
	hr = lpD3DDevice->CreateTexture( image.w, image.h, numMips, 0, D3DFMT_A8R8G8B8, pool, texture, NULL );
	if (FAILED(hr))
	{
		DebugPrintf("couldn't create texture\n");
		return hr;
	}

	// copy data into the texture
	{
		int y, x;
		int size = 4;
		int pitch = size*image.w;
		D3DLOCKED_RECT lrect;
		BYTE* pBits;
		LPTEXTURE _texture = *texture;
		_texture->LockRect(0,&lrect,NULL,D3DLOCK_DISCARD);
		pBits = (BYTE*)lrect.pBits;
		for (y = 0; y < image.h; y++)
		{
			for (x = 0; x < image.w; x++)
			{
				// move to the correct offset in the table
				// y is the row and pitch is the size of a row
				// (x*size) is the length of each pixel data (column)
				DWORD index = (y*pitch)+(x*size);
				// d3d has it's own pitch value cause it stores data at the end of the row
				DWORD index_p = (y*lrect.Pitch)+(x*size);
				// D3DFMT_A8R8G8B8 data will be accessible backwards: bgra
				// image.data is packed in rgba
				pBits[index_p]   = (BYTE)gamma_table[image.data[index+2]]; // blue
				pBits[index_p+1] = (BYTE)gamma_table[image.data[index+1]]; // green
				pBits[index_p+2] = (BYTE)gamma_table[image.data[index]];   // red
				// colour key
				if( image.colorkey && (pBits[index_p] + pBits[index_p+1] + pBits[index_p+2]) == 0 )
					pBits[index_p+3] = 0; // alpha - pixel will not be coppied do to alpha=0 ignore
				// do not colour key
				else
					pBits[index_p+3] = (BYTE)gamma_table[image.data[index+3]]; // alpha

			}
		}
		_texture->UnlockRect(0);
	}

	// return the values
	*width  = (uint16) image.w;
	*height = (uint16) image.h;
	(*colorkey) = (BOOL) image.colorkey;

	// info
	DebugPrintf( "Created texture: file=%s, width=%d, height=%d, colorkey=%s\n", 
		image.path, image.w, image.h, (image.colorkey ? "true" : "false") );

	//
	destroy_image( &image );

	//
	return S_OK;

#else

	LPDIRECT3DTEXTURE9 * texture = (LPDIRECT3DTEXTURE9*)t;
	D3DXIMAGE_INFO imageInfo;

	// if the file doesn't exist
	char real_file[256];
	if( ! File_Exists( (char*) path ) )
	{
		// try to find a png version
		Change_Ext( path, real_file, ".PNG" );
		path = &real_file[0];
	}
	if( ! File_Exists( (char*) path ) )
	{
		// try to find a png version
		Change_Ext( path, real_file, ".BMP" );
		path = &real_file[0];
		DebugPrintf("Could not find PNG version of: %s\n",path);
	}
	if( ! File_Exists( (char*) path ) )
	{
		DebugPrintf("Could not find texture file: %s\n",path);
		return S_FALSE;
	}

	HRESULT LastError = D3DXCreateTextureFromFileEx(lpD3DDevice, 
				path, 
				0,//*width, 
				0,//*height, 
				numMips, 
				0,
				// most likely will end up as the format the file is in
				D3DFMT_A8R8G8B8, // all texure data will be accessible in LockRect()'s pBits backwards: bgra
				pool,
				D3DX_DEFAULT,
				D3DX_DEFAULT,
				(*colorkey) ? OPAQUE_BLACK : 0, // colour key
				&imageInfo,
				NULL,
				texture);

	if (FAILED(LastError))
	{
		DebugPrintf("couldn't create texture\n");
	}
	
	// return the values
	*width  = (uint16) imageInfo.Width;
	*height = (uint16) imageInfo.Height;

	// image has no alpha layer
	// only way image would have an alpha layer
	// is the updated textures we created
	// which will have exactly this format!
	if( imageInfo.Format != D3DFMT_A8R8G8B8 )
		(*colorkey) = FALSE;

	// debugging info
	DebugPrintf("Created texture from %s\n",path);
	DebugPrintf("Texture Details: width=%d, height=%d, depth=%d, mips=%d, image_format=%s, file_format=%s\n",
					imageInfo.Width, imageInfo.Height, imageInfo.Depth, imageInfo.MipLevels, 
					d3d_format(imageInfo.Format), d3d_image_file_formats[imageInfo.ImageFileFormat] );

	/*
	// saves the texture in it's loaded format to a file
	{
		static int count = 0;
		sprintf(buf, ".\\Dumps\\%s.png", fileName);
		D3DXSaveTextureToFile(buf, D3DXIFF_PNG, (*texture), 0);
		count++;
	}
	*/

	//
	// gamma correction
	//
	{
		unsigned int y, x;
		int size = 4;
		D3DLOCKED_RECT lrect;
		BYTE* pBits;
		LPDIRECT3DTEXTURE9 _texture = *texture;
		_texture->LockRect(0,&lrect,NULL,D3DLOCK_DISCARD);
		pBits = (BYTE*)lrect.pBits;
		for (y = 0; y < imageInfo.Height; y++)
		{
			for (x = 0; x < imageInfo.Width; x++)
			{
				// move to the correct off set in the table
				// pitch is the width of a row
				// (x*size) is the length of each pixel data
				DWORD index = (x*size)+(y*lrect.Pitch);
				// D3DFMT_A8R8G8B8 data will be accessible backwards: bgra
				pBits[index]   = (BYTE)gamma_table[pBits[index]];	// Blue
				pBits[index+1] = (BYTE)gamma_table[pBits[index+1]];// Green
				pBits[index+2] = (BYTE)gamma_table[pBits[index+2]];// Red
				// i did not see any alpha values changed for gamma in d3d6 version
				//pBits[index+3] = (BYTE)gamma_table[pBits[index+3]];// Alpha
			}
		}
		_texture->UnlockRect(0);
	}

	return LastError;

#endif
}
Beispiel #3
0
static bool BSP_LoadPortals( char *fname )
{
	char	Filename[ 256 ];
	long			File_Size;
	long			Read_Size;
	char		*	Buffer;
	char		*	OrgBuffer;
	int16_t		*	int16_tpnt;
	int16_t			i;
	u_int32_t		*	Uint32Pnt;
	u_int32_t			MagicNumber;
	u_int32_t			VersionNumber;

	Bsp_Portal_Header.state = false;

	Change_Ext( fname, Filename, ".PBS" );

	File_Size = Get_File_Size( Filename );	
	if( !File_Size )
	{
		Msg( "Bsp_Portalload() no PBS file %s", Filename );
		return false;
	}

	Buffer = malloc( File_Size );
	OrgBuffer = Buffer;

	if( Buffer == NULL ) return false;

	Read_Size = Read_File( Filename, Buffer, File_Size );

	if( Read_Size != File_Size ) return false;

	Uint32Pnt = (u_int32_t *) Buffer;
	MagicNumber = *Uint32Pnt++;
	VersionNumber = *Uint32Pnt++;
	Buffer = (char *) Uint32Pnt;

	if( ( MagicNumber != MAGIC_NUMBER ) || ( VersionNumber != BSP_PORTAL_VERSION_NUMBER  ) )
	{
		Msg( "Bsp_Portalload() Incompatible PBS file %s", Filename );
		return( false );
	}
	int16_tpnt = ( int16_t * ) Buffer;
	Bsp_Portal_Header.groups = *int16_tpnt++;
	Buffer = (char * ) int16_tpnt;

	for ( i = 0; i < Bsp_Portal_Header.groups; i++ )
	{
		if ( !BSP_LoadPortalGroup( &Bsp_Portal_Header.group[ i ], &Buffer ) )
		{
			return false;
		}
	}

	free( OrgBuffer );

	Bsp_Portal_Header.state = true;

	return true;
}