Now the TXD.img file is being created correctly, and the textures are reduced to half their size, saving a lot of memory and making the game run more smoothly. The game no longer crashes and it looks very good. That said, I still couldn’t increase the frame rate—it stays between 8 and 15 FPS, but usually runs around 8

This commit is contained in:
Dante Leoncini 2026-05-06 03:22:18 -03:00
parent 39c75bb8c5
commit 6d9929b7ce
4 changed files with 189 additions and 107 deletions

View file

@ -270,7 +270,7 @@ CStreaming::Init2(void)
void void
CStreaming::Init(void) CStreaming::Init(void)
{ {
/*#ifdef USE_TXD_CDIMAGE #ifdef USE_TXD_CDIMAGE
int txdHandle = CFileMgr::OpenFile("MODELS\\TXD.IMG", "r"); int txdHandle = CFileMgr::OpenFile("MODELS\\TXD.IMG", "r");
if (txdHandle) if (txdHandle)
CFileMgr::CloseFile(txdHandle); CFileMgr::CloseFile(txdHandle);
@ -285,9 +285,9 @@ CStreaming::Init(void)
CStreaming::Init2(); CStreaming::Init2();
} }
} }
#else*/ #else
CStreaming::Init2(); CStreaming::Init2();
//#endif #endif
} }
void void

View file

@ -93,8 +93,77 @@ static int clamp(int size, int targetSize)
return size; return size;
} }
static void downscaleTexture(RwTexture *texture) void debugRaster(RwRaster *r){
{ if(!r){
debug("Raster NULL");
return;
}
debug("Raster: %dx%d | depth: %d | format: 0x%X | stride: %d | platform: %d | pixels: %p",
r->width,
r->height,
r->depth,
r->format,
r->stride,
r->platform,
r->pixels);
}
static void downscaleTexture(RwTexture *texture){
#ifdef RW_GLES1
if(texture == nil)
return;
RwRaster *oldRaster = RwTextureGetRaster(texture);
if(oldRaster == nil)
return;
int oldWidth = RwRasterGetWidth(oldRaster);
int oldHeight = RwRasterGetHeight(oldRaster);
// dividir a la mitad, mínimo 16x16
int newWidth = oldWidth > 16 ? oldWidth / 2 : oldWidth;
int newHeight = oldHeight > 16 ? oldHeight / 2 : oldHeight;
// si ya es suficientemente chica, no hacer nada
if(newWidth == oldWidth && newHeight == oldHeight)
return;
// toImage() funciona porque el backingStore está populado
RwImage *image = oldRaster->toImage();
if(image == nil){
debug("downscaleTexture: toImage FAILED for %dx%d", oldWidth, oldHeight);
return;
}
RwImage *resized = resizeImage(image, newWidth, newHeight);
RwImageDestroy(image);
if(resized == nil){
debug("downscaleTexture: resizeImage FAILED");
return;
}
// crear nuevo raster en formato compatible con GLES1
int32 w, h, d, f;
rw::Raster::imageFindRasterFormat(resized, rw::Raster::TEXTURE, &w, &h, &d, &f);
RwRaster *newRaster = rw::Raster::create(w, h, d, f | rw::Raster::TEXTURE, rw::PLATFORM_GLES1);
if(newRaster == nil){
debug("downscaleTexture: raster create FAILED");
RwImageDestroy(resized);
return;
}
if(newRaster->setFromImage(resized) == nil){
debug("downscaleTexture: setFromImage FAILED");
RwRasterDestroy(newRaster);
RwImageDestroy(resized);
return;
}
RwImageDestroy(resized);
RwTextureSetRaster(texture, newRaster);
RwRasterDestroy(oldRaster);
#else
RwRaster *oldRaster = RwTextureGetRaster(texture); RwRaster *oldRaster = RwTextureGetRaster(texture);
if (oldRaster == nil) if (oldRaster == nil)
return; return;
@ -123,6 +192,7 @@ static void downscaleTexture(RwTexture *texture)
RwTextureSetRaster(texture, newRaster); RwTextureSetRaster(texture, newRaster);
RwRasterDestroy(oldRaster); RwRasterDestroy(oldRaster);
#endif
} }
#endif #endif

View file

@ -308,9 +308,7 @@ flushGlRenderState(void)
void void setAlphaBlend(bool32 enable){
setAlphaBlend(bool32 enable)
{
if(rwStateCache.blendEnable != enable){ if(rwStateCache.blendEnable != enable){
rwStateCache.blendEnable = enable; rwStateCache.blendEnable = enable;
setGlRenderState(RWGL_BLEND, enable); setGlRenderState(RWGL_BLEND, enable);

View file

@ -416,9 +416,7 @@ rasterLock(Raster *raster, int32 level, int32 lockMode)
#endif #endif
} }
void void rasterUnlock(Raster *raster, int32 level){
rasterUnlock(Raster *raster, int32 level)
{
#ifdef RW_GLES1 #ifdef RW_GLES1
Gl1Raster *natras = GETGL1RASTEREXT(raster); Gl1Raster *natras = GETGL1RASTEREXT(raster);
@ -430,61 +428,73 @@ rasterUnlock(Raster *raster, int32 level)
case Raster::CAMERATEXTURE: case Raster::CAMERATEXTURE:
if(raster->privateFlags & Raster::LOCKWRITE){ if(raster->privateFlags & Raster::LOCKWRITE){
if(level != 0) break; if(level != 0) break;
// === GUARDAR EN BACKING STORE ANTES DE SUBIR A GPU ===
if(natras->backingStore == nil){
int numLevels = natras->numLevels;
// RasterLevels tiene: int numlevels; struct { uint32 size; uint8 *data; } levels[];
natras->backingStore = (RasterLevels*)malloc(
sizeof(RasterLevels) + sizeof(natras->backingStore->levels[0]) * numLevels);
natras->backingStore->numlevels = numLevels;
for(int i = 0; i < numLevels; i++){
uint32 sz = getLevelSize(raster, i);
natras->backingStore->levels[i].size = sz;
natras->backingStore->levels[i].data = (uint8*)malloc(sz);
memset(natras->backingStore->levels[i].data, 0, sz);
}
}
// copiar píxeles actuales al backing store
{
uint32 sz = getLevelSize(raster, level);
assert(level < natras->backingStore->numlevels);
memcpy(natras->backingStore->levels[level].data, raster->pixels, sz);
}
// === FIN BACKING STORE ===
uint32 prev = bindTexture(natras->texid); uint32 prev = bindTexture(natras->texid);
if(natras->isCompressed){
glCompressedTexImage2D(GL_TEXTURE_2D, level, natras->internalFormat, // flip vertical (igual que antes)
raster->width, raster->height, 0, {
getLevelSize(raster, level),
raster->pixels);
}else{
int stride = raster->width * natras->bpp; int stride = raster->width * natras->bpp;
uint8_t *tmp = (uint8_t*)malloc(stride); uint8_t *tmp = (uint8_t*)malloc(stride);
for(int y = 0; y < raster->height / 2; y++){ for(int y = 0; y < raster->height / 2; y++){
uint8_t *a = (uint8_t*)raster->pixels + y * stride; uint8_t *a = (uint8_t*)raster->pixels + y * stride;
uint8_t *b = (uint8_t*)raster->pixels + (raster->height - 1 - y) * stride; uint8_t *b = (uint8_t*)raster->pixels + (raster->height - 1 - y) * stride;
memcpy(tmp, a, stride); memcpy(tmp, a, stride);
memcpy(a, b, stride); memcpy(a, b, stride);
memcpy(b, tmp, stride); memcpy(b, tmp, stride);
} }
free(tmp); free(tmp);
}
if (raster->pixels != nil && natras->format == GL_RGBA && natras->type == GL_UNSIGNED_BYTE) { if(!natras->isCompressed){
// convert to 16-bit if(natras->format == GL_RGBA && natras->type == GL_UNSIGNED_BYTE){
uint16_t* pixels16 = (uint16_t*)malloc(raster->width * raster->height * sizeof(uint16_t)); // convertir RGBA8888 → RGBA4444
uint16_t *pixels16 = (uint16_t*)malloc(raster->width * raster->height * 2);
uint8_t *pixels8 = (uint8_t*)raster->pixels; uint8_t *pixels8 = (uint8_t*)raster->pixels;
for(int i = 0; i < raster->width * raster->height; i++){ for(int i = 0; i < raster->width * raster->height; i++){
uint8_t r = pixels8[i*4+0]; uint8_t r = pixels8[i*4+0];
uint8_t g = pixels8[i*4+1]; uint8_t g = pixels8[i*4+1];
uint8_t b = pixels8[i*4+2]; uint8_t b = pixels8[i*4+2];
uint8_t a = pixels8[i*4+3]; uint8_t a = pixels8[i*4+3];
pixels16[i] = ((r>>4)<<12)|((g>>4)<<8)|((b>>4)<<4)|(a>>4); pixels16[i] = ((r>>4)<<12)|((g>>4)<<8)|((b>>4)<<4)|(a>>4);
} }
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, raster->width, raster->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixels16); glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA,
raster->width, raster->height, 0,
GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixels16);
free(pixels16); free(pixels16);
} else } else {
{
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, level, natras->internalFormat, glTexImage2D(GL_TEXTURE_2D, level, natras->internalFormat,
raster->width, raster->height, raster->width, raster->height, 0,
0, natras->format, natras->type, raster->pixels); natras->format, natras->type, raster->pixels);
} }
} }
if(natras->backingStore){
assert(level < natras->backingStore->numlevels);
memcpy(natras->backingStore->levels[level].data, raster->pixels,
natras->backingStore->levels[level].size);
}
bindTexture(prev); bindTexture(prev);
} }
break; break;
case Raster::CAMERA: case Raster::CAMERA:
// TODO: write?
break; break;
} }
@ -769,8 +779,12 @@ destroyNativeRaster(void *object, int32 offset, int32)
natras->texid = 0; natras->texid = 0;
natras->fbo = 0; natras->fbo = 0;
if(natras->backingStore){
for(int i = 0; i < natras->backingStore->numlevels; i++)
free(natras->backingStore->levels[i].data);
free(natras->backingStore); free(natras->backingStore);
natras->backingStore = nil;
}
#endif #endif
return object; return object;
} }