Serialization for wii April 04, 2009 07:44PM | Registered: 16 years ago Posts: 74 |
Re: Serialization for wii April 05, 2009 10:40AM | Registered: 15 years ago Posts: 25 |
Re: Serialization for wii April 05, 2009 01:47PM | Registered: 16 years ago Posts: 265 |
Re: Serialization for wii April 05, 2009 07:40PM | Registered: 16 years ago Posts: 74 |
Re: Serialization for wii April 05, 2009 10:37PM | Registered: 15 years ago Posts: 25 |
typedef struct { tileobject levelcontents[32]; // Defines an array of pre-rendered graphical tiles to build the level with u8 contentgrid[200][40]; // Defines a 200x40 grid of tile values u8 spawnpointx, spawnpointy; // Defines where the player starts u8 endpointx, endpointy; // Defines where the player ends } levellayout;
levellayout *level1; // The pointer for our levellayout struct void * mem; // The pointer to get from malloc if (( mem = malloc( sizeof(levellayout) ) ) == NULL) { // do the malloc - supposed to be whatever contiguous memory allocation function your OS uses printf("ERROR ALLOCATING mybuffer\n"); exit; } level1 = (levellayout *) mem; // cast the pointer {/* ... code to set the various struct values ... */} int bytesize = sizeof(levellayout); // get the size in bytes of the struct byte *level1bytes = (byte*) mem; // create a byte pointer to the malloc-ed memory FILE *file; file = fopen ("level1.bin","wt"); // open the file for writing if (file != NULL) { for (int i = 0; i < bytesize; i++) { // step through the array and write the bytes. fprint("%c",level1bytes); } fclose (file); }
void * mem; // The pointer to get from memalign mem = memalign(32, sizeof(levellayout)); byte * level1bytes = (byte *) mem; {/* ... Code to read the bytes from the file to the level1bytes pointer; probably would use a buffer over a for loop similar to before, and too lazy to code myself right now! >:3 */ } levellayout *level1 = (levellayout *) mem; ... // use the level1 data in memory as necessary
Re: Serialization for wii April 06, 2009 07:23AM | Registered: 16 years ago Posts: 265 |
Re: Serialization for wii April 06, 2009 12:00PM | Registered: 15 years ago Posts: 25 |
Is that a problem? I'd have figured you just wrap what you write to your file around a simple translation function to write according to whatever endian mode you want to run in (which I think is Big on the Wii by default, so yes, you would need to swap the order of the bits in each byte from most architectures, but that's not terribly complex to do).Quote
henke37
Oh, and it still doesn't do anything about endianness.
My bad!Quote
Use fwrite for such jobs.
True, but all you'd need to add to fix that would be a signature and a few checksum tests to make sure the file hasn't been tampered with. If players muck about with the output they get from a level designer, failure to load is quite reasonable behaviour.Quote
But it's trusting the player to not load faulty data.
It's a matter of good memory management, really. Don't write pointers in your files unless you know they're pointing in run-time to where they should be, but if you do then feel free!Quote
And it does not work very well if you have pointers in the data.
If you wanted to use C++, you could make all this redundant anyway by providing a Serializable base class and having the necessary objects implement their own serialization. C doesn't have that luxury.Quote
And if you are using c++, you are likely using some of the standard templates.
Re: Serialization for wii April 06, 2009 02:23PM | Registered: 16 years ago Posts: 265 |
Re: Serialization for wii April 06, 2009 06:36PM | Registered: 16 years ago Posts: 74 |