From 20791ebcea8247ce298cd96ca9049730117ee2c3 Mon Sep 17 00:00:00 2001 From: jaburns Date: Mon, 12 Oct 2020 14:08:55 -0600 Subject: [PATCH] Better api to get texture data --- src/gfx_adapter.c | 8 ++++---- src/libsm64.c | 15 +++------------ src/libsm64.h | 9 +++++---- src/load_tex_data.c | 25 +++++++++---------------- src/load_tex_data.h | 9 +++++++-- 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/gfx_adapter.c b/src/gfx_adapter.c index 4f3f823..5738aec 100644 --- a/src/gfx_adapter.c +++ b/src/gfx_adapter.c @@ -6,6 +6,7 @@ #include "guMtxF2L.h" #include "gfx_adapter.h" #include "gfx_adapter_commands.h" +#include "load_tex_data.h" static Mat4 s_curMatrix; static float s_curColor[3]; @@ -34,8 +35,7 @@ static void convert_uv_to_atlas( float *atlas_uv_out, short tc[] ) float u = (float)((tc[0] * s_scaleS >> 16) - 8*s_uls) / 32.0f / s_texWidth; float v = (float)((tc[1] * s_scaleT >> 16) - 8*s_ult) / 32.0f / s_texHeight; - // TODO define 11 (number of used textures) - atlas_uv_out[0] = u * s_texWidth / 64.0f / 11.0f + (float)s_textureIndex / 11.0f; + atlas_uv_out[0] = u * s_texWidth / 64.0f / (float)NUM_USED_TEXTURES + (float)s_textureIndex / (float)NUM_USED_TEXTURES; atlas_uv_out[1] = v * s_texHeight / 64.0f; } @@ -159,8 +159,8 @@ static void process_display_list( void *dl ) int64_t i = *ptr++; s_textureIndex = (int)i; - s_texWidth = 32.0f; - s_texHeight = 32.0f; + s_texWidth = mario_tex_widths[s_textureIndex]; + s_texHeight = mario_tex_heights[s_textureIndex]; break; } diff --git a/src/libsm64.c b/src/libsm64.c index 1020224..bcd13fa 100644 --- a/src/libsm64.c +++ b/src/libsm64.c @@ -76,13 +76,11 @@ static struct Area *hack_build_area( void ) return result; } - - -void sm64_global_init( uint8_t *rom, SM64DebugPrintFunctionPtr debugPrintFunction ) +void sm64_global_init( uint8_t *rom, uint8_t *outTexture, SM64DebugPrintFunctionPtr debugPrintFunction ) { gDebugPrint = debugPrintFunction; - load_mario_textures_from_rom( rom ); + load_mario_textures_from_rom( rom, outTexture ); load_mario_anims_from_rom( rom ); gMarioObject = hack_allocate_mario(); @@ -92,16 +90,9 @@ void sm64_global_init( uint8_t *rom, SM64DebugPrintFunctionPtr debugPrintFunctio s_mario_geo_pool = alloc_only_pool_init(); s_mario_graph_node = process_geo_layout( s_mario_geo_pool, mario_geo_ptr ); - D_80339D10.animDmaTable = NULL; // gMarioAnimsPtr; // mario_anims_ptr; + D_80339D10.animDmaTable = NULL; D_80339D10.currentAnimAddr = NULL; D_80339D10.targetAnim = NULL; - - //DEBUG_LOG( "Mario animations loaded from address %lu", (uint64_t)D_80339D10.animDmaTable ); -} - -uint8_t *sm64_get_texture( void ) -{ - return gLibSm64TextureRgba; } void sm64_load_surfaces( uint16_t terrainType, const struct SM64Surface *surfaceArray, size_t numSurfaces ) diff --git a/src/libsm64.h b/src/libsm64.h index c662bf4..73834aa 100644 --- a/src/libsm64.h +++ b/src/libsm64.h @@ -5,8 +5,6 @@ #include #include -#define SM64_GEO_BUFFER_SIZE 9216 // 1024 triangles * 9 floats per triangle - struct SM64Surface { int16_t type; @@ -41,8 +39,11 @@ struct SM64MarioGeometryBuffers typedef void (*SM64DebugPrintFunctionPtr)( const char * ); -extern void sm64_global_init( uint8_t *rom, SM64DebugPrintFunctionPtr debugPrintFunction ); -extern uint8_t *sm64_get_texture( void ); +static const size_t SM64_TEXTURE_WIDTH = 64 * 11; +static const size_t SM64_TEXTURE_HEIGHT = 64; +static const size_t SM64_GEO_BUFFER_SIZE = 1024; + +extern void sm64_global_init( uint8_t *rom, uint8_t *outTexture, SM64DebugPrintFunctionPtr debugPrintFunction ); extern void sm64_load_surfaces( uint16_t terrainType, const struct SM64Surface *surfaceArray, size_t numSurfaces ); extern void sm64_mario_reset( int16_t marioX, int16_t marioY, int16_t marioZ ); extern void sm64_mario_tick( const struct SM64MarioInputs *inputs, struct SM64MarioState *outState, struct SM64MarioGeometryBuffers *outBuffers ); diff --git a/src/load_tex_data.c b/src/load_tex_data.c index 1bfdaac..069fe7c 100644 --- a/src/load_tex_data.c +++ b/src/load_tex_data.c @@ -2,39 +2,32 @@ #include #include +#include "libsm64.h" #include "tools/libmio0.h" #include "tools/n64graphics.h" -uint8_t *gLibSm64TextureRgba; - #define MARIO_TEX_ROM_OFFSET 1132368 -#define NUM_USED_TEXTURES 11 #define ATLAS_WIDTH (NUM_USED_TEXTURES * 64) #define ATLAS_HEIGHT 64 -static int mario_tex_offsets[NUM_USED_TEXTURES] = { 144, 4240, 6288, 8336, 10384, 12432, 14480, 16528, 30864, 32912, 37008 }; -static int mario_tex_widths [NUM_USED_TEXTURES] = { 64, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }; -static int mario_tex_heights[NUM_USED_TEXTURES] = { 32, 32, 32, 32, 32, 32, 32, 32, 32, 64, 64 }; - -static void blt_image_to_atlas( rgba *img, int i, int w, int h ) +static void blt_image_to_atlas( rgba *img, int i, int w, int h, uint8_t *outTexture ) { for( int iy = 0; iy < h; ++iy ) for( int ix = 0; ix < w; ++ix ) { int o = (ix + 64 * i) + iy * ATLAS_WIDTH; int q = ix + iy * w; - gLibSm64TextureRgba[4*o + 0] = img[q].red; - gLibSm64TextureRgba[4*o + 1] = img[q].green; - gLibSm64TextureRgba[4*o + 2] = img[q].blue; - gLibSm64TextureRgba[4*o + 3] = img[q].alpha; + outTexture[4*o + 0] = img[q].red; + outTexture[4*o + 1] = img[q].green; + outTexture[4*o + 2] = img[q].blue; + outTexture[4*o + 3] = img[q].alpha; } } -void load_mario_textures_from_rom( uint8_t *rom ) +void load_mario_textures_from_rom( uint8_t *rom, uint8_t *outTexture ) { - gLibSm64TextureRgba = malloc( 4 * ATLAS_WIDTH * ATLAS_HEIGHT ); - memset( gLibSm64TextureRgba, 0, 4 * ATLAS_WIDTH * ATLAS_HEIGHT ); + memset( outTexture, 0, 4 * ATLAS_WIDTH * ATLAS_HEIGHT ); mio0_header_t head; uint8_t *in_buf = rom + MARIO_TEX_ROM_OFFSET; @@ -47,7 +40,7 @@ void load_mario_textures_from_rom( uint8_t *rom ) { uint8_t *raw = out_buf + mario_tex_offsets[i]; rgba *img = raw2rgba( raw, mario_tex_widths[i], mario_tex_heights[i], 16 ); - blt_image_to_atlas( img, i, mario_tex_widths[i], mario_tex_heights[i] ); + blt_image_to_atlas( img, i, mario_tex_widths[i], mario_tex_heights[i], outTexture ); free( img ); } diff --git a/src/load_tex_data.h b/src/load_tex_data.h index b1f3faa..f53521a 100644 --- a/src/load_tex_data.h +++ b/src/load_tex_data.h @@ -25,5 +25,10 @@ enum MarioTextures mario_texture_eyes_down }; -extern uint8_t *gLibSm64TextureRgba; -extern void load_mario_textures_from_rom( uint8_t *rom ); \ No newline at end of file +#define NUM_USED_TEXTURES 11 + +static int mario_tex_offsets[NUM_USED_TEXTURES] = { 144, 4240, 6288, 8336, 10384, 12432, 14480, 16528, 30864, 32912, 37008 }; +static int mario_tex_widths [NUM_USED_TEXTURES] = { 64, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 }; +static int mario_tex_heights[NUM_USED_TEXTURES] = { 32, 32, 32, 32, 32, 32, 32, 32, 32, 64, 64 }; + +extern void load_mario_textures_from_rom( uint8_t *rom, uint8_t *outTexture ); \ No newline at end of file