From 197e440f1c2e3d0a8b11a1022b105e6cc2af9b98 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 23 Jun 2024 17:25:02 +1000 Subject: [PATCH] Saturn: WIP textures --- src/Graphics_Saturn.c | 52 +++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/Graphics_Saturn.c b/src/Graphics_Saturn.c index 123657aaf..6802c1e04 100644 --- a/src/Graphics_Saturn.c +++ b/src/Graphics_Saturn.c @@ -72,10 +72,12 @@ static GfxResourceID white_square; void Gfx_RestoreState(void) { InitDefaultResources(); - // 2x2 dummy white texture struct Bitmap bmp; - BitmapCol pixels[4] = { BitmapColor_RGB(255, 0, 0), BITMAPCOLOR_WHITE, BITMAPCOLOR_WHITE, BITMAPCOLOR_WHITE }; - Bitmap_Init(bmp, 2, 2, pixels); + BitmapCol pixels[8 * 8]; + Mem_Set(pixels, 0xFF, sizeof(pixels)); + pixels[0] = BitmapColor_RGB(255, 0, 0); + + Bitmap_Init(bmp, 8, 8, pixels); white_square = Gfx_CreateTexture(&bmp, 0, false); } @@ -98,8 +100,8 @@ void Gfx_Create(void) { Gfx.MinTexWidth = 8; Gfx.MinTexHeight = 8; - Gfx.MaxTexWidth = 128; - Gfx.MaxTexHeight = 128; + Gfx.MaxTexWidth = 16; // 128 + Gfx.MaxTexHeight = 16; // 128 Gfx.Created = true; } @@ -114,33 +116,42 @@ void Gfx_Free(void) { #define BGRA8_to_SATURN(src) \ ((src[2] & 0xF8) >> 3) | ((src[1] & 0xF8) << 2) | ((src[0] & 0xF8) << 7) | ((src[3] & 0x80) << 8) +typedef struct CCTexture { + int width, height; + cc_uint16 pixels[]; +} CCTexture; + static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) { - cc_uint16* tmp = Mem_TryAlloc(bmp->width * bmp->height, 2); - if (!tmp) return NULL; + CCTexture* tex = Mem_TryAlloc(4 + bmp->width * bmp->height, 2); + if (!tex) return NULL; for (int y = 0; y < bmp->height; y++) { - cc_uint32* src = bmp->scan0 + y * rowWidth; - cc_uint16* dst = tmp + y * bmp->width; - - for (int x = 0; x < bmp->width; x++) + cc_uint32* src = bmp->scan0 + y * rowWidth; + cc_uint16* dst = tex->pixels + y * bmp->width; + + for (int x = 0; x < bmp->width; x++) { cc_uint8* color = (cc_uint8*)&src[x]; dst[x] = BGRA8_to_SATURN(color); dst[x] = 0xFEEE; } } - - scu_dma_transfer(0, _vdp1_vram_partitions.texture_base, tmp, bmp->width * bmp->height * 2); - scu_dma_transfer_wait(0); - Mem_Free(tmp); - return (void*)1; + return tex; } void Gfx_BindTexture(GfxResourceID texId) { + if (!texId) return; + CCTexture* tex = (CCTexture*)texId; + + scu_dma_transfer(0, _vdp1_vram_partitions.texture_base, tex->pixels, tex->width * tex->height * 2); + scu_dma_transfer_wait(0); } - + void Gfx_DeleteTexture(GfxResourceID* texId) { + CCTexture* tex = *texId; + if (tex) Mem_Free(tex); + *texId = NULL; } void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) { @@ -377,18 +388,21 @@ static void DrawTexturedQuads2D(int verticesCount, int startVertex) { vdp1_cmdt_t* cmd; + cmd = NextPrimitive(); vdp1_cmdt_polygon_set(cmd); vdp1_cmdt_color_set(cmd, RGB1555(1, R >> 3, G >> 3, B >> 3)); vdp1_cmdt_draw_mode_set(cmd, color_draw_mode); vdp1_cmdt_vtx_set(cmd, points); - /*cmd = NextPrimitive(); +/* + cmd = NextPrimitive(); vdp1_cmdt_distorted_sprite_set(cmd); vdp1_cmdt_char_size_set(cmd, 8, 8); vdp1_cmdt_char_base_set(cmd, (vdp1_vram_t)_vdp1_vram_partitions.texture_base); vdp1_cmdt_draw_mode_set(cmd, texture_draw_mode); - vdp1_cmdt_vtx_set(cmd, points);*/ + vdp1_cmdt_vtx_set(cmd, points); +*/ } }