![]() |
| | #1 |
| Registered User Join Date: Feb 2006
Posts: 6
| Here's the code I have... The macros and structures for the graphics memory I currently have. Code: #define GraphicsSize(Block) (Block->Size + 1)
#define GraphicsNumber(Block) (Block->Number + 1)
struct GraphicsBlock
{
unsigned char Size; // Width & Height
unsigned char Number;
unsigned char **Images;
} Tiles;
Code: int LoadGraphics(const char *Filename, struct GraphicsBlock *Block)
{
FILE *File;
int Character;
unsigned char Number;
if ((File = fopen(Filename, "rb")) == NULL)
{
// Couldn't open the file: "Filename".
return 1;
}
if ((Character = fgetc(File)) == EOF)
{
// Couldn't get the images sizes.
fclose(File); return 1;
}
Block->Size = Character;
if ((Character = fgetc(File)) == EOF)
{
// Couldn't get the number of images.
fclose(File); return 1;
}
Block->Number = Character;
Block->Images = malloc(GraphicsNumber(Block) * sizeof(unsigned char *));
if (Block->Images == NULL)
{
// Out of memory for storing image locations.
fclose(File); return 1;
}
for (Number = 0; Number <= Block->Number; Number++)
{
Block->Images[Number] = malloc(GraphicsSize(Block) * GraphicsSize(Block));
if (Block->Images[Number] == NULL)
{
// Not enough memory for storing the images.
for (Character = 0; Character < Number; Character++)
free(Block->Images[Character]);
fclose(File); free(Block->Images); return 1;
}
if (fread(Block->Images[Number], GraphicsSize(Block) * GraphicsSize(Block), 1, File)
!= GraphicsSize(Block) * GraphicsSize(Block))
{
// Couldn't read images from the file.
for (Character = 0; Character <= Number; Character++)
free(Block->Images[Character]);
fclose(File); free(Block->Images); return 1;
}
}
fclose(File); return 0;
}
My problem is with the fread() function. It's only is reading a single byte from the file, when it should be reading 64 bytes for the file I'm loading. Both feof() & ferror() reports that nothing is wrong. I don't have any real ideas why this is happening. Anybody else know? Thanks in advance. |
| Drahcir is offline | |
| | #2 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Why are you using this idiom? Code: for (Number = 0; Number <= Block->Number; Number++) Where are the calls to feof and ferror? Why not check for success instead of failure; i.e., you received the amount of data you expected?
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #3 | |
| Registered User Join Date: Feb 2006
Posts: 6
| Code: for (Number = 0; Number <= Block->Number; Number++) Quote:
I just added and removed the feof() and ferror() functions to try to find out the reason for my trouble. Both functions returned 0 (for no errors). | |
| Drahcir is offline | |
| | #4 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| By compileable, I meant something that can compile. [edit]Since for an array of size N, it is addressable from 0 to N-1. Or Code: for ( i =0; i < N; ++i) Your idiom generally overshoots the array size by one. [edit=2 or 3]But then I'm having an off night.
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* Last edited by Dave_Sinkula; 02-03-2006 at 09:58 PM. |
| Dave_Sinkula is offline | |
| | #5 |
| Registered User Join Date: Feb 2006
Posts: 6
| The code is correct on my end. I have 7 images in my file. Which means when Block.Number is loaded, it should be set to 6 (just like the raw byte in the file). It's set this way since this function could never load a graphics file with zero images contained within it.When the program needs to find out the total number of images loaded within a block, it'll use the GraphicsNumber() macro. The loop uses the Block->Number which is already the total number of images - 1. So these two lines of code end up with the completely same outcome... Code: for (Number = 0; Number <= Block->Number; Number++) Code: for (Number = 0; Number < GraphicsNumber(Block); Number++) |
| Drahcir is offline | |
| | #6 | |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Code: #define GraphicsSize(Block) (Block->Size + 1)
#define GraphicsNumber(Block) (Block->Number + 1)
struct GraphicsBlock
{
unsigned char Size; // Width & Height
unsigned char Number;
unsigned char **Images;
} Tiles;
int LoadGraphics(const char *Filename, struct GraphicsBlock *Block)
{
FILE *File;
int Character;
unsigned char Number;
if ((File = fopen(Filename, "rb")) == NULL)
{
// Couldn't open the file: "Filename".
return 1;
}
if ((Character = fgetc(File)) == EOF)
{
// Couldn't get the images sizes.
fclose(File); return 1;
}
Block->Size = Character;
if ((Character = fgetc(File)) == EOF)
{
// Couldn't get the number of images.
fclose(File); return 1;
}
Block->Number = Character;
Block->Images = malloc(GraphicsNumber(Block) * sizeof(unsigned char *));
if (Block->Images == NULL)
{
// Out of memory for storing image locations.
fclose(File); return 1;
}
for (Number = 0; Number <= Block->Number; Number++)
{
Block->Images[Number] = malloc(GraphicsSize(Block) * GraphicsSize(Block));
if (Block->Images[Number] == NULL)
{
// Not enough memory for storing the images.
for (Character = 0; Character < Number; Character++)
free(Block->Images[Character]);
fclose(File); free(Block->Images); return 1;
}
if (fread(Block->Images[Number], GraphicsSize(Block) * GraphicsSize(Block), 1, File)
!= GraphicsSize(Block) * GraphicsSize(Block))
{
// Couldn't read images from the file.
for (Character = 0; Character <= Number; Character++)
free(Block->Images[Character]);
fclose(File); free(Block->Images); return 1;
}
}
fclose(File); return 0;
}
Quote:
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* | |
| Dave_Sinkula is offline | |
| | #7 |
| Registered User Join Date: Feb 2006
Posts: 6
| That code wasn't complete. I only posted the structure and function because that's all I having trouble with. The code can compile correctly.The fread() function is what I'm was having trouble with. The arrays are working correctly (well on my side at least) |
| Drahcir is offline | |
| | #8 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > My problem is with the fread() function. It's only is reading a single byte from the file, when it should be reading 64 bytes for the file But it is reading 64 bytes from the file, your call and your test are inconsistent. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size = GraphicsSize(Block) * GraphicsSize(Block), nmemb = 1, You told it to read 1 element of whatever size that is, 64 I guess. Written like this, it's an all or nothing deal, you either get the whole record or you get nothing, and the return result is either 0 or 1 records read. If you want to go the other way and swap the middle two parameters, do this fread(Block->Images[Number], 1, GraphicsSize(Block) * GraphicsSize(Block), File ) Then you can partially read a record and get any return result from 0 to say 64 (in this case) |
| Salem is offline | |
| | #9 |
| Registered User Join Date: Feb 2006
Posts: 6
| Thanks for you help Salem. I've expected the function to return the total bytes read. |
| Drahcir is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting number | Leslie | C Programming | 8 | 05-20-2009 04:23 AM |
| Trouble Generating Game Algorithm | justlivelife15 | Game Programming | 3 | 06-13-2007 09:58 AM |
| 2D Game project requires extra C++ programmers, new or experienced | drallstars | Projects and Job Recruitment | 2 | 05-16-2007 10:46 AM |
| PC Game project requires c++ programmers | drallstars | Projects and Job Recruitment | 2 | 02-22-2006 12:23 AM |
| trouble with pointer to object | drb2k2 | C++ Programming | 3 | 04-16-2003 03:10 PM |