libretro-wolfenstein3d/id_pm.c

163 lines
4.1 KiB
C
Raw Permalink Normal View History

#include "wl_def.h"
2015-03-27 13:20:22 +00:00
#include <retro_endian.h>
int ChunksInFile;
int PMSpriteStart;
int PMSoundStart;
bool PMSoundInfoPagePadded = false;
2015-03-26 11:02:02 +00:00
/* holds the whole VSWAP */
uint32_t *PMPageData;
size_t PMPageDataSize;
2015-03-26 11:02:02 +00:00
/*
* ChunksInFile+1 pointers to page starts.
* The last pointer points one byte after the last page.
*/
uint8_t **PMPages;
2015-03-26 11:02:02 +00:00
void PM_Startup(void)
{
2015-03-27 13:20:22 +00:00
int i, j, k;
2015-03-26 11:02:02 +00:00
long fileSize, pageDataSize;
FILE *file;
uint32_t *pageOffsets;
uint32_t dataStart;
word *pageLengths;
uint8_t *ptr;
int alignPadding = 0;
char fname[13] = "vswap.";
strcat(fname,extension);
file = fopen(fname,"rb");
if(!file)
CA_CannotOpen(fname);
ChunksInFile = 0;
fread(&ChunksInFile, sizeof(word), 1, file);
2015-03-27 13:20:22 +00:00
ChunksInFile = Retro_SwapLES32(ChunksInFile);
2015-03-26 11:02:02 +00:00
PMSpriteStart = 0;
fread(&PMSpriteStart, sizeof(word), 1, file);
2015-03-27 13:20:22 +00:00
PMSpriteStart = Retro_SwapLES32(PMSpriteStart);
2015-03-26 11:02:02 +00:00
PMSoundStart = 0;
fread(&PMSoundStart, sizeof(word), 1, file);
2015-03-27 13:20:22 +00:00
PMSoundStart = Retro_SwapLES32(PMSoundStart);
2015-03-26 11:02:02 +00:00
pageOffsets = (uint32_t *) malloc((ChunksInFile + 1) * sizeof(int32_t));
CHECKMALLOCRESULT(pageOffsets);
fread(pageOffsets, sizeof(uint32_t), ChunksInFile, file);
2015-03-27 13:20:22 +00:00
for (k = 0; k < ChunksInFile; k++)
{
pageOffsets[k] = (uint32_t)Retro_SwapLES32(pageOffsets[k]);
}
2015-03-26 11:02:02 +00:00
pageLengths = (word *) malloc(ChunksInFile * sizeof(word));
CHECKMALLOCRESULT(pageLengths);
fread(pageLengths, sizeof(word), ChunksInFile, file);
2015-03-27 13:20:22 +00:00
for(j = 0; j < ChunksInFile; j++)
{
pageLengths[j] = (word)Retro_SwapLES16(pageLengths[j]);
}
2015-03-26 11:02:02 +00:00
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
pageDataSize = fileSize - pageOffsets[0];
if(pageDataSize > (size_t) -1)
Quit("The page file \"%s\" is too large!", fname);
pageOffsets[ChunksInFile] = fileSize;
dataStart = pageOffsets[0];
/* Check that all pageOffsets are valid */
for(i = 0; i < ChunksInFile; i++)
{
if(!pageOffsets[i])
continue; /* sparse page */
if(pageOffsets[i] < dataStart || pageOffsets[i] >= (size_t) fileSize)
Quit("Illegal page offset for page %i: %u (filesize: %u)",
i, pageOffsets[i], fileSize);
}
/* Calculate total amount of padding needed for sprites and sound info page */
for(i = PMSpriteStart; i < PMSoundStart; i++)
{
uint32_t offs;
if(!pageOffsets[i])
continue; /* sparse page */
offs = pageOffsets[i] - dataStart + alignPadding;
if(offs & 1)
alignPadding++;
}
if((pageOffsets[ChunksInFile - 1] - dataStart + alignPadding) & 1)
alignPadding++;
PMPageDataSize = (size_t) pageDataSize + alignPadding;
PMPageData = (uint32_t *) malloc(PMPageDataSize);
CHECKMALLOCRESULT(PMPageData);
PMPages = (uint8_t **) malloc((ChunksInFile + 1) * sizeof(uint8_t *));
CHECKMALLOCRESULT(PMPages);
/* Load pages and initialize PMPages pointers */
ptr = (uint8_t *) PMPageData;
for(i = 0; i < ChunksInFile; i++)
{
uint32_t size;
2015-04-28 05:00:17 +00:00
if(((i >= PMSpriteStart) && (i < PMSoundStart)) || (i == ChunksInFile - 1))
2015-03-26 11:02:02 +00:00
{
size_t offs = ptr - (uint8_t *) PMPageData;
/* pad with zeros to make it 2-byte aligned */
if(offs & 1)
{
*ptr++ = 0;
if(i == ChunksInFile - 1) PMSoundInfoPagePadded = true;
}
}
PMPages[i] = ptr;
if(!pageOffsets[i])
continue; // sparse page
/* Use specified page length,
* when next page is sparse page.
* Otherwise, calculate size from
* the offset difference between this and the next page. */
if(!pageOffsets[i + 1]) size = pageLengths[i];
else size = pageOffsets[i + 1] - pageOffsets[i];
fseek(file, pageOffsets[i], SEEK_SET);
fread(ptr, 1, size, file);
ptr += size;
}
/* last page points after page buffer */
PMPages[ChunksInFile] = ptr;
free(pageLengths);
free(pageOffsets);
fclose(file);
}
2015-03-26 11:02:02 +00:00
void PM_Shutdown(void)
{
2015-03-26 11:02:02 +00:00
free(PMPages);
free(PMPageData);
}