MOVES/PCX.CPP
2021-09-08 21:30:32 +02:00

205 lines
5.7 KiB
C++

#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define ERROR 1
#define OK 1
typedef unsigned char DacPalette256[256][3];
void setvgapalette256(DacPalette256 *PalBuf);
DacPalette256 Palette256;
void InicializaSVGA(void);
extern int far _Cdecl Svga256_fdriver[];
int huge DetectVGA256(){ return 2; }
int MuestraImagen( char *file );
int CargaPaleta(char *file );
void main( void )
{
int index, salida;
// Inicializamos el modo SVGA 640*480 256 c. ( VESA )
InicializaSVGA();
MuestraImagen( "DEMO.PCX" );
while( !kbhit() );
salida = 0;
for( ; salida == 0; )
{
for (index=0; index<256; index++)
{
// set components
if( Palette256[index][0]>0 ) Palette256[index][0]--;
if( Palette256[index][1]>0 ) Palette256[index][1] --;
if( Palette256[index][2]>0 ) Palette256[index][2] --;
if( Palette256[index][0]==0 && Palette256[index][1]==0 && Palette256[index][2]==0 )
salida = 1;
else
salida = 0;
} // end for index
setvgapalette256( &Palette256 );
delay(50);
}
closegraph();
}
#define RES_X 640
#define RES_Y 480
/**************************************************************************\
|* *|
|* MuestraImagen *|
|* *|
|* Descripci¢n: *|
|* Descomprime y copia a la pagina indicada un PCX *|
|* *|
|* Entradas: nombre de la imagen *|
|* *|
|* *|
|* Salidas: OK Todo ha ido bien *|
|* ERROR Algo va mal *|
|* *|
\**************************************************************************/
int MuestraImagen( char *file )
{
int alto, ancho, contador;
unsigned char byte;
FILE *fp;
CargaPaleta( file );
if ( (fp = fopen( file,"rb")) != NULL )
{
// Saltamos la cabecera
fseek( fp, 128, SEEK_SET );
for(alto=0; alto<RES_Y; alto++)
{
for(ancho=0; ancho<RES_X; )
{
byte=getc(fp);
if(byte<=0xC0)
{
putpixel (ancho, alto, byte);
ancho++;
} else {
contador=byte&0x3F; byte=getc(fp);
for(; contador>0; contador--)
{
putpixel (ancho, alto, byte);
ancho++;
}
}
}
}
fclose(fp);
} else return ERROR;
return OK;
}
/**************************************************************************\
|* *|
|* CargaPaleta *|
|* *|
|* Descripci¢n: *|
|* Carga la paleta con los colores por defecto *|
|* *|
|* *|
|* Entradas: achivo PCX de donde cargar la paleta *|
|* *|
|* Salidas: OK Todo ha ido bien *|
|* ERROR Algo va mal *|
|* *|
\**************************************************************************/
int CargaPaleta(char *file )
{
int index;
FILE *fp;
if ( (fp=fopen( file, "rb" ) ) == NULL )
return ERROR;
if ( fseek( fp, -768L, SEEK_END ) == 0 )
{
for (index=0; index<256; index++)
{
// get the red component
// get the green component
// get the blue component
// set components
Palette256[index][0] = (getc(fp) >> 2)*64 / 256;
Palette256[index][1] = (getc(fp) >> 2)*64 / 256;
Palette256[index][2] = (getc(fp) >> 2)*64 / 256;
} // end for index
}
setvgapalette256( &Palette256 );
fclose( fp );
return OK;
}
/* Setvgapalette256 sets the entire 256 color palette */
/* PalBuf contains RGB values for all 256 colors */
/* R,G,B values range from 0 to 63 */
/* Usage: */
/* DacPalette256 dac256; */
/* */
/* setvgapalette256(&dac256); */
void setvgapalette256(DacPalette256 *PalBuf)
{
struct REGPACK reg;
reg.r_ax = 0x1012;
reg.r_bx = 0;
reg.r_cx = 256;
reg.r_es = FP_SEG(PalBuf);
reg.r_dx = FP_OFF(PalBuf);
intr(0x10,&reg);
}
void InicializaSVGA(void) {
int Gd = DETECT, Gm;
int Drv, errorcode;
installuserdriver("Svga256",DetectVGA256);
// registerfarbgidriver(Svga256_fdriver);
initgraph(&Gd,&Gm,"");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
cprintf("Graphics error: %s\n", grapherrormsg(errorcode));
cprintf("Presione una tecla para finalizar:");
getch();
exit(1); /* return with error code */
}
}