From 3583aa29bc5da067f6aba88d6548bfbd00ac75d5 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 2 Feb 2024 21:37:53 +1100 Subject: [PATCH] Dreamcast: Try to fix freeze/crash after leaving in-game and then trying to go in-game again --- src/Window_Dreamcast.c | 29 ++++++++++++++++++----------- third_party/gldc/src/texture.c | 31 +++++++++++++------------------ 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/Window_Dreamcast.c b/src/Window_Dreamcast.c index 94ba5f9db..105fc0764 100644 --- a/src/Window_Dreamcast.c +++ b/src/Window_Dreamcast.c @@ -31,21 +31,19 @@ void Window_Init(void) { Input.Sources = INPUT_SOURCE_GAMEPAD; DisplayInfo.ContentOffsetX = 10; DisplayInfo.ContentOffsetY = 20; + + vid_set_mode(DEFAULT_VID_MODE, DEFAULT_PIXEL_MODE); + vid_flip(0); + // TODO: Why doesn't 32 bit work on real hardware for in-game? } void Window_Free(void) { } void Window_Create2D(int width, int height) { launcherMode = true; - vid_set_mode(DEFAULT_VID_MODE, PM_RGB888); - vid_flip(0); } - void Window_Create3D(int width, int height) { launcherMode = false; - vid_set_mode(DEFAULT_VID_MODE, DEFAULT_PIXEL_MODE); - vid_flip(0); - // TODO: Why doesn't 32 bit work on real hardware? } void Window_SetTitle(const cc_string* title) { } @@ -234,21 +232,30 @@ void Window_UpdateRawMouse(void) { } *------------------------------------------------------Framebuffer--------------------------------------------------------* *#########################################################################################################################*/ static struct Bitmap fb_bmp; + void Window_AllocFramebuffer(struct Bitmap* bmp) { bmp->scan0 = (BitmapCol*)Mem_Alloc(bmp->width * bmp->height, 4, "window pixels"); fb_bmp = *bmp; } void Window_DrawFramebuffer(Rect2D r) { - // TODO probably bogus.. - // TODO: Don't redraw everything - int size = fb_bmp.width * fb_bmp.height * 4; - // TODO: double buffering ?? // https://dcemulation.org/phpBB/viewtopic.php?t=99999 // https://dcemulation.org/phpBB/viewtopic.php?t=43214 vid_waitvbl(); - sq_cpy(vram_l, fb_bmp.scan0, size); + + for (int y = r.y; y < r.y + r.Height; y++) + { + BitmapCol* src = Bitmap_GetRow(&fb_bmp, y); + uint16_t* dst = vram_s + vid_mode->width * y; + + for (int x = r.x; x < r.x + r.Width; x++) + { + BitmapCol color = src[x]; + // 888 to 565 (discard least significant bits) + dst[x] = ((BitmapCol_R(color) & 0xF8) << 8) | ((BitmapCol_G(color) & 0xFC) << 3) | (BitmapCol_B(color) >> 3); + } + } } void Window_FreeFramebuffer(struct Bitmap* bmp) { diff --git a/third_party/gldc/src/texture.c b/third_party/gldc/src/texture.c index bc887d483..2fb3fd291 100644 --- a/third_party/gldc/src/texture.c +++ b/third_party/gldc/src/texture.c @@ -16,31 +16,30 @@ TextureObject* TEXTURE_ACTIVE = NULL; static TextureObject TEXTURE_LIST[MAX_TEXTURE_COUNT]; static unsigned char TEXTURE_USED[MAX_TEXTURE_COUNT / 8]; -static char texture_id_map_used(unsigned int id) { +static int texture_id_map_used(unsigned int id) { unsigned int i = id / 8; unsigned int j = id % 8; - unsigned char v = TEXTURE_USED[i] & (unsigned char) (1 << j); - return !!(v); + return TEXTURE_USED[i] & (unsigned char)(1 << j); } static void texture_id_map_reserve(unsigned int id) { - unsigned int j = (id % 8); unsigned int i = id / 8; - - TEXTURE_USED[i] |= (unsigned char) 1 << j; + unsigned int j = id % 8; + TEXTURE_USED[i] |= (unsigned char)(1 << j); } static void texture_id_map_release(unsigned int id) { unsigned int i = id / 8; unsigned int j = id % 8; - TEXTURE_USED[i] &= (unsigned char) ~(1 << j); + TEXTURE_USED[i] &= (unsigned char)~(1 << j); } unsigned int texture_id_map_alloc(void) { unsigned int id; - for(id = 0; id < MAX_TEXTURE_COUNT; ++id) { + // ID 0 is reserved for default texture + for(id = 1; id < MAX_TEXTURE_COUNT; ++id) { if(!texture_id_map_used(id)) { texture_id_map_reserve(id); return id; @@ -71,11 +70,11 @@ static void* yalloc_alloc_and_defrag(size_t size) { #define GL_KOS_INTERNAL_DEFAULT_MIPMAP_LOD_BIAS 4 static void _glInitializeTextureObject(TextureObject* txr, unsigned int id) { - txr->index = id; - txr->width = txr->height = 0; + txr->index = id; + txr->width = txr->height = 0; txr->mipmap = 0; - txr->env = GPU_TXRENV_MODULATEALPHA; - txr->data = NULL; + txr->env = GPU_TXRENV_MODULATEALPHA; + txr->data = NULL; txr->minFilter = GL_NEAREST; txr->magFilter = GL_NEAREST; txr->mipmap_bias = GL_KOS_INTERNAL_DEFAULT_MIPMAP_LOD_BIAS; @@ -83,8 +82,6 @@ static void _glInitializeTextureObject(TextureObject* txr, unsigned int id) { GLubyte _glInitTextures() { memset(TEXTURE_USED, 0, sizeof(TEXTURE_USED)); - // Reserve zero so that it is never given to anyone as an ID! - texture_id_map_reserve(0); // Initialize zero as an actual texture object though because apparently it is! TextureObject* default_tex = &TEXTURE_LIST[0]; @@ -121,10 +118,8 @@ GLuint APIENTRY gldcGenTexture(void) { void APIENTRY gldcDeleteTexture(GLuint id) { TRACE(); - if(id == 0) { - /* Zero is the "default texture" and we never allow deletion of it */ - return; - } + if(id == 0) return; + /* Zero is the "default texture" and we never allow deletion of it */ if(texture_id_map_used(id)) { TextureObject* txr = &TEXTURE_LIST[id];