[WIP] Fixed loading of base texture pack

This commit is contained in:
KiritoDv 2021-05-07 14:45:00 +00:00
parent 356561a5b4
commit d86c5246cd
2 changed files with 24 additions and 43 deletions

View File

@ -80,7 +80,7 @@ void Moon_LoadAddon(string path){
string rawname = texName.substr(0, texName.find_last_of("."));
TextureFileEntry *entry = new TextureFileEntry();
file.read_texture(name, &entry);
bit->textures.insert(pair<string, TextureFileEntry*>(texName, entry));
bit->textures.insert(pair<string, TextureFileEntry*>(rawname, entry));
}
}
if(!name.rfind(textsPath, 0)){
@ -134,7 +134,7 @@ void Moon_LoadAddonTextures(BitModule* module){
texIt = textureMap.find(entry->first);
if(texIt != textureMap.end()){
cout << "Erasing: " << entry->first << endl;
// cout << "Reloading: " << entry->first << endl;
textureMap.erase(texIt);
}
@ -146,13 +146,12 @@ void Moon_LoadAddonTextures(BitModule* module){
}
void Moon_SaveTexture(TextureData* data, string tex){
cout << "Saving: " << tex << endl;
textureMap.insert(pair<string, TextureData*>(tex, data));
}
void Moon_LoadBaseTexture(char* data, long size, string texture){
if(baseGameTextures.find(texture) == baseGameTextures.end()){
baseGameTextures.insert(pair<string, TextureFileEntry*>(texture.substr(4), new TextureFileEntry({.size = size, .data = data})));
baseGameTextures.insert(pair<string, TextureFileEntry*>(texture, new TextureFileEntry({.size = size, .data = data})));
}
}
@ -161,9 +160,9 @@ TextureData* Moon_GetTexture(string texture){
}
void Moon_PreInitModEngine(){
// Moon_LoadDefaultAddon();
Moon_LoadAddon("/home/alex/disks/uwu/Projects/UnderVolt/example.bit");
Moon_LoadAddon("/home/alex/disks/uwu/Projects/UnderVolt/owo.bit");
Moon_LoadDefaultAddon();
// Moon_LoadAddon("/home/alex/disks/uwu/Projects/UnderVolt/example.bit");
Moon_LoadAddon("/home/alex/Downloads/packs/converted/mc.bit");
}
void Moon_InitModEngine(){

View File

@ -282,12 +282,26 @@ static bool gfx_texture_cache_lookup(int tile, struct TextureData **n, const uin
return false;
}
static inline void preload_base_texture(const char *fullpath) {
static bool preload_base_texture(void* user, const char *fullpath) {
int w, h;
u64 imgsize = 0;
u8 *imgdata = fs_load_file(fullpath, &imgsize);
if (imgdata) moon_load_base_texture(imgdata, imgsize, fullpath);
if (imgdata) {
char texname[SYS_MAX_PATH];
strncpy(texname, fullpath, sizeof(texname));
texname[sizeof(texname)-1] = 0;
char *dot = strrchr(texname, '.');
if (dot) *dot = 0;
char *actualname = texname;
if (!strncmp(FS_TEXTUREDIR "/", actualname, 4)) actualname += 4;
actualname = sys_strdup(actualname);
assert(actualname);
moon_load_base_texture(imgdata, imgsize, actualname);
}
return true;
}
static inline void load_texture(const char *fullpath) {
@ -365,48 +379,16 @@ static bool texname_to_texformat(const char *name, u8 *fmt, u8 *siz) {
return false;
}
// calls import_texture() on every texture in the res folder
// we can get the format and size from the texture files
// and then cache them using gfx_texture_cache_lookup
static bool preload_texture(void *user, const char *path) {
// strip off the extension
char texname[SYS_MAX_PATH];
strncpy(texname, path, sizeof(texname));
texname[sizeof(texname)-1] = 0;
char *dot = strrchr(texname, '.');
if (dot) *dot = 0;
// get the format and size from filename
u8 fmt, siz;
if (!texname_to_texformat(texname, &fmt, &siz)) {
fprintf(stderr, "unknown texture format: `%s`, skipping\n", texname);
return true; // just skip it, might be a stray skybox or something
}
char *actualname = texname;
// strip off the prefix // TODO: make a fs_ function for this shit
if (!strncmp(FS_TEXTUREDIR "/", actualname, 4)) actualname += 4;
// this will be stored in the hashtable, so make a copy
actualname = sys_strdup(actualname);
assert(actualname);
preload_base_texture(path);
return true;
}
void overload_memory_texture(void* data, long size, const char *path) {
// strip off the extension
char texname[SYS_MAX_PATH];
strncpy(texname, path, sizeof(texname));
texname[sizeof(texname)-1] = 0;
char *dot = strrchr(texname, '.');
if (dot) *dot = 0;
// get the format and size from filename
u8 fmt, siz;
if (!texname_to_texformat(texname, &fmt, &siz)) {
fprintf(stderr, "unknown texture format: `%s`, skipping\n", texname);
// fprintf(stderr, "unknown texture format: `%s`, skipping\n", texname);
return true; // just skip it, might be a stray skybox or something
}
@ -1540,7 +1522,7 @@ void gfx_init(struct GfxWindowManagerAPI *wapi, struct GfxRenderingAPI *rapi, co
void gfx_precache_textures(void) {
// preload all textures
fs_walk(FS_TEXTUREDIR, preload_texture, NULL, true);
fs_walk(FS_TEXTUREDIR, preload_base_texture, NULL, true);
}
struct GfxRenderingAPI *gfx_get_current_rendering_api(void) {