Merge branch 'ClassiCube:master' into master

This commit is contained in:
whatsavalue3 2025-01-26 08:21:35 +01:00 committed by GitHub
commit c35ae6f714
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 61 additions and 49 deletions

View File

@ -72,7 +72,9 @@ typedef void (*GL_SetupVBFunc)(void);
typedef void (*GL_SetupVBRangeFunc)(int startVertex); typedef void (*GL_SetupVBRangeFunc)(int startVertex);
static GL_SetupVBFunc gfx_setupVBFunc; static GL_SetupVBFunc gfx_setupVBFunc;
static GL_SetupVBRangeFunc gfx_setupVBRangeFunc; static GL_SetupVBRangeFunc gfx_setupVBRangeFunc;
#include "_GLShared.h" #include "_GLShared.h"
static void GLBackend_Init(void);
void Gfx_Create(void) { void Gfx_Create(void) {
GLContext_Create(); GLContext_Create();

View File

@ -36,6 +36,8 @@ static const struct DynamicLibSym core_funcs[] = {
#include "../misc/opengl/GL1Macros.h" #include "../misc/opengl/GL1Macros.h"
#include "_GLShared.h" #include "_GLShared.h"
static void GLBackend_Init(void);
static GfxResourceID white_square; static GfxResourceID white_square;
static int postProcess; static int postProcess;
enum PostProcess { POSTPROCESS_NONE, POSTPROCESS_GRAYSCALE }; enum PostProcess { POSTPROCESS_NONE, POSTPROCESS_GRAYSCALE };
@ -49,6 +51,7 @@ void Gfx_Create(void) {
Gfx.BackendType = CC_GFX_BACKEND_GL2; Gfx.BackendType = CC_GFX_BACKEND_GL2;
GL_InitCommon(); GL_InitCommon();
GLBackend_Init();
Gfx_RestoreState(); Gfx_RestoreState();
GLContext_SetVSync(gfx_vsync); GLContext_SetVSync(gfx_vsync);
} }

View File

@ -152,10 +152,10 @@ void Gfx_DisableTextureOffset(void) {
static int FindColorInPalette(cc_uint16* pal, int pal_size, cc_uint16 col) { static int FindColorInPalette(cc_uint16* pal, int pal_size, cc_uint16 col) {
if ((col >> 15) == 0) return 0; if ((col >> 15) == 0) return 0;
for (int i = 1; i < pal_size; i++) { for (int i = 1; i < pal_size; i++)
{
if (pal[i] == col) return i; if (pal[i] == col) return i;
} }
return -1; return -1;
} }
@ -173,8 +173,7 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags,
// Palettize texture if possible // Palettize texture if possible
int pal_size = 1; int pal_size = 1;
cc_uint16* tmp_palette = Mem_TryAlloc(256, 2); cc_uint16 tmp_palette[256];
if (!tmp_palette) return 0;
tmp_palette[0] = 0; tmp_palette[0] = 0;
for (int i = 0; i < bmp->width * bmp->height; i++) { for (int i = 0; i < bmp->width * bmp->height; i++) {
@ -189,57 +188,59 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags,
} }
} }
int texFormat = GL_RGBA; int textureID;
if(pal_size <= 4) texFormat = GL_RGB4; glGenTextures(1, &textureID);
else if(pal_size <= 16) texFormat = GL_RGB16; glBindTexture(0, textureID);
else if(pal_size <= 256) texFormat = GL_RGB256;
if(texFormat != GL_RGBA) {
char* tmp_chr = (char*)tmp; char* tmp_chr = (char*)tmp;
for (int i = 0; i < bmp->width * bmp->height; i++) { if (pal_size <= 4) {
for (int i = 0; i < bmp->width * bmp->height; i++)
{
cc_uint16 col = tmp[i]; cc_uint16 col = tmp[i];
int idx = FindColorInPalette(tmp_palette, pal_size, col); 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) { if ((i & 3) == 0) {
tmp_chr[i >> 2] = idx; tmp_chr[i >> 2] = idx;
} else { } else {
tmp_chr[i >> 2] |= idx << (2 * (i & 3)); 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;
} }
} }
// Load texture in vram glTexImage2D(0, 0, GL_RGB16, bmp->width, bmp->height, 0, 0, tmp);
int textureID; glColorTableEXT(0, 0, 16, 0, 0, tmp_palette);
glGenTextures(1, &textureID); } else if(pal_size <= 256) {
glBindTexture(0, textureID); for (int i = 0; i < bmp->width * bmp->height; i++)
glTexImage2D(0, 0, texFormat, bmp->width, bmp->height, 0, 0, tmp); {
if (texFormat != GL_RGBA) { cc_uint16 col = tmp[i];
int glPalSize; tmp_chr[i] = FindColorInPalette(tmp_palette, pal_size, col);
if(texFormat == GL_RGB4) glPalSize = 4; }
else if(texFormat == GL_RGB16) glPalSize = 16;
else glPalSize = 256;
glColorTableEXT(0, 0, glPalSize, 0, 0, tmp_palette); 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); glTexParameter(0, GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T | TEXGEN_TEXCOORD | GL_TEXTURE_COLOR0_TRANSPARENT);
cc_uint16* vram_ptr = glGetTexturePointer(textureID); cc_uint16* vram_ptr = glGetTexturePointer(textureID);
if (!vram_ptr) Platform_Log2("No VRAM for %i x %i texture", &bmp->width, &bmp->height); if (!vram_ptr) Platform_Log2("No VRAM for %i x %i texture", &bmp->width, &bmp->height);
Mem_Free(tmp); Mem_Free(tmp);
Mem_Free(tmp_palette);
return (void*)textureID; return (void*)textureID;
} }

View File

@ -585,6 +585,14 @@ void Platform_Init(void) {
Platform_Log1("Running in %c mode with NDS wifi", dsiMode ? "DSi" : "DS"); Platform_Log1("Running in %c mode with NDS wifi", dsiMode ? "DSi" : "DS");
#endif #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(); InitFilesystem();
InitNetworking(); InitNetworking();
cpuStartTiming(1); cpuStartTiming(1);

View File

@ -25,8 +25,6 @@
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------General---------------------------------------------------------* *---------------------------------------------------------General---------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static void GLBackend_Init(void);
static void GLContext_GetAll(const struct DynamicLibSym* syms, int count) { static void GLContext_GetAll(const struct DynamicLibSym* syms, int count) {
int i; int i;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)