0 Members and 1 Guest are viewing this topic.
void SaveGame(short type_of_game){ short i,x,y; const char const* filename = type_of_game ? "doasava" : "doasavb"; // size of file: short totalDataSize = sizeof(gameglobals) + 21 + (8 * MAX_LEVEL_SPRITES) + ((14 * NR_SPRITE_TYPE_DEFS) * 2) + (sizeof(FATSPRITE) * MAX_SPRITES) + (sizeof(SpriteStruct) * MAX_SPRITES) + (sizeof(SwitchStruct) * MAX_NR_SWITCHES) + (sizeof(GlassWall) * MAX_GLASS_WALLS) + (sizeof(ExplodingFalseWall) * MAX_NR_DOORS) + (sizeof(PushableWall) * MAX_NR_DOORS) + (sizeof(LockedDoor) * MAX_NR_DOORS) + (4 * TOTAL_NR_WEAPONS) + (4 * gg.nr_doors); HSym hsym = SymAddMain(SYMSTR (filename)); if (hsym.folder == 0) return; SYM_ENTRY *SymPtr = (DerefSym(hsym)); SymPtr->handle = HeapAlloc(totalDataSize); if (SymPtr->handle == H_NULL) return; unsigned char *VarPtr = HLock(SymPtr->handle); if (VarPtr == NULL) return; *(unsigned short*)VarPtr = (totalDataSize - 2); VarPtr+=2; // 2 *(unsigned short*)VarPtr = fc.cam_xpos; VarPtr+=2; // 4 *(unsigned short*)VarPtr = fc.cam_xpos; VarPtr+=2; // 6 *(unsigned short*)VarPtr = fc.cam_orientation; VarPtr+=2; // 8 *(short*)VarPtr = nr_item_sprites; VarPtr+=2; // 10 *(short*)VarPtr = nr_static_sprites; VarPtr +=2; // 12 *(short*)VarPtr = nr_dynamic_sprites; VarPtr +=2; // 14 memcpy(VarPtr,&gg,sizeof(gameglobals)); VarPtr += sizeof(gameglobals); for (i = 0; i <MAX_LEVEL_SPRITES; i++) { *(short*)VarPtr = item_sprites[i]; VarPtr+=2; *(short*)VarPtr = static_sprites[i]; VarPtr+=2; *(short*)VarPtr = dynamic_sprites[i]; VarPtr+=2; *(short*)VarPtr = sprite_checked[i]; VarPtr+=2; } for (y = 0; y < NR_SPRITE_TYPE_DEFS; y++) { for (x = 0; x < 7; x++) { *(short*)VarPtr = sprite_action_steps[y][x]; VarPtr+=2; *(short*)VarPtr = sprite_action_nr_sprites[y][x]; VarPtr+=2; } } for (i = 0; i < MAX_SPRITES; i++) { memcpy(VarPtr,&sprites[i],sizeof(FATSPRITE)); VarPtr+=sizeof(FATSPRITE); memcpy(VarPtr,&sprt_data[i],sizeof(SpriteStruct)); VarPtr+=sizeof(SpriteStruct); } for (i = 0; i < MAX_NR_SWITCHES; i++) { memcpy(VarPtr,&Switches[i],sizeof(SwitchStruct)); VarPtr+=sizeof(SwitchStruct); } for (i = 0; i < MAX_GLASS_WALLS; i++) { memcpy(VarPtr,&glass_walls[i],sizeof(GlassWall)); VarPtr+=sizeof(GlassWall); } for (i = 0; i < MAX_NR_DOORS; i++) { memcpy(VarPtr,&exploding_false_walls[i],sizeof(ExplodingFalseWall)); VarPtr+=sizeof(ExplodingFalseWall); } for (i = 0; i < MAX_NR_DOORS; i++) { memcpy(VarPtr,&push_walls[i],sizeof(PushableWall)); VarPtr+=sizeof(PushableWall); } for (i = 0; i < MAX_NR_DOORS; i++) { memcpy(VarPtr,&lockedDoors[i],sizeof(LockedDoor)); VarPtr+=sizeof(LockedDoor); } for (i = 0; i < TOTAL_NR_WEAPONS; i++) { *(short*)VarPtr = weapon_array[i]->have_weapon; VarPtr+=2; *(short*)VarPtr = weapon_array[i]->ammo; VarPtr+=2; } FATDOOR *doorsarray = FAT_GetDoorsArray(); for (i = 0; i < gg.nr_doors; i++) { *VarPtr = doorsarray[i].type; VarPtr++; *VarPtr = doorsarray[i].state; VarPtr++; *(unsigned short*)VarPtr = doorsarray[i].position; VarPtr+=2; } *VarPtr = 0; // 15 VarPtr++; *VarPtr = 'd'; // 16 VarPtr++; *VarPtr = 'o'; // 17 VarPtr++; *VarPtr = 'a'; // 18 VarPtr++; *VarPtr = 's'; // 19 VarPtr++; *VarPtr = 0; // 20 VarPtr++; *VarPtr = OTH_TAG; // 21 HeapUnlock(SymPtr->handle); }
short LoadGame(short type_of_game){ short i,x,y; const char const* filename = type_of_game ? "doasava" : "doasavb"; // open file returns the pointer to the actual data in the file, past the size bytes. unsigned char* VarPtr = OpenFile(filename); if (VarPtr == NULL) return 0; fc.cam_xpos = *(unsigned short*)VarPtr; VarPtr+=2; // 4 fc.cam_xpos = *(unsigned short*)VarPtr; VarPtr+=2; // 6 fc.cam_orientation = *(unsigned short*)VarPtr; VarPtr+=2; // 8 nr_item_sprites = *(short*)VarPtr; VarPtr+=2; // 10 nr_static_sprites = *(short*)VarPtr; VarPtr +=2; // 12 nr_dynamic_sprites = *(short*)VarPtr; VarPtr +=2; // 14 memcpy(&gg,VarPtr,sizeof(gameglobals)); VarPtr += sizeof(gameglobals); for (i = 0; i <MAX_LEVEL_SPRITES; i++) { item_sprites[i] = *(short*)VarPtr; VarPtr+=2; static_sprites[i] = *(short*)VarPtr; VarPtr+=2; dynamic_sprites[i] = *(short*)VarPtr; VarPtr+=2; sprite_checked[i] = *(short*)VarPtr; VarPtr+=2; } for (y = 0; y < NR_SPRITE_TYPE_DEFS; y++) { for (x = 0; x < 7; x++) { sprite_action_steps[y][x] = *(short*)VarPtr; VarPtr+=2; sprite_action_nr_sprites[y][x] = *(short*)VarPtr; VarPtr+=2; } } for (i = 0; i < MAX_SPRITES; i++) { memcpy(&sprites[i],VarPtr,sizeof(FATSPRITE)); VarPtr+=sizeof(FATSPRITE); memcpy(&sprt_data[i],VarPtr,sizeof(SpriteStruct)); VarPtr+=sizeof(SpriteStruct); } for (i = 0; i < MAX_NR_SWITCHES; i++) { memcpy(&Switches[i],VarPtr,sizeof(SwitchStruct)); VarPtr+=sizeof(SwitchStruct); } for (i = 0; i < MAX_GLASS_WALLS; i++) { memcpy(&glass_walls[i],VarPtr,sizeof(GlassWall)); VarPtr+=sizeof(GlassWall); } for (i = 0; i < MAX_NR_DOORS; i++) { memcpy(&exploding_false_walls[i],VarPtr,sizeof(ExplodingFalseWall)); VarPtr+=sizeof(ExplodingFalseWall); } for (i = 0; i < MAX_NR_DOORS; i++) { memcpy(&push_walls[i],VarPtr,sizeof(PushableWall)); VarPtr+=sizeof(PushableWall); } for (i = 0; i < MAX_NR_DOORS; i++) { memcpy(&lockedDoors[i],VarPtr,sizeof(LockedDoor)); VarPtr+=sizeof(LockedDoor); } for (i = 0; i < TOTAL_NR_WEAPONS; i++) { weapon_array[i]->have_weapon = *(short*)VarPtr; VarPtr+=2; weapon_array[i]->ammo = *(short*)VarPtr; VarPtr+=2; } FATDOOR *doorsarray = FAT_GetDoorsArray(); for (i = 0; i < gg.nr_doors; i++) { doorsarray[i].type = *VarPtr; VarPtr++; doorsarray[i].state= *VarPtr; VarPtr++; doorsarray[i].position = *(unsigned short*)VarPtr; VarPtr+=2; } CloseFile(filename); // the game engine may not have the correct number of sprites to work with, so // set it to what we loaded from the file. fc.nr_sprites = gg.number_total_sprites; // this part here has to be here, because the texconfig pointers for each sprite // may be pointing to some random place in archive memory, that may not correspond // to the textures anymore, so I just use the function I already have to // set the current texture frame for the current animation, to set the // pointers back to what they should be. for (i = 0; i < fc.nr_sprites; i++) { SetSpriteToCurrentAnimationFrame(&sprites[i],&sprt_data[i]); } return 1; }