Compare commits
5 Commits
20d5bd4ac7
...
9134fde7dc
Author | SHA1 | Date |
---|---|---|
|
9134fde7dc | |
|
51618cadc6 | |
|
789d1b2577 | |
|
e32fd0fc55 | |
|
5cfa0c0fc0 |
|
@ -25,6 +25,7 @@ set(GAME_LIBS
|
||||||
|
|
||||||
|
|
||||||
add_subdirectory(scenes)
|
add_subdirectory(scenes)
|
||||||
|
add_subdirectory(res)
|
||||||
|
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
main.c
|
main.c
|
||||||
|
|
7
main.c
7
main.c
|
@ -30,8 +30,11 @@ int main(void)
|
||||||
init_memory_pools();
|
init_memory_pools();
|
||||||
|
|
||||||
init_assets(&engine.assets);
|
init_assets(&engine.assets);
|
||||||
load_from_infofile("res/assets.info", &engine.assets);
|
//load_from_infofile("res/assets.info", &engine.assets);
|
||||||
init_player_creation("res/player_spr.info", &engine.assets);
|
//init_player_creation("res/player_spr.info", &engine.assets);
|
||||||
|
load_from_rres("res/myresources.rres", &engine.assets);
|
||||||
|
init_player_creation_rres("res/myresources.rres", "player_spr.info", &engine.assets);
|
||||||
|
init_item_creation(&engine.assets);
|
||||||
|
|
||||||
LevelScene_t sandbox_scene;
|
LevelScene_t sandbox_scene;
|
||||||
sandbox_scene.scene.engine = &engine;
|
sandbox_scene.scene.engine = &engine;
|
||||||
|
|
|
@ -3,3 +3,4 @@
|
||||||
!ldtk_repacker.py
|
!ldtk_repacker.py
|
||||||
!test_assets.info
|
!test_assets.info
|
||||||
!testLevels.lvldata
|
!testLevels.lvldata
|
||||||
|
!rres_packer.c
|
||||||
|
|
|
@ -0,0 +1,370 @@
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
#define RRES_IMPLEMENTATION
|
||||||
|
#include "rres.h" // Required to read rres data chunks
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// Load a continuous data buffer from rresResourceChunkData struct
|
||||||
|
static unsigned char *LoadDataBuffer(rresResourceChunkData data, unsigned int rawSize)
|
||||||
|
{
|
||||||
|
unsigned char *buffer = (unsigned char *)RRES_CALLOC((data.propCount + 1)*sizeof(unsigned int) + rawSize, 1);
|
||||||
|
|
||||||
|
memcpy(buffer, &data.propCount, sizeof(unsigned int));
|
||||||
|
for (int i = 0; i < data.propCount; i++) memcpy(buffer + (i + 1)*sizeof(unsigned int), &data.props[i], sizeof(unsigned int));
|
||||||
|
memcpy(buffer + (data.propCount + 1)*sizeof(unsigned int), data.raw, rawSize);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload data buffer
|
||||||
|
static void UnloadDataBuffer(unsigned char *buffer)
|
||||||
|
{
|
||||||
|
RRES_FREE(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void addTextFile(rresFileHeader* header, const char* filename, FILE* rresFile, rresDirEntry* entry)
|
||||||
|
{
|
||||||
|
rresResourceChunkInfo chunkInfo = { 0 }; // Chunk info
|
||||||
|
rresResourceChunkData chunkData = { 0 }; // Chunk data
|
||||||
|
//
|
||||||
|
unsigned char *buffer = NULL;
|
||||||
|
|
||||||
|
// ----- Text files
|
||||||
|
char *text = LoadFileText(filename);
|
||||||
|
if (text == NULL)
|
||||||
|
{
|
||||||
|
printf("Cannot pack text file %s\n", filename);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsigned int rawSize = strlen(text);
|
||||||
|
|
||||||
|
// Define chunk info: TEXT
|
||||||
|
chunkInfo.type[0] = 'T'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[1] = 'E'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[2] = 'X'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[3] = 'T'; // Resource chunk type (FourCC)
|
||||||
|
|
||||||
|
// Resource chunk identifier (generated from filename CRC32 hash)
|
||||||
|
chunkInfo.id = rresComputeCRC32((unsigned char*)filename, strlen(filename));
|
||||||
|
|
||||||
|
if (entry != NULL)
|
||||||
|
{
|
||||||
|
entry->id = chunkInfo.id;
|
||||||
|
entry->offset = ftell(rresFile);
|
||||||
|
entry->fileNameSize = strlen(filename);
|
||||||
|
strcpy(entry->fileName, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
chunkInfo.compType = RRES_COMP_NONE, // Data compression algorithm
|
||||||
|
chunkInfo.cipherType = RRES_CIPHER_NONE, // Data encription algorithm
|
||||||
|
chunkInfo.flags = 0, // Data flags (if required)
|
||||||
|
chunkInfo.baseSize = 5*sizeof(unsigned int) + rawSize; // Data base size (uncompressed/unencrypted)
|
||||||
|
chunkInfo.packedSize = chunkInfo.baseSize; // Data chunk size (compressed/encrypted + custom data appended)
|
||||||
|
chunkInfo.nextOffset = 0; // Next resource chunk global offset (if resource has multiple chunks)
|
||||||
|
chunkInfo.reserved = 0; // <reserved>
|
||||||
|
//
|
||||||
|
// Define chunk data: TEXT
|
||||||
|
chunkData.propCount = 4;
|
||||||
|
chunkData.props = (unsigned int *)RRES_CALLOC(chunkData.propCount, sizeof(unsigned int));
|
||||||
|
chunkData.props[0] = rawSize; // props[0]:size (bytes)
|
||||||
|
chunkData.props[1] = RRES_TEXT_ENCODING_UNDEFINED; // props[1]:rresTextEncoding
|
||||||
|
chunkData.props[2] = RRES_CODE_LANG_UNDEFINED; // props[2]:rresCodeLang
|
||||||
|
chunkData.props[3] = 0x0409; // props[3]:cultureCode: en-US: English - United States
|
||||||
|
chunkData.raw = text;
|
||||||
|
// Get a continuous data buffer from chunkData
|
||||||
|
buffer = LoadDataBuffer(chunkData, rawSize);
|
||||||
|
|
||||||
|
// Compute data chunk CRC32 (propCount + props[] + data)
|
||||||
|
chunkInfo.crc32 = rresComputeCRC32(buffer, chunkInfo.packedSize);
|
||||||
|
|
||||||
|
// Write resource chunk into rres file
|
||||||
|
fwrite(&chunkInfo, sizeof(rresResourceChunkInfo), 1, rresFile);
|
||||||
|
fwrite(buffer, 1, chunkInfo.packedSize, rresFile);
|
||||||
|
|
||||||
|
// Free required memory
|
||||||
|
RRES_FREE(chunkData.props);
|
||||||
|
UnloadDataBuffer(buffer);
|
||||||
|
UnloadFileText(text);
|
||||||
|
|
||||||
|
header->chunkCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool addRawData(rresFileHeader* header, const char* filename, FILE* rresFile, const char* ext, rresDirEntry* entry)
|
||||||
|
{
|
||||||
|
rresResourceChunkInfo chunkInfo = { 0 }; // Chunk info
|
||||||
|
rresResourceChunkData chunkData = { 0 }; // Chunk data
|
||||||
|
unsigned char *buffer = NULL;
|
||||||
|
|
||||||
|
// Load file data
|
||||||
|
unsigned int size = 0;
|
||||||
|
unsigned char* raw = LoadFileData(filename, &size);
|
||||||
|
if (raw == NULL)
|
||||||
|
{
|
||||||
|
printf("Cannot pack raw file %s\n", filename);
|
||||||
|
UnloadDataBuffer(buffer);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define chunk info: RAWD
|
||||||
|
chunkInfo.type[0] = 'R'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[1] = 'A'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[2] = 'W'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[3] = 'D'; // Resource chunk type (FourCC)
|
||||||
|
|
||||||
|
// Resource chunk identifier (generated from filename CRC32 hash)
|
||||||
|
chunkInfo.id = rresComputeCRC32((unsigned char*)filename, strlen(filename));
|
||||||
|
|
||||||
|
if (entry != NULL)
|
||||||
|
{
|
||||||
|
entry->id = chunkInfo.id;
|
||||||
|
entry->offset = ftell(rresFile);
|
||||||
|
entry->fileNameSize = strlen(filename);
|
||||||
|
strcpy(entry->fileName, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
chunkInfo.compType = RRES_COMP_NONE, // Data compression algorithm
|
||||||
|
chunkInfo.cipherType = RRES_CIPHER_NONE, // Data encription algorithm
|
||||||
|
chunkInfo.flags = 0, // Data flags (if required)
|
||||||
|
chunkInfo.baseSize = 5*sizeof(unsigned int) + size; // Data base size (uncompressed/unencrypted)
|
||||||
|
chunkInfo.packedSize = chunkInfo.baseSize; // Data chunk size (compressed/encrypted + custom data appended)
|
||||||
|
chunkInfo.nextOffset = 0, // Next resource chunk global offset (if resource has multiple chunks)
|
||||||
|
chunkInfo.reserved = 0, // <reserved>
|
||||||
|
|
||||||
|
// Define chunk data: IMGE
|
||||||
|
chunkData.propCount = 4;
|
||||||
|
chunkData.props = (unsigned int *)RRES_CALLOC(chunkData.propCount, sizeof(unsigned int));
|
||||||
|
chunkData.props[0] = size; // props[0]:width
|
||||||
|
memcpy(chunkData.props + 1, ext, 4);
|
||||||
|
chunkData.props[2] = 0x0; // props[2]:rresPixelFormat
|
||||||
|
chunkData.props[3] = 0x0; // props[2]:rresPixelFormat
|
||||||
|
chunkData.raw = raw;
|
||||||
|
|
||||||
|
|
||||||
|
// Get a continuous data buffer from chunkData
|
||||||
|
buffer = LoadDataBuffer(chunkData, size);
|
||||||
|
|
||||||
|
// Compute data chunk CRC32 (propCount + props[] + data)
|
||||||
|
chunkInfo.crc32 = rresComputeCRC32(buffer, chunkInfo.packedSize);
|
||||||
|
|
||||||
|
// Write resource chunk into rres file
|
||||||
|
fwrite(&chunkInfo, sizeof(rresResourceChunkInfo), 1, rresFile);
|
||||||
|
fwrite(buffer, 1, chunkInfo.packedSize, rresFile);
|
||||||
|
|
||||||
|
// Free required memory
|
||||||
|
RRES_FREE(chunkData.props);
|
||||||
|
UnloadDataBuffer(buffer);
|
||||||
|
UnloadFileData(raw);
|
||||||
|
|
||||||
|
header->chunkCount++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void addCentralDir(rresFileHeader* header, const rresCentralDir* central, FILE* rresFile)
|
||||||
|
{
|
||||||
|
rresResourceChunkInfo chunkInfo = { 0 }; // Chunk info
|
||||||
|
unsigned char *buffer = NULL;
|
||||||
|
|
||||||
|
if (header->cdOffset != 0)
|
||||||
|
{
|
||||||
|
puts("There's is already a Central Dir");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
header->cdOffset = ftell(rresFile) - sizeof(rresFileHeader);
|
||||||
|
|
||||||
|
unsigned int size = 0;
|
||||||
|
// TODO: Compute correctly the size
|
||||||
|
for (unsigned int i = 0; i < central->count; ++i)
|
||||||
|
{
|
||||||
|
size += 4 * sizeof(unsigned int) + central->entries[i].fileNameSize;
|
||||||
|
}
|
||||||
|
printf("Cdir size: %d\n", size);
|
||||||
|
|
||||||
|
unsigned int props[2] = {1, central->count};
|
||||||
|
|
||||||
|
// Define chunk info: CDIR
|
||||||
|
chunkInfo.type[0] = 'C'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[1] = 'D'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[2] = 'I'; // Resource chunk type (FourCC)
|
||||||
|
chunkInfo.type[3] = 'R'; // Resource chunk type (FourCC)
|
||||||
|
|
||||||
|
|
||||||
|
chunkInfo.id = 0xCD010203;
|
||||||
|
chunkInfo.compType = RRES_COMP_NONE, // Data compression algorithm
|
||||||
|
chunkInfo.cipherType = RRES_CIPHER_NONE, // Data encription algorithm
|
||||||
|
chunkInfo.flags = 0, // Data flags (if required)
|
||||||
|
chunkInfo.baseSize = sizeof(props) + size; // Data base size (uncompressed/unencrypted)
|
||||||
|
chunkInfo.packedSize = chunkInfo.baseSize; // Data chunk size (compressed/encrypted + custom data appended)
|
||||||
|
chunkInfo.nextOffset = 0; // Next resource chunk global offset (if resource has multiple chunks)
|
||||||
|
chunkInfo.reserved = 0; // <reserved>
|
||||||
|
|
||||||
|
buffer = (unsigned char *)RRES_CALLOC(sizeof(props) + size, 1);
|
||||||
|
memcpy(buffer, props, sizeof(props));
|
||||||
|
|
||||||
|
unsigned char* data_ptr = buffer + sizeof(props);
|
||||||
|
for (unsigned int i = 0; i < central->count; ++i)
|
||||||
|
{
|
||||||
|
printf("Recording CDIR for %s\n", central->entries[i].fileName);
|
||||||
|
unsigned int n_write = 4 * sizeof(unsigned int) + central->entries[i].fileNameSize;
|
||||||
|
memcpy(data_ptr, central->entries + i, n_write);
|
||||||
|
data_ptr += n_write;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute data chunk CRC32 (propCount + props[] + data)
|
||||||
|
chunkInfo.crc32 = rresComputeCRC32(buffer, chunkInfo.packedSize);
|
||||||
|
|
||||||
|
// Write resource chunk into rres file
|
||||||
|
fwrite(&chunkInfo, sizeof(rresResourceChunkInfo), 1, rresFile);
|
||||||
|
fwrite(buffer, 1, chunkInfo.packedSize, rresFile);
|
||||||
|
|
||||||
|
// Free required memory
|
||||||
|
UnloadDataBuffer(buffer);
|
||||||
|
header->chunkCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef enum AssetInfoType
|
||||||
|
{
|
||||||
|
TEXTURE_INFO,
|
||||||
|
SPRITE_INFO,
|
||||||
|
LEVELPACK_INFO,
|
||||||
|
INVALID_INFO
|
||||||
|
}AssetInfoType_t;
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
FILE *rresFile = fopen("myresources.rres", "wb");
|
||||||
|
|
||||||
|
// Define rres file header
|
||||||
|
// NOTE: We are loading 4 files that generate 5 resource chunks to save in rres
|
||||||
|
rresFileHeader header = {
|
||||||
|
.id[0] = 'r', // File identifier: rres
|
||||||
|
.id[1] = 'r', // File identifier: rres
|
||||||
|
.id[2] = 'e', // File identifier: rres
|
||||||
|
.id[3] = 's', // File identifier: rres
|
||||||
|
.version = 100, // File version: 100 for version 1.0
|
||||||
|
.chunkCount = 0, // Number of resource chunks in the file (MAX: 65535)
|
||||||
|
.cdOffset = 0, // Central Directory offset in file (0 if not available)
|
||||||
|
.reserved = 0 // <reserved>
|
||||||
|
};
|
||||||
|
|
||||||
|
#define STARTING_CHUNKS 8
|
||||||
|
// Central Directory
|
||||||
|
rresCentralDir central = {
|
||||||
|
.count = 0,
|
||||||
|
.entries = NULL
|
||||||
|
};
|
||||||
|
central.entries = RRES_CALLOC(sizeof(rresDirEntry), STARTING_CHUNKS);
|
||||||
|
fseek(rresFile, sizeof(rresFileHeader), SEEK_SET);
|
||||||
|
|
||||||
|
|
||||||
|
addTextFile(&header, "assets.info", rresFile, central.entries);
|
||||||
|
addTextFile(&header, "player_spr.info", rresFile, central.entries + 1);
|
||||||
|
central.count = 2;
|
||||||
|
uint16_t max_chunks = STARTING_CHUNKS;
|
||||||
|
|
||||||
|
{
|
||||||
|
FILE* in_file = fopen("assets.info", "r");
|
||||||
|
if (in_file == NULL)
|
||||||
|
{
|
||||||
|
puts("assets.info must be present");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buffer[256];
|
||||||
|
char* tmp;
|
||||||
|
AssetInfoType_t info_type = INVALID_INFO;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
tmp = fgets(buffer, 256, in_file);
|
||||||
|
if (tmp == NULL) break;
|
||||||
|
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||||
|
|
||||||
|
if (central.count == max_chunks)
|
||||||
|
{
|
||||||
|
void* new_ptr = realloc(central.entries, (max_chunks*2) * sizeof(rresDirEntry));
|
||||||
|
if (new_ptr == NULL)
|
||||||
|
{
|
||||||
|
puts("Cannot realloc central entries");
|
||||||
|
fclose(in_file);
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
central.entries = new_ptr;
|
||||||
|
max_chunks *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tmp[0] == '-')
|
||||||
|
{
|
||||||
|
tmp++;
|
||||||
|
if (strcmp(tmp, "Texture") == 0)
|
||||||
|
{
|
||||||
|
info_type = TEXTURE_INFO;
|
||||||
|
}
|
||||||
|
else if (strcmp(tmp, "LevelPack") == 0)
|
||||||
|
{
|
||||||
|
info_type = LEVELPACK_INFO;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
info_type = INVALID_INFO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char* name = strtok(buffer, ":");
|
||||||
|
char* info_str = strtok(NULL, ":");
|
||||||
|
if (name == NULL || info_str == NULL) continue;
|
||||||
|
|
||||||
|
while(*name == ' ' || *name == '\t') name++;
|
||||||
|
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||||
|
switch(info_type)
|
||||||
|
{
|
||||||
|
case TEXTURE_INFO:
|
||||||
|
{
|
||||||
|
// ---- SpriteSheets
|
||||||
|
if (
|
||||||
|
addRawData(
|
||||||
|
&header, info_str,
|
||||||
|
rresFile, GetFileExtension(info_str),
|
||||||
|
central.entries + central.count
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
central.count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LEVELPACK_INFO:
|
||||||
|
{
|
||||||
|
// ---- Compressed Level Data
|
||||||
|
if (
|
||||||
|
addRawData(
|
||||||
|
&header, info_str,
|
||||||
|
rresFile, ".lpk",
|
||||||
|
central.entries + central.count
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
central.count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(in_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
addCentralDir(&header, ¢ral, rresFile);
|
||||||
|
|
||||||
|
// Write rres file header
|
||||||
|
fseek(rresFile, 0, SEEK_SET);
|
||||||
|
fwrite(&header, sizeof(rresFileHeader), 1, rresFile);
|
||||||
|
|
||||||
|
end:
|
||||||
|
fclose(rresFile);
|
||||||
|
RRES_FREE(central.entries);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -40,7 +40,18 @@ bool load_from_rres(const char* file, Assets_t* assets)
|
||||||
rres_file.dir = rresLoadCentralDirectory(file);
|
rres_file.dir = rresLoadCentralDirectory(file);
|
||||||
rres_file.fname = file;
|
rres_file.fname = file;
|
||||||
|
|
||||||
rresResourceChunk chunk = rresLoadResourceChunk(file, rresGetResourceId(rres_file.dir, "assets.info")); // Hardcoded
|
if (rres_file.dir.count == 0)
|
||||||
|
{
|
||||||
|
puts("Empty central directory");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int res_id = rresGetResourceId(rres_file.dir, "assets.info");
|
||||||
|
rresResourceChunk chunk = rresLoadResourceChunk(file, res_id); // Hardcoded
|
||||||
|
bool okay = false;
|
||||||
|
|
||||||
|
if (chunk.info.baseSize > 0)
|
||||||
|
{
|
||||||
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||||
|
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
|
@ -137,10 +148,11 @@ bool load_from_rres(const char* file, Assets_t* assets)
|
||||||
line_num++;
|
line_num++;
|
||||||
}
|
}
|
||||||
fclose(in_file);
|
fclose(in_file);
|
||||||
|
okay = true;
|
||||||
|
}
|
||||||
rresUnloadResourceChunk(chunk);
|
rresUnloadResourceChunk(chunk);
|
||||||
rresUnloadCentralDirectory(rres_file.dir);
|
rresUnloadCentralDirectory(rres_file.dir);
|
||||||
return true;
|
return okay;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_from_infofile(const char* file, Assets_t* assets)
|
bool load_from_infofile(const char* file, Assets_t* assets)
|
||||||
|
|
|
@ -87,19 +87,25 @@ Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* file
|
||||||
uint8_t tex_idx = n_loaded[0];
|
uint8_t tex_idx = n_loaded[0];
|
||||||
assert(tex_idx < MAX_TEXTURES);
|
assert(tex_idx < MAX_TEXTURES);
|
||||||
|
|
||||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, rresGetResourceId(rres_file->dir, filename));
|
int res_id = rresGetResourceId(rres_file->dir, filename);
|
||||||
|
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, res_id);
|
||||||
|
|
||||||
|
Texture2D* out_tex = NULL;
|
||||||
|
if (chunk.info.baseSize > 0)
|
||||||
|
{
|
||||||
//Expect RAW type of png extension
|
//Expect RAW type of png extension
|
||||||
Image image = LoadImageFromMemory(GetFileExtension(filename), chunk.data.raw, chunk.info.baseSize);
|
Image image = LoadImageFromMemory(GetFileExtension(filename), chunk.data.raw, chunk.info.baseSize);
|
||||||
Texture2D tex = LoadTextureFromImage(image);
|
Texture2D tex = LoadTextureFromImage(image);
|
||||||
UnloadImage(image);
|
UnloadImage(image);
|
||||||
rresUnloadResourceChunk(chunk);
|
|
||||||
|
|
||||||
textures[tex_idx].texture = tex;
|
textures[tex_idx].texture = tex;
|
||||||
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
|
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
|
||||||
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
|
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
|
||||||
n_loaded[0]++;
|
n_loaded[0]++;
|
||||||
return &textures[tex_idx].texture;
|
out_tex = &textures[tex_idx].texture;
|
||||||
|
}
|
||||||
|
rresUnloadResourceChunk(chunk);
|
||||||
|
return out_tex;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
|
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
|
||||||
|
@ -173,6 +179,7 @@ static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, FILE*
|
||||||
ZSTD_inBuffer input = { level_decompressor.in_buffer, read, 0 };
|
ZSTD_inBuffer input = { level_decompressor.in_buffer, read, 0 };
|
||||||
ZSTD_outBuffer output = { level_decompressor.out_buffer, 4, 0 };
|
ZSTD_outBuffer output = { level_decompressor.out_buffer, 4, 0 };
|
||||||
|
|
||||||
|
ZSTD_DCtx_reset(level_decompressor.ctx, ZSTD_reset_session_only);
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (input.pos == input.size)
|
if (input.pos == input.size)
|
||||||
|
@ -312,14 +319,21 @@ LevelPack_t* uncompress_level_pack(Assets_t* assets, const char* name, const cha
|
||||||
|
|
||||||
LevelPack_t* add_level_pack_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file)
|
LevelPack_t* add_level_pack_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file)
|
||||||
{
|
{
|
||||||
|
int res_id = rresGetResourceId(rres_file->dir, filename);
|
||||||
|
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, res_id);
|
||||||
|
|
||||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, rresGetResourceId(rres_file->dir, filename));
|
LevelPack_t* pack = NULL;
|
||||||
|
if (chunk.info.baseSize > 0 && strcmp(".lpk", (const char*)(chunk.data.props + 1)) == 0)
|
||||||
|
{
|
||||||
FILE* f_in = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
FILE* f_in = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||||
|
pack = add_level_pack_zst(assets, name, f_in);
|
||||||
LevelPack_t* pack = add_level_pack_zst(assets, name, f_in);
|
|
||||||
fclose(f_in);
|
fclose(f_in);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Cannot load level pack for %s\n", name);
|
||||||
|
}
|
||||||
rresUnloadResourceChunk(chunk);
|
rresUnloadResourceChunk(chunk);
|
||||||
|
|
||||||
return pack;
|
return pack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -183,11 +183,22 @@ bool init_player_creation_rres(const char* rres_fname, const char* file, Assets_
|
||||||
rres_file.dir = rresLoadCentralDirectory(rres_fname);
|
rres_file.dir = rresLoadCentralDirectory(rres_fname);
|
||||||
rres_file.fname = rres_fname;
|
rres_file.fname = rres_fname;
|
||||||
|
|
||||||
rresResourceChunk chunk = rresLoadResourceChunk(rres_file.fname, rresGetResourceId(rres_file.dir, file)); // Hardcoded
|
if (rres_file.dir.count == 0)
|
||||||
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
{
|
||||||
|
puts("Empty central directory");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool okay = init_player_file(in_file, assets);
|
int res_id = rresGetResourceId(rres_file.dir, file);
|
||||||
|
rresResourceChunk chunk = rresLoadResourceChunk(rres_file.fname, res_id);
|
||||||
|
|
||||||
|
bool okay = false;
|
||||||
|
if (chunk.info.id == res_id)
|
||||||
|
{
|
||||||
|
FILE* in_file = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
|
||||||
|
okay = init_player_file(in_file, assets);
|
||||||
fclose(in_file);
|
fclose(in_file);
|
||||||
|
}
|
||||||
|
|
||||||
rresUnloadResourceChunk(chunk);
|
rresUnloadResourceChunk(chunk);
|
||||||
rresUnloadCentralDirectory(rres_file.dir);
|
rresUnloadCentralDirectory(rres_file.dir);
|
||||||
|
|
Loading…
Reference in New Issue