typedef struct {
const char* name;
int display_lists;
struct /*dlist[]*/ {
char* display_data;
int vtx_calls;
struct /*vtx[]*/ {
uint8 start;
uint16 size;
uint8 half;
uint8 bank;
uint32 offset;
int vertices;
char* vertex_data;
struct {
int x;
int y;
int z;
int u;
int v;
unsigned int r;
unsigned int g;
unsigned int b;
unsigned int a;
} vertex[];
} vtx[];
}dlist[];
} zobj;
The program scans the file for a pattern of bytes and, once found, loads zobj object.dlist[].display_data one byte at a time until another pattern is found. All goes well until I reach
fread(&object->dlist[j].display_data[k], 1, 1, z_object);where I get a segmentation fault. I suspect I need to allocate memory to each display_data in the array but I'm unsure how to do this. Do I need to create a loop and malloc() each one, then free() each one at the end of the program? I may have simply gone about the whole thing wrong and over-complicated it. Here's the function giving me trouble.
int load_zobj(const char* filename, zobj* object)
{
FILE* z_object;
z_object = fopen(filename, "r");
object->name = filename;
long size = 100;
fseek(z_object, 0, SEEK_END);
size = ftell(z_object);
rewind(z_object);
if(!z_object)
{
fprintf(stderr, "Could not open file.\n");
}
/* Parse for display lists. */
unsigned int* buffer = malloc(sizeof(unsigned int));
object->dlist->display_data = malloc(size); /* This did not appear to work */
int i = 0;
int j = 0;
int k = 0;
printf("Loading structure.\n");
while(i < size)
{
fseek(z_object, i, SEEK_SET);
fread(buffer, 8, 1, z_object);
*buffer = ((*buffer & 0xFF000000) >> 24|(*buffer & 0x00FF0000) >> 8|(*buffer & 0x0000FF00) << 8|(*buffer & 0x000000FF) << 24); /* Byte swap from Big Endian */
printf("i = %x, and buffer = %08X\n", i, *buffer);
if(*buffer == 0xE7000000)
{
k = 0;
while(*buffer != 0xDF000000)
{
printf("i = %x, and buffer = %08X\n", i, *buffer);
i++;
fseek(z_object, i, SEEK_SET);
fread(buffer, 8, 1, z_object);
// printf("%i\n", object->dlist[j].display_data[k]);
fread(&object->dlist[j].display_data[k], 1, 1, z_object);
*buffer = ((*buffer & 0xFF000000) >> 24|(*buffer & 0x00FF0000) >> 8|(*buffer & 0x0000FF00) << 8|(*buffer & 0x000000FF) << 24);
k++;
}
printf("done\n");
//i += (k + 8);
j++;
}
i++;
}
printf("Parsing data.\n");
object->display_lists = j;
/* Get vtx structure from display_data. */
i = 0;
while(i < object->display_lists)
{
j = 0;
while(j < sizeof(object->dlist[i].display_data))
{
k = 0;
if((object->dlist[i].display_data[j] == 0x01) && (object->dlist[i].display_data[j+4] == 0x06)) /* Check for vtx command and ram bank 06 request. */
{
/* Copy data from display list to struct. */
object->dlist[i].vtx[k].start = object->dlist[i].display_data[j];
memcpy(&object->dlist[i].vtx[k].size, &object->dlist[i].display_data[j+1], 2);
object->dlist[i].vtx[k].half = object->dlist[i].display_data[j+3];
object->dlist[i].vtx[k].bank = object->dlist[i].display_data[j+4];
memcpy(&object->dlist[i].vtx[k].offset, &object->dlist[i].display_data[j+5], 3);
k++;
}
object->dlist[i].vtx_calls = k;
}
i++;
}
printf("Loading vertex data.\n");
/* load vertex data */
i = 0;
while(i < object->display_lists)
{
j = 0;
while(j < object->dlist[i].vtx_calls)
{
/* Load vertex_data. */
fseek(z_object, object->dlist[i].vtx[j].offset, SEEK_SET);
fread(object->dlist[i].vtx[j].vertex_data, object->dlist[i].vtx[j].size, 1, z_object);
/* Load vertex[]. */
k = 0;
while(k < (object->dlist[i].vtx[j].half/2)) /* Indicates the number of vertices. */
{
/* Copy vertex data from offset (k), 16 bytes. */
memcpy(&object->dlist[i].vtx[j].vertex[k].x, &object->dlist[i].vtx[j].vertex_data + k, 2);
memcpy(&object->dlist[i].vtx[j].vertex[k].y, &object->dlist[i].vtx[j].vertex_data + k + 2, 2);
memcpy(&object->dlist[i].vtx[j].vertex[k].z, &object->dlist[i].vtx[j].vertex_data + k + 4, 2);
memcpy(&object->dlist[i].vtx[j].vertex[k].u, &object->dlist[i].vtx[j].vertex_data + k + 8, 2);
memcpy(&object->dlist[i].vtx[j].vertex[k].v, &object->dlist[i].vtx[j].vertex_data + k + 10, 2);
memcpy(&object->dlist[i].vtx[j].vertex[k].r, &object->dlist[i].vtx[j].vertex_data + k + 12, 1);
memcpy(&object->dlist[i].vtx[j].vertex[k].g, &object->dlist[i].vtx[j].vertex_data + k + 13, 1);
memcpy(&object->dlist[i].vtx[j].vertex[k].b, &object->dlist[i].vtx[j].vertex_data + k + 14, 1);
memcpy(&object->dlist[i].vtx[j].vertex[k].a, &object->dlist[i].vtx[j].vertex_data + k + 15, 1);
k = k + 16;
}
}
i++;
}
printf("Finished.\n");
free(object->dlist->display_data = malloc(size));
free(buffer);
fclose(z_object);
return(0);
}

New Topic/Question
Reply


MultiQuote




|