Original Post
I am making a game (with svgalib).
But when I load a pcx file it draws weird pixels on my pcx
I don't know what's wrong.Why it's drawing these pixels on every pcx file I load ?
Here are my source code's
********************************************
-------------------game.c-------------------
********************************************
#include
#include
#include
#include
void *readpcx(FILE *file, char *palette,unsigned short int *length, unsigned short int *height);
void init_svgalib (GraphicsContext *physicalscreen);
void init_svgalib (GraphicsContext *virtualscreen);
void exit_svgalib();
GraphicsContext *physicalscreen;
GraphicsContext *virtualscreen;
int main(void)
{
FILE *file;
FILE *file2;
FILE *file3;
void *image;
void *image2;
void *image3;
int i;
int x2,y2;
char ch;
unsigned short int length, height;
unsigned char palette[768];
int x;
char *keymap;
int i2,
j,
b,
y,
c;
x2=0;
y2=0;
vga_init();
vga_setmode(5);
gl_setcontextvga(5);
physicalscreen = gl_allocatecontext();
gl_getcontext(physicalscreen);
gl_setcontextvgavirtual(5);
virtualscreen = gl_allocatecontext();
gl_getcontext(virtualscreen);
if ((file3=fopen("frank5.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
}
if ((file2=fopen("frank3.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image2=readpcx(file2,palette,&length,&height))==NULL)
{
}
if ((file=fopen("land.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image=readpcx(file,palette,&length,&height))==NULL)
{
}
fclose(file);
printf("Image is %dx%d sized.\n",length,height);
if((length>320)||(height>200)) {
printf("Image is too big!\n");
return(1);
}
gl_setcontext(virtualscreen);
// for ( ; ; )
// {
// gl_copyscreen(physicalscreen);
// gl_setpalettecolor(c, 0, 0, 0);
//// y = 0;
// c = 3;
// for (i = 0; i < 64; i++)
{
// for (i=1;i<768;i++) palette=palette[i]>>8;
//init_svgalib(&virtualscreen);
//gl_setpalette(palette);
// for (i=1;i<768;i++) palette[i]=palette[i]>>4;
gl_clearscreen(0);
gl_copyscreen(virtualscreen);
gl_putbox(10,10,320,200,file);
gl_putbox(x2,y2,length,height,file3);
vga_waitretrace();
}
keyboard_init();
while( 1 )
{
keyboard_update();
if( keyboard_keypressed( SCANCODE_CURSORBLOCKUP ))
{
y2 = y2 -3;
gl_setcontext(virtualscreen);
if ((file3=fopen("frank5.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
}
gl_clearscreen(0);
gl_putbox(10,10,320,200,file);
gl_putbox(x2,y2,length,height,file3);
vga_waitretrace();
gl_copyscreen(physicalscreen);
}
if( keyboard_keypressed( SCANCODE_CURSORBLOCKDOWN ))
{
y2 = y2 +3;
gl_clearscreen(0);
gl_putbox(10,10,320,200,file);
gl_putbox(x2,y2,length,height,file3);
vga_waitretrace();
gl_copyscreen(physicalscreen);
if ((file2=fopen("wizzard.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image2=readpcx(file2,palette,&length,&height))==NULL)
{
}
if ((file=fopen("land.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image=readpcx(file,palette,&length,&height))==NULL)
{
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
}
if ((file3=fopen("frank3.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
}
}
if( keyboard_keypressed( SCANCODE_CURSORBLOCKRIGHT ))
{
x2 = x2+3;
if ((file3=fopen("frank2.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
}
gl_putbox(10,10,320,200,file);
gl_putbox(x2,y2,length,height,file3);
vga_waitretrace();
gl_copyscreen(physicalscreen);
}
if( keyboard_keypressed( SCANCODE_CURSORBLOCKLEFT ))
{
x2 = x2-3;
if ((file3=fopen("frank4.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
}
gl_clearscreen(0);
gl_putbox(10,10,320,200,file);
gl_putbox(x2,y2,60,40, file3);
vga_waitretrace();
gl_copyscreen(physicalscreen);
}
if( keyboard_keypressed( SCANCODE_ESCAPE ) )
{
break;
}
}
gl_clearscreen(0);
vga_setmode(0);
exit_svgalib();
keyboard_close();
return(0);
}
}
***************************************************
-----------------------pcx.c-----------------------
***************************************************
#include
#include
#include
typedef struct
{
char signature;
char version;
char encoding;
char bytes_per_pixel;
unsigned short int xmin;
unsigned short int ymin;
unsigned short int xmax;
unsigned short int ymax;
unsigned short int vres;
unsigned short int hres;
char palette[48];
char reserved;
char color_layers;
unsigned short int bytes_per_line;
unsigned short int palette_type;
char unused[58];
}PCX_Header;
void readpcximage(FILE * file,void * target,int size)
{
unsigned char buf;
unsigned int counter;
int i=0;
while(i<=size) /* Image not entirely read? */
{
/* Get one byte */
fread(&buf,1,1,file);
/* Check the 2 most significant bits */
if ((buf&192)==192)
{
/* We have 11xxxxxx */
counter=(buf&63); /* Number of times to repeat next byte */
fread(&buf,1,1,file); /* Get next byte */
for(;counter>0;counter--) /* and copy it counter times */
{ ((char*)target)[i]=buf;
i++; /* increase the number of bytes written */
}
}
else
{
/* Just copy the byte */
((char*)target)[i]=buf;
i++; /* Increase the number of bytes written */
}
}
}
void *readpcx(FILE *file, char *palette,unsigned short int *length, unsigned short int *height)
/* Returns NULL if failed, otherwise a pointer to the loaded image */
{
PCX_Header header;
void *target;
fseek(file,0,SEEK_SET);
fread(&header,sizeof(PCX_Header),1,file); /* read the header */
/* Check if this file is in pcx format */
if((header.signature!=0x0a)||(header.version!=5))
return(NULL);
else
{/* it is! */
/* Return height and length */
*length=header.xmax+1-header.xmin;
*height=header.ymax+1-header.ymin;
/* Allocate the sprite buffer */
target=(void *)malloc((*length)*(*height));
/* Read the image */
readpcximage(file,target,(*length)*(*height));
fseek(file,-768,SEEK_END);
/* Get the palette */
fread(palette,1,768,file);
/* PCX succesfully read! */
return(target);
}
}
*************************************************
----------------------svgacommon.c--------------
*************************************************
#include
#include
#include
void init_svgalib(GraphicsContext * virtualscreen)
{
if (vga_init()!=0) {
printf("Error initialising svgalib!\n");
exit(1);
}
if (vga_setmode(G640x480x256)!=0) {
printf("Can't set mode 320x200x256!\n");
exit(1);
}
if (gl_setcontextvga(G640x480x256)!=0) {
printf("Error setting context!\n");
exit(1);
}
gl_getcontext(virtualscreen);
if (gl_setcontextvgavirtual(G640x480x256)!=0) {
printf("Error setting virtual context!\n");
exit(1);
}
}
void exit_svgalib()
{
vga_setmode(TEXT);
}
************************************
---------compile line --------------
************************************
gcc game.c pcx.c svgacommon.c -o game -lvgagl -lvga
Edited by - Half Wolf on 11/5/00 6:11:22 AM