Optimize rendering and add faster fixed-step walking
This commit is contained in:
1 parent
3f9244962b
commit
6f9e6b5ca5
3 files changed
+578
-33
No files matched your search
+249
-33
@@ -38,6 +38,29 @@ static void mark_dirty(int x, int y, int w, int h)
|
||||
}
|
||||
|
||||
enum { A = 1, B = 2, SELECT = 4, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN = 128 };
|
||||
enum { MOVEMENT_HZ = 60, MOVEMENT_CLOCK_HZ = 16384, WALK_SPEED = 2, MAX_MOVEMENT_TICKS = 5 };
|
||||
static struct { unsigned last, fraction; } movement_clock;
|
||||
static unsigned diagonal_fraction;
|
||||
|
||||
static void pause_movement_at(unsigned now)
|
||||
{
|
||||
movement_clock.last = now;
|
||||
movement_clock.fraction = 0;
|
||||
diagonal_fraction = 0;
|
||||
}
|
||||
|
||||
/* Fixed 60 Hz simulation, independent of how often a frame is drawn. */
|
||||
static int movement_ticks_at(unsigned now)
|
||||
{
|
||||
unsigned elapsed = now - movement_clock.last;
|
||||
movement_clock.last = now;
|
||||
if (elapsed > MOVEMENT_CLOCK_HZ / 4) elapsed = MOVEMENT_CLOCK_HZ / 4;
|
||||
unsigned total = movement_clock.fraction + elapsed * MOVEMENT_HZ;
|
||||
unsigned ticks = total / MOVEMENT_CLOCK_HZ;
|
||||
movement_clock.fraction = total % MOVEMENT_CLOCK_HZ;
|
||||
/* Drop excess backlog after a long stall rather than teleporting. */
|
||||
return ticks > MAX_MOVEMENT_TICKS ? MAX_MOVEMENT_TICKS : (int)ticks;
|
||||
}
|
||||
|
||||
/* These are good first things to change! Colors use numbers from 0 to 31. */
|
||||
static const u16 grass = RGB(15, 26, 12);
|
||||
@@ -47,6 +70,14 @@ enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGE
|
||||
enum Room { OUTSIDE, COTTAGE, ROUTE, MELLON_GARDEN, MELLON_HOUSE };
|
||||
static enum Room current_room = OUTSIDE;
|
||||
static int door_open = 0;
|
||||
/* One scenery-only cache; the frame buffer still holds actors and UI. */
|
||||
static u16 background_cache[WIDTH * HEIGHT];
|
||||
static enum Room background_room;
|
||||
static int background_valid, background_door_open;
|
||||
#ifdef GAME_TEST
|
||||
static unsigned scenery_samples;
|
||||
static unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
|
||||
#endif
|
||||
enum Facing { FACING_DOWN, FACING_UP, FACING_LEFT, FACING_RIGHT };
|
||||
static enum Facing player_facing = FACING_DOWN;
|
||||
enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 };
|
||||
@@ -286,6 +317,7 @@ static int use_map_exit(int *x, int *y, unsigned keys)
|
||||
}
|
||||
door_open = 0;
|
||||
grass_steps = 0;
|
||||
diagonal_fraction = 0;
|
||||
reset_follower(*x, *y);
|
||||
return 1;
|
||||
}
|
||||
@@ -319,6 +351,7 @@ static int use_door(int *x, int *y)
|
||||
door_open = 0;
|
||||
player_facing = FACING_DOWN;
|
||||
grass_steps = 0;
|
||||
diagonal_fraction = 0;
|
||||
reset_follower(*x, *y);
|
||||
return 1;
|
||||
}
|
||||
@@ -329,6 +362,7 @@ static void reset_position(int *x, int *y)
|
||||
*y = indoors() ? 119 : current_room == ROUTE ? 60 : current_room == MELLON_GARDEN ? 113 : 103;
|
||||
player_facing = FACING_DOWN;
|
||||
grass_steps = 0;
|
||||
diagonal_fraction = 0;
|
||||
reset_follower(*x, *y);
|
||||
}
|
||||
|
||||
@@ -410,6 +444,9 @@ static int advance_conversation(int page)
|
||||
|
||||
static void rectangle(int x, int y, int w, int h, u16 color)
|
||||
{
|
||||
#ifdef GAME_TEST
|
||||
++rectangle_calls;
|
||||
#endif
|
||||
mark_dirty(x, y, w, h);
|
||||
for (int row = y; row < y + h; ++row) {
|
||||
for (int col = x; col < x + w; ++col) {
|
||||
@@ -818,6 +855,9 @@ static u16 mellon_house_scenery(int x, int y)
|
||||
|
||||
static u16 scenery(int x, int y)
|
||||
{
|
||||
#ifdef GAME_TEST
|
||||
++scenery_samples;
|
||||
#endif
|
||||
if (current_room == ROUTE) return route_scenery(x, y);
|
||||
if (current_room == COTTAGE) return cottage_scenery(x, y);
|
||||
if (current_room == MELLON_GARDEN) return mellon_garden_scenery(x, y);
|
||||
@@ -825,26 +865,139 @@ static u16 scenery(int x, int y)
|
||||
return village_scenery(x, y);
|
||||
}
|
||||
|
||||
static void restore(int x, int y, int w, int h)
|
||||
static void ensure_background(void)
|
||||
{
|
||||
mark_dirty(x, y, w, h);
|
||||
for (int row = y; row < y + h; ++row)
|
||||
for (int col = x; col < x + w; ++col)
|
||||
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
|
||||
SCREEN[row * WIDTH + col] = scenery(col, row);
|
||||
if (!background_valid || background_room != current_room) {
|
||||
for (int row = 0; row < HEIGHT; ++row)
|
||||
for (int col = 0; col < WIDTH; ++col)
|
||||
background_cache[row * WIDTH + col] = scenery(col, row);
|
||||
background_room = current_room;
|
||||
background_door_open = door_open;
|
||||
background_valid = 1;
|
||||
} else if (background_door_open != door_open) {
|
||||
/* Only these door pixels change; keep the rest of the map cached. */
|
||||
if (current_room == OUTSIDE || current_room == MELLON_GARDEN)
|
||||
for (int row = 54; row < 72; ++row)
|
||||
for (int col = 171; col < 184; ++col)
|
||||
background_cache[row * WIDTH + col] = scenery(col, row);
|
||||
background_door_open = door_open;
|
||||
}
|
||||
}
|
||||
|
||||
/* Transparent pixel art. Each character selects a color in the palette. */
|
||||
static void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
|
||||
int mirrored)
|
||||
static void restore(int x, int y, int w, int h)
|
||||
{
|
||||
for (int row = 0; row < rows; ++row)
|
||||
for (int col = 0; art[row][col]; ++col)
|
||||
int left = x < 0 ? 0 : x;
|
||||
int right = x + w > WIDTH ? WIDTH : x + w;
|
||||
int top = y < 0 ? 0 : y;
|
||||
int bottom = y + h > HEIGHT ? HEIGHT : y + h;
|
||||
if (left >= right || top >= bottom) return;
|
||||
ensure_background();
|
||||
mark_dirty(left, top, right - left, bottom - top);
|
||||
for (int row = top; row < bottom; ++row) {
|
||||
const u16 *source = &background_cache[row * WIDTH + left];
|
||||
u16 *destination = &SCREEN[row * WIDTH + left];
|
||||
for (int col = 0; col < right - left; ++col) destination[col] = source[col];
|
||||
}
|
||||
}
|
||||
|
||||
enum { SPRITE_SIZE = 24, SPRITE_CACHE_SIZE = 7 };
|
||||
struct DecodedSprite {
|
||||
const char *const *art;
|
||||
unsigned char width, rows;
|
||||
unsigned char first[SPRITE_SIZE], end[SPRITE_SIZE];
|
||||
unsigned char pixels[SPRITE_SIZE * SPRITE_SIZE];
|
||||
};
|
||||
static struct DecodedSprite sprite_cache[SPRITE_CACHE_SIZE];
|
||||
static unsigned sprite_cache_next;
|
||||
|
||||
static const struct DecodedSprite *decode_sprite(const char *const *art, int width, int rows)
|
||||
{
|
||||
for (int i = 0; i < SPRITE_CACHE_SIZE; ++i)
|
||||
if (sprite_cache[i].art == art && sprite_cache[i].width == width &&
|
||||
sprite_cache[i].rows == rows) return &sprite_cache[i];
|
||||
struct DecodedSprite *decoded = &sprite_cache[sprite_cache_next];
|
||||
sprite_cache_next = (sprite_cache_next + 1) % SPRITE_CACHE_SIZE;
|
||||
decoded->art = art;
|
||||
decoded->width = (unsigned char)width;
|
||||
decoded->rows = (unsigned char)rows;
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
decoded->first[row] = (unsigned char)width;
|
||||
decoded->end[row] = 0;
|
||||
for (int col = 0; col < width; ++col) {
|
||||
unsigned char encoded = 0;
|
||||
if (art[row][col] != '.') {
|
||||
char pixel = art[row][col];
|
||||
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
|
||||
rectangle(x + (mirrored ? 15 - col : col), y + row, 1, 1, palette[index]);
|
||||
/* Zero means transparent; palette entry zero is still usable. */
|
||||
encoded = (unsigned char)(index + 1);
|
||||
if (decoded->first[row] == width) decoded->first[row] = (unsigned char)col;
|
||||
decoded->end[row] = (unsigned char)(col + 1);
|
||||
}
|
||||
decoded->pixels[row * SPRITE_SIZE + col] = encoded;
|
||||
}
|
||||
}
|
||||
#ifdef GAME_TEST
|
||||
++sprite_decodes;
|
||||
#endif
|
||||
return decoded;
|
||||
}
|
||||
|
||||
/* Decode once, then write clipped rows directly into the RAM frame buffer. */
|
||||
static void blit_sprite(int x, int y, const char *const *art, int width, int rows,
|
||||
const u16 *palette, int scale, int mirrored)
|
||||
{
|
||||
if (width <= 0 || width > SPRITE_SIZE || rows <= 0 || rows > SPRITE_SIZE || scale <= 0)
|
||||
return;
|
||||
if (x >= WIDTH || y >= HEIGHT || x + width * scale <= 0 || y + rows * scale <= 0) return;
|
||||
const struct DecodedSprite *decoded = decode_sprite(art, width, rows);
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
int first = decoded->first[row], end = decoded->end[row];
|
||||
if (first >= end) continue;
|
||||
int top = y + row * scale, bottom = top + scale;
|
||||
if (bottom <= 0 || top >= HEIGHT) continue;
|
||||
int left = x + (mirrored ? width - end : first) * scale;
|
||||
int right = x + (mirrored ? width - first : end) * scale;
|
||||
if (left < 0) left = 0;
|
||||
if (right > WIDTH) right = WIDTH;
|
||||
if (top < 0) top = 0;
|
||||
if (bottom > HEIGHT) bottom = HEIGHT;
|
||||
if (left >= right) continue;
|
||||
mark_dirty(left, top, right - left, bottom - top);
|
||||
#ifdef GAME_TEST
|
||||
++sprite_row_marks;
|
||||
#endif
|
||||
const unsigned char *source = &decoded->pixels[row * SPRITE_SIZE];
|
||||
if (scale == 1) {
|
||||
u16 *destination = &SCREEN[top * WIDTH + left];
|
||||
int col = mirrored ? width - 1 - (left - x) : left - x;
|
||||
int advance = mirrored ? -1 : 1;
|
||||
for (int offset = 0; offset < right - left; ++offset, col += advance) {
|
||||
unsigned char pixel = source[col];
|
||||
if (pixel) destination[offset] = palette[pixel - 1];
|
||||
}
|
||||
} else {
|
||||
for (int col = first; col < end; ++col) {
|
||||
unsigned char pixel = source[col];
|
||||
if (!pixel) continue;
|
||||
u16 color = palette[pixel - 1];
|
||||
int start = x + (mirrored ? width - 1 - col : col) * scale;
|
||||
int finish = start + scale;
|
||||
if (start < left) start = left;
|
||||
if (finish > right) finish = right;
|
||||
for (int screen_y = top; screen_y < bottom; ++screen_y) {
|
||||
u16 *destination = &SCREEN[screen_y * WIDTH];
|
||||
for (int screen_x = start; screen_x < finish; ++screen_x)
|
||||
destination[screen_x] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
|
||||
int mirrored)
|
||||
{
|
||||
blit_sprite(x, y, art, 16, rows, palette, 1, mirrored);
|
||||
}
|
||||
|
||||
static const char *const creature_art[] = {
|
||||
@@ -880,11 +1033,7 @@ static void scaled_creature(int x, int y, int scale, const u16 *palette)
|
||||
{
|
||||
rectangle(x - 7 * scale, y, 14 * scale, 2 * scale,
|
||||
indoors() ? RGB(15, 11, 8) : RGB(8, 16, 7));
|
||||
for (int row = 0; row < 16; ++row)
|
||||
for (int col = 0; col < 16; ++col)
|
||||
if (creature_art[row][col] != '.')
|
||||
rectangle(x + (col - 8) * scale, y + (row - 16) * scale, scale, scale,
|
||||
palette[creature_art[row][col] - '0']);
|
||||
blit_sprite(x - 8 * scale, y - 16 * scale, creature_art, 16, 16, palette, scale, 0);
|
||||
}
|
||||
|
||||
#include "wild_creatures.h"
|
||||
@@ -893,11 +1042,7 @@ static void draw_wild_creature(enum Species enemy, int x, int y)
|
||||
{
|
||||
const struct WildArt *art = &wild_art[enemy - FERNFOX];
|
||||
rectangle(x - 20, y, 40, 3, RGB(8, 16, 7));
|
||||
for (int row = 0; row < 24; ++row)
|
||||
for (int col = 0; col < 24; ++col)
|
||||
if (art->rows[row][col] != '.')
|
||||
rectangle(x + (col - 12) * 2, y + (row - 24) * 2, 2, 2,
|
||||
art->palette[art->rows[row][col] - '0']);
|
||||
blit_sprite(x - 24, y - 48, art->rows, 24, 24, art->palette, 2, 0);
|
||||
}
|
||||
|
||||
static void draw_sprout(int x, int y, int scale)
|
||||
@@ -1052,6 +1197,38 @@ static int can_walk(int x, int y)
|
||||
return 1;
|
||||
}
|
||||
|
||||
enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER };
|
||||
|
||||
static enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks)
|
||||
{
|
||||
player_facing = facing_for_keys(keys, player_facing);
|
||||
int dx = !!(keys & RIGHT) - !!(keys & LEFT);
|
||||
int dy = !!(keys & DOWN) - !!(keys & UP);
|
||||
if (!dx && !dy) { diagonal_fraction = 0; return WALK_NORMAL; }
|
||||
if (ticks > MAX_MOVEMENT_TICKS) ticks = MAX_MOVEMENT_TICKS;
|
||||
for (int tick = 0; tick < ticks; ++tick) {
|
||||
int pixels = WALK_SPEED;
|
||||
if (dx && dy) {
|
||||
/* 181/256 approximates 1/sqrt(2): no diagonal speed boost. */
|
||||
diagonal_fraction += WALK_SPEED * 181;
|
||||
pixels = (int)(diagonal_fraction / 256);
|
||||
diagonal_fraction %= 256;
|
||||
} else {
|
||||
diagonal_fraction = 0;
|
||||
}
|
||||
for (int pixel = 0; pixel < pixels; ++pixel) {
|
||||
int old_x = *x, old_y = *y;
|
||||
if (can_walk(*x + dx, *y)) *x += dx;
|
||||
if (can_walk(*x, *y + dy)) *y += dy;
|
||||
/* Record every substep, preserving Sprout's existing path gap. */
|
||||
move_follower(old_x, old_y, *x, *y);
|
||||
if (use_map_exit(x, y, keys)) return WALK_MAP_CHANGED;
|
||||
if (check_grass_encounter(old_x, old_y, *x, *y)) return WALK_ENCOUNTER;
|
||||
}
|
||||
}
|
||||
return WALK_NORMAL;
|
||||
}
|
||||
|
||||
static void status(const char *message)
|
||||
{
|
||||
rectangle(0, 0, WIDTH, 18, RGB(29, 30, 25));
|
||||
@@ -1204,6 +1381,30 @@ static void wait_for_frame(void)
|
||||
while (VCOUNT < 160) { }
|
||||
}
|
||||
|
||||
/* Timers 2+3 form a cascaded 32-bit clock at 16,777,216 / 1024 Hz. */
|
||||
static unsigned movement_time(void)
|
||||
{
|
||||
volatile u16 *low = (volatile u16 *)0x04000108;
|
||||
volatile u16 *high = (volatile u16 *)0x0400010c;
|
||||
u16 upper, lower;
|
||||
do {
|
||||
upper = *high;
|
||||
lower = *low;
|
||||
} while (upper != *high);
|
||||
return ((unsigned)upper << 16) | lower;
|
||||
}
|
||||
|
||||
static void start_movement_timer(void)
|
||||
{
|
||||
volatile u16 *timer2 = (volatile u16 *)0x04000108;
|
||||
volatile u16 *timer3 = (volatile u16 *)0x0400010c;
|
||||
timer2[1] = timer3[1] = 0;
|
||||
timer2[0] = timer3[0] = 0;
|
||||
timer3[1] = 0x0084; /* Enable cascade from timer 2. */
|
||||
timer2[1] = 0x0083; /* Enable, prescaler 1024; no IRQ required. */
|
||||
pause_movement_at(movement_time());
|
||||
}
|
||||
|
||||
struct DmaChannel {
|
||||
const void *source;
|
||||
volatile void *destination;
|
||||
@@ -1235,17 +1436,25 @@ int main(void)
|
||||
int dialogue_page = -1; /* -1 means we are not talking. */
|
||||
int team_open = 0;
|
||||
unsigned previous = 0;
|
||||
start_movement_timer();
|
||||
load_progress();
|
||||
reset_follower(x, y);
|
||||
reset_dirty_rows();
|
||||
draw_scene(x, y);
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
|
||||
for (;;) {
|
||||
unsigned keys = (~KEYS) & 0x03ff;
|
||||
unsigned pressed = keys & ~previous;
|
||||
previous = keys;
|
||||
unsigned now = movement_time();
|
||||
int movement_ticks = 0;
|
||||
if (team_open || battle.phase != BATTLE_OFF || dialogue_page >= 0)
|
||||
pause_movement_at(now);
|
||||
else
|
||||
movement_ticks = movement_ticks_at(now);
|
||||
random_state += 0x9e3779b9u; /* Mix in how long the player waits. */
|
||||
if (team_open) {
|
||||
if (pressed & (SELECT | A | B)) {
|
||||
@@ -1254,6 +1463,7 @@ int main(void)
|
||||
draw_scene(x, y);
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
} else {
|
||||
present_frame();
|
||||
}
|
||||
@@ -1268,6 +1478,7 @@ int main(void)
|
||||
status("SPROUT RESTED AND IS READY AGAIN");
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
} else {
|
||||
draw_battle_hud();
|
||||
present_frame();
|
||||
@@ -1305,38 +1516,39 @@ int main(void)
|
||||
draw_team();
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
continue;
|
||||
}
|
||||
int old_x = x, old_y = y;
|
||||
int old_follower_x = follower_x, old_follower_y = follower_y;
|
||||
enum Facing old_facing = player_facing;
|
||||
player_facing = facing_for_keys(keys, player_facing);
|
||||
int next_x = x + !!(keys & RIGHT) - !!(keys & LEFT);
|
||||
int next_y = y + !!(keys & DOWN) - !!(keys & UP);
|
||||
if (can_walk(next_x, y)) x = next_x;
|
||||
if (can_walk(x, next_y)) y = next_y;
|
||||
enum WalkResult walk = WALK_NORMAL;
|
||||
if (pressed & START) {
|
||||
reset_position(&x, &y);
|
||||
greeting = 0;
|
||||
location_status(x, y);
|
||||
pause_movement_at(now);
|
||||
movement_ticks = 0;
|
||||
} else {
|
||||
move_follower(old_x, old_y, x, y);
|
||||
walk = walk_movement(keys, &x, &y, movement_ticks);
|
||||
}
|
||||
if (use_map_exit(&x, &y, keys)) {
|
||||
if (walk == WALK_MAP_CHANGED) {
|
||||
greeting = 0;
|
||||
dialogue_page = -1;
|
||||
DISPLAY = 0x0080;
|
||||
draw_scene(x, y);
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
continue;
|
||||
}
|
||||
if (check_grass_encounter(old_x, old_y, x, y)) {
|
||||
if (walk == WALK_ENCOUNTER) {
|
||||
greeting = 0;
|
||||
DISPLAY = 0x0080;
|
||||
draw_battle();
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
continue;
|
||||
}
|
||||
if (pressed & A) {
|
||||
@@ -1357,6 +1569,7 @@ int main(void)
|
||||
draw_scene(x, y);
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
continue;
|
||||
} else if (near_npc(x, y)) {
|
||||
begin_conversation(x, y);
|
||||
@@ -1380,6 +1593,7 @@ int main(void)
|
||||
draw_battle();
|
||||
present_frame();
|
||||
DISPLAY = 0x0403;
|
||||
pause_movement_at(movement_time());
|
||||
continue;
|
||||
} else if (near_golden(x, y)) {
|
||||
status("HELLO NEW FRIEND");
|
||||
@@ -1389,7 +1603,10 @@ int main(void)
|
||||
greeting = 180;
|
||||
}
|
||||
}
|
||||
if (greeting && --greeting == 0) location_status(x, y);
|
||||
if (greeting) {
|
||||
greeting = greeting > movement_ticks ? greeting - movement_ticks : 0;
|
||||
if (!greeting) location_status(x, y);
|
||||
}
|
||||
if (x != old_x || y != old_y || player_facing != old_facing ||
|
||||
(partner_owned && (old_follower_x != follower_x || old_follower_y != follower_y))) {
|
||||
restore(old_x - 8, old_y - 20, 17, 24);
|
||||
@@ -1405,7 +1622,6 @@ int main(void)
|
||||
}
|
||||
if (dialogue_page >= 0) dialogue_box(dialogue_page);
|
||||
present_frame();
|
||||
++frame;
|
||||
if (frame == 1024) frame = 0;
|
||||
frame = (frame + movement_ticks) & 1023;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user