Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Little bit of trouble with loading from memory.  (Read 2669 times)

0 Members and 1 Guest are viewing this topic.

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[SOLVED] Little bit of trouble with loading from memory.
« on: December 09, 2009, 08:34:34 pm »
I've been trying to create resource files for a game I've been working on and I have never worked with them before.

I found a tutorial online with C and SDL and have been trying to modify it to my own needs.

I use http://gpwiki.org/index.php/C:Custom_Resource_Files that tutorial to create the custom resource file.

Then I use http://gpwiki.org/index.php/C:Displaying_a_Bitmap_from_a_Custom_Resource_File_using_SDL_RWops that tutorial, modified to use with SFML, and I've been having some trouble.

I keep getting "Failed to load image from memory. Reason: Unknown image type".

The image is a PNG. The size passed to the function is correct.

I attempted to use a BMP, and it displayed it, but the colors were corrupted.

Am I going about this the wrong way?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED] Little bit of trouble with loading from memory.
« Reply #1 on: December 09, 2009, 09:33:30 pm »
You should first save your memory file to the disk (and try to open it with an image viewer), to see if it is corrupted.

If not, then you should show us your code ;)
Laurent Gomila - SFML developer

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[SOLVED] Little bit of trouble with loading from memory.
« Reply #2 on: December 09, 2009, 11:30:53 pm »
It's definitely corrupting the image.

This is the code that packs the .dat file.

Code: [Select]

void packfile(char *filename, int fd) {
 
int totalsize = 0; //This integer will be used to track the total number of bytes written to file
 
//Handy little output
printf("PACKING: '%s' SIZE: %i\n", filename, getfilesize(filename));
 
//In the 'header' area of the resource, write the location of the file about to be added
lseek(fd, currentfile * sizeof(int), SEEK_SET);
write(fd, &currentloc, sizeof(int));
 
//Seek to the location where we'll be storing this new file info
lseek(fd, currentloc, SEEK_SET);
 
//Write the size of the file
int filesize = getfilesize(filename);
write(fd, &filesize, sizeof(filesize));
totalsize += sizeof(int);
 
//Write the LENGTH of the NAME of the file
int filenamelen = strlen(filename);
write(fd, &filenamelen, sizeof(int));
totalsize += sizeof(int);
 
//Write the name of the file
write(fd, filename, strlen(filename));
totalsize += strlen(filename);
 
//Write the file contents
int fd_read = open(filename, O_RDONLY); //Open the file
char *buffer = (char *) malloc(filesize); //Create a buffer for its contents
read(fd_read, buffer, filesize); //Read the contents into the buffer
write(fd, buffer, filesize); //Write the buffer to the resource file
close(fd_read); //Close the file
free(buffer); //Free the buffer
totalsize += filesize; //Add the file size to the total number of bytes written
 
//Increment the currentloc and current file values
currentfile++;
currentloc += totalsize;
}


Is there something wrong with this code?

Syphod

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[SOLVED] Little bit of trouble with loading from memory.
« Reply #3 on: December 10, 2009, 12:00:29 am »
You could post also the reading function...

If ever you're using C++ take a look at this : http://www.sfml-dev.org/wiki/fr/tutoriels/formatdat.The explanations are in french, yet it is well commented in english so you can count on it ;)

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[SOLVED] Little bit of trouble with loading from memory.
« Reply #4 on: December 10, 2009, 12:11:22 am »
This is the reading method.

Code: [Select]

static char* GetBufferFromResource(char *resourcefilename, char *resourcename, int *filesize)
{
//Try to open the resource file in question
int fd = open(resourcefilename, O_RDONLY);
if (fd < 0)
{
perror("Error opening resource file");
exit(1);
}

//Make sure we're at the beginning of the file
lseek(fd, 0, SEEK_SET);

//Read the first INT, which will tell us how many files are in this resource
int numfiles;
read(fd, &numfiles, sizeof(int));
std::cout << "Number of files: " << numfiles << std::endl;

//Get the pointers to the stored files
int *filestart = (int *) malloc(sizeof(int) * numfiles);
read(fd, filestart, sizeof(int) * numfiles);
std::cout << "File starts at: " << *filestart << std::endl;

//Loop through the files, looking for the file in question
int filenamesize;
char *buffer;
int i;
for(i=0;i<numfiles;i++)
{
char *filename;
//Seek to the location
lseek(fd, filestart[i], SEEK_SET);
//Get the filesize value
read(fd, filesize, sizeof(int));
//Get the size of the filename string
read(fd, &filenamesize, sizeof(int));
//Size the buffer and read the filename
filename = (char *) malloc(filenamesize + 1);
read(fd, filename, filenamesize);
//Remember to terminate the string properly!
filename[filenamesize] = '\0';
//Compare to the string we're looking for
std::cout << "Comparing " << resourcename << " to " << filename << std::endl;
if (strcmp(filename, resourcename) == 0)
{
//Get the contents of the file
std::cout << resourcename << " is equal to " << filename << std::endl;
buffer = (char *) malloc(*filesize);
read(fd, buffer, *filesize);
free(filename);
break;
}
//Free the filename buffer
free(filename);
}

//Release memory
free(filestart);

//Close the resource file!
close(fd);

//Did we find the file within the resource that we were looking for?
if (buffer == NULL)
{
printf("Unable to find '%s' in the resource file!\n", resourcename);
exit(1);
}

//Return the buffer
return buffer;
}

Sicarius43696

  • Newbie
  • *
  • Posts: 16
    • View Profile
[SOLVED] Little bit of trouble with loading from memory.
« Reply #5 on: December 10, 2009, 02:44:35 am »
I used that tutorial and it worked. Thanks!