diff --git a/CHANGELOG.md b/CHANGELOG.md index 47ea89e..fc9ba60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased][] - Allow compilation and installation with cmake. +- Added `GRRLIB_LoadTextureTPL` to load TPL data (Texture Palette Library) into a `GRRLIB_texImg`. - Added `GRRLIB_CreateEmptyTextureFmt()` to create an empty texture with a given format. ## [4.5.1][] - 2024-03-02 diff --git a/GRRLIB/GRRLIB/GRRLIB_texEdit.c b/GRRLIB/GRRLIB/GRRLIB_texEdit.c index a4c6892..de6049d 100644 --- a/GRRLIB/GRRLIB/GRRLIB_texEdit.c +++ b/GRRLIB/GRRLIB/GRRLIB_texEdit.c @@ -28,6 +28,64 @@ THE SOFTWARE. #include +#define TPL_HDR_VERSION_FIELD 0 /**< Version field. */ +#define TPL_HDR_NTEXTURE_FIELD 4 /**< Texture field. */ +#define TPL_HDR_HDRSIZE_FIELD 8 /**< Header size field. */ +#define TPL_HDR_DESCR_FIELD 12 /**< Descriptor field. */ + +/** + * Texture header. + */ +typedef struct _tplimgheader TPLImgHeader; + +/** + * Texture header. + */ +struct _tplimgheader { + u16 height; /**< Height of the texture in pixels. */ + u16 width; /**< Width of the texture in pixels. */ + u32 fmt; /**< Format of the texture. */ + void *data; /**< Pointer to the texture data. */ + u32 wraps; /**< Texture coordinate wrapping strategy in the S direction. */ + u32 wrapt; /**< Texture coordinate wrapping strategy in the T direction. */ + u32 minfilter; /**< Filter mode when the texel/pixel ratio is ≤ 1.0. */ + u32 magfilter; /**< Filter mode when the texel/pixel ratio is > 1.0. */ + f32 lodbias; /**< LOD bias. */ + u8 edgelod; /**< Edge LOD. */ + u8 minlod; /**< Minimum LOD value. */ + u8 maxlod; /**< Maximum LOD value. */ + u8 unpacked; /**< Internal flag. */ +} ATTRIBUTE_PACKED; + +/** + * Texture palette header. + */ +typedef struct _tplpalheader TPLPalHeader; + +/** + * Texture palette header. + */ +struct _tplpalheader { + u16 nitems; /**< Number of palette entries. */ + u8 unpacked; /**< Internal flag. */ + u8 pad; /**< Padding. */ + u32 fmt; /**< Format of the color lookup table (CLUT). */ + void *data; /**< Pointer to the color lookup table (CLUT) data. */ +} ATTRIBUTE_PACKED; + +/** + * Texture descriptor. + */ +typedef struct _tpldesc TPLDescHeader; + +/** + * Texture descriptor. + */ +struct _tpldesc { + TPLImgHeader *imghead; /**< Pointer to texture header structure. */ + TPLPalHeader *palhead; /**< Pointer to color lookup table (CLUT) header. */ +} ATTRIBUTE_PACKED; + /** * This structure contains information about the type, size, and layout of a file that containing a device-independent bitmap (DIB). */ @@ -143,6 +201,44 @@ GRRLIB_texImg* GRRLIB_CreateEmptyTexture (const u32 width, const u32 height) return GRRLIB_CreateEmptyTextureFmt(width, height, GX_TF_RGBA8); } +/** + * Set TPL data to a GRRLIB_texImg structure. + * @param my_tpl The TPL buffer to set. + * @param size Size of the TPL buffer to set. + * @param id ID of the TPL buffer to set. + * @return A GRRLIB_texImg structure filled with image information. + */ +GRRLIB_texImg* GRRLIB_LoadTextureTPL (const u8 *my_tpl, const int size, u32 id) { + if(my_tpl == NULL || size <= 0) { + return NULL; + } + + const u32 ntextures = *(u32*)(my_tpl + TPL_HDR_NTEXTURE_FIELD); + if(id > ntextures) { + return NULL; + } + + GRRLIB_texImg *my_texture = calloc(1, sizeof(GRRLIB_texImg)); + if (my_texture == NULL) { + return NULL; + } + + const TPLDescHeader *deschead = (TPLDescHeader*)(my_tpl + TPL_HDR_DESCR_FIELD); + + u32 pos = (u32)deschead[id].imghead; + const TPLImgHeader *imghead = (TPLImgHeader*)(my_tpl + pos); + + pos = (u32)imghead->data; + my_texture->data = (u8*)(my_tpl + pos); + my_texture->w = imghead->width; + my_texture->h = imghead->height; + my_texture->format = imghead->fmt; + GRRLIB_SetHandle( my_texture, 0, 0 ); + GRRLIB_FlushTex( my_texture ); + + return my_texture; +} + /** * Load a texture from a buffer. * @param my_img The JPEG, PNG or Bitmap buffer to load. diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index d79a6aa..874fd30 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -142,6 +142,7 @@ GRRLIB_texImg* GRRLIB_LoadTexturePNG (const u8 *my_png); GRRLIB_texImg* GRRLIB_LoadTextureJPG (const u8 *my_jpg); GRRLIB_texImg* GRRLIB_LoadTextureJPGEx (const u8 *my_jpg, const u32 my_size); GRRLIB_texImg* GRRLIB_LoadTextureBMP (const u8 *my_bmp); +GRRLIB_texImg* GRRLIB_LoadTextureTPL (const u8 *my_tpl, const int size, u32 id); //------------------------------------------------------------------------------ // GRRLIB_gecko.c - USB_Gecko output facilities diff --git a/examples/gamecube/template/Makefile b/examples/gamecube/template/Makefile index eb145d8..8b383ad 100644 --- a/examples/gamecube/template/Makefile +++ b/examples/gamecube/template/Makefile @@ -19,6 +19,7 @@ TARGET := $(notdir $(CURDIR)) BUILD := build SOURCES := source DATA := data +TEXTURES := textures INCLUDES := #--------------------------------------------------------------------------------- @@ -54,7 +55,8 @@ ifneq ($(BUILD),$(notdir $(CURDIR))) export OUTPUT := $(CURDIR)/$(TARGET) export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ - $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ + $(foreach dir,$(TEXTURES),$(CURDIR)/$(dir)) export DEPSDIR := $(CURDIR)/$(BUILD) @@ -66,6 +68,8 @@ CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) +SCFFILES := $(foreach dir,$(TEXTURES),$(notdir $(wildcard $(dir)/*.scf))) +TPLFILES := $(SCFFILES:.scf=.tpl) #--------------------------------------------------------------------------------- # use CXX for linking C++ projects, CC for standard C @@ -76,11 +80,11 @@ else export LD := $(CXX) endif -export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) $(addsuffix .o,$(TPLFILES)) export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o) export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) -export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) +export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) $(addsuffix .h,$(subst .,_,$(TPLFILES))) #--------------------------------------------------------------------------------- # build a list of include paths @@ -142,6 +146,14 @@ $(OFILES_SOURCES) : $(HFILES) @echo $(notdir $<) $(bin2o) +#--------------------------------------------------------------------------------- +# This rule links in binary data with the .tpl extension +#--------------------------------------------------------------------------------- +%.tpl.o %_tpl.h : %.tpl +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + -include $(DEPENDS) #--------------------------------------------------------------------------------- diff --git a/examples/template/Makefile b/examples/template/Makefile index c62800a..45bfa8a 100644 --- a/examples/template/Makefile +++ b/examples/template/Makefile @@ -19,6 +19,7 @@ TARGET := $(notdir $(CURDIR)) BUILD := build SOURCES := source DATA := data +TEXTURES := textures INCLUDES := #--------------------------------------------------------------------------------- @@ -55,7 +56,8 @@ ifneq ($(BUILD),$(notdir $(CURDIR))) export OUTPUT := $(CURDIR)/$(TARGET) export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ - $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ + $(foreach dir,$(TEXTURES),$(CURDIR)/$(dir)) export DEPSDIR := $(CURDIR)/$(BUILD) @@ -67,6 +69,8 @@ CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) +SCFFILES := $(foreach dir,$(TEXTURES),$(notdir $(wildcard $(dir)/*.scf))) +TPLFILES := $(SCFFILES:.scf=.tpl) #--------------------------------------------------------------------------------- # use CXX for linking C++ projects, CC for standard C @@ -77,11 +81,11 @@ else export LD := $(CXX) endif -export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) $(addsuffix .o,$(TPLFILES)) export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o) export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) -export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) +export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) $(addsuffix .h,$(subst .,_,$(TPLFILES))) #--------------------------------------------------------------------------------- # build a list of include paths @@ -143,6 +147,14 @@ $(OFILES_SOURCES) : $(HFILES) @echo $(notdir $<) $(bin2o) +#--------------------------------------------------------------------------------- +# This rule links in binary data with the .tpl extension +#--------------------------------------------------------------------------------- +%.tpl.o %_tpl.h : %.tpl +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + -include $(DEPENDS) #---------------------------------------------------------------------------------