le textures

This commit is contained in:
Dominicentek 2024-10-02 22:46:58 +02:00
parent 2c7b4775d2
commit 89419ec718
3 changed files with 57 additions and 0 deletions

View file

@ -1,5 +1,7 @@
#include "dynos.cpp.h"
extern void ModelTextureAdd(char* id, char* data, int w, int h);
//
// Pointers
//
@ -104,6 +106,7 @@ static void LoadTextureData(FILE *aFile, GfxData *aGfxData) {
_Node->mData->mRawFormat = G_IM_FMT_RGBA;
_Node->mData->mRawSize = G_IM_SIZ_32b;
_Node->mData->mRawData = Array<u8>(_RawData, _RawData + (_Node->mData->mRawWidth * _Node->mData->mRawHeight * 4));
ModelTextureAdd((std::string("gfx/") + _Node->mName.begin()).data(), (char*)_RawData, _Node->mData->mRawWidth, _Node->mData->mRawHeight);
free(_RawData);
} else { // Probably a palette
_Node->mData->mRawData = Array<u8>();

View file

@ -514,6 +514,14 @@ void load_texture(const char *fullpath) {
int w, h;
uint64_t imgsize = 0;
extern char* ModelTextureGet(char* id, int* w, int* h);
char* model_data = ModelTextureGet(fullpath, &w, &h);
if (model_data) {
gfx_rapi->upload_texture(model_data, w, h);
return;
}
u8 *imgdata = fs_load_file(fullpath, &imgsize);
if (imgdata) {
// TODO: implement stbi_callbacks or some shit instead of loading the whole texture

View file

@ -165,3 +165,49 @@ Model LoadModelData(std::string folderPath) {
}
return model;
}
struct ModelTexture {
struct ModelTexture* next;
char* id;
char* data;
int w, h;
};
struct ModelTexture* gModelTextureList;
void ModelTextureAdd(char* id, char* data, int w, int h) {
struct ModelTexture* curr = gModelTextureList;
while (curr) {
if (strcmp(curr->id, id) == 0) return;
curr = curr->next;
}
struct ModelTexture* tex = (struct ModelTexture*)malloc(sizeof(struct ModelTexture));
int texid_len = strlen(id) + 1;
char* texid = (char*)malloc(texid_len);
char* texdata = (char*)malloc(w * h * 4);
memcpy(texid, id, texid_len);
memcpy(texdata, data, w * h * 4);
tex->w = w;
tex->h = h;
tex->id = texid;
tex->data = texdata;
tex->next = nullptr;
if (gModelTextureList) {
struct ModelTexture* prev = gModelTextureList;
while (prev->next) prev = prev->next;
prev->next = tex;
}
else gModelTextureList = tex;
}
extern "C" char* ModelTextureGet(char* id, int* w, int* h) {
struct ModelTexture* curr = gModelTextureList;
while (curr) {
if (strcmp(curr->id, id) == 0) {
*w = curr->w;
*h = curr->h;
return curr->data;
}
curr = curr->next;
}
return nullptr;
}