Use static memory when allocating most character data

This commit is contained in:
Lucas S. Vieira 2024-07-21 13:10:51 -03:00
parent 8a131be368
commit bb29596a0d
3 changed files with 8 additions and 11 deletions

View file

@ -31,7 +31,7 @@ typedef struct {
uint16_t prectx, precty;
} Chara;
Chara *load_chara(const char *filename, TIM_IMAGE *tim);
void load_chara(Chara *chara, const char *filename, TIM_IMAGE *tim);
void free_chara(Chara *chara);
void chara_render_frame(Chara *chara, int16_t framenum,

View file

@ -6,8 +6,8 @@
#include <stdlib.h>
#include <stdio.h>
Chara *
load_chara(const char *filename, TIM_IMAGE *tim)
void
load_chara(Chara *chara, const char *filename, TIM_IMAGE *tim)
{
uint8_t *bytes;
uint32_t b, length;
@ -15,12 +15,11 @@ load_chara(const char *filename, TIM_IMAGE *tim)
bytes = file_read(filename, &length);
if(bytes == NULL) {
printf("Error reading %s from the CD.\n", bytes);
return NULL;
return;
}
b = 0;
Chara *chara = (Chara *)malloc(sizeof(Chara));
chara->width = get_short_be(bytes, &b);
chara->height = get_short_be(bytes, &b);
chara->numframes = get_short_be(bytes, &b);
@ -71,7 +70,6 @@ load_chara(const char *filename, TIM_IMAGE *tim)
chara->prectx = 320;
chara->precty = 0; // why not loading correctly???
return chara;
free(bytes);
}
@ -84,7 +82,6 @@ free_chara(Chara *chara)
}
free(chara->frames);
free(chara->anims);
free(chara);
}
}

View file

@ -45,7 +45,7 @@ static VECTOR pos = { 0, 0, 450 };
static VECTOR scale = { ONE, ONE, ONE };
static MATRIX world = { 0 };
static Chara *sonic_chara = NULL;
static Chara sonic_chara;
#define MUSIC_NUM_CHANNELS 2
static uint8_t music_channel = 0;
@ -67,11 +67,11 @@ engine_init()
free(timfile);
}
sonic_chara = load_chara("\\SPRITES\\SONIC.CHARA;1", &tim);
load_chara(&sonic_chara, "\\SPRITES\\SONIC.CHARA;1", &tim);
// Start playback after we don't need the CD anymore.
sound_play_xa("\\AUDIO\\BGM001.XA;1", 0, music_channel);
sound_play_xa("\\AUDIO\\BGM001.XA;1", 0, music_channel, 7100);
}
void
@ -99,7 +99,7 @@ engine_update()
void
engine_draw()
{
chara_render_test(sonic_chara);
chara_render_test(&sonic_chara);
// Gouraud-shaded SQUARE
POLY_G4 *poly = (POLY_G4 *) get_next_prim();