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

📄 blitbuf.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	rect.bottom=y2;

	if( (dest->xsize-destx-1)<x2 )
		x2=dest->xsize-destx-1;
	if( (dest->ysize-desty-1)<y2 )
		y2=dest->ysize-desty-1;

	// Copio de vidmem a vidmem
	dest->surface->BltFast( destx, desty, source->surface, &rect, DDBLTFAST_WAIT );
}	
		
/* Copia seccion del BlitBuf a otro BlitBuf nuevo */
/* Los extremos x2 e y2 no entran!!!!! */

void
new_blitbuf(blitbuf *source, blitbuf *dest,
		unsigned short x1, unsigned short y1,
		unsigned short x2, unsigned short y2) 
{
	alloc_blitbuf(dest, (x2-x1), (y2-y1) );				// Seria correcto sumar 1 para que entren los bordes bien
	copy_blitbuf(source, dest, x1, y1, x2, y2, 0, 0);
}

/* Salva un BlitBuf en un PCX */
/* NO IMPLEMENTADA */
void
save_blitbufPCX(char *fname, blitbuf *buf)
{
/*    FILE *fp;
    unsigned int i, size, temp_int;
    BYTE VGA_pal[768];
	BYTE VGA_pal2[1024];
    BYTE *buf_ptr;
    BYTE temp_char, match, count;

    fp = fopen(fname, "wb");

    if (fp != NULL) {
        // Write manufacturer's byte
        temp_char = 10;
        fwrite(&temp_char, 1, 1, fp);

        // Write version of PCX.  5 = 256 color (PCX Version 5.0)
        temp_char = 5;
        fwrite(&temp_char, 1, 1, fp);

        // Write encoding type, always 1 for RLE.
        temp_char = 1;
        fwrite(&temp_char, 1, 1, fp);

        // Write bits_per_pixel = 8.
        temp_char = 8;
        fwrite(&temp_char, 1, 1, fp);

        // Write starting X and Y coords
        temp_int = 0;
        fwrite(&temp_int, 2, 1, fp);
        fwrite(&temp_int, 2, 1, fp);

        // Write X size
        temp_int = (buf->xsize - 1);
        fwrite(&temp_int, 2, 1, fp);

        // Write Y size
        temp_int = (buf->ysize - 1);
        fwrite(&temp_int, 2, 1, fp);

        // Do HRES and VRES **
        temp_int = buf->xsize;
        fwrite(&temp_int, 2, 1, fp);
        temp_int = buf->ysize;
        fwrite(&temp_int, 2, 1, fp);

        // Write 16 color palette, not used.
        temp_int = 0;
        i=24;
        while (i--) {
            fwrite(&temp_int, 2, 1, fp);
        }

        // Write vmode byte.
        temp_char = 0;
        fwrite(&temp_char, 1, 1, fp);

        // Write bit_planes
        temp_char = 1;
        fwrite(&temp_char, 1, 1, fp);

        // Write bytes_per_line
        temp_int = buf->xsize;
        fwrite(&temp_int, 2, 1, fp);

        // Write palette type
        temp_int = 1;
        fwrite(&temp_int, 2, 1, fp);

        // Write junk filler
        temp_int = 0;
        i=29;
        while (i--) {
            fwrite(&temp_int, 2, 1, fp);
        }

        // Write the actual image
        buf_ptr = buf->image;
        size = (buf->xsize * buf->ysize);

        count = 0;
        match = *buf_ptr;

        i=size;
        while (i--) {
            temp_char = *buf_ptr++;

            if ((temp_char == match) && (count < 63)) {
               count++;
            } else {
                if ((count == 1) && (match < 192)) {
                    // Write single byte
                    fwrite(&match,1,1,fp);
                } else {
                    // Write run of pixels
                    count += 192;
                    fwrite(&count, 1, 1, fp);
                    fwrite(&match, 1, 1, fp);
                }
                count = 1;
                match = temp_char;
            }
        }

        if ((count == 1) && (match < 192)) {
            // Write single byte
            fwrite(&match,1,1,fp);
        } else {
            // Write run of pixels
            count += 192;
            fwrite(&count, 1, 1, fp);
            fwrite(&match, 1, 1, fp);
        }

        // Write palette verification byte
        temp_char = 12;
        fwrite(&temp_char, 1, 1, fp);

		GetPalette( (LPPALETTEENTRY) VGA_pal2 );

		for( int x=0; x<256; x++ )
		{
			VGA_pal[x*3]=VGA_pal2[x*4];
			VGA_pal[x*3+1]=VGA_pal2[x*4+1];
			VGA_pal[x*3+2]=VGA_pal2[x*4+2];
		}
//			fwrite(VGA_pal+x*4, 1, 3, fp);
  
		
//		get_paletteX(VGA_pal);
    
		// Write 256 color palette

        
		fwrite(VGA_pal, 1, 768, fp);

        fclose(fp);
    }
*/
}

/* Carga un PCX en un BlitBuf */

int
load_blitbufPCX(char *fname, blitbuf *buf) 
{

    FILE *fp;
    int size;
    unsigned char VGA_pal[1024];
    unsigned char PCX_byte, RLE_byte;
	unsigned char *image;
    unsigned char *buf_ptr;
    unsigned char *end_of_buf;
	int r, g, b, act;

	if( !LUTinicializado )
		initLUTs();

	fplog( "loadPCX: %s\n", fname );

    fp = fopen(fname, "rb");

    if (fp == NULL) 
    {
		fplog( "ERROR: No se encontro %s\n", fname );

        return 0;
    } 
    else 
    {
        fseek(fp, 8, SEEK_SET);
        fread(&buf->xsize, 2, 1, fp);
        fread(&buf->ysize, 2, 1, fp);

        buf->xsize++;
        buf->ysize++;

		fplog( "load xsize %d ysize %d\n", buf->xsize, buf->ysize );

        size = (buf->xsize * buf->ysize);

		// Seteo los pointers
        image = (unsigned char *) mymalloc( size );
		if( image==NULL ) return 0;

		buf_ptr=image;
        end_of_buf = buf_ptr + size;

        // Load 256 color PCX palette
        fseek(fp, -768, SEEK_END);
		for( int i=0; i<256; i++)
			fread(VGA_pal+i*4, 1, 3, fp);

        fseek(fp, 128, SEEK_SET);

        while (buf_ptr < end_of_buf)
		{
            // Read next packet
            fread(&PCX_byte, 1, 1, fp);

            if (PCX_byte < 192)
			{
                // Raw Pixel
                *buf_ptr++ = PCX_byte;
            }
			else
			{
                // RLE Pixels
                PCX_byte = (PCX_byte & 0x3F);
                fread(&RLE_byte, 1, 1, fp);
                memset(buf_ptr, RLE_byte, PCX_byte);
                buf_ptr += PCX_byte;
            }
        }
        fclose(fp);

		DDSURFACEDESC2 ddsd;
		pixelT *buffer;

		ZeroMemory( &ddsd, sizeof(ddsd) );
		ddsd.dwSize=sizeof(ddsd);
		if( buf->surface->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) )
			return 0;

		buffer=(pixelT *)ddsd.lpSurface;

		// Convierto buf_ptr en una superficie
		for( int y=0; y<(int)ddsd.dwHeight; y++ )
		{
			for( int x=0; x<(int)ddsd.dwWidth; x++ )
			{
				act=*(image+y*buf->xsize+x);
				r=(int) *(VGA_pal+act*4);
				g=(int) *(VGA_pal+act*4+1);
				b=(int) *(VGA_pal+act*4+2);

				*(buffer+x)=PureRedLUT[r] | PureGreenLUT[g] | PureBlueLUT[b];
			}
			buffer+=(ddsd.lPitch/sizeof(pixelT));
		}

		buf->surface->Unlock( NULL );

		free( image );

        return 1;
    }
}

/* Carga un PCX en un BlitBuf y lo aloca */

int
allocload_blitbufPCX(char *fname, blitbuf *buf) 
{
    FILE *fp;
    int size;
    unsigned char VGA_pal[1024];
    unsigned char PCX_byte, RLE_byte;
	unsigned char *image;
    unsigned char *buf_ptr;
    unsigned char *end_of_buf;
	int r, g, b, act;
	unsigned short xs, ys;

	if( !LUTinicializado )
		initLUTs();

	fplog( "allocloadPCX: %s\n", fname );

	// Abro el PCX y guardo en *image
    fp = fopen(fname, "rb");

    if (fp == NULL) 
    {
		fplog( "ERROR: No se encontro %s\n", fname );

        return 0;
    } 
    else 
    {
        fseek(fp, 8, SEEK_SET);
        fread(&xs, 2, 1, fp);
        fread(&ys, 2, 1, fp);

        xs++;
        ys++;

		fplog( "load xsize %d ysize %d\n", xs, ys );

        size = (xs * ys);

		// Seteo los pointers
        image = (unsigned char *) mymalloc( size, "allocload_blitbufPCX->image" );
		if( image==NULL ) return 0;

		buf_ptr=image;
        end_of_buf = buf_ptr + size;

        // Load 256 color PCX palette
        fseek(fp, -768, SEEK_END);
		for( int i=0; i<256; i++)
			fread(VGA_pal+i*4, 1, 3, fp);

        fseek(fp, 128, SEEK_SET);

        while (buf_ptr < end_of_buf)
		{
            // Read next packet
            fread(&PCX_byte, 1, 1, fp);

            if (PCX_byte < 192)
			{
                // Raw Pixel
                *buf_ptr++ = PCX_byte;
            }
			else
			{
                // RLE Pixels
                PCX_byte = (PCX_byte & 0x3F);
                fread(&RLE_byte, 1, 1, fp);
                memset(buf_ptr, RLE_byte, PCX_byte);
                buf_ptr += PCX_byte;
            }
        }
        fclose(fp);

		// Creo el nuevo blitbuf
		alloc_blitbuf( buf, xs, ys );
		
		// Convierto a superficies
		DDSURFACEDESC2 ddsd;
		pixelT *buffer;

		ZeroMemory( &ddsd, sizeof(ddsd) );
		ddsd.dwSize=sizeof(ddsd);
		if( buf->surface->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL ) )
			return 0;

		buffer=(pixelT *)ddsd.lpSurface;

		// Convierto buf_ptr en una superficie
		for( int y=0; y<(int)ddsd.dwHeight; y++ )
		{
			for( int x=0; x<(int)ddsd.dwWidth; x++ )
			{
				act=*(image+y*buf->xsize+x);
				r=(int) *(VGA_pal+act*4);
				g=(int) *(VGA_pal+act*4+1);
				b=(int) *(VGA_pal+act*4+2);

				*(buffer+x)=PureRedLUT[r] | PureGreenLUT[g] | PureBlueLUT[b];
			}
			buffer+=(ddsd.lPitch/sizeof(pixelT));
		}

		buf->surface->Unlock( NULL );

		myfree( image, "allocload_blitbufPCX->image" );

        return 1;
    }
}
               
/* Da vuelta un blitbuf verticalmente */
/* NO IMPLEMENTADA */

void
flip_vertical_blitbuf(blitbuf *buf)
{
}

/* Da vuelta un blitbuf horizontalmente */
/* NO IMPLEMENTADA */

void
flip_horizontal_blitbuf(blitbuf *buf)
{
}

⌨️ 快捷键说明

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