Archived

This topic is now archived and is closed to further replies.

Half Wolf

Problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
*FLINCHES* Ouch. That hurt my ears.

Ok.

Lesson : Never ever ever ask that. Ever.

Since I'm a nice soul, I'm actually going to bother to look at it, and try (with some difficulty) to decipher what you have there. 99/100 times, you wont get a single response. Posting straight source code is possibly as pointless as saying:

"I have a problem. What is it?" Only additionally giving a dump of your entire brain...

OK. In your code...

Let me summerize it, and correct me if I'm wrong...

* You start off in your main loop and init all the VGA stuff. Cool. Then you load all your files...and only close one of the files, print out a message.

* Then do dont do some stuff:

        
// 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;


Which just looks like crap to me, but I dunno. Why is this in the source code?

* Then you do some stuff...

gl_clearscreen(0);
gl_copyscreen(virtualscreen);
gl_putbox(10,10,320,200,file);
gl_putbox(x2,y2,length,height,file3);

Which is just weird. I dont get it at all. What does gl_putbox do? I'm not familiar with it, if its an openGL function. And why the hell are you passing it file3, and file (ESPECIALLY file, which you just CLOSED with fclose!!)???

* Then you head into the keyboard testing...

Using your files, which only one of you bothered to close, open other files, and use that putbox function again. Hang a tick, I'll go look and see if putbox is an open gl function...

No. Its not.

Ok. I give up here and now, trying to trace the flow of your program. What is your code doing?

You load images, but never use them. You use "gl_putbox" on file pointers, and never draw your images to the screen. Unless you source in incomplete, I cant help more. I'll go and see if I can see anything wrong with your pcx.c, but I doubt I'll be any help.

Nb. If you repost source, trying quoting in a box, so you preserve formatting, and we can see what includes you used...


PCX.c
=====

readpcximage seems ok; although I'm not familar with the pcs image format, so I cant speak on that.

readpcx also looks ok; if convoluted; which may just be formatting.

In conclusion: I cant help you. There simply isnt enough to work on. How do you draw images? Using what? Where?

If you have a problem like this, put the source code up on a webpage, in TXT format, so it can be seen the way it actually is, and put a link to it. Explain carefully what the problem is and under what circumstances. Sorry. I tried.

Having said so there are a couple of possibilities...

1) You are incorrect in your image format. Unlikely, if only a few pixels are wrong, but its possbile that when you do it you misload a few pixels somewhere.

2) Your drawing algorithim isnt working. Again, same thing; possible that a few pixels are screwed up. Depends where your wrong pixels are. Are they in a corner, the middle, changing?

3) You're over writing your memory. Possibly a likely reason. In c you can step beyond the bounds of an array / structure accidentally, ESPECIALLY using fread and void * pointers. You may be randomly chaning values by this method.

Offhand, I'd say 3 is likely; although I didnt specifically see anything wrong in that direction looking at the code (but then again, finding the matching brackets is almost an Epic Quest (Tm) in itself, so I might have missed it).

Edited by - Shadow Mint on November 6, 2000 7:54:12 AM

Edited by - Shadow Mint on November 6, 2000 7:55:52 AM

Share this post


Link to post
Share on other sites
*YAWNS* Yes. I know. I''m bored.

Fixed up the pcx code to be semi-readable. Anyone who actually knows the PCX format care to comment?

    
// Function reads the byte values from the file

void readpcximage(FILE *file, void *target, int size)
{
// Variables

unsigned char buf; // Buffer (unsigned??)
unsigned int counter; // Counter (now Why is this unsigned?)
int i=0; // Looping variable I assume


// Assuming size is CORRECT, read that many bytes
while(i <= size)
{
/* Get one byte: store it in buf */
fread(&buf,1,1,file);

/* Check the 2 most significant bits */
if ((buf&192)==192)
{
/* Number of times to repeat next byte */
counter=(buf&63);

/* Get the next byte */
fread(&buf,1,1,file);

/* Copy byte read counter times into target */
for(;counter > 0; counter--)
{
// I''m dubious to validity of casting target

// as an array of char and setting target<i>,
// This could easily be the problem, since
// we dont break if i >= size.

((char*)target)[i]=buf;
i++;
}


}
else
{
// Again I''m dubious. But whatever.

((char*)target)[i]=buf;
i++;
}
}
}

// Allocates an image structure and returns it

void *readpcx(FILE *file, char *palette,unsigned short int *length, unsigned short int *height)
{
PCX_Header header;
void *target;

// Move to start of file

fseek(file,0,SEEK_SET);

// Read the header information: incidentally, why is everything
// unsigned? I''m not sure if do that works ok with unsigned values...

fread(&header,sizeof(PCX_Header),1,file);

// Check the header signature

if ((header.signature! = 0x0a) || (header.version! = 5))
return(NULL);
else
{
/* Set the height and length values in the argument*/
*length=header.xmax+1-header.xmin;
*height=header.ymax+1-header.ymin;

/* Allocate the sprite buffer; simply H*W bytes */
target = (void *) malloc((*length)*(*height));

/* Read the image */
readpcximage(file, target, (*length)*(*height));

// Seek to 768 bytes from the end

fseek(file,-768,SEEK_END);

/* Get the palette */

fread(palette, 1, 768, file);

/* Return memory structure
return(target);
}
}

Share this post


Link to post
Share on other sites
I have deleted a few lines of comment it''s better to read now...

But It draws pixels on every pcx .Here are my source code''s again :

game :
    

#include <stdio.h>
#include <vga.h>
#include <vgagl.h>
#include <vgakeyboard.h>


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 *file3;
void *image;
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;

vga_init();
vga_setmode(5);
gl_setcontextvga(5);
physicalscreen = gl_allocatecontext();
gl_getcontext(physicalscreen);
gl_setcontextvgavirtual(5);
virtualscreen = gl_allocatecontext();
gl_getcontext(virtualscreen);

x2=0;
y2=0;

if ((file3=fopen("frank5.pcx","r"))==NULL)
{
printf("Can''t open file!\n");
return(1);
}

if ((image3=readpcx(file3,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);

{
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 ((file=fopen("land.pcx","r"))==NULL)
{
printf("Can''t open file!\n");
return(1);
}

if ((image=readpcx(file,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_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_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);
}

[/source]
pcx.c :

[source]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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)
{

fread(&buf,1,1,file);

if ((buf&192)==192)
{

counter=(buf&63);
fread(&buf,1,1,file);
for(;counter>0;counter--)
{ ((char*)target)<i>=buf;
i++;
}
}
else
{

((char*)target)[i]=buf;
i++;
}
}
}


void *readpcx(FILE *file, char *palette,unsigned short int *length, unsigned short int *height)

{
PCX_Header header;
void *target;
fseek(file,0,SEEK_SET);
fread(&header,sizeof(PCX_Header),1,file);
if((header.signature!=0x0a)||(header.version!=5))
return(NULL);
else
{
target=(void *)malloc((*length)*(*height));
readpcximage(file,target,(*length)*(*height));
fseek(file,-768,SEEK_END);
fread(palette,1,768,file);
return(target);
return(0);
}
}




btw : I''m just a kid of 13 years old , sow I''m a beginner with svga....

Share this post


Link to post
Share on other sites
*Makes a mild frustrated noise* Ok. I''ll go and see. Can you explain at least WTF gl_putbox is?

Nb. As you can see multiple source tags dont work; I think the code one does, not sure..

By the way: Does it the picture it draws actually look like the picture is should?

Share this post


Link to post
Share on other sites
RIGHT. Now, there are three major issues:

1) You pass file and file3 to gl_putbox, not image and image3,
as you should.

2) You never run init_svgalib.

Combined, these screw up any hope of finding if the image format is correct. ...unfortunately, it isnt.

3) The image you read is incorrectly formatted.

Here's the source I used to test this. Watch out, svgalib behaves badly with some pcx files.

    
#include <stdio.h>
#include <unistd.h>
#include <vga.h>
#include <vgagl.h>
#include <vgakeyboard.h>
#include <stdlib.h>
#include <string.h>


// Structures

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;


// Prototyping

void *readpcx (FILE *file,
char *palette,
unsigned short int *length,
unsigned short int *height);


void init_svgalib (GraphicsContext *gc);
void exit_svgalib();


// Globals

GraphicsContext *physicalscreen;
GraphicsContext *virtualscreen;


int main(void)
{
FILE *file;
FILE *file3;
void *image;
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;


// Load file 1

if ((file3=fopen("one.pcx","r"))==NULL)
{
printf ("Can't open file!\n");
exit (1);
}
if ((image3=readpcx(file3,palette,&length,&height))==NULL)
{
printf ("File load failure.\n");
exit (0);
}


// Load file 2

if ((file=fopen("one.pcx","r"))==NULL)
{
printf("Can't open file!\n");
return(1);
}
if ((image=readpcx(file,palette,&length,&height))==NULL)
{
printf ("File load failure.\n");
exit (0);
}


// Close files

fclose (file);
fclose (file3);


// Init vga

vga_init();
vga_setmode(5);
gl_setcontextvga(5);
physicalscreen = gl_allocatecontext();
//gl_getcontext(physicalscreen);
init_svgalib (physicalscreen);
gl_setcontextvgavirtual(5);
virtualscreen = gl_allocatecontext();
//gl_getcontext(virtualscreen);
init_svgalib (virtualscreen);


// Init positions

x2=0;
y2=0;


// Print image data

printf("Image is %dx%d sized.\n",length,height);
if ((length>320) || (height>200))
{
printf ("Image is too big!\n");
exit (1);
}


// Virtual context stuff

gl_setcontext(virtualscreen);
gl_clearscreen(0);
gl_copyscreen(virtualscreen);


// Init key listener

keyboard_init();


// Enter main loop

while( 1 )
{
keyboard_update();


if(keyboard_keypressed(SCANCODE_ESCAPE))
{
gl_clearscreen(0);
vga_setmode(0);
exit_svgalib();
keyboard_close();
return(0);
}


// Draw it all again

gl_fillbox (0, 0, 299, 199, gl_rgbcolor (0, 50, 3));
gl_putbox(10,10,length,height,image);
gl_putbox(50,50,length,height,image3);
gl_line (0, 0, x2, y2, gl_rgbcolor (0, 3, 50));
gl_copyscreen(physicalscreen);
++x2;
++y2;
printf ("X is: %d, Y is: %d\n", x2, y2);


// Sleep for a bit

sleep (1);
}


return (0);
}


void readpcximage(FILE * file,void * target,int size)
{
unsigned char buf;
unsigned int counter;
int i=0;


while (i <= size)
{
fread(&buf,1,1,file);


if ((buf&192)==192)
{
counter=(buf&63);


fread(&buf,1,1,file);


for(;counter>0;counter--)
{
((char*)target)<i>=buf;
i++;

if (i >= size)
break;
}
}
else
{
((char*)target)[i]=buf;
i++;
}
}
}


void *readpcx(FILE *file, char *palette,unsigned short int *length, unsigned short int *height)
{
PCX_Header header;
void *target;
fseek(file,0,SEEK_SET);


fread(&header,sizeof(PCX_Header),1,file);


*length=header.xmax+1-header.xmin;
*height=header.ymax+1-header.ymin;


if((header.signature!=0x0a)||(header.version!=5))
return(NULL);
else
{
target=(void *)malloc((*length)*(*height));


readpcximage(file,target,(*length)*(*height));


fseek(file,-768,SEEK_END);


fread(palette,1,768,file);


return(target);
}
}


void init_svgalib (GraphicsContext *gc)
{
if (vga_init()!=0)
{
printf("Error initialising svgalib!\n");
exit(1);
}


if (vga_setmode(G640x480x64K)!=0)
{
printf("Can't set mode 640x480x64K!\n");
exit(1);
}


if (gl_setcontextvga(G640x480x64K)!=0)
{
printf("Error setting context!\n");
exit(1);
}


gl_getcontext(gc);


if (gl_setcontextvgavirtual(G640x480x64K)!=0)
{
printf("Error setting virtual context!\n");
exit(1);
}
}


void exit_svgalib()
{
vga_setmode(TEXT);
}



Edited by - Shadow Mint on November 6, 2000 10:16:45 PM

Share this post


Link to post
Share on other sites