diff --git a/src/Graphics_GL1.c b/src/Graphics_GL1.c index e8af44134..95202da52 100644 --- a/src/Graphics_GL1.c +++ b/src/Graphics_GL1.c @@ -72,7 +72,9 @@ typedef void (*GL_SetupVBFunc)(void); typedef void (*GL_SetupVBRangeFunc)(int startVertex); static GL_SetupVBFunc gfx_setupVBFunc; static GL_SetupVBRangeFunc gfx_setupVBRangeFunc; + #include "_GLShared.h" +static void GLBackend_Init(void); void Gfx_Create(void) { GLContext_Create(); diff --git a/src/Graphics_GL2.c b/src/Graphics_GL2.c index 579900b4e..c030a26f8 100644 --- a/src/Graphics_GL2.c +++ b/src/Graphics_GL2.c @@ -36,6 +36,8 @@ static const struct DynamicLibSym core_funcs[] = { #include "../misc/opengl/GL1Macros.h" #include "_GLShared.h" +static void GLBackend_Init(void); + static GfxResourceID white_square; static int postProcess; enum PostProcess { POSTPROCESS_NONE, POSTPROCESS_GRAYSCALE }; @@ -49,6 +51,7 @@ void Gfx_Create(void) { Gfx.BackendType = CC_GFX_BACKEND_GL2; GL_InitCommon(); + GLBackend_Init(); Gfx_RestoreState(); GLContext_SetVSync(gfx_vsync); } diff --git a/src/Graphics_NDS.c b/src/Graphics_NDS.c index 7455c8ff3..7f3f5a1e5 100644 --- a/src/Graphics_NDS.c +++ b/src/Graphics_NDS.c @@ -152,10 +152,10 @@ void Gfx_DisableTextureOffset(void) { static int FindColorInPalette(cc_uint16* pal, int pal_size, cc_uint16 col) { if ((col >> 15) == 0) return 0; - for (int i = 1; i < pal_size; i++) { - if(pal[i] == col) return i; + for (int i = 1; i < pal_size; i++) + { + if (pal[i] == col) return i; } - return -1; } @@ -173,8 +173,7 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, // Palettize texture if possible int pal_size = 1; - cc_uint16* tmp_palette = Mem_TryAlloc(256, 2); - if (!tmp_palette) return 0; + cc_uint16 tmp_palette[256]; tmp_palette[0] = 0; for (int i = 0; i < bmp->width * bmp->height; i++) { @@ -188,58 +187,60 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, tmp_palette[pal_size - 1] = col; } } - - int texFormat = GL_RGBA; - if(pal_size <= 4) texFormat = GL_RGB4; - else if(pal_size <= 16) texFormat = GL_RGB16; - else if(pal_size <= 256) texFormat = GL_RGB256; - - if(texFormat != GL_RGBA) { - char* tmp_chr = (char*) tmp; - - for (int i = 0; i < bmp->width * bmp->height; i++) { - cc_uint16 col = tmp[i]; - int idx = FindColorInPalette(tmp_palette, pal_size, col); - - if(texFormat == GL_RGB256) { - tmp_chr[i] = idx; - } else if(texFormat == GL_RGB16) { - if((i & 1) == 0) { - tmp_chr[i >> 1] = idx; - } else { - tmp_chr[i >> 1] |= idx << 4; - } - } else { - if((i & 3) == 0) { - tmp_chr[i >> 2] = idx; - } else { - tmp_chr[i >> 2] |= idx << (2 * (i & 3)); - } - } - } - } - - // Load texture in vram + int textureID; glGenTextures(1, &textureID); glBindTexture(0, textureID); - glTexImage2D(0, 0, texFormat, bmp->width, bmp->height, 0, 0, tmp); - if (texFormat != GL_RGBA) { - int glPalSize; - if(texFormat == GL_RGB4) glPalSize = 4; - else if(texFormat == GL_RGB16) glPalSize = 16; - else glPalSize = 256; - - glColorTableEXT(0, 0, glPalSize, 0, 0, tmp_palette); + char* tmp_chr = (char*)tmp; + + if (pal_size <= 4) { + for (int i = 0; i < bmp->width * bmp->height; i++) + { + cc_uint16 col = tmp[i]; + int idx = FindColorInPalette(tmp_palette, pal_size, col); + + if ((i & 3) == 0) { + tmp_chr[i >> 2] = idx; + } else { + tmp_chr[i >> 2] |= idx << (2 * (i & 3)); + } + } + + glTexImage2D(0, 0, GL_RGB4, bmp->width, bmp->height, 0, 0, tmp); + glColorTableEXT(0, 0, 4, 0, 0, tmp_palette); + } else if (pal_size <= 16) { + for (int i = 0; i < bmp->width * bmp->height; i++) + { + cc_uint16 col = tmp[i]; + int idx = FindColorInPalette(tmp_palette, pal_size, col); + + if ((i & 1) == 0) { + tmp_chr[i >> 1] = idx; + } else { + tmp_chr[i >> 1] |= idx << 4; + } + } + + glTexImage2D(0, 0, GL_RGB16, bmp->width, bmp->height, 0, 0, tmp); + glColorTableEXT(0, 0, 16, 0, 0, tmp_palette); + } else if(pal_size <= 256) { + for (int i = 0; i < bmp->width * bmp->height; i++) + { + cc_uint16 col = tmp[i]; + tmp_chr[i] = FindColorInPalette(tmp_palette, pal_size, col); + } + + glTexImage2D(0, 0, GL_RGB256, bmp->width, bmp->height, 0, 0, tmp); + glColorTableEXT(0, 0, 256, 0, 0, tmp_palette); + } else { + glTexImage2D(0, 0, GL_RGBA, bmp->width, bmp->height, 0, 0, tmp); } glTexParameter(0, GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T | TEXGEN_TEXCOORD | GL_TEXTURE_COLOR0_TRANSPARENT); - cc_uint16* vram_ptr = glGetTexturePointer(textureID); if (!vram_ptr) Platform_Log2("No VRAM for %i x %i texture", &bmp->width, &bmp->height); Mem_Free(tmp); - Mem_Free(tmp_palette); return (void*)textureID; } diff --git a/src/Platform_NDS.c b/src/Platform_NDS.c index b78628377..14fe340c4 100644 --- a/src/Platform_NDS.c +++ b/src/Platform_NDS.c @@ -585,6 +585,14 @@ void Platform_Init(void) { Platform_Log1("Running in %c mode with NDS wifi", dsiMode ? "DSi" : "DS"); #endif + // By default, the "heap limit" is calculated in `sbrk` based on current + // stack pointer value - however this does not reliably in ClassiCube's + // case as e.g. map gen and bitmap decoding use much more stack space + // So to avoid having the stack overlapping the heap when more stack space + // is used, manually reduce the heap limit by 32 kb + extern char* fake_heap_end; + if (fake_heap_end) fake_heap_end -= 32768; + InitFilesystem(); InitNetworking(); cpuStartTiming(1); diff --git a/src/_GLShared.h b/src/_GLShared.h index a7d28e0d4..ced222efb 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -25,8 +25,6 @@ /*########################################################################################################################* *---------------------------------------------------------General---------------------------------------------------------* *#########################################################################################################################*/ -static void GLBackend_Init(void); - static void GLContext_GetAll(const struct DynamicLibSym* syms, int count) { int i; for (i = 0; i < count; i++)