Improve rendering by a mile!

This commit is contained in:
Lucas S. Vieira 2024-07-15 17:53:40 -03:00
parent e036a9ea6b
commit d888019af8
7 changed files with 74 additions and 104 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB

Before After
Before After

View file

@ -4,29 +4,29 @@
#include <psxgpu.h>
typedef struct {
unsigned char x;
unsigned char y;
unsigned char cols;
unsigned char rows;
unsigned short *tiles;
uint8_t x;
uint8_t y;
uint8_t cols;
uint8_t rows;
uint16_t *tiles;
} CharaFrame;
typedef struct {
char name[16];
unsigned char start;
unsigned char end;
uint8_t start;
uint8_t end;
} CharaAnim;
typedef struct {
unsigned short width;
unsigned short height;
unsigned short numframes;
unsigned short numanims;
uint16_t width;
uint16_t height;
uint16_t numframes;
uint16_t numanims;
CharaFrame *frames;
CharaAnim *anims;
unsigned short crectx, crecty;
unsigned short prectx, precty;
uint16_t crectx, crecty;
uint16_t prectx, precty;
} Chara;
Chara *load_chara(const char *filename, TIM_IMAGE *tim);

View file

@ -8,11 +8,11 @@ int RotAverageNclip4(SVECTOR *a, SVECTOR *b, SVECTOR *c, SVECTOR *d,
long *xy0, long *xy1, long *xy2, long *xy3,
int *otz);
char *file_read(const char *filename, unsigned long *length);
void load_texture(char *data, TIM_IMAGE *tim);
uint8_t *file_read(const char *filename, uint32_t *length);
void load_texture(uint8_t *data, TIM_IMAGE *tim);
char get_byte(char *bytes, unsigned long *b);
short get_short_be(char *bytes, unsigned long *b);
short get_short_le(char *bytes, unsigned long *b);
uint8_t get_byte(uint8_t *bytes, uint32_t *b);
uint16_t get_short_be(uint8_t *bytes, uint32_t *b);
uint16_t get_short_le(uint8_t *bytes, uint32_t *b);
#endif

View file

@ -7,8 +7,8 @@
Chara *
load_chara(const char *filename, TIM_IMAGE *tim)
{
char *bytes;
unsigned long b, length;
uint8_t *bytes;
uint32_t b, length;
bytes = file_read(filename, &length);
if(bytes == NULL) {
@ -25,20 +25,20 @@ load_chara(const char *filename, TIM_IMAGE *tim)
chara->numanims = get_short_be(bytes, &b);
chara->frames = (CharaFrame *)malloc(chara->numframes * sizeof(CharaFrame));
for(unsigned int i = 0; i < chara->numframes; i++) {
for(uint16_t i = 0; i < chara->numframes; i++) {
chara->frames[i].x = get_byte(bytes, &b);
chara->frames[i].y = get_byte(bytes, &b);
chara->frames[i].cols = get_byte(bytes, &b);
chara->frames[i].rows = get_byte(bytes, &b);
short numtiles = (short)chara->frames[i].cols * (short)chara->frames[i].rows;
chara->frames[i].tiles = (unsigned short *)malloc(numtiles * sizeof(unsigned short));
for(short j = 0; j < numtiles; j++) {
chara->frames[i].tiles[j] = get_short_be(bytes, &b);
uint16_t numtiles = chara->frames[i].cols * chara->frames[i].rows;
chara->frames[i].tiles = (uint16_t *)malloc(numtiles * sizeof(uint16_t));
for(uint16_t j = 0; j < numtiles; j++) {
chara->frames[i].tiles[j] = (uint16_t) get_short_be(bytes, &b);
}
}
chara->anims = (CharaAnim *)malloc(chara->numanims * sizeof(CharaAnim));
for(unsigned int i = 0; i < chara->numanims; i++) {
for(uint16_t i = 0; i < chara->numanims; i++) {
for(int j = 0; j < 15; j++) {
chara->anims[i].name[j] = get_byte(bytes, &b);
}
@ -75,70 +75,65 @@ free_chara(Chara *chara)
#include <psxgpu.h>
#include "render.h"
#include <inline_c.h>
#define ADVANCE_TIMER 30
#define ADVANCE_TIMER 15
static short sx = 50, sy = 50;
static short advance = ADVANCE_TIMER;
static short framenum = 0;
/* static SVECTOR c_rot = { 0 }; */
/* static VECTOR c_pos = { 50, 50, 0 }; */
/* static VECTOR c_scale = { ONE, ONE, ONE }; */
/* static MATRIX c_world = { 0 }; */
void
chara_render_test(Chara *chara)
{
int changed = 0;
advance--;
if(advance < 0) {
advance = ADVANCE_TIMER;
framenum = (framenum + 1) % chara->numframes;
changed = 1;
}
/* RotMatrix(&c_rot, &c_world); */
/* TransMatrix(&c_world, &c_pos); */
/* ScaleMatrix(&c_world, &c_scale); */
/* gte_SetRotMatrix(&c_world); */
/* gte_SetTransMatrix(&c_world); */
uint16_t SEVEN = ONE * 7;
CharaFrame *frame = &chara->frames[framenum];
for(unsigned short row = 0; row < frame->rows; row++) {
for(unsigned short col = 0; col < frame->cols; col++) {
unsigned short idx = (row * frame->cols) + col;
for(uint16_t row = 0; row < frame->rows; row++) {
for(uint16_t col = 0; col < frame->cols; col++) {
uint16_t idx = (row * frame->cols) + col;
idx = frame->tiles[idx];
if(idx == 0) continue;
// Get upper left UV from frame index on tileset
unsigned char v0idx = idx >> 5; // divide by 32
unsigned char u0idx = idx - (v0idx << 5);
uint16_t v0idx = idx >> 5; // divide by 32
uint16_t u0idx = idx - (v0idx << 5);
unsigned char u0, v0;
u0 = u0idx * 8; v0 = v0idx * 8;
/* unsigned char u1, v1, u2, v2, u3, v3; */
/* u1 = u0 + 7; v1 = v0; */
/* u2 = u0; v2 = v0 + 7; */
/* u3 = u0 + 7; v3 = v0 + 7; */
/*printf("%d => uv0 = (%d %d), uv1 = (%d %d), uv2 = (%d %d), uv3 = (%d %d)\n",
idx, u0, v0, u1, v1, u2, v2, u3, v3);*/
uint8_t u0, v0;
u0 = (char)u0idx * 8; v0 = (char)v0idx * 8;
short x0 = sx + (col * 7) + frame->x;
short y0 = sy + (row * 7) + frame->y;
uint16_t x0 = sx + (col * 8) + frame->x;
uint16_t y0 = sy + (row * 8) + frame->y;
SPRT_8 *sprt = (SPRT_8 *) get_next_prim();
increment_prim(sizeof(SPRT_8));
SPRT *sprt = (SPRT *) get_next_prim();
increment_prim(sizeof(SPRT));
setSprt8(sprt);
setXY0(sprt, x0, y0);
setWH(sprt, SEVEN, SEVEN);
setRGB0(sprt, 128, 128, 128);
setUV0(sprt, u0, v0);
setClut(sprt, chara->crectx, chara->crecty);
sort_prim(sprt, 1);
/* POLY_FT4 *poly = (POLY_FT4 *) get_next_prim(); */
/* increment_prim(sizeof(POLY_FT4)); */
/* setPolyFT4(poly); */
/* setRGB0(poly, 128, 128, 128); */
/* poly->tpage = getTPage(0, 0, chara->prectx, chara->precty); */
/* setClut(poly, chara->crectx, chara->crecty); */
/* setUV4(poly, u0, v0, u1, v1, u2, v2, u3, v3); */
/* setXY4(poly, */
/* x0, y0, */
/* x0 + 7, y0, */
/* x0, y0 + 7, */
/* x0 + 7, y0 + 7); */
/* sort_prim(poly, 1); */
}
}

View file

@ -52,9 +52,9 @@ engine_init()
set_clear_color(63, 0, 127);
CdInit();
unsigned long filelength;
uint32_t filelength;
TIM_IMAGE tim;
char *timfile = file_read("\\SPRITES\\SONIC.TIM;1", &filelength);
uint8_t *timfile = file_read("\\SPRITES\\SONIC.TIM;1", &filelength);
if(timfile) {
load_texture(timfile, &tim);
free(timfile);

View file

@ -31,12 +31,12 @@ exit:
}
char *
file_read(const char *filename, unsigned long *length)
uint8_t *
file_read(const char *filename, uint32_t *length)
{
CdlFILE filepos;
int numsectors;
char *buffer;
uint8_t *buffer;
buffer = NULL;
if(CdSearchFile(&filepos, filename) == NULL) {
@ -45,13 +45,13 @@ file_read(const char *filename, unsigned long *length)
}
numsectors = (filepos.size + 2047) / 2048;
buffer = (char *) malloc(2048 * numsectors);
buffer = (uint8_t *) malloc(2048 * numsectors);
if(!buffer) {
printf("Error allocating %d sectors.\n", numsectors);
return NULL;
}
CdControl(CdlSetloc, (unsigned char *) &filepos.pos, 0);
CdControl(CdlSetloc, (uint8_t *) &filepos.pos, 0);
CdRead(numsectors, (uint32_t *) buffer, CdlModeSpeed);
CdReadSync(0, 0);
@ -60,7 +60,7 @@ file_read(const char *filename, unsigned long *length)
}
void
load_texture(char *data, TIM_IMAGE *tim)
load_texture(uint8_t *data, TIM_IMAGE *tim)
{
GetTimInfo((const uint32_t *)data, tim);
LoadImage(tim->prect, tim->paddr);
@ -70,26 +70,26 @@ load_texture(char *data, TIM_IMAGE *tim)
}
char
get_byte(char *bytes, unsigned long *b)
uint8_t
get_byte(uint8_t *bytes, uint32_t *b)
{
return (char) bytes[(*b)++];
return (uint8_t) bytes[(*b)++];
}
short
get_short_be(char *bytes, unsigned long *b)
uint16_t
get_short_be(uint8_t *bytes, uint32_t *b)
{
unsigned short value = 0;
uint16_t value = 0;
value |= bytes[(*b)++] << 8;
value |= bytes[(*b)++];
return (short) value;
return value;
}
short
get_short_le(char *bytes, unsigned long *b)
uint16_t
get_short_le(uint8_t *bytes, uint32_t *b)
{
unsigned short value = 0;
uint16_t value = 0;
value |= bytes[(*b)++];
value |= bytes[(*b)++] << 8;
return (short) value;
return value;
}

View file

@ -82,7 +82,6 @@ def parse_layout(j):
"num_animations": len(animations),
"animations": animations,
}
# pprint(res)
return res
@ -102,29 +101,6 @@ def parse_layout(j):
# 6.1. Name: 16 bytes.
# 6.2. Frame start: byte (8 bits)
# 6.3. Frame end: byte (8 bits)
# def print_binary_layout(layout):
# print("# header")
# print("spritewidth:", c_ushort(layout.get("sprite_width")))
# print("spriteheight:", c_ushort(layout.get("sprite_height")))
# print("numframes:", c_ushort(layout.get("num_frames")))
# print("numanims:", c_ushort(layout.get("num_animations")))
# print("# frame data")
# for i, frame in enumerate(layout.get("frames")):
# print(f"## frame {i}")
# print("x:", c_ubyte(frame.get("x")))
# print("y:", c_ubyte(frame.get("y")))
# print("cols:", c_ubyte(frame.get("cols")))
# print("rows:", c_ubyte(frame.get("rows")))
# tiles = [c_ushort(t) for t in frame.get("tiles")]
# print("tiles:", tiles)
# for anim in layout.get("animations"):
# name = [c_ubyte(0 if x == " " else ord(x)) for x in list(anim.get("name"))]
# print("name:", name)
# print("start:", c_ubyte(anim.get("start")))
# print("end:", c_ubyte(anim.get("end")))
def write_binary_data(layout, f):
f.write(c_ushort(layout.get("sprite_width")))
f.write(c_ushort(layout.get("sprite_height")))
@ -150,7 +126,6 @@ def main():
outfile = sys.argv[2]
j = load_json(jsonfile)
l = parse_layout(j)
# print_binary_layout(l)
with open(outfile, "wb") as f:
write_binary_data(l, f)