Automatic VRAM tile allocation functions
Posted: Fri Nov 16, 2012 11:16 pm
Here is a little code I've been using for a short time. Haven't tested it a lot but I feel it *works*. Title pretends to be self explanatory; it looks for room in the VRAM table and returns a position. That simple.
Works with SGDK and hope it'll be useful for someone (newbies like me, I'm talking to you). Use it on your own way.
Works with SGDK and hope it'll be useful for someone (newbies like me, I'm talking to you). Use it on your own way.
Code: Select all
#include <genesis.h>
typedef struct _vram
{
u32 pos;
u32 tiles;
struct _vram *next;
}
VRAM;
static VRAM *vram;
void vram_init ( )
{
VRAM *aux;
while ( vram != NULL )
{
aux = vram->next;
MEM_free ( vram );
vram = aux;
}
vram = NULL;
}
u16 vram_new ( u16 nb_tiles )
{
u16 pos = 1; // VRAM_BASE_POS;
VRAM *repeat = vram;
VRAM *new = MEM_alloc ( sizeof ( VRAM ) );
VRAM *next = NULL;
if ( !repeat )
{
vram = new;
}
while ( repeat )
{
pos = repeat->pos + repeat->tiles;
next = repeat->next;
if ( next->pos > pos + nb_tiles )
{
break;
}
repeat = next;
}
new->pos = pos;
new->tiles = nb_tiles;
new->next = next;
repeat->next = new;
return pos;
}
void vram_delete ( u16 pos )
{
VRAM *repeat = vram;
VRAM *prev = NULL;
while ( repeat )
{
if ( repeat->pos == pos )
{
prev->next = repeat->next;
MEM_free ( repeat );
return;
}
prev = repeat;
repeat = repeat->next;
}
}
void vram_sample ( void )
{
vram_init();
vram_new(7);
u16 x = vram_new(20);
vram_new(5);
vram_delete ( x );
u16 y = vram_new(5);
vram_new(1);
vram_new(8);
vram_delete ( y );
vram_new(3);
vram_info();
}
void vram_info ()
{
VRAM *aux = vram;
u8 i = 3;
u8 str[10];
VDP_drawText ( " Nb Pos Tiles", 1, 0 );
VDP_drawText ( "--- ---- -----", 1, 1 );
while ( aux )
{
uintToStr(i-2, str, 1);
VDP_drawText(str, 1, i);
uintToStr(aux->pos, str, 1);
VDP_drawText(str, 5, i);
uintToStr(aux->tiles, str, 1);
VDP_drawText(str, 11, i);
aux = aux->next;
i++;
}
}