Compare commits

...

10 commits

Author SHA1 Message Date
Crayon2000
507186821c Add comments 2024-08-15 22:13:56 -04:00
Crayon2000
3bb5817f33 Add comments 2024-08-15 22:13:55 -04:00
Crayon2000
7d08f7eed5 Allow different texture format 2024-08-15 22:13:55 -04:00
Crayon2000
6795c0b1ce Remove useless loop 2024-08-15 22:13:54 -04:00
Crayon2000
d65a16765b Add id parameter to GRRLIB_LoadTextureTPL 2024-08-15 22:13:54 -04:00
Crayon2000
d67e646e67 Use less memory allocation 2024-08-15 22:13:53 -04:00
Crayon2000
fee01d1f4c Align TPL code with GRRLIB 2024-08-15 22:13:53 -04:00
Crayon2000
efdbfedb30 First limited TPL implementation 2024-08-15 22:13:52 -04:00
Crayon2000
d93847e6a3 Use short arguments 2024-04-18 01:07:29 -04:00
Crayon2000
2416530613 Use short arguments 2024-04-18 00:20:03 -04:00
12 changed files with 135 additions and 12 deletions

View file

@ -28,6 +28,64 @@ THE SOFTWARE.
#include <grrlib.h> #include <grrlib.h>
#define TPL_HDR_VERSION_FIELD 0 /**< Version field. */
#define TPL_HDR_NTEXTURE_FIELD 4 /**< Textrure 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). * 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); 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.
* @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;
my_texture->freedata = true;
GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture );
return my_texture;
}
/** /**
* Load a texture from a buffer. * Load a texture from a buffer.
* @param my_img The JPEG, PNG or Bitmap buffer to load. * @param my_img The JPEG, PNG or Bitmap buffer to load.

View file

@ -34,7 +34,8 @@ BUILD := build
SOURCES := . SOURCES := .
INCLUDES := . ../lib/pngu INCLUDES := . ../lib/pngu
HDR := $(TARGET).h $(wildcard $(TARGET)/*.h) HDR := $(TARGET).h
INL := $(wildcard $(TARGET)/*.h)
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# options for code generation # options for code generation
@ -94,9 +95,10 @@ clean:
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
install: install:
@mkdir --parents $(DESTDIR)/lib $(DESTDIR)/include @mkdir -p $(DESTDIR)/lib $(DESTDIR)/include/grrlib
@cp --force $(OUTPUT) $(DESTDIR)/lib @cp -f $(OUTPUT) $(DESTDIR)/lib
@cp --force --parents $(HDR) $(DESTDIR)/include @cp -f $(HDR) $(DESTDIR)/include
@cp -f $(INL) $(DESTDIR)/include/grrlib
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
else else

View file

@ -123,6 +123,7 @@ typedef struct GRRLIB_texImg {
f32 ofnormaltexy;/**< Offset of normalized texture on y. */ f32 ofnormaltexy;/**< Offset of normalized texture on y. */
void *data; /**< Pointer to the texture data. */ void *data; /**< Pointer to the texture data. */
bool freedata; /**< Should the data be freed. */
} GRRLIB_texImg; } GRRLIB_texImg;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View file

@ -142,6 +142,7 @@ GRRLIB_texImg* GRRLIB_LoadTexturePNG (const u8 *my_png);
GRRLIB_texImg* GRRLIB_LoadTextureJPG (const u8 *my_jpg); GRRLIB_texImg* GRRLIB_LoadTextureJPG (const u8 *my_jpg);
GRRLIB_texImg* GRRLIB_LoadTextureJPGEx (const u8 *my_jpg, const u32 my_size); GRRLIB_texImg* GRRLIB_LoadTextureJPGEx (const u8 *my_jpg, const u32 my_size);
GRRLIB_texImg* GRRLIB_LoadTextureBMP (const u8 *my_bmp); 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 // GRRLIB_gecko.c - USB_Gecko output facilities

View file

@ -49,7 +49,7 @@ void GRRLIB_FreeTexture (GRRLIB_texImg *tex) {
if(tex == NULL) { if(tex == NULL) {
return; return;
} }
if (tex->data != NULL) { if (tex->data != NULL && tex->freedata == true) {
free(tex->data); free(tex->data);
} }
free(tex); free(tex);
@ -61,7 +61,7 @@ void GRRLIB_FreeTexture (GRRLIB_texImg *tex) {
*/ */
INLINE INLINE
void GRRLIB_ClearTex(GRRLIB_texImg* tex) { void GRRLIB_ClearTex(GRRLIB_texImg* tex) {
if(tex == NULL) { if(tex == NULL || tex->freedata == false) {
return; return;
} }
memset(tex->data, 0, GX_GetTexBufferSize(tex->w, tex->h, tex->format, 0, 0)); memset(tex->data, 0, GX_GetTexBufferSize(tex->w, tex->h, tex->format, 0, 0));

View file

@ -86,9 +86,9 @@ clean:
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
install: install:
@mkdir --parents $(DESTDIR)/lib $(DESTDIR)/include @mkdir -p $(DESTDIR)/lib $(DESTDIR)/include
@cp --force $(OUTPUT) $(DESTDIR)/lib @cp -f $(OUTPUT) $(DESTDIR)/lib
@cp --force --parents $(HDR) $(DESTDIR)/include @cp -f $(HDR) $(DESTDIR)/include
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
else else

View file

@ -19,6 +19,7 @@ TARGET := $(notdir $(CURDIR))
BUILD := build BUILD := build
SOURCES := source SOURCES := source
DATA := data DATA := data
TEXTURES := textures
INCLUDES := INCLUDES :=
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
@ -51,7 +52,8 @@ ifneq ($(BUILD),$(notdir $(CURDIR)))
export OUTPUT := $(CURDIR)/$(TARGET) export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 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) export DEPSDIR := $(CURDIR)/$(BUILD)
@ -63,6 +65,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)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 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 # use CXX for linking C++ projects, CC for standard C
@ -73,11 +77,11 @@ else
export LD := $(CXX) export LD := $(CXX)
endif 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_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) 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 # build a list of include paths
@ -155,6 +159,12 @@ $(OFILES_SOURCES) : $(HFILES)
@echo $(notdir $<) @echo $(notdir $<)
$(bin2o) $(bin2o)
#---------------------------------------------------------------------------------
%.tpl.o %_tpl.h : %.tpl
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS) -include $(DEPENDS)
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------

View file

@ -22,6 +22,9 @@
#include "ocean_bmf.h" #include "ocean_bmf.h"
#include "frontal_bmf.h" #include "frontal_bmf.h"
#include "textures_tpl.h"
#include "textures.h"
// Tile stuff // Tile stuff
#define TILE_DELAY 10 #define TILE_DELAY 10
#define TILE_UP 12 * 0 #define TILE_UP 12 * 0
@ -91,6 +94,10 @@ int main() {
GRRLIB_texImg *tex_BMfont5 = GRRLIB_LoadTexture(BMfont5_png); GRRLIB_texImg *tex_BMfont5 = GRRLIB_LoadTexture(BMfont5_png);
GRRLIB_InitTileSet(tex_BMfont5, 8, 16, 0); GRRLIB_InitTileSet(tex_BMfont5, 8, 16, 0);
GRRLIB_texImg *tex_test_tpl1 = GRRLIB_LoadTextureTPL(textures_tpl, textures_tpl_size, ballsprites);
GRRLIB_texImg *tex_test_tpl2 = GRRLIB_LoadTextureTPL(textures_tpl, textures_tpl_size, pirate);
GRRLIB_texImg *tex_test_tpl3 = GRRLIB_LoadTextureTPL(textures_tpl, textures_tpl_size, girl);
while(1) { while(1) {
WPAD_SetVRes(0, 640, 480); WPAD_SetVRes(0, 640, 480);
WPAD_ScanPads(); WPAD_ScanPads();
@ -109,6 +116,9 @@ int main() {
GRRLIB_DrawImg(10, 50, tex_test_jpg, 0, 1, 1, GRRLIB_WHITE); // Draw a jpeg GRRLIB_DrawImg(10, 50, tex_test_jpg, 0, 1, 1, GRRLIB_WHITE); // Draw a jpeg
GRRLIB_DrawImg(350, 50, tex_test_bmp, 0, 4, 4, GRRLIB_WHITE); // Draw a bitmap GRRLIB_DrawImg(350, 50, tex_test_bmp, 0, 4, 4, GRRLIB_WHITE); // Draw a bitmap
GRRLIB_DrawImg(400, 200, tex_test_tpl1, 0, 2, 2, GRRLIB_WHITE); // Draw a TPL
GRRLIB_DrawImg(100, 200, tex_test_tpl3, 0, 1, 1, GRRLIB_WHITE); // Draw a TPL
// Draw a sprite // Draw a sprite
GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee
GRRLIB_DrawTile(320+left, 240+top, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, frame); GRRLIB_DrawTile(320+left, 240+top, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, frame);

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View file

@ -0,0 +1,3 @@
<filepath="ballsprites.png" id="ballsprites" colfmt=6 />
<filepath="pirate.png" id="pirate" colfmt=6 />
<filepath="girl.png" id="girl" colfmt=5 />