Compare commits

..
2 Commits
20 changed files with 2611 additions and 2431 deletions

No files matched your search

+15 -5
View File
@@ -3,8 +3,13 @@ PYTHON ?= /usr/bin/python3
CC := $(TOOL_PREFIX)gcc CC := $(TOOL_PREFIX)gcc
OBJCOPY := $(TOOL_PREFIX)objcopy OBJCOPY := $(TOOL_PREFIX)objcopy
CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack
GAME_SOURCES := source/main.c source/video.c source/scenery.c source/world.c \
source/actors.c source/battle.c source/story.c source/hud.c
GAME_OBJECTS := $(GAME_SOURCES:source/%.c=build/%.o)
TEST_SOURCES := tests/test_game.c tests/test_support.c tests/test_maps.c \
tests/test_story.c tests/test_battle.c tests/test_rendering.c tests/test_movement.c
.PHONY: all play check .PHONY: all play check check-host
all: game.gba all: game.gba
build: build:
@@ -13,11 +18,13 @@ build:
build/start.o: source/start.s | build build/start.o: source/start.s | build
$(CC) -mcpu=arm7tdmi -marm -c $< -o $@ $(CC) -mcpu=arm7tdmi -marm -c $< -o $@
build/main.o: source/main.c source/wild_creatures.h | build build/%.o: source/%.c source/game.h | build
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
build/game.elf: build/start.o build/main.o gba.ld build/actors.o: source/wild_creatures.h
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,-Map,build/game.map build/start.o build/main.o -lgcc -o $@
build/game.elf: build/start.o $(GAME_OBJECTS) gba.ld
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,-Map,build/game.map build/start.o $(GAME_OBJECTS) -lgcc -o $@
game.gba: build/game.elf tools/rom_header.py game.gba: build/game.elf tools/rom_header.py
$(OBJCOPY) -O binary $< $@ $(OBJCOPY) -O binary $< $@
@@ -26,7 +33,10 @@ game.gba: build/game.elf tools/rom_header.py
check: game.gba check: game.gba
$(PYTHON) tools/rom_header.py --check game.gba $(PYTHON) tools/rom_header.py --check game.gba
$(PYTHON) -m unittest discover -s tests -v $(PYTHON) -m unittest discover -s tests -v
/usr/bin/clang -Wall -Wextra -Werror tests/test_game.c -o build/test_game $(MAKE) check-host
check-host: | build
/usr/bin/clang -Wall -Wextra -Werror -DGAME_TEST $(TEST_SOURCES) $(GAME_SOURCES) -o build/test_game
./build/test_game ./build/test_game
play: game.gba play: game.gba
+15 -5
View File
@@ -9,7 +9,7 @@ Double-click **Build and Play.command** in this folder. It builds your ROM, runs
Or open Terminal and run: Or open Terminal and run:
```sh ```sh
cd ~/gba-game cd ~/Developer/pokemon-gba
make play make play
``` ```
@@ -35,17 +35,25 @@ If those keys do not match your emulator settings, check mGBA's keyboard input s
### Your first change ### Your first change
Open `source/main.c` in a text editor. Find: Open `source/actors.c` in a text editor. Find:
```c ```c
static const u16 shirt = RGB(5, 14, 29); TEST_INTERNAL const u16 shirt = RGB(5, 14, 29);
``` ```
Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)` for a red shirt. Save, then double-click **Build and Play.command** again. Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)` for a red shirt. Save, then double-click **Build and Play.command** again.
## Project files ## Project files
- `source/main.c`: map, character, creature, and game rules. - `source/main.c`: GBA hardware, timers, and the main game loop.
- `source/game.h`: shared game types and module declarations.
- `source/video.c`: frame buffer, dirty rows, text, and sprite renderer.
- `source/scenery.c`: map artwork and the scenery cache.
- `source/world.c`: movement, collisions, doors, map travel, and following.
- `source/actors.c`: character and creature artwork and drawing.
- `source/battle.c`: species, battle rules, and the battle screen.
- `source/story.c`: conversations, gifts, and cartridge SRAM saves.
- `source/hud.c`: map status, scene drawing, and the team screen.
- `source/wild_creatures.h`: original wild-creature sprites and palettes. - `source/wild_creatures.h`: original wild-creature sprites and palettes.
- `source/start.s`: code that starts the GBA hardware. - `source/start.s`: code that starts the GBA hardware.
- `gba.ld`: tells the compiler where cartridge code and working memory go. - `gba.ld`: tells the compiler where cartridge code and working memory go.
@@ -53,6 +61,8 @@ Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)`
- `Makefile`: builds the game with the installed ARM compiler. - `Makefile`: builds the game with the installed ARM compiler.
- `tests/`: cartridge header, NPC interaction, drawing, house entry/exit, and room collision tests. `make check` also generates scene previews in `build/`. - `tests/`: cartridge header, NPC interaction, drawing, house entry/exit, and room collision tests. `make check` also generates scene previews in `build/`.
Run `make check-host` to check C game behavior on your Mac using only host clang, generating scene previews in `build/`. It does not need a ROM or the ARM compiler. The C tests are grouped by maps, story, battles, rendering, and movement, with shared helpers in `tests/test_support.c`. `make check` builds the ROM, checks its header, runs the ROM-dependent Python tests, and runs these host checks.
Graphics are drawn into a RAM buffer first. Only the changed spans of each row are copied to the GBA screen during VBlank, so walking does not expose a half-erased character. Full map changes are copied while the display is temporarily blanked. Graphics are drawn into a RAM buffer first. Only the changed spans of each row are copied to the GBA screen during VBlank, so walking does not expose a half-erased character. Full map changes are copied while the display is temporarily blanked.
The active map's scenery is cached in a separate 76,800-byte RAM buffer. Walking and dialogue cleanup copy background rows from that cache instead of recalculating every pixel. The cache rebuilds when maps change; opening or closing an exterior door updates only its 13×18-pixel patch. Actors and UI are never stored in the scenery cache. The active map's scenery is cached in a separate 76,800-byte RAM buffer. Walking and dialogue cleanup copy background rows from that cache instead of recalculating every pixel. The cache rebuilds when maps change; opening or closing an exterior door updates only its 13×18-pixel patch. Actors and UI are never stored in the scenery cache.
@@ -67,7 +77,7 @@ Receiving Sprout or Finn's Poké Balls automatically writes a small cartridge SR
### Change what Mila says ### Change what Mila says
Find `dialogue` near the top of `source/main.c`. Each pair of strings is one page of her conversation. Use CAPITAL letters and spaces, with no more than 34 characters on each line. Save and rebuild to try your new conversation. Find `dialogue` in `source/story.c`. Each pair of strings is one page of her conversation. Use CAPITAL letters and spaces, with no more than 34 characters on each line. Save and rebuild to try your new conversation.
## Mac setup ## Mac setup
+173
View File
@@ -0,0 +1,173 @@
#include "game.h"
/* These are good first things to change! Colors use numbers from 0 to 31. */
TEST_INTERNAL const u16 shirt = RGB(5, 14, 29);
TEST_INTERNAL const char *const creature_art[] = {
"..11........11..",
".1221......1221.",
".1231......1321.",
"..123111111321..",
"..123333333321..",
".12344444443321.",
".12341334143321.",
".12341534153321.",
".12643333443621.",
"..123337733321..",
"...1233333321...",
"...1234444321...",
"..123444444321..",
"..123344443321..",
"...1223113221...",
"....111..111...."
};
static void creature(void)
{
static const u16 palette[] = {
0, RGB(9, 8, 6), RGB(23, 15, 5), RGB(30, 23, 7),
RGB(31, 28, 14), RGB(31, 31, 28), RGB(29, 13, 10), RGB(17, 8, 7)
};
rectangle(175, 102, 14, 3, RGB(8, 16, 7));
sprite(174, 88, creature_art, 16, palette, 0);
}
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));
blit_sprite(x - 8 * scale, y - 16 * scale, creature_art, 16, 16, palette, scale, 0);
}
#include "wild_creatures.h"
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));
blit_sprite(x - 24, y - 48, art->rows, 24, 24, art->palette, 2, 0);
}
void draw_sprout(int x, int y, int scale)
{
static const u16 palette[] = {
0, RGB(5, 10, 7), RGB(6, 16, 8), RGB(12, 24, 10),
RGB(24, 30, 14), RGB(31, 31, 28), RGB(30, 17, 12), RGB(12, 9, 7)
};
scaled_creature(x, y, scale, palette);
rectangle(x - scale, y - 16 * scale, 2 * scale, 3 * scale, RGB(5, 17, 8));
rectangle(x, y - 17 * scale, 4 * scale, 2 * scale, RGB(13, 24, 9));
rectangle(x + scale, y - 17 * scale, 2 * scale, scale, RGB(24, 30, 14));
}
TEST_INTERNAL const char *const person_art[] = {
".....111111.....",
"....12222221....",
"...1223333221...",
"...1233333321...",
"...1222222221...",
"..111111111111..",
"...1444444441...",
"...1466666641...",
"....16166161....",
"....16677661....",
".....166661.....",
"....11888811....",
"...1888998881...",
"..161889988161..",
"..161888888161..",
"..166188881661..",
"...1111111111...",
"....1AA11AA1....",
"....1AA11AA1....",
"....111..111...."
};
TEST_INTERNAL const char *const person_back_art[] = {
".....111111.....", "....12222221....", "...1223333221...", "...1233333321...",
"...1233333321...", "...1222222221...", "...1444444441...", "....14444441....",
"....14444441....", ".....166661.....", "....11888811....", "...1888888881...",
"..1618BBBB8161..", "..1618BCCB8161..", "..1618BCCB8161..", "..1668BBBB8661..",
"...1111111111...", "....1AA11AA1....", "....1AA11AA1....", "....111..111...."
};
TEST_INTERNAL const char *const person_side_art[] = {
"......11111.....", ".....1222221....", "....123333321...", "....123333321...",
"....1222222221..", "....1111111111..", "....144466661...", "....144661661...",
".....14666661...", ".....1467761....", "......16661.....", ".....188881.....",
"....1BB88881....", "....1BC81661....", "....1BB81661....", ".....188161.....",
".....111111.....", ".....1AA1A1.....", ".....1AA1A1.....", ".....111111....."
};
static u16 lighter(u16 color)
{
int r = color & 31, g = (color >> 5) & 31, b = (color >> 10) & 31;
return RGB(r + (31 - r) / 3, g + (31 - g) / 3, b + (31 - b) / 3);
}
static u16 darker(u16 color)
{
return RGB((color & 31) * 2 / 3, ((color >> 5) & 31) * 2 / 3,
((color >> 10) & 31) * 2 / 3);
}
/* x and y mark the feet. The art fits inside the original erase area. */
TEST_INTERNAL void person(int x, int y, int step, u16 clothes, u16 hat, enum Facing facing)
{
u16 palette[] = {
0, RGB(5, 7, 9), darker(hat), hat, RGB(11, 8, 7),
RGB(24, 16, 12), RGB(31, 25, 19), RGB(25, 13, 12),
clothes, lighter(clothes), RGB(9, 12, 17), RGB(24, 17, 8), RGB(30, 24, 12)
};
rectangle(x - 7, y - 1, 15, 3,
indoors() ? RGB(15, 11, 8) : RGB(8, 16, 7));
const char *const *art = facing == FACING_UP ? person_back_art :
facing == FACING_DOWN ? person_art : person_side_art;
sprite(x - 8, y - 19, art, 20, palette, facing == FACING_LEFT);
rectangle(x - 4, y, 3, 2 - step, palette[1]);
rectangle(x + 1, y, 3, 1 + step, palette[1]);
}
static void player(int x, int y, int step)
{
person(x, y, step, shirt, RGB(28, 8, 9), player_facing);
}
void actors(int x, int y, int step)
{
enum { HERO, SPROUT, MILA, GOLDEN, PROFESSOR, RANGER };
struct Actor { int x, y, kind; } list[4];
int count = 0;
if (partner_owned && follower_ready)
list[count++] = (struct Actor){follower_x, follower_y, SPROUT};
list[count++] = (struct Actor){x, y, HERO};
if (current_room == MELLON_HOUSE) {
list[count++] = (struct Actor){PROF_X, PROF_Y, PROFESSOR};
}
if (current_room == OUTSIDE) {
list[count++] = (struct Actor){182, 104, GOLDEN};
list[count++] = (struct Actor){NPC_X, NPC_Y, MILA};
}
if (current_room == ROUTE) list[count++] = (struct Actor){RANGER_X, RANGER_Y, RANGER};
/* Stable depth ordering keeps Sprout behind the hero on equal feet. */
for (int i = 1; i < count; ++i) {
struct Actor actor = list[i];
int j = i;
while (j > 0 && list[j - 1].y > actor.y) {
list[j] = list[j - 1];
--j;
}
list[j] = actor;
}
for (int i = 0; i < count; ++i) {
struct Actor actor = list[i];
if (actor.kind == HERO) player(actor.x, actor.y, step);
else if (actor.kind == SPROUT) draw_sprout(actor.x, actor.y, 1);
else if (actor.kind == GOLDEN) creature();
else if (actor.kind == MILA)
person(actor.x, actor.y, 0, RGB(4, 21, 13), RGB(12, 7, 4), FACING_DOWN);
else if (actor.kind == RANGER)
person(actor.x, actor.y, 0, RGB(27, 16, 7), RGB(6, 15, 11), FACING_DOWN);
else person(actor.x, actor.y, 0, RGB(29, 30, 27), RGB(20, 21, 19), FACING_DOWN);
}
}
+146
View File
@@ -0,0 +1,146 @@
#include "game.h"
TEST_INTERNAL const struct SpeciesInfo species[SPECIES_COUNT] = {
{"GOLDEN", "LV THREE", "WILD GOLDEN CREATURE", "GOLDEN USED BUMP", "NORMAL", GOLDEN_HP, 5, 7},
{"FERNFOX", "LV FOUR", "WILD FERNFOX APPEARED", "FERNFOX USED LEAF SWIPE", "GRASS", 18, 4, 7},
{"EMBERWING", "LV FOUR", "WILD EMBERWING APPEARED", "EMBERWING USED SPARK", "FIRE", 20, 4, 4},
{"PEBBLIT", "LV FIVE", "WILD PEBBLIT APPEARED", "PEBBLIT USED ROLL", "ROCK", 22, 5, 9}
};
struct BattleState battle;
unsigned random_state = 0x6d2b79f5u;
int grass_steps;
TEST_INTERNAL unsigned next_random(void)
{
if (!random_state) random_state = 0x6d2b79f5u;
random_state ^= random_state << 13;
random_state ^= random_state >> 17;
random_state ^= random_state << 5;
return random_state;
}
TEST_INTERNAL int start_battle(enum Species enemy)
{
if (!partner_owned || battle.phase != BATTLE_OFF || (unsigned)enemy >= SPECIES_COUNT)
return 0;
battle.phase = BATTLE_TURN;
battle.enemy = enemy;
battle.sprout_hp = SPROUT_HP;
battle.enemy_hp = species[enemy].hp;
battle.move = 0;
battle.turns = 0;
grass_steps = 0;
return 1;
}
int near_golden(int x, int y)
{
return current_room == OUTSIDE && x >= 163 && x <= 205 && y >= 84 && y <= 126;
}
int begin_battle(int x, int y)
{
if (!near_golden(x, y)) return 0;
return start_battle(GOLDEN);
}
int check_grass_encounter(int old_x, int old_y, int x, int y)
{
if (!partner_owned || !in_tall_grass(x, y)) { grass_steps = 0; return 0; }
if (battle.phase != BATTLE_OFF || (old_x == x && old_y == y)) return 0;
++grass_steps;
/* No encounters while idle, and a 24-pixel breather after every battle. */
if (grass_steps < 24 || (grass_steps - 24) % 8) return 0;
if ((next_random() & 3u) && grass_steps < 64) return 0;
return start_battle((enum Species)(FERNFOX + next_random() % 3u));
}
/* One fresh button press performs one turn. The golden creature replies. */
int battle_input(unsigned pressed)
{
if (battle.phase == BATTLE_OFF || !pressed) return 0;
if (battle.phase != BATTLE_TURN) {
if (!(pressed & (A | B))) return 0;
battle.phase = BATTLE_OFF;
return 1;
}
if (pressed & B) {
battle.phase = BATTLE_FLED;
return 1;
}
if (pressed & LEFT) battle.move = 0;
if (pressed & RIGHT) battle.move = 1;
if (!(pressed & A)) return !!(pressed & (LEFT | RIGHT));
battle.enemy_hp -= battle.move ? species[battle.enemy].leaf_damage : 4;
++battle.turns;
if (battle.enemy_hp <= 0) {
battle.enemy_hp = 0;
battle.phase = BATTLE_WON;
} else {
battle.sprout_hp -= species[battle.enemy].attack;
if (battle.sprout_hp <= 0) {
battle.sprout_hp = 0;
battle.phase = BATTLE_LOST;
}
}
return 1;
}
static void health_bar(int x, int y, int hp, int maximum)
{
rectangle(x, y, 92, 6, RGB(7, 12, 10));
rectangle(x + 1, y + 1, 90, 4, RGB(21, 22, 18));
u16 color = hp * 4 <= maximum ? RGB(29, 9, 8) :
hp * 2 <= maximum ? RGB(29, 23, 7) : RGB(10, 25, 12);
rectangle(x + 1, y + 1, hp * 90 / maximum, 4, color);
}
void draw_battle_hud(void)
{
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
health_bar(16, 44, battle.enemy_hp, species[battle.enemy].hp);
health_bar(136, 97, battle.sprout_hp, SPROUT_HP);
rectangle(8, 111, 224, 43, ink);
rectangle(10, 113, 220, 39, cream);
if (battle.phase == BATTLE_TURN) {
large_text(22, 119, "TACKLE", ink);
large_text(132, 119, "LEAF TAP", ink);
int arrow_x = battle.move ? 122 : 12;
rectangle(arrow_x, 119, 2, 7, RGB(7, 17, 11));
rectangle(arrow_x + 2, 121, 2, 3, RGB(7, 17, 11));
text(16, 132, battle.turns ? species[battle.enemy].reply :
"WHAT WILL SPROUT DO", ink);
text(16, 143, "LEFT RIGHT CHOOSE A ATTACK B RUN", ink);
} else {
const char *result = battle.phase == BATTLE_WON ? "YOU WON" :
battle.phase == BATTLE_LOST ? "SPROUT NEEDS A REST" : "YOU GOT AWAY SAFELY";
large_text(20, 120, result, ink);
text(20, 134, "A BACK TO ADVENTURE", ink);
text(20, 144, "SPROUT WILL REST AND RECOVER", ink);
}
}
void draw_battle(void)
{
rectangle(0, 0, WIDTH, HEIGHT, RGB(28, 29, 23));
status(species[battle.enemy].intro);
rectangle(0, 18, WIDTH, 44, RGB(23, 29, 25));
rectangle(0, 62, WIDTH, 49, RGB(18, 26, 15));
rectangle(155, battle.enemy == GOLDEN ? 61 : 70, 64, 5, RGB(12, 21, 11));
rectangle(18, 104, 78, 6, RGB(12, 21, 11));
static const u16 gold_palette[] = {
0, RGB(9, 8, 6), RGB(23, 15, 5), RGB(30, 23, 7),
RGB(31, 28, 14), RGB(31, 31, 28), RGB(29, 13, 10), RGB(17, 8, 7)
};
if (battle.enemy == GOLDEN) scaled_creature(184, 62, 2, gold_palette);
else draw_wild_creature(battle.enemy, 184, 71);
draw_sprout(57, 106, 3);
rectangle(10, 24, 106, 29, RGB(30, 30, 26));
large_text(16, 28, species[battle.enemy].name, RGB(5, 8, 11));
text(16, 37, species[battle.enemy].type, RGB(5, 8, 11));
text(69, 37, species[battle.enemy].level, RGB(5, 8, 11));
rectangle(130, 76, 102, 31, RGB(30, 30, 26));
large_text(136, 80, "SPROUT", RGB(5, 8, 11));
text(179, 90, "LV FIVE", RGB(5, 8, 11));
draw_battle_hud();
}
+139
View File
@@ -0,0 +1,139 @@
#ifndef GAME_H
#define GAME_H
typedef unsigned short u16;
#define RGB(r, g, b) ((u16)((r) | ((g) << 5) | ((b) << 10)))
#define WIDTH 240
#define HEIGHT 160
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 };
enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGER_Y = 97 };
enum Room { OUTSIDE, COTTAGE, ROUTE, MELLON_GARDEN, MELLON_HOUSE };
enum Facing { FACING_DOWN, FACING_UP, FACING_LEFT, FACING_RIGHT };
enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 };
enum { FOLLOW_STEPS = 14, SPROUT_HP = 24, GOLDEN_HP = 18 };
enum BattlePhase { BATTLE_OFF, BATTLE_TURN, BATTLE_WON, BATTLE_LOST, BATTLE_FLED };
enum Species { GOLDEN, FERNFOX, EMBERWING, PEBBLIT, SPECIES_COUNT };
enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER };
struct BattleState {
enum BattlePhase phase;
enum Species enemy;
int sprout_hp, enemy_hp, move, turns;
};
extern struct BattleState battle;
struct SpeciesInfo {
const char *name, *level, *intro, *reply, *type;
int hp, attack, leaf_damage;
};
struct WildArt { const char *const *rows; const u16 *palette; };
#ifdef GAME_TEST
#define TEST_INTERNAL
extern u16 test_pixels[WIDTH * HEIGHT + 2];
extern unsigned char test_save[32];
#define SCREEN (test_pixels + 1)
#define SAVE_MEMORY test_save
#else
#define TEST_INTERNAL static
extern u16 frame_buffer[WIDTH * HEIGHT];
#define SCREEN frame_buffer
#endif
extern u16 dirty_first[HEIGHT];
extern u16 dirty_end[HEIGHT];
extern enum Room current_room;
extern int door_open;
extern enum Facing player_facing;
extern int follower_x, follower_y, follower_ready;
extern unsigned random_state;
extern int grass_steps;
extern int partner_owned;
extern int ball_count, balls_given;
void reset_dirty_rows(void);
void mark_dirty(int x, int y, int w, int h);
void rectangle(int x, int y, int w, int h, u16 color);
void text(int x, int y, const char *message, u16 color);
void large_text(int x, int y, const char *message, u16 color);
void blit_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored);
void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
int mirrored);
int in_tall_grass(int x, int y);
void restore(int x, int y, int w, int h);
void pause_movement_at(unsigned now);
int movement_ticks_at(unsigned now);
void reset_follower(int x, int y);
int indoors(void);
int near_door(int x, int y);
int near_map_exit(int x, int y);
int near_route_sign(int x, int y);
int use_door(int *x, int *y);
void reset_position(int *x, int *y);
enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks);
void scaled_creature(int x, int y, int scale, const u16 *palette);
void draw_wild_creature(enum Species enemy, int x, int y);
void draw_sprout(int x, int y, int scale);
void actors(int x, int y, int step);
int near_golden(int x, int y);
int begin_battle(int x, int y);
int check_grass_encounter(int old_x, int old_y, int x, int y);
int battle_input(unsigned pressed);
void draw_battle_hud(void);
void draw_battle(void);
void load_progress(void);
int near_npc(int x, int y);
int begin_conversation(int x, int y);
int advance_conversation(int page);
void dialogue_box(int page);
void status(const char *message);
void location_status(int x, int y);
void draw_scene(int x, int y);
void draw_team(void);
#ifdef GAME_TEST
extern const unsigned char large_alphabet[26][7];
extern u16 background_cache[WIDTH * HEIGHT];
extern enum Room background_room;
extern int background_valid, background_door_open;
extern unsigned scenery_samples;
extern unsigned diagonal_fraction;
extern const u16 shirt;
extern const char *const creature_art[16];
extern const char *const person_art[20];
extern const char *const person_back_art[20];
extern const char *const person_side_art[20];
extern const struct SpeciesInfo species[SPECIES_COUNT];
extern int gift_conversation;
extern const char *const dialogue[][2];
extern const char *const professor_dialogue[][2];
extern const char *const professor_followup[][2];
extern const char *const ranger_dialogue[][2];
extern const char *const ranger_followup[][2];
extern int trail_head, ball_gift_conversation;
extern unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
extern const int DIALOGUE_PAGES;
extern const char *const fernfox_art[24], *const emberwing_art[24], *const pebblit_art[24];
extern const struct WildArt wild_art[3];
u16 cottage_scenery(int x, int y);
u16 route_scenery(int x, int y);
u16 village_scenery(int x, int y);
u16 mellon_garden_scenery(int x, int y);
u16 scenery(int x, int y);
void move_follower(int old_x, int old_y, int x, int y);
enum Facing facing_for_keys(unsigned keys, enum Facing previous);
int use_map_exit(int *x, int *y, unsigned keys);
int can_walk(int x, int y);
void person(int x, int y, int step, u16 clothes, u16 hat, enum Facing facing);
unsigned next_random(void);
int start_battle(enum Species enemy);
int next_dialogue_page(int page);
int receive_partner(void);
int receive_balls(void);
#endif
#endif
+69
View File
@@ -0,0 +1,69 @@
#include "game.h"
void status(const char *message)
{
rectangle(0, 0, WIDTH, 18, RGB(29, 30, 25));
rectangle(0, 16, WIDTH, 2, RGB(10, 18, 14));
text(8, 6, message, RGB(5, 9, 11));
}
void location_status(int x, int y)
{
if (near_map_exit(x, y)) {
if (current_room == MELLON_GARDEN) status("WALK LEFT TO GREENLEAF VILLAGE");
else if (current_room == ROUTE) status("WALK UP TO GREENLEAF VILLAGE");
else status(x >= 216 ? "WALK RIGHT TO PROFESSOR MELLON" : "WALK DOWN TO SUNPETAL TRAIL");
}
else if (near_route_sign(x, y)) status("A READ SIGN");
else if (current_room == ROUTE && near_npc(x, y)) status("A TALK TO RANGER FINN");
else if (current_room == ROUTE) status("SUNPETAL TRAIL");
else if (near_door(x, y)) status(indoors() ? "A GO OUTSIDE" : "A OPEN DOOR");
else if (current_room == MELLON_GARDEN) status("PROFESSOR MELLONS GARDEN");
else if (current_room == MELLON_HOUSE) status("PROFESSOR MELLONS HOUSE");
else if (partner_owned && near_golden(x, y) && !near_npc(x, y)) status("A BATTLE THE GOLDEN CREATURE");
else status(current_room == COTTAGE ? "INSIDE THE COTTAGE" : "GREENLEAF VILLAGE");
}
void draw_scene(int x, int y)
{
restore(0, 0, WIDTH, HEIGHT);
location_status(x, y);
text(8, 148, "ARROWS MOVE A USE SELECT TEAM START RESET", RGB(5, 9, 11));
actors(x, y, 0);
}
void draw_team(void)
{
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
rectangle(0, 0, WIDTH, HEIGHT, RGB(15, 24, 20));
status("YOUR TEAM");
rectangle(8, 25, 224, 112, ink);
rectangle(10, 27, 220, 108, cream);
if (partner_owned) {
draw_sprout(50, 105, 3);
large_text(94, 42, "SPROUT", ink);
large_text(94, 58, "GRASS TYPE", RGB(7, 17, 11));
large_text(94, 73, "LEVEL FIVE", ink);
text(94, 89, "HEALTH FULL", ink);
rectangle(94, 99, 106, 6, RGB(7, 14, 11));
rectangle(95, 100, 104, 4, RGB(10, 25, 12));
} else {
large_text(28, 57, "NO PARTNER YET", ink);
large_text(28, 76, "VISIT PROFESSOR MELLON", ink);
text(28, 96, "HIS HOUSE IS EAST OF GREENLEAF VILLAGE", ink);
}
/* A small bag summary is visible even before you receive Sprout. */
rectangle(24, 118, 8, 8, ink);
rectangle(25, 119, 6, 3, RGB(29, 8, 9));
rectangle(25, 122, 6, 3, RGB(31, 31, 28));
rectangle(27, 121, 2, 2, ink);
text(38, 119, "POKE BALLS", ink);
char count_text[3];
int count_index = 0;
if (ball_count >= 10) count_text[count_index++] = (char)('0' + ball_count / 10);
count_text[count_index++] = (char)('0' + ball_count % 10);
count_text[count_index] = 0;
text(88, 119, count_text, ink);
rectangle(0, 142, WIDTH, 18, cream);
text(8, 148, "SELECT OR A OR B TO RETURN", ink);
}
+4 -1371
View File
File diff suppressed because it is too large. Load diff
+403
View File
@@ -0,0 +1,403 @@
#include "game.h"
static const u16 grass = RGB(15, 26, 12);
/* One scenery-only cache; the frame buffer still holds actors and UI. */
TEST_INTERNAL u16 background_cache[WIDTH * HEIGHT];
TEST_INTERNAL enum Room background_room;
TEST_INTERNAL int background_valid, background_door_open;
#ifdef GAME_TEST
TEST_INTERNAL unsigned scenery_samples;
#endif
int in_tall_grass(int x, int y)
{
return current_room == ROUTE &&
((x >= 32 && x < 81 && y >= 53 && y < 87) ||
(x >= 151 && x < 208 && y >= 112 && y < 133));
}
/* Little shaded trees, made from pixels rather than downloaded artwork. */
static u16 tree_pixel(int x, int y, u16 ground)
{
int dx = x - 9, dy = y - 8;
int distance = dx * dx + dy * dy;
if (y >= 15 && y < 21 && x >= 7 && x <= 11)
ground = x < 9 ? RGB(11, 8, 5) : RGB(17, 12, 6);
if (distance < 76) {
if (distance > 54) return RGB(3, 10, 7);
if (y > 10 || x > 12) return RGB(5, 15, 8);
if ((x + y * 2) % 7 < 2) return RGB(13, 24, 10);
return RGB(8, 20, 9);
}
return ground;
}
/* A cozy room: wooden floor, a bed, bookcase, dresser, plant, and table. */
TEST_INTERNAL u16 cottage_scenery(int x, int y)
{
u16 color = RGB(3, 7, 10);
if (y < 18 || y >= 142) return RGB(29, 30, 25);
if (x < 18 || x >= 222 || y < 24) return color;
color = RGB(13, 9, 7);
if (x >= 24 && x < 216 && y >= 28 && y < 54) {
color = y < 43 ? RGB(29, 27, 21) : RGB(16, 21, 18);
if (y == 42 || y == 51) color = RGB(10, 13, 12);
if (y > 43 && x % 16 == 0) color = RGB(12, 17, 15);
}
if (x >= 26 && x < 214 && y >= 54 && y < 135) {
color = ((y / 8) & 1) ? RGB(24, 18, 12) : RGB(26, 20, 13);
if (y % 8 == 0 || (x + (y / 8) * 23) % 47 == 0) color = RGB(17, 12, 9);
if (y % 8 == 1) color = RGB(29, 23, 16);
if (y % 8 == 5 && (x * 3 + y) % 41 < 8) color = RGB(22, 16, 11);
}
/* Window and sill above the bed. */
if (x >= 43 && x < 70 && y >= 30 && y < 49) {
color = RGB(11, 10, 9);
if (x >= 45 && x < 68 && y >= 32 && y < 46) color = RGB(12, 25, 29);
if (x >= 46 && x < 54 && y >= 33 && y < 38) color = RGB(25, 31, 30);
if (x == 56 || y == 39) color = RGB(27, 25, 19);
if (y >= 47) color = RGB(25, 20, 14);
}
/* A bookcase fixed to the back wall. */
if (x >= 150 && x < 184 && y >= 29 && y < 54) {
color = RGB(11, 8, 6);
if (x >= 153 && x < 181 && y >= 32 && y < 51) {
int book = (x - 153) / 4;
color = book % 3 == 0 ? RGB(24, 10, 10) :
book % 3 == 1 ? RGB(9, 18, 21) : RGB(25, 21, 12);
if (x % 4 == 0) color = RGB(30, 27, 18);
}
if (y == 41 || y == 52) color = RGB(24, 16, 10);
}
/* Bed: headboard, pillow, blue blanket, and wooden feet. */
if (x >= 34 && x < 69 && y >= 59 && y < 98) {
color = RGB(10, 8, 7);
if (x >= 37 && x < 66 && y >= 62 && y < 94) color = RGB(29, 27, 21);
if (x >= 40 && x < 63 && y >= 64 && y < 72)
color = y == 71 ? RGB(23, 24, 23) : RGB(31, 31, 28);
if (x >= 37 && x < 66 && y >= 75 && y < 94) {
color = RGB(7, 15, 24);
if (x < 41 || x > 62) color = RGB(5, 11, 20);
if (y == 76 || y == 89) color = RGB(15, 25, 29);
if (y > 78 && y < 87 && (x + y) % 10 == 0) color = RGB(22, 29, 30);
}
if (y < 62 || y == 95) color = RGB(20, 13, 8);
}
/* Dresser with two little brass handles. */
if (x >= 96 && x < 132 && y >= 54 && y < 69) {
color = RGB(13, 9, 6);
if (x > 98 && x < 129 && y > 56 && y < 67) color = RGB(23, 16, 10);
if (y == 61) color = RGB(15, 10, 7);
if (x >= 111 && x < 116 && (y == 59 || y == 64)) color = RGB(31, 25, 10);
if (y == 54) color = RGB(29, 22, 14);
}
/* Red woven rug, clear of the furniture. */
if (x >= 86 && x < 140 && y >= 83 && y < 113) {
color = RGB(22, 9, 12);
if (x < 89 || x >= 137 || y < 86 || y >= 110) color = RGB(29, 23, 15);
else if ((x + y) % 12 == 0 || (x - y + 240) % 12 == 0) color = RGB(28, 15, 16);
}
/* Dining table, shaded edge, legs, and a little cup. */
if (x >= 150 && x < 188 && y >= 76 && y < 102) {
color = RGB(12, 8, 6);
if (y < 97 && x > 151 && x < 186) color = RGB(25, 18, 11);
if (y == 78) color = RGB(30, 24, 16);
if (y >= 97 && x >= 156 && x < 182) return RGB(18, 13, 9);
if (x >= 168 && x < 174 && y >= 83 && y < 89) color = RGB(30, 29, 24);
if (x >= 169 && x < 173 && y == 83) color = RGB(12, 9, 7);
}
/* Potted plant tucked into the top-right corner. */
if (x >= 197 && x < 207 && y >= 65 && y < 77)
color = y < 68 ? RGB(28, 17, 10) : RGB(21, 11, 8);
int px = x - 201, py = y - 60;
if (px * px + py * py < 51)
color = (x + y) % 4 == 0 ? RGB(15, 25, 10) : RGB(5, 17, 9);
/* Entry mat and a bright opening through the bottom wall. */
if (x >= 108 && x < 132 && y >= 120 && y < 134)
color = y % 3 == 0 ? RGB(21, 17, 11) : RGB(29, 25, 17);
if (x >= 108 && x < 132 && y >= 134) {
color = x < 111 || x >= 129 ? RGB(25, 18, 11) : RGB(12, 23, 11);
if (x >= 113 && x < 127) color = RGB(27, 25, 16);
}
return color;
}
/* Sunpetal Trail, the first area outside Greenleaf Village. */
TEST_INTERNAL u16 route_scenery(int x, int y)
{
if (y < 18 || y >= 142) return RGB(29, 30, 25);
u16 color = ((x / 16 + y / 16) & 1) ? RGB(16, 27, 13) : grass;
if ((x & 15) == 4 && (y & 7) >= 5) color = RGB(11, 22, 10);
if ((x & 15) == 11 && (y & 15) == 3) color = RGB(21, 29, 15);
if (x < 24 || x >= 216 || y >= 136) color = RGB(7, 18, 9);
if (y < 40 && (x < 88 || x >= 128))
color = tree_pixel((x - 8) % 20, y - 19, color);
if (x < 24) color = tree_pixel(x - 2, (y - 20) % 22, color);
if (x >= 216) color = tree_pixel(x - 217, (y - 20) % 22, color);
if (y >= 128) color = tree_pixel((x - 8) % 20, y - 127, color);
/* A central trail and a short branch to the pond lookout. */
if ((x >= 97 && x < 120 && y < 136) ||
(x >= 97 && x < 210 && y >= 89 && y < 104)) {
color = RGB(29, 27, 18);
if ((x == 97 || x == 119) && (y < 89 || y >= 104)) color = RGB(20, 23, 12);
if ((y == 89 || y == 103) && x >= 120) color = RGB(20, 23, 12);
if ((x & 7) == 3 && (y & 7) == 5) color = RGB(25, 23, 15);
if ((x & 15) == 10 && (y & 7) == 2) color = RGB(31, 29, 21);
}
/* The encounter check uses these exact same grass boundaries. */
if (in_tall_grass(x, y)) {
int tx = x & 7, ty = y & 7;
if ((tx == 1 && ty >= 2 && ty <= 5) || (tx == 3 && ty >= 1 && ty <= 5) ||
(tx == 5 && ty >= 3 && ty <= 6)) color = RGB(4, 15, 8);
if ((tx == 2 && ty >= 3 && ty <= 6) || (tx == 4 && ty >= 2 && ty <= 6))
color = RGB(9, 22, 9);
if ((tx == 1 && ty == 1) || (tx == 3 && ty == 0)) color = RGB(22, 29, 12);
}
if (x >= 152 && x < 208 && y >= 46 && y < 79) {
color = RGB(27, 25, 16);
if (x >= 155 && x < 205 && y >= 49 && y < 76) {
color = y < 54 ? RGB(6, 15, 25) : RGB(8, 20, 29);
if (y % 9 == 0 && (x + y * 3) % 19 < 7) color = RGB(17, 28, 30);
if (y == 75) color = RGB(13, 25, 28);
}
if (x == 154 || y == 48) color = RGB(31, 29, 20);
}
/* A readable sign pointing north, on two wooden posts. */
if ((x == 128 || x == 142) && y >= 64 && y < 74) color = RGB(16, 11, 7);
if (x >= 124 && x < 147 && y >= 55 && y < 68) {
color = RGB(13, 9, 7);
if (x > 125 && x < 145 && y > 56 && y < 66) color = RGB(29, 24, 15);
if ((x == 135 && y >= 59 && y <= 64) ||
(y == 60 && x >= 134 && x <= 136) ||
(y == 61 && x >= 133 && x <= 137)) color = RGB(10, 15, 11);
}
/* A small boulder beside the lower trail. */
int rx = x - 49, ry = y - 114;
if (rx * rx + ry * ry * 2 < 190) {
color = RGB(10, 13, 14);
if (rx * rx + ry * ry * 2 < 145) color = RGB(19, 22, 22);
if (y < 112 && x < 53 && rx * rx + ry * ry * 2 < 130) color = RGB(26, 28, 26);
if (y > 118) color = RGB(13, 17, 17);
}
return color;
}
/* Calculate scenery at one pixel so moving never leaves sprite trails. */
TEST_INTERNAL u16 village_scenery(int x, int y)
{
if (y < 18 || y >= 142) return RGB(29, 30, 25);
u16 color = grass;
if (((x >> 4) + (y >> 4)) & 1) color = RGB(16, 27, 13);
int gx = x & 15, gy = y & 15;
if ((gx == 4 && gy >= 9 && gy <= 11) ||
(gx == 3 && gy == 8) || (gx == 6 && gy == 9))
color = RGB(11, 22, 10);
if (gx == 11 && gy == 4) color = RGB(20, 29, 15);
if (x < 8 || x >= 232 || y >= 136) {
/* Wooden boundary fences, with a gap where the path leaves town. */
int post = (x < 8 || x >= 232) ? y % 16 : x % 16;
color = RGB(9, 20, 10);
if (post < 4) color = post == 0 ? RGB(13, 10, 7) : RGB(27, 22, 14);
else if (y >= 138 || (x < 8 && x >= 3) || (x >= 232 && x < 237))
color = RGB(22, 17, 11);
if (y >= 136 && x > 96 && x < 120) color = RGB(28, 26, 18);
if (x >= 232 && y > 106 && y < 120) color = RGB(29, 27, 18);
return color;
}
if (y < 40 && (x < 88 || x >= 128))
color = tree_pixel((x - 8) % 20, y - 19, color);
/* Warm sand, darker edges, and little scattered pebbles. */
if ((x > 96 && x < 120) || (y > 106 && y < 120)) {
color = RGB(29, 27, 18);
if (((x == 97 || x == 119) && (y <= 106 || y >= 120)) ||
((y == 107 || y == 119) && (x <= 96 || x >= 120)))
color = RGB(20, 23, 12);
if ((x & 7) == 3 && (y & 7) == 5) color = RGB(25, 23, 15);
if ((x & 15) == 10 && (y & 7) == 2) color = RGB(31, 29, 21);
}
/* Rounded pond, shoreline, wavelets, reeds, and a flowering lily. */
if (x >= 22 && x < 76 && y >= 44 && y < 86) {
int corner = (x < 26 || x > 71) && (y < 48 || y > 81);
if (!corner) {
color = RGB(22, 22, 13);
if (x >= 24 && x < 74 && y >= 46 && y < 84) color = RGB(29, 27, 18);
if (x >= 26 && x < 72 && y >= 48 && y < 82) {
color = y < 53 ? RGB(6, 15, 25) : RGB(8, 20, 29);
if (y >= 78) color = RGB(10, 23, 26);
if (y % 9 == 0 && (x + y * 3) % 19 < 7) color = RGB(17, 28, 29);
if (y % 9 == 1 && (x + y * 3) % 19 < 5) color = RGB(8, 21, 27);
int lx = x - 61, ly = y - 59;
if (lx * lx + ly * ly < 19 && !(lx > 0 && ly < 0))
color = ly < 0 ? RGB(16, 26, 10) : RGB(8, 20, 10);
if ((x == 59 && y >= 55 && y <= 57) ||
(y == 56 && x >= 58 && x <= 60)) color = RGB(31, 19, 24);
}
}
if ((x == 25 || x == 29 || x == 31) && y >= 76 && y <= 81)
color = RGB(7, 17, 7);
}
/* Cottage: chimney, individual roof tiles, beams, glass, and steps. */
if (x >= 154 && x < 204 && y >= 70 && y < 76) color = RGB(8, 17, 9);
if (x >= 170 && x < 185 && y >= 71 && y < 78)
color = y % 3 == 0 ? RGB(15, 16, 14) : RGB(24, 24, 19);
if (x >= 191 && x < 198 && y >= 27 && y < 39) {
color = ((y % 4 == 0) || (x == 194 && y % 8 < 4)) ? RGB(14, 10, 9) : RGB(22, 16, 13);
if (y == 27) color = RGB(10, 8, 8);
}
if (x >= 152 && x < 201 && y >= 49 && y < 72) {
color = RGB(29, 27, 20);
if (y < 52) color = RGB(21, 19, 14);
if (x < 155 || x >= 198 || y == 69 || y == 71) color = RGB(13, 10, 8);
if (x >= 171 && x < 184 && y >= 54) {
color = RGB(12, 8, 6);
if (door_open) {
color = RGB(3, 5, 7);
if (x == 171) color = RGB(23, 16, 10);
} else {
if (x >= 173 && x < 182 && y > 55) color = RGB(20, 13, 8);
if (x == 177) color = RGB(16, 10, 7);
if (x == 180 && y == 63) color = RGB(31, 25, 10);
}
}
if (((x >= 157 && x < 167) || (x >= 186 && x < 196)) && y >= 54 && y < 65) {
int wx = x >= 186 ? x - 186 : x - 157;
color = RGB(12, 11, 10);
if (wx > 0 && wx < 9 && y > 54 && y < 63) color = RGB(10, 21, 27);
if (wx > 1 && wx < 4 && y > 55 && y < 59) color = RGB(23, 30, 30);
if (wx == 4 || y == 59) color = RGB(21, 19, 14);
if (y == 64) color = RGB(26, 24, 18);
}
}
if (y >= 32 && y < 49 && x >= 147 + (48 - y) / 2 && x < 206 - (48 - y) / 2) {
color = RGB(25, 10, 10);
if ((y - 32) % 5 == 0 || (x + ((y - 32) / 5) * 4) % 9 == 0)
color = RGB(16, 7, 8);
else if ((y - 32) % 5 == 1) color = RGB(30, 15, 12);
if (y == 48) color = RGB(10, 8, 8);
}
/* Flowers have stems, petals, and golden centers. */
if ((x > 126 && x < 146 && y > 77 && y < 98) ||
(x > 28 && x < 72 && y > 95 && y < 104)) {
int fx = x % 7, fy = y % 8;
if (fx == 3 && fy >= 4 && fy <= 6) color = RGB(5, 16, 7);
if ((fx == 2 && fy == 5) || (fx == 4 && fy == 6)) color = RGB(7, 20, 8);
if ((fx == 3 && (fy == 1 || fy == 3)) || (fy == 2 && (fx == 2 || fx == 4)))
color = (x / 7) & 1 ? RGB(31, 20, 24) : RGB(30, 30, 25);
if (fx == 3 && fy == 2) color = RGB(31, 24, 6);
}
/* Decorative tall-grass tiles: walk through them, no encounters yet. */
if (x >= 24 && x < 80 && y >= 123 && y < 134) {
int tx = x & 7, ty = (y - 123) & 7;
if ((tx == 1 && ty >= 2 && ty <= 5) || (tx == 3 && ty >= 1 && ty <= 5) ||
(tx == 5 && ty >= 3 && ty <= 6)) color = RGB(4, 15, 8);
if ((tx == 2 && ty >= 3 && ty <= 6) || (tx == 4 && ty >= 2 && ty <= 6))
color = RGB(9, 22, 9);
if ((tx == 1 && ty == 1) || (tx == 3 && ty == 0)) color = RGB(22, 29, 12);
}
return color;
}
TEST_INTERNAL u16 mellon_garden_scenery(int x, int y)
{
u16 color = village_scenery(x, y);
if (y < 18 || y >= 142) return color;
/* This garden connects west to town, not east or south. */
if (x < 8 && y > 106 && y < 120) color = RGB(29, 27, 18);
if ((x >= 232 && y > 106 && y < 120) ||
(y >= 136 && x > 96 && x < 120)) color = RGB(9, 20, 10);
/* Melon beds take the place of the village pond. */
if (x >= 22 && x < 76 && y >= 44 && y < 86) {
color = RGB(20, 15, 9);
if (x == 22 || x == 75 || y == 44 || y == 85) color = RGB(29, 23, 14);
if (y % 12 == 0) color = RGB(13, 10, 7);
int mx = (x - 24) % 16 - 7, my = (y - 45) % 18 - 8;
if (mx * mx + my * my < 30) {
color = x % 4 < 2 ? RGB(9, 19, 8) : RGB(19, 28, 12);
if (my > 3) color = RGB(6, 14, 7);
}
if (my == -6 && mx == 0) color = RGB(6, 15, 7);
}
/* A blue roof makes the professor's house easy to recognize. */
if (x >= 147 && x < 206 && y >= 32 && y < 49) {
if (color == RGB(25, 10, 10)) color = RGB(8, 16, 24);
else if (color == RGB(16, 7, 8)) color = RGB(5, 10, 17);
else if (color == RGB(30, 15, 12)) color = RGB(14, 23, 29);
}
return color;
}
static u16 mellon_house_scenery(int x, int y)
{
u16 color = cottage_scenery(x, y);
if (color == RGB(22, 9, 12)) color = RGB(7, 17, 15);
else if (color == RGB(28, 15, 16)) color = RGB(12, 23, 20);
/* A little research computer on the desk at the back of the room. */
if (x >= 104 && x < 124 && y >= 36 && y < 54) {
color = RGB(10, 13, 16);
if (x > 106 && x < 121 && y > 38 && y < 49) color = RGB(9, 23, 26);
if (x > 108 && x < 118 && (y == 41 || y == 45)) color = RGB(23, 30, 26);
if (y >= 50 && (x < 112 || x > 116)) color = cottage_scenery(x, y);
}
/* An open notebook on the table. */
if (x >= 157 && x < 166 && y >= 83 && y < 91) {
color = x == 161 ? RGB(18, 19, 16) : RGB(31, 30, 25);
if ((y == 85 || y == 88) && x != 161) color = RGB(18, 23, 23);
}
return color;
}
TEST_INTERNAL 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);
if (current_room == MELLON_HOUSE) return mellon_house_scenery(x, y);
return village_scenery(x, y);
}
static void ensure_background(void)
{
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;
}
}
void restore(int x, int y, int w, int h)
{
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];
}
}
+146
View File
@@ -0,0 +1,146 @@
#include "game.h"
int partner_owned = 0;
TEST_INTERNAL int gift_conversation = 0;
int ball_count = 0, balls_given = 0;
TEST_INTERNAL int ball_gift_conversation = 0;
/* mGBA detects this marker and provides cartridge SRAM for the gift record. */
static const char save_signature[] __attribute__((used)) = "SRAM_V113";
#ifndef GAME_TEST
#define SAVE_MEMORY ((volatile unsigned char *)0x0e000000)
#endif
void load_progress(void)
{
partner_owned = ball_count = balls_given = 0;
if (SAVE_MEMORY[0] != 'F' || SAVE_MEMORY[1] != 'Q' ||
SAVE_MEMORY[2] != 'S' || SAVE_MEMORY[3] != 'P') return;
/* Keep Sprout from the original version-one save format. */
if (SAVE_MEMORY[4] == 1) {
partner_owned = SAVE_MEMORY[5] == 1 && SAVE_MEMORY[6] == 0xa5 && SAVE_MEMORY[7] == 0x5a;
return;
}
unsigned flags = SAVE_MEMORY[5], count = SAVE_MEMORY[6];
if (SAVE_MEMORY[4] != 2 || flags > 3 || count > 99 ||
(!(flags & 2) && count != 0) || SAVE_MEMORY[8] != 0x5a ||
SAVE_MEMORY[7] != (0xa5 ^ 2 ^ flags ^ count)) return;
partner_owned = !!(flags & 1);
balls_given = !!(flags & 2);
ball_count = (int)count;
}
static void save_progress(void)
{
unsigned flags = (partner_owned ? 1u : 0u) | (balls_given ? 2u : 0u);
/* Commit the first magic byte last so an interrupted write is invalid. */
SAVE_MEMORY[0] = 0;
SAVE_MEMORY[1] = 'Q';
SAVE_MEMORY[2] = 'S';
SAVE_MEMORY[3] = 'P';
SAVE_MEMORY[4] = 2;
SAVE_MEMORY[5] = (unsigned char)flags;
SAVE_MEMORY[6] = (unsigned char)ball_count;
SAVE_MEMORY[7] = (unsigned char)(0xa5 ^ 2 ^ flags ^ (unsigned)ball_count);
SAVE_MEMORY[8] = 0x5a;
SAVE_MEMORY[0] = 'F';
}
/* Change these lines to write Mila's conversation! Use CAPITAL letters. */
TEST_INTERNAL const char *const dialogue[][2] = {
{"HELLO I AM MILA", "WELCOME TO OUR VILLAGE"},
{"THAT GOLDEN CREATURE IS FRIENDLY", "WALK OVER AND SAY HELLO"}
};
enum { STORY_DIALOGUE_PAGES = sizeof(dialogue) / sizeof(dialogue[0]) };
#ifdef GAME_TEST
const int DIALOGUE_PAGES = STORY_DIALOGUE_PAGES;
#endif
#define DIALOGUE_PAGES STORY_DIALOGUE_PAGES
TEST_INTERNAL const char *const professor_dialogue[DIALOGUE_PAGES][2] = {
{"I HAVE A PARTNER FOR YOU", "MEET SPROUT A GRASS TYPE"},
{"YOU RECEIVED SPROUT", "TAKE GOOD CARE OF YOUR NEW FRIEND"}
};
TEST_INTERNAL const char *const professor_followup[DIALOGUE_PAGES][2] = {
{"HOW IS SPROUT DOING", "OPEN YOUR TEAM WITH SELECT"},
{"EXPLORE SUNPETAL TRAIL TOGETHER", "I AM GLAD YOU ARE FRIENDS"}
};
TEST_INTERNAL const char *const ranger_dialogue[DIALOGUE_PAGES][2] = {
{"HI I AM RANGER FINN", "HERE ARE FIVE POKE BALLS FOR YOU"},
{"YOU RECEIVED FIVE POKE BALLS", "KEEP THEM SAFE FOR YOUR ADVENTURE"}
};
TEST_INTERNAL const char *const ranger_followup[DIALOGUE_PAGES][2] = {
{"YOUR POKE BALLS ARE IN YOUR BAG", "OPEN YOUR TEAM WITH SELECT TO LOOK"},
{"ENJOY EXPLORING SUNPETAL TRAIL", "TAKE GOOD CARE OF YOUR PARTNER"}
};
int near_npc(int x, int y)
{
if (current_room == ROUTE)
return x >= RANGER_X - 24 && x <= RANGER_X + 24 &&
y >= RANGER_Y - 22 && y <= RANGER_Y + 22;
if (current_room == MELLON_HOUSE)
return x >= PROF_X - 24 && x <= PROF_X + 24 &&
y >= PROF_Y - 22 && y <= PROF_Y + 22;
return current_room == OUTSIDE && x >= NPC_X - 24 && x <= NPC_X + 24 &&
y >= NPC_Y - 20 && y <= NPC_Y + 20;
}
TEST_INTERNAL int next_dialogue_page(int page)
{
return page + 1 < DIALOGUE_PAGES ? page + 1 : -1;
}
int begin_conversation(int x, int y)
{
if (!near_npc(x, y)) return 0;
gift_conversation = current_room == MELLON_HOUSE && !partner_owned;
ball_gift_conversation = current_room == ROUTE && !balls_given;
return 1;
}
TEST_INTERNAL int receive_partner(void)
{
if (current_room != MELLON_HOUSE || !gift_conversation || partner_owned) return 0;
partner_owned = 1;
save_progress();
reset_follower(PROF_X, PROF_Y + 22);
return 1;
}
TEST_INTERNAL int receive_balls(void)
{
if (current_room != ROUTE || !ball_gift_conversation || balls_given) return 0;
ball_count += 5;
if (ball_count > 99) ball_count = 99;
balls_given = 1;
save_progress();
return 1;
}
int advance_conversation(int page)
{
int next = next_dialogue_page(page);
if (next == 1 && gift_conversation) receive_partner();
if (next == 1 && ball_gift_conversation) receive_balls();
if (next < 0) gift_conversation = ball_gift_conversation = 0;
return next;
}
void dialogue_box(int page)
{
u16 ink = RGB(5, 8, 11);
rectangle(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H, ink);
rectangle(9, 97, 222, 42, RGB(15, 20, 21));
rectangle(11, 99, 218, 38, RGB(30, 30, 26));
rectangle(12, 100, 216, 1, RGB(31, 31, 30));
rectangle(16, 110, 208, 1, RGB(23, 26, 22));
int professor = current_room == MELLON_HOUSE;
int ranger = current_room == ROUTE;
const char *const (*lines)[2] = ranger ?
(ball_gift_conversation ? ranger_dialogue : ranger_followup) : professor ?
(gift_conversation ? professor_dialogue : professor_followup) : dialogue;
large_text(16, 102, ranger ? "RANGER FINN" : professor ? "PROF MELLON" : "MILA", RGB(7, 16, 12));
text(192, 103, page == DIALOGUE_PAGES - 1 ? "A CLOSE" : "A NEXT", ink);
large_text(16, 115, lines[page][0], ink);
large_text(16, 127, lines[page][1], ink);
}
+206
View File
@@ -0,0 +1,206 @@
#include "game.h"
#ifndef GAME_TEST
/* Never erase a character on the visible screen. Finish drawing in RAM. */
u16 frame_buffer[WIDTH * HEIGHT];
#endif
u16 dirty_first[HEIGHT];
u16 dirty_end[HEIGHT];
void reset_dirty_rows(void)
{
for (int y = 0; y < HEIGHT; ++y) {
dirty_first[y] = WIDTH;
dirty_end[y] = 0;
}
}
void mark_dirty(int x, int y, int w, int h)
{
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;
for (int row = top; row < bottom; ++row) {
if (left < dirty_first[row]) dirty_first[row] = left;
if (right > dirty_end[row]) dirty_end[row] = right;
}
}
#ifdef GAME_TEST
unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
#endif
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) {
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
SCREEN[row * WIDTH + col] = color;
}
}
}
/* Tiny original 3-by-5 alphabet: one row of bits per letter. */
static const unsigned char alphabet[26][5] = {
{2,5,7,5,5}, {6,5,6,5,6}, {3,4,4,4,3}, {6,5,5,5,6},
{7,4,6,4,7}, {7,4,6,4,4}, {3,4,5,5,3}, {5,5,7,5,5},
{7,2,2,2,7}, {1,1,1,5,2}, {5,5,6,5,5}, {4,4,4,4,7},
{5,7,7,5,5}, {5,7,7,7,5}, {2,5,5,5,2}, {6,5,6,4,4},
{2,5,5,3,1}, {6,5,6,5,5}, {3,4,2,1,6}, {7,2,2,2,2},
{5,5,5,5,7}, {5,5,5,5,2}, {5,5,7,7,5}, {5,5,2,5,5},
{5,5,2,2,2}, {7,1,2,4,7}
};
static const unsigned char digit_font[10][5] = {
{7,5,5,5,7}, {2,6,2,2,7}, {6,1,2,4,7}, {6,1,2,1,6}, {5,5,7,1,1},
{7,4,6,1,6}, {3,4,7,5,7}, {7,1,2,2,2}, {7,5,7,5,7}, {7,5,7,1,6}
};
void text(int x, int y, const char *message, u16 color)
{
for (; *message; ++message, x += 4) {
const unsigned char *letter;
if (*message >= 'A' && *message <= 'Z') letter = alphabet[*message - 'A'];
else if (*message >= '0' && *message <= '9') letter = digit_font[*message - '0'];
else continue;
for (int row = 0; row < 5; ++row)
for (int col = 0; col < 3; ++col)
if (letter[row] & (4 >> col))
rectangle(x + col, y + row, 1, 1, color);
}
}
/* Larger, easy-to-read letters for the RPG conversation window. */
TEST_INTERNAL const unsigned char large_alphabet[26][7] = {
{14,17,17,31,17,17,17}, {30,17,17,30,17,17,30},
{14,17,16,16,16,17,14}, {30,17,17,17,17,17,30},
{31,16,16,30,16,16,31}, {31,16,16,30,16,16,16},
{14,17,16,23,17,17,14}, {17,17,17,31,17,17,17},
{14,4,4,4,4,4,14}, {7,2,2,2,18,18,12},
{17,18,20,24,20,18,17}, {16,16,16,16,16,16,31},
{17,27,21,21,17,17,17}, {17,25,21,19,17,17,17},
{14,17,17,17,17,17,14}, {30,17,17,30,16,16,16},
{14,17,17,17,21,18,13}, {30,17,17,30,20,18,17},
{15,16,16,14,1,1,30}, {31,4,4,4,4,4,4},
{17,17,17,17,17,17,14}, {17,17,17,17,17,10,4},
{17,17,17,21,21,21,10}, {17,17,10,4,10,17,17},
{17,17,10,4,4,4,4}, {31,1,2,4,8,16,31}
};
void large_text(int x, int y, const char *message, u16 color)
{
for (; *message; ++message, x += 6) {
if (*message < 'A' || *message > 'Z') continue;
const unsigned char *letter = large_alphabet[*message - 'A'];
for (int row = 0; row < 7; ++row)
for (int col = 0; col < 5; ++col)
if (letter[row] & (16 >> col))
rectangle(x + col, y + row, 1, 1, color);
}
}
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;
/* 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. */
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;
}
}
}
}
}
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);
}
+4 -5
View File
@@ -2,7 +2,7 @@
#define WILD_CREATURES_H #define WILD_CREATURES_H
/* Original 24x24 pixel art. Dots are transparent; digits select colors. */ /* Original 24x24 pixel art. Dots are transparent; digits select colors. */
static const char *const fernfox_art[] = { TEST_INTERNAL const char *const fernfox_art[] = {
"........................", "........................",
"....1.............1.....", "....1.............1.....",
"...131...........131....", "...131...........131....",
@@ -29,7 +29,7 @@ static const char *const fernfox_art[] = {
".......1...1...1........" ".......1...1...1........"
}; };
static const char *const emberwing_art[] = { TEST_INTERNAL const char *const emberwing_art[] = {
".......11.......11......", ".......11.......11......",
"......1411.....1141.....", "......1411.....1141.....",
"....1121111...111211....", "....1121111...111211....",
@@ -56,7 +56,7 @@ static const char *const emberwing_art[] = {
"........................" "........................"
}; };
static const char *const pebblit_art[] = { TEST_INTERNAL const char *const pebblit_art[] = {
"........................", "........................",
"..............1.........", "..............1.........",
".......1.....161........", ".......1.....161........",
@@ -96,8 +96,7 @@ static const u16 pebblit_palette[] = {
RGB(9, 23, 26), RGB(26, 31, 30), RGB(11, 11, 15), RGB(18, 25, 28) RGB(9, 23, 26), RGB(26, 31, 30), RGB(11, 11, 15), RGB(18, 25, 28)
}; };
struct WildArt { const char *const *rows; const u16 *palette; }; TEST_INTERNAL const struct WildArt wild_art[] = {
static const struct WildArt wild_art[] = {
{fernfox_art, fernfox_palette}, {fernfox_art, fernfox_palette},
{emberwing_art, emberwing_palette}, {emberwing_art, emberwing_palette},
{pebblit_art, pebblit_palette} {pebblit_art, pebblit_palette}
+224
View File
@@ -0,0 +1,224 @@
#include "game.h"
static struct { unsigned last, fraction; } movement_clock;
TEST_INTERNAL unsigned diagonal_fraction;
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. */
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;
}
enum Room current_room = OUTSIDE;
int door_open = 0;
enum Facing player_facing = FACING_DOWN;
static short trail_x[FOLLOW_STEPS], trail_y[FOLLOW_STEPS];
TEST_INTERNAL int trail_head;
int follower_x, follower_y, follower_ready;
void reset_follower(int x, int y)
{
for (int i = 0; i < FOLLOW_STEPS; ++i) {
trail_x[i] = (short)x;
trail_y[i] = (short)y;
}
trail_head = 0;
follower_x = x;
follower_y = y;
follower_ready = 1;
}
TEST_INTERNAL void move_follower(int old_x, int old_y, int x, int y)
{
if (!partner_owned || (old_x == x && old_y == y)) return;
if (!follower_ready) reset_follower(old_x, old_y);
trail_x[trail_head] = (short)old_x;
trail_y[trail_head] = (short)old_y;
trail_head = (trail_head + 1) % FOLLOW_STEPS;
follower_x = trail_x[trail_head];
follower_y = trail_y[trail_head];
}
int indoors(void)
{
return current_room == COTTAGE || current_room == MELLON_HOUSE;
}
TEST_INTERNAL enum Facing facing_for_keys(unsigned keys, enum Facing previous)
{
if (keys & RIGHT) return FACING_RIGHT;
if (keys & LEFT) return FACING_LEFT;
if (keys & DOWN) return FACING_DOWN;
if (keys & UP) return FACING_UP;
return previous;
}
int near_door(int x, int y)
{
if (indoors())
return x >= 110 && x <= 130 && y >= 125 && y <= 131;
return (current_room == OUTSIDE || current_room == MELLON_GARDEN) &&
x >= 169 && x <= 185 && y >= 78 && y <= 89;
}
int near_map_exit(int x, int y)
{
if (current_room == OUTSIDE && x >= 216 && x <= 222 && y >= 108 && y <= 117)
return 1;
if (current_room == MELLON_GARDEN)
return x >= 17 && x <= 24 && y >= 108 && y <= 117;
if (x < 102 || x > 114) return 0;
if (current_room == OUTSIDE) return y >= 124 && y <= 131;
if (current_room == ROUTE) return y >= 47 && y <= 54;
return 0;
}
/* Walk across a map boundary. Arrivals are away from the return trigger. */
TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys)
{
if (!near_map_exit(*x, *y)) return 0;
if (current_room == OUTSIDE && *y == 131 && (keys & DOWN)) {
current_room = ROUTE;
*x = 108;
*y = 60;
player_facing = FACING_DOWN;
} else if (current_room == ROUTE && *y == 47 && (keys & UP)) {
current_room = OUTSIDE;
*x = 108;
*y = 125;
player_facing = FACING_UP;
} else if (current_room == OUTSIDE && *x == 222 && (keys & RIGHT)) {
current_room = MELLON_GARDEN;
*x = 31;
*y = 113;
player_facing = FACING_RIGHT;
} else if (current_room == MELLON_GARDEN && *x == 17 && (keys & LEFT)) {
current_room = OUTSIDE;
*x = 210;
*y = 113;
player_facing = FACING_LEFT;
} else {
return 0;
}
door_open = 0;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1;
}
int near_route_sign(int x, int y)
{
return current_room == ROUTE && x >= 121 && x <= 151 && y >= 77 && y <= 87;
}
/* Only the doorway can change maps; both arrivals are on safe floor. */
int use_door(int *x, int *y)
{
if (!near_door(*x, *y)) return 0;
if (current_room == OUTSIDE) {
current_room = COTTAGE;
*x = 120;
*y = 119;
} else if (current_room == COTTAGE) {
current_room = OUTSIDE;
*x = 177;
*y = 83;
} else if (current_room == MELLON_GARDEN) {
current_room = MELLON_HOUSE;
*x = 120;
*y = 119;
} else {
current_room = MELLON_GARDEN;
*x = 177;
*y = 83;
}
door_open = 0;
player_facing = FACING_DOWN;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1;
}
void reset_position(int *x, int *y)
{
*x = indoors() ? 120 : current_room == MELLON_GARDEN ? 31 : 108;
*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);
}
TEST_INTERNAL int can_walk(int x, int y)
{
if (current_room == ROUTE) {
if (x < 32 || x > 207 || y < 47 || y > 131) return 0;
if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */
if (x > 116 && x < 154 && y > 53 && y < 79) return 0; /* Sign. */
if (x > 28 && x < 72 && y > 104 && y < 130) return 0; /* Boulder. */
if (x > RANGER_X - 13 && x < RANGER_X + 13 &&
y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0;
return 1;
}
if (indoors()) {
if (x < 34 || x > 205 || y < 75 || y > 131) return 0;
if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */
if (x > 141 && x < 196 && y < 109) return 0; /* Table. */
if (x > 187 && y < 85) return 0; /* Plant. */
if (current_room == MELLON_HOUSE && x > PROF_X - 13 && x < PROF_X + 13 &&
y > PROF_Y - 9 && y < PROF_Y + 9) return 0;
return 1;
}
if (x < 17 || x > 222 || y < 47 || y > 131) return 0;
if (x > 16 && x < 82 && y > 42 && y < 92) return 0;
if (x > 141 && x < 212 && y < 78) return 0;
if (current_room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 &&
y > NPC_Y - 9 && y < NPC_Y + 9) return 0;
return 1;
}
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;
}
+223
View File
@@ -0,0 +1,223 @@
#include "test_support.h"
void check_following_and_battle(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
static const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
partner_owned = 1;
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) {
current_room = (enum Room)room;
int x, y;
reset_position(&x, &y);
if (current_room == ROUTE) { y = 94; reset_follower(x, y); }
assert(follower_x == x && follower_y == y);
render_scene(x, y);
copy_pending_rows(visible);
for (int frame = 0; frame < 100; ++frame) {
int old_x = x, old_y = y;
int old_fx = follower_x, old_fy = follower_y;
int old_head = trail_head;
unsigned keys = directions[(frame / 10) % 4];
player_facing = facing_for_keys(keys, player_facing);
int nx = x + !!(keys & RIGHT) - !!(keys & LEFT);
int ny = y + !!(keys & DOWN) - !!(keys & UP);
if (can_walk(nx, y)) x = nx;
if (can_walk(x, ny)) y = ny;
move_follower(old_x, old_y, x, y);
assert(can_walk(follower_x, follower_y));
if (old_x == x && old_y == y) assert(trail_head == old_head);
restore(old_x - 8, old_y - 20, 17, 24);
restore(old_fx - 8, old_fy - 20, 17, 24);
actors(x, y, 0);
location_status(x, y);
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
memcpy(expected, SCREEN, sizeof(expected));
render_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
}
}
current_room = OUTSIDE;
reset_follower(108, 103);
for (int step = 0; step < 20; ++step) move_follower(108 + step, 103, 109 + step, 103);
assert(follower_x == 114 && follower_y == 103); /* Exactly 14 steps behind. */
int head = trail_head;
move_follower(128, 103, 128, 103);
assert(trail_head == head);
int x = 108, y = 131;
assert(use_map_exit(&x, &y, DOWN));
assert(follower_x == x && follower_y == y && current_room == ROUTE);
current_room = OUTSIDE;
reset_follower(108, 103);
x = 108;
y = 103;
for (int step = 0; step < 20; ++step) {
move_follower(x, y, x + 1, y);
++x;
}
render_scene(x, y);
write_preview("build/follower-preview.ppm");
partner_owned = 0;
assert(!begin_battle(182, 113));
partner_owned = 1;
current_room = ROUTE;
assert(!begin_battle(182, 113));
current_room = OUTSIDE;
assert(!begin_battle(108, 103));
assert(!near_golden(162, 113));
assert(begin_battle(182, 113));
assert(!begin_battle(182, 113));
assert(battle.phase == BATTLE_TURN && battle.sprout_hp == SPROUT_HP &&
battle.enemy_hp == GOLDEN_HP);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
draw_battle();
write_preview("build/battle-preview.ppm");
copy_pending_rows(visible);
assert(!battle_input(0));
assert(!battle_input(SELECT));
assert(battle.turns == 0);
assert(battle_input(RIGHT));
assert(battle.move == 1 && battle.enemy_hp == GOLDEN_HP);
assert(battle_input(A));
assert(battle.enemy_hp == 11 && battle.sprout_hp == 19 && battle.turns == 1);
draw_battle_hud();
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
memcpy(expected, SCREEN, sizeof(expected));
draw_battle();
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(!battle_input(0)); /* A held key without a new press adds no turn. */
assert(battle.turns == 1);
assert(battle_input(A));
assert(battle.enemy_hp == 4 && battle.sprout_hp == 14);
assert(battle_input(A));
assert(battle.phase == BATTLE_WON && battle.enemy_hp == 0 && battle.sprout_hp == 14);
draw_battle_hud();
assert(pending_pixels() < 16500);
write_preview("build/battle-win-preview.ppm");
assert(!battle_input(RIGHT));
assert(battle_input(A) && battle.phase == BATTLE_OFF);
assert(begin_battle(182, 113));
assert(battle_input(B) && battle.phase == BATTLE_FLED);
assert(battle.sprout_hp == SPROUT_HP && battle.enemy_hp == GOLDEN_HP);
assert(battle_input(B) && battle.phase == BATTLE_OFF);
assert(begin_battle(182, 113));
battle.sprout_hp = 1;
assert(battle_input(A) && battle.phase == BATTLE_LOST);
assert(battle.sprout_hp == 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
assert(begin_battle(182, 113));
assert(battle.sprout_hp == SPROUT_HP); /* Free recovery after each battle. */
for (int turn = 0; turn < 5; ++turn) assert(battle_input(A));
assert(battle.phase == BATTLE_WON && battle.sprout_hp == 4 && battle.enemy_hp == 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
assert(partner_owned && memcmp(saved, test_save, sizeof(saved)) == 0);
partner_owned = 0;
follower_ready = 0;
player_facing = FACING_DOWN;
}
void check_wild_encounters(void)
{
for (int kind = 0; kind < 3; ++kind) {
int opaque = 0;
for (int row = 0; row < 24; ++row) {
assert(strlen(wild_art[kind].rows[row]) == 24);
for (int col = 0; col < 24; ++col) {
char pixel = wild_art[kind].rows[row][col];
if (pixel == '.') continue;
assert(pixel >= '1' && pixel <= '8');
++opaque;
}
}
assert(opaque > 180); /* Full original sprites, not palette swaps. */
}
partner_owned = 1;
current_room = OUTSIDE;
assert(!in_tall_grass(48, 70));
assert(!check_grass_encounter(48, 70, 49, 70));
current_room = ROUTE;
assert(in_tall_grass(32, 53) && in_tall_grass(80, 86));
assert(in_tall_grass(151, 112) && in_tall_grass(207, 132));
assert(!in_tall_grass(31, 70) && !in_tall_grass(81, 70));
assert(!in_tall_grass(48, 87) && !in_tall_grass(108, 94));
partner_owned = 0;
for (int i = 0; i < 100; ++i) assert(!check_grass_encounter(48, 70, 49, 70));
assert(grass_steps == 0);
partner_owned = 1;
for (int i = 0; i < 100; ++i) assert(!check_grass_encounter(48, 70, 48, 70));
assert(grass_steps == 0); /* Waiting in the grass never starts a battle. */
for (int i = 0; i < 100; ++i) assert(!check_grass_encounter(108, 94, 109, 94));
assert(grass_steps == 0);
unsigned seen = 0;
for (unsigned trial = 1; trial <= 64; ++trial) {
random_state = trial * 719u;
grass_steps = 0;
int steps;
for (steps = 1; steps <= 64; ++steps) {
int x = 48 + (steps & 1), old_x = 48 + ((steps + 1) & 1);
if (check_grass_encounter(old_x, 70, x, 70)) break;
assert(battle.phase == BATTLE_OFF);
}
assert(steps >= 24 && steps <= 64);
assert(battle.enemy >= FERNFOX && battle.enemy <= PEBBLIT);
assert(battle.phase == BATTLE_TURN && battle.enemy_hp == species[battle.enemy].hp);
assert(grass_steps == 0);
seen |= 1u << battle.enemy;
assert(!check_grass_encounter(48, 70, 49, 70)); /* No nested battle. */
assert(battle_input(B) && battle.phase == BATTLE_FLED);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
for (int i = 0; i < 23; ++i) assert(!check_grass_encounter(48, 70, 49, 70));
assert(!check_grass_encounter(80, 70, 81, 70));
assert(grass_steps == 0); /* Leaving the grass resets the meter. */
}
assert(seen == ((1u << FERNFOX) | (1u << EMBERWING) | (1u << PEBBLIT)));
int right_steps = 0;
while (++right_steps <= 64)
if (check_grass_encounter(180, 121, 181, 121)) break;
assert(right_steps <= 64 && battle.phase == BATTLE_TURN);
assert(battle_input(B) && battle_input(A));
assert(battle.phase == BATTLE_OFF);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
static u16 visible[WIDTH * HEIGHT];
const char *previews[] = {
"build/wild-fernfox-preview.ppm", "build/wild-emberwing-preview.ppm", "build/wild-pebblit-preview.ppm"
};
for (int kind = FERNFOX; kind <= PEBBLIT; ++kind) {
assert(start_battle((enum Species)kind));
assert(battle.enemy == (enum Species)kind);
draw_battle();
write_preview(previews[kind - FERNFOX]);
copy_pending_rows(visible);
assert(battle_input(RIGHT));
int turns = 0;
while (battle.phase == BATTLE_TURN && turns < 10) {
assert(battle_input(A));
++turns;
assert(battle.enemy_hp >= 0 && battle.enemy_hp <= species[kind].hp);
assert(battle.sprout_hp >= 0 && battle.sprout_hp <= SPROUT_HP);
draw_battle_hud();
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
assert(battle.phase == BATTLE_WON && battle.sprout_hp > 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
}
assert(!start_battle(SPECIES_COUNT));
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
random_state = 0;
assert(next_random() != 0);
partner_owned = 0;
grass_steps = 0;
current_room = OUTSIDE;
}
+1 -1045
View File
File diff suppressed because it is too large. Load diff
+221
View File
@@ -0,0 +1,221 @@
#include "test_support.h"
void check_buffered_walking(void)
{
static u16 visible[WIDTH * HEIGHT], before[WIDTH * HEIGHT];
static const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
reset_dirty_rows();
mark_dirty(-2, -2, 4, 4);
assert(pending_pixels() == 4);
reset_dirty_rows();
mark_dirty(239, 159, 4, 4);
assert(pending_pixels() == 1);
reset_dirty_rows();
mark_dirty(-8, 0, 2, 2);
mark_dirty(240, 160, 4, 4);
assert(pending_pixels() == 0);
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) {
current_room = (enum Room)room;
player_facing = FACING_DOWN;
int x, y;
reset_position(&x, &y);
if (current_room == ROUTE) y = 94;
render_scene(x, y);
assert(pending_pixels() == WIDTH * HEIGHT);
copy_pending_rows(visible);
for (int frame = 0; frame < 80; ++frame) {
memcpy(before, visible, sizeof(before));
unsigned keys = directions[(frame / 10) % 4];
int old_x = x, old_y = y;
player_facing = facing_for_keys(keys, player_facing);
int nx = x + !!(keys & RIGHT) - !!(keys & LEFT);
int ny = y + !!(keys & DOWN) - !!(keys & UP);
if (can_walk(nx, y)) x = nx;
if (can_walk(x, ny)) y = ny;
restore(old_x - 8, old_y - 20, 17, 24);
actors(x, y, (frame >> 3) & 1);
if (near_door(old_x, old_y) != near_door(x, y) ||
near_map_exit(old_x, old_y) != near_map_exit(x, y) ||
near_route_sign(old_x, old_y) != near_route_sign(x, y)) location_status(x, y);
assert(pending_pixels() < 16500);
/* Neither erasing nor drawing the head may touch the display. */
assert(memcmp(before, visible, sizeof(before)) == 0);
copy_pending_rows(visible);
assert(pending_pixels() == 0);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
status("TALKING TO MILA");
dialogue_box(0);
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(x, y, 0);
location_status(x, y);
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
void check_route_travel(void)
{
static u16 visible[WIDTH * HEIGHT];
current_room = OUTSIDE;
assert(reachable(108, 103, 108, 131));
int x = 80, y = 131;
assert(!use_map_exit(&x, &y, DOWN)); /* No exit through the fence. */
x = 108;
assert(!use_map_exit(&x, &y, UP)); /* Must walk out, not back in. */
y = 130;
assert(!use_map_exit(&x, &y, DOWN)); /* Not across the edge yet. */
y = 131;
assert(!use_map_exit(&x, &y, A)); /* No button press needed. */
for (int trip = 0; trip < 5; ++trip) {
x = 108;
y = 131;
assert(use_map_exit(&x, &y, DOWN));
assert(current_room == ROUTE && x == 108 && y == 60);
assert(player_facing == FACING_DOWN && can_walk(x, y));
assert(!use_map_exit(&x, &y, DOWN)); /* Held direction cannot bounce. */
assert(!near_door(177, 83));
assert(!near_npc(NPC_X, NPC_Y));
assert(!use_door(&x, &y));
assert(!can_walk(175, 60)); /* Pond. */
assert(!can_walk(135, 65)); /* Sign. */
assert(!can_walk(49, 114)); /* Boulder. */
assert(!can_walk(20, 94)); /* Forest boundary. */
assert(!can_walk(108, 132)); /* No unfinished south exit. */
assert(can_walk(48, 70)); /* Grass patches are walkable. */
assert(can_walk(183, 121));
assert(near_route_sign(135, 80));
assert(!near_route_sign(108, 54));
assert(reachable(x, y, 135, 80));
assert(reachable(x, y, 183, 121));
assert(reachable(x, y, 108, 47));
render_scene(x, y);
assert(pending_pixels() == WIDTH * HEIGHT);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
assert(SCREEN[NPC_Y * WIDTH + NPC_X] == route_scenery(NPC_X, NPC_Y));
assert(SCREEN[95 * WIDTH + 181] == route_scenery(181, 95));
write_preview("build/route-preview.ppm");
x = 180;
y = 100;
reset_position(&x, &y);
assert(current_room == ROUTE && x == 108 && y == 60);
assert(player_facing == FACING_DOWN);
for (; y > 47; --y) assert(can_walk(x, y));
assert(!use_map_exit(&x, &y, DOWN));
assert(use_map_exit(&x, &y, UP));
assert(current_room == OUTSIDE && x == 108 && y == 125);
assert(player_facing == FACING_UP && can_walk(x, y));
assert(!use_map_exit(&x, &y, UP));
assert(reachable(x, y, 108, 103));
assert(reachable(x, y, 177, 83)); /* House still reachable on return. */
render_scene(x, y);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
current_room = COTTAGE;
x = 108;
y = 131;
assert(!use_map_exit(&x, &y, DOWN)); /* Cottage keeps its own exit. */
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
void check_mellon_visit(void)
{
static u16 expected[WIDTH * HEIGHT];
current_room = OUTSIDE;
assert(reachable(108, 103, 222, 113));
int x = 222, y = 100;
assert(!use_map_exit(&x, &y, RIGHT)); /* No passage through the fence. */
y = 113;
assert(!use_map_exit(&x, &y, LEFT));
for (int visit = 0; visit < 5; ++visit) {
x = 222;
y = 113;
assert(use_map_exit(&x, &y, RIGHT));
assert(current_room == MELLON_GARDEN && x == 31 && y == 113);
assert(player_facing == FACING_RIGHT && can_walk(x, y));
assert(!use_map_exit(&x, &y, RIGHT));
assert(!near_npc(NPC_X, NPC_Y));
assert(reachable(x, y, 177, 83));
assert(reachable(x, y, 17, 113));
assert(!can_walk(40, 65)); /* Melon beds. */
reset_position(&x, &y);
assert(x == 31 && y == 113);
render_scene(x, y);
write_preview("build/mellon-garden-preview.ppm");
assert(village_scenery(160, 41) != mellon_garden_scenery(160, 41));
x = 177;
y = 83;
assert(near_door(x, y));
assert(use_door(&x, &y));
assert(current_room == MELLON_HOUSE && x == 120 && y == 119);
assert(can_walk(x, y) && !near_door(x, y));
assert(!can_walk(PROF_X, PROF_Y));
assert(near_npc(PROF_X, PROF_Y + 22));
assert(!near_npc(PROF_X, PROF_Y + 23));
assert(reachable(x, y, PROF_X, PROF_Y + 22));
assert(reachable(x, y, 120, 125));
assert(!near_map_exit(222, 113));
reset_position(&x, &y);
assert(x == 120 && y == 119 && current_room == MELLON_HOUSE);
render_scene(x, y);
write_preview("build/mellon-house-preview.ppm");
/* Repainting near either side of the professor must not leave trails. */
const int positions[][2] = {{100, 92}, {126, 92}, {113, 101}, {113, 83}};
for (unsigned i = 0; i < sizeof(positions) / sizeof(positions[0]); ++i) {
int nx = positions[i][0], ny = positions[i][1];
assert(can_walk(nx, ny));
render_scene(nx, ny);
memcpy(expected, SCREEN, sizeof(expected));
render_scene(x, y);
restore(x - 8, y - 20, 17, 24);
actors(nx, ny, 0);
location_status(nx, ny);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
dialogue_box(0);
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(nx, ny, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
}
render_scene(PROF_X, PROF_Y + 22);
dialogue_box(0);
write_preview("build/mellon-dialogue-preview.ppm");
for (int page = 0; page < DIALOGUE_PAGES; ++page)
for (int line = 0; line < 2; ++line) {
const char *message = professor_dialogue[page][line];
assert(strlen(message) <= 34);
for (; *message; ++message)
assert(*message == ' ' || (*message >= 'A' && *message <= 'Z'));
}
x = 120;
y = 125;
assert(use_door(&x, &y));
assert(current_room == MELLON_GARDEN && x == 177 && y == 83);
assert(can_walk(x, y) && reachable(x, y, 17, 113));
x = 17;
y = 113;
assert(!use_map_exit(&x, &y, RIGHT));
assert(use_map_exit(&x, &y, LEFT));
assert(current_room == OUTSIDE && x == 210 && y == 113);
assert(player_facing == FACING_LEFT && can_walk(x, y));
assert(!use_map_exit(&x, &y, LEFT));
assert(reachable(x, y, 108, 131)); /* South route still accessible. */
assert(reachable(x, y, 177, 83)); /* Original cottage still accessible. */
}
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
+134
View File
@@ -0,0 +1,134 @@
#include "test_support.h"
void check_movement_clock_and_speed(void)
{
const int render_rates[] = {15, 20, 30, 60, 120};
for (int following = 0; following < 2; ++following)
for (unsigned rate = 0; rate < sizeof(render_rates) / sizeof(render_rates[0]); ++rate) {
current_room = OUTSIDE;
partner_owned = following;
int x = 40, y = 125, total_ticks = 0;
reset_follower(x, y);
pause_movement_at(0);
for (int frame = 1; frame <= render_rates[rate]; ++frame) {
unsigned now = (unsigned)(frame * MOVEMENT_CLOCK_HZ / render_rates[rate]);
int ticks = movement_ticks_at(now);
total_ticks += ticks;
assert(walk_movement(RIGHT, &x, &y, ticks) == WALK_NORMAL);
}
assert(total_ticks == MOVEMENT_HZ && x == 160 && y == 125);
if (following) assert(follower_x == x - FOLLOW_STEPS && follower_y == y);
}
/* Timer rollover, idle reads, bounded catch-up, and paused menus. */
pause_movement_at(0xffffff00u);
assert(movement_ticks_at(0x00000100u) == 1);
assert(movement_ticks_at(0x00000100u) == 0);
pause_movement_at(0);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == MAX_MOVEMENT_TICKS);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == 0); /* No hidden backlog. */
pause_movement_at(123456u);
assert(movement_ticks_at(123456u) == 0);
pause_movement_at(999999u); /* Time spent in a menu is discarded. */
assert(movement_ticks_at(999999u) == 0);
assert(movement_ticks_at(999999u + MOVEMENT_CLOCK_HZ / 60 + 1) == 1);
partner_owned = 0;
current_room = OUTSIDE;
int x = 108, y = 103;
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_RIGHT);
assert(walk_movement(UP, &x, &y, 0) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_UP);
x = 132; y = 106;
assert(walk_movement(RIGHT, &x, &y, MAX_MOVEMENT_TICKS) == WALK_NORMAL);
assert(x == 133 && can_walk(x, y)); /* Never tunnel through Mila. */
x = 84; y = 70;
assert(walk_movement(LEFT, &x, &y, 2) == WALK_NORMAL);
assert(x == 82 && can_walk(x, y)); /* Stop at the pond, pixel by pixel. */
x = 177; y = 80;
assert(walk_movement(UP, &x, &y, 2) == WALK_NORMAL);
assert(y == 78 && near_door(x, y)); /* Door remains usable. */
pause_movement_at(0);
x = 160; y = 90;
for (int tick = 0; tick < 20; ++tick)
assert(walk_movement(RIGHT | DOWN, &x, &y, 1) == WALK_NORMAL);
assert(x == 188 && y == 118); /* Approximately 40 pixels, not 56 diagonally. */
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 190 && diagonal_fraction == 0);
assert(walk_movement(RIGHT | LEFT, &x, &y, 5) == WALK_NORMAL);
assert(x == 190 && y == 118);
/* Crossing an exit stops remaining substeps instead of skipping its trigger. */
partner_owned = 1;
x = 108; y = 129;
reset_follower(x, y);
assert(walk_movement(DOWN, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == ROUTE && x == 108 && y == 60);
assert(follower_x == x && follower_y == y);
y = 48;
assert(walk_movement(UP, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 108 && y == 125);
x = 221; y = 113;
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_MAP_CHANGED);
assert(current_room == MELLON_GARDEN && x == 31 && y == 113);
x = 18;
assert(walk_movement(LEFT, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 210 && y == 113);
current_room = COTTAGE;
x = 120; y = 125;
assert(walk_movement(DOWN, &x, &y, 5) == WALK_NORMAL);
assert(current_room == COTTAGE && y == 131); /* Indoor exits still require A. */
/* Two-pixel ticks still count every traveled substep for grass and following. */
current_room = ROUTE;
x = 40; y = 70;
reset_follower(x, y);
grass_steps = 0;
random_state = 1; /* First roll does not trigger an encounter. */
for (int tick = 0; tick < 12; ++tick)
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 64 && grass_steps == 24 && follower_x == 50 && follower_y == 70);
grass_steps = 63; /* Next traveled pixel guarantees an encounter. */
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_ENCOUNTER);
assert(x == 65 && y == 70 && battle.phase == BATTLE_TURN);
assert(battle_input(B) && battle_input(A) && battle.phase == BATTLE_OFF);
/* Faster and catch-up frames must erase both old sprites completely. */
static u16 snapshot[WIDTH * HEIGHT];
const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
const int tick_counts[] = {1, 2, 1, 3, 0};
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) {
current_room = (enum Room)room;
reset_position(&x, &y);
if (current_room == OUTSIDE) { x = 40; y = 125; }
if (current_room == ROUTE) y = 94;
reset_follower(x, y);
render_scene(x, y);
reset_dirty_rows();
int phase = 0;
for (int frame = 0; frame < 40; ++frame) {
int old_x = x, old_y = y, old_fx = follower_x, old_fy = follower_y;
int ticks = tick_counts[frame % 5];
assert(walk_movement(directions[frame / 10], &x, &y, ticks) == WALK_NORMAL);
restore(old_x - 8, old_y - 20, 17, 24);
restore(old_fx - 8, old_fy - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
location_status(x, y);
assert(pending_pixels() < 16500);
memcpy(snapshot, SCREEN, sizeof(snapshot));
render_scene(x, y);
restore(x - 8, y - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
assert(memcmp(snapshot, SCREEN, sizeof(snapshot)) == 0);
reset_dirty_rows();
phase += ticks;
}
}
current_room = OUTSIDE;
partner_owned = follower_ready = 0;
grass_steps = 0;
player_facing = FACING_DOWN;
pause_movement_at(0);
puts("Movement: 120 pixels/second at 15/20/30/60/120 render updates, with or without Sprout");
}
+187
View File
@@ -0,0 +1,187 @@
#include "test_support.h"
void check_background_cache(void)
{
static u16 reference[WIDTH * HEIGHT];
background_valid = 0;
scenery_samples = 0;
reset_dirty_rows();
restore(-10, -10, 2, 2);
restore(240, 160, 4, 4);
assert(!background_valid && scenery_samples == 0 && pending_pixels() == 0);
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) {
current_room = (enum Room)room;
door_open = 0;
unsigned before = scenery_samples;
restore(0, 0, WIDTH, HEIGHT);
assert(scenery_samples - before == WIDTH * HEIGHT);
assert(background_valid && background_room == current_room);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
reference[y * WIDTH + x] = scenery(x, y);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
/* Reference cost: every 17x24 restore previously evaluated 408 pixels. */
before = scenery_samples;
for (int y = 63; y < 87; ++y)
for (int x = 100; x < 117; ++x) (void)scenery(x, y);
assert(scenery_samples - before == 408);
before = scenery_samples;
for (int frame = 0; frame < 500; ++frame) {
restore(100, 63, 17, 24);
restore(120, 63, 17, 24); /* Same restoration work with Sprout. */
}
assert(scenery_samples == before); /* Zero procedural work while walking. */
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
/* Actors and menu graphics must never leak into the scenery cache. */
int x, y;
reset_position(&x, &y);
actors(x, y, 0);
draw_team();
restore(0, 0, WIDTH, HEIGHT);
assert(scenery_samples == before);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
}
const enum Room houses[] = {OUTSIDE, MELLON_GARDEN};
for (unsigned i = 0; i < sizeof(houses) / sizeof(houses[0]); ++i) {
current_room = houses[i];
door_open = 0;
restore(0, 0, WIDTH, HEIGHT);
for (int open = 1; open >= 0; --open) {
door_open = open;
unsigned before = scenery_samples;
restore(170, 54, 15, 24);
assert(scenery_samples - before == 13 * 18); /* Patch only the door. */
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
reference[y * WIDTH + x] = scenery(x, y);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
before = scenery_samples;
restore(170, 54, 15, 24);
assert(scenery_samples == before);
}
}
/* Clip once before copying, without altering pixels outside the request. */
current_room = OUTSIDE;
restore(0, 0, WIDTH, HEIGHT);
const u16 marker = RGB(1, 2, 3);
rectangle(0, 0, WIDTH, HEIGHT, marker);
reset_dirty_rows();
unsigned before = scenery_samples;
restore(-3, -2, 5, 4);
restore(239, 159, 5, 4);
restore(0, 0, 0, 10);
restore(0, 0, 10, 0);
assert(scenery_samples == before && pending_pixels() == 5);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x) {
u16 expected = ((x < 2 && y < 2) || (x == 239 && y == 159)) ?
background_cache[y * WIDTH + x] : marker;
assert(SCREEN[y * WIDTH + x] == expected);
}
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
puts("Background cache: 5 maps match reference; warm restores use 0 scenery evaluations (was 408 each)");
}
/* Reference implementation of the previous per-pixel rectangle renderer. */
static void reference_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored)
{
for (int row = 0; row < rows; ++row)
for (int col = 0; col < width; ++col) {
char pixel = art[row][col];
if (pixel == '.') continue;
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
rectangle(x + (mirrored ? width - 1 - col : col) * scale, y + row * scale,
scale, scale, palette[index]);
}
}
static void sprite_test_canvas(void)
{
for (int i = 0; i < WIDTH * HEIGHT; ++i) SCREEN[i] = (u16)((i * 3 ^ 0x5a5a) & 0x7fff);
reset_dirty_rows();
}
void check_sprite_rows(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
static const char *const zero_art[] = {"0A..B1..", "..1.....", "........"};
const struct { const char *const *art; int width, rows; } images[] = {
{person_art, 16, 20}, {person_back_art, 16, 20}, {person_side_art, 16, 20},
{creature_art, 16, 16}, {fernfox_art, 24, 24}, {emberwing_art, 24, 24},
{pebblit_art, 24, 24}, {zero_art, 8, 3}
};
const int positions[][2] = {
{70, 60}, {-8, 40}, {232, 40}, {80, -12}, {80, 155},
{-5, -7}, {236, 156}, {-100, -100}, {240, 30}, {50, 160}
};
u16 palette[13], alternate[13];
for (int i = 0; i < 13; ++i) {
palette[i] = RGB(i, i * 2, 31 - i);
alternate[i] = RGB(31 - i, i, i * 2);
}
palette[0] = 0; /* Palette entry zero is opaque black, not transparency. */
for (unsigned image = 0; image < sizeof(images) / sizeof(images[0]); ++image)
for (int scale = 1; scale <= 3; ++scale)
for (int mirror = 0; mirror < 2; ++mirror)
for (unsigned p = 0; p < sizeof(positions) / sizeof(positions[0]); ++p) {
int x = positions[p][0], y = positions[p][1];
const u16 *colors = p & 1 ? alternate : palette;
sprite_test_canvas();
reference_sprite(x, y, images[image].art, images[image].width,
images[image].rows, colors, scale, mirror);
memcpy(expected, SCREEN, sizeof(expected));
sprite_test_canvas();
memcpy(visible, SCREEN, sizeof(visible));
unsigned before_rectangles = rectangle_calls;
unsigned before_rows = sprite_row_marks;
blit_sprite(x, y, images[image].art, images[image].width,
images[image].rows, colors, scale, mirror);
assert(rectangle_calls == before_rectangles);
assert(sprite_row_marks - before_rows <= (unsigned)images[image].rows);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(memcmp(expected, visible, sizeof(expected)) == 0);
}
/* Palette changes reuse decoded indices, rather than cached colors. */
sprite_test_canvas();
blit_sprite(20, 20, zero_art, 8, 3, palette, 1, 0);
assert(SCREEN[20 * WIDTH + 20] == palette[0]);
unsigned before_decodes = sprite_decodes;
blit_sprite(20, 20, zero_art, 8, 3, alternate, 1, 0);
assert(sprite_decodes == before_decodes);
assert(SCREEN[20 * WIDTH + 20] == alternate[0]);
sprite_test_canvas();
unsigned before_rectangles = rectangle_calls;
reference_sprite(80, 60, person_art, 16, 20, palette, 1, 0);
unsigned old_rectangles = rectangle_calls - before_rectangles;
assert(old_rectangles > 100);
sprite_test_canvas();
before_rectangles = rectangle_calls;
unsigned before_rows = sprite_row_marks;
blit_sprite(80, 60, person_art, 16, 20, palette, 1, 0);
assert(rectangle_calls == before_rectangles);
assert(sprite_row_marks - before_rows == 20);
before_decodes = sprite_decodes;
for (int frame = 0; frame < 1000; ++frame)
blit_sprite(80, 60, person_art, 16, 20, palette, 1, frame & 1);
assert(sprite_decodes == before_decodes && rectangle_calls == before_rectangles);
sprite_test_canvas();
blit_sprite(0, 0, zero_art, 0, 3, palette, 1, 0);
blit_sprite(0, 0, zero_art, 8, 25, palette, 1, 0);
blit_sprite(0, 0, zero_art, 8, 3, palette, 0, 0);
assert(pending_pixels() == 0);
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
printf("Sprite rows: character body uses 0 rectangle calls (was %u), 20 row marks, no repeat decoding\n",
old_rectangles);
}
+191
View File
@@ -0,0 +1,191 @@
#include "test_support.h"
void check_starter_gift(void)
{
memset(test_save, 0xff, sizeof(test_save));
load_progress();
assert(!partner_owned);
draw_team();
assert(pending_pixels() == WIDTH * HEIGHT);
write_preview("build/team-empty-preview.ppm");
current_room = OUTSIDE;
assert(!begin_conversation(108, 103));
assert(begin_conversation(NPC_X - 13, NPC_Y));
assert(!gift_conversation);
assert(advance_conversation(0) == 1);
assert(!partner_owned && test_save[0] == 0xff);
assert(advance_conversation(1) == -1);
assert(!receive_partner());
current_room = MELLON_HOUSE;
int x = PROF_X, y = PROF_Y + 22;
assert(can_walk(x, y));
assert(!begin_conversation(200, 120));
assert(begin_conversation(x, y));
assert(gift_conversation && !partner_owned);
render_scene(x, y);
dialogue_box(0);
write_preview("build/starter-intro-preview.ppm");
assert(advance_conversation(0) == 1);
assert(partner_owned && gift_conversation);
dialogue_box(1);
write_preview("build/starter-gift-preview.ppm");
assert(!receive_partner()); /* The same gift cannot add a second partner. */
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
assert(advance_conversation(1) == -1);
assert(!gift_conversation);
assert(begin_conversation(x, y));
assert(!gift_conversation && partner_owned); /* Follow-up, not another gift. */
assert(advance_conversation(0) == 1);
assert(advance_conversation(1) == -1);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
for (int i = 9; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
/* Ownership survives map changes, position reset, and loading SRAM. */
x = 120;
y = 125;
assert(use_door(&x, &y));
assert(current_room == MELLON_GARDEN && partner_owned);
x = 17;
y = 113;
assert(use_map_exit(&x, &y, LEFT));
assert(current_room == OUTSIDE && partner_owned);
reset_position(&x, &y);
assert(partner_owned);
partner_owned = 0;
load_progress();
assert(partner_owned);
draw_team();
write_preview("build/team-preview.ppm");
static u16 expected[WIDTH * HEIGHT];
render_scene(x, y);
memcpy(expected, SCREEN, sizeof(expected));
draw_team();
render_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
/* Reject damaged, incomplete, or unknown-version save records. */
for (int byte = 0; byte < 9; ++byte) {
memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1;
load_progress();
assert(!partner_owned);
}
memcpy(test_save, saved, sizeof(saved));
test_save[0] = 0;
load_progress();
assert(!partner_owned);
memcpy(test_save, saved, sizeof(saved));
load_progress();
assert(partner_owned);
for (int page = 0; page < DIALOGUE_PAGES; ++page)
for (int line = 0; line < 2; ++line) {
const char *message = professor_followup[page][line];
assert(strlen(message) <= 34);
for (; *message; ++message)
assert(*message == ' ' || (*message >= 'A' && *message <= 'Z'));
}
partner_owned = 0;
gift_conversation = 0;
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
void check_ball_gift(void)
{
static u16 expected[WIDTH * HEIGHT];
/* The original Sprout-only save must survive the inventory upgrade. */
const unsigned char legacy[] = {'F', 'Q', 'S', 'P', 1, 1, 0xa5, 0x5a};
memset(test_save, 0xff, sizeof(test_save));
memcpy(test_save, legacy, sizeof(legacy));
load_progress();
assert(partner_owned && !balls_given && ball_count == 0);
assert(test_save[4] == 1); /* Loading alone does not rewrite the save. */
current_room = ROUTE;
assert(!can_walk(RANGER_X, RANGER_Y));
assert(can_walk(RANGER_X + 13, RANGER_Y));
assert(near_npc(99, 97) && !near_npc(108, 60));
assert(reachable(108, 60, 99, 97));
assert(reachable(108, 60, 108, 47));
assert(reachable(108, 60, 183, 121));
assert(!begin_conversation(108, 60));
assert(!receive_balls());
reset_follower(108, 103);
render_scene(99, 97);
write_preview("build/ranger-preview.ppm");
memcpy(expected, SCREEN, sizeof(expected));
assert(begin_conversation(99, 97));
assert(ball_gift_conversation && !gift_conversation);
dialogue_box(0);
assert(ball_count == 0); /* The gift arrives on its receipt page. */
assert(advance_conversation(0) == 1);
assert(ball_count == 5 && balls_given && partner_owned);
dialogue_box(1);
write_preview("build/ball-gift-preview.ppm");
assert(!receive_balls());
assert(advance_conversation(1) == -1);
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(99, 97, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
assert(test_save[4] == 2);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
for (int visit = 0; visit < 10; ++visit) {
assert(begin_conversation(99, 97) && !ball_gift_conversation);
assert(advance_conversation(0) == 1);
assert(advance_conversation(1) == -1);
assert(ball_count == 5 && memcmp(saved, test_save, sizeof(saved)) == 0);
}
partner_owned = balls_given = ball_count = 0;
load_progress();
assert(partner_owned && balls_given && ball_count == 5);
draw_team();
write_preview("build/poke-balls-preview.ppm");
for (int i = 9; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
int x = 108, y = 47;
assert(use_map_exit(&x, &y, UP));
assert(ball_count == 5 && balls_given);
/* Getting the balls before Sprout is allowed and must also persist. */
memset(test_save, 0xff, sizeof(test_save));
load_progress();
current_room = ROUTE;
assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
load_progress();
assert(!partner_owned && balls_given && ball_count == 5);
current_room = MELLON_HOUSE;
assert(begin_conversation(PROF_X, PROF_Y + 22));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
load_progress();
assert(partner_owned && balls_given && ball_count == 5);
memcpy(saved, test_save, sizeof(saved));
for (int byte = 0; byte < 9; ++byte) {
memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1;
load_progress();
assert(!partner_owned && !balls_given && ball_count == 0);
}
memcpy(test_save, saved, sizeof(saved));
test_save[6] = 100;
test_save[7] = (unsigned char)(0xa5 ^ 2 ^ test_save[5] ^ test_save[6]);
load_progress();
assert(!partner_owned && !balls_given && ball_count == 0);
for (int page = 0; page < DIALOGUE_PAGES; ++page)
for (int line = 0; line < 2; ++line) {
const char *messages[] = {ranger_dialogue[page][line], ranger_followup[page][line]};
for (int i = 0; i < 2; ++i) {
assert(strlen(messages[i]) <= 34);
for (const char *p = messages[i]; *p; ++p)
assert(*p == ' ' || (*p >= 'A' && *p <= 'Z'));
}
}
current_room = OUTSIDE;
gift_conversation = ball_gift_conversation = 0;
partner_owned = balls_given = ball_count = follower_ready = 0;
}
+84
View File
@@ -0,0 +1,84 @@
#include "test_support.h"
u16 test_pixels[WIDTH * HEIGHT + 2];
unsigned char test_save[32];
void render_scene(int x, int y)
{
draw_scene(x, y);
}
int reachable(int start_x, int start_y, int end_x, int end_y)
{
static unsigned char visited[HEIGHT][WIDTH];
static int queue[WIDTH * HEIGHT][2];
static const int directions[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
memset(visited, 0, sizeof(visited));
int head = 0, tail = 1;
queue[0][0] = start_x;
queue[0][1] = start_y;
visited[start_y][start_x] = 1;
while (head < tail) {
int x = queue[head][0], y = queue[head][1];
++head;
if (x == end_x && y == end_y) return 1;
for (int direction = 0; direction < 4; ++direction) {
int nx = x + directions[direction][0], ny = y + directions[direction][1];
if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= HEIGHT) continue;
if (visited[ny][nx] || !can_walk(nx, ny)) continue;
visited[ny][nx] = 1;
queue[tail][0] = nx;
queue[tail][1] = ny;
++tail;
}
}
return 0;
}
void check_art(const char *const *art, int rows, int colors)
{
for (int row = 0; row < rows; ++row) {
assert(strlen(art[row]) == 16);
for (const char *p = art[row]; *p; ++p) {
if (*p == '.') continue;
int index = *p <= '9' ? *p - '0' : *p - 'A' + 10;
assert(index > 0 && index < colors);
}
}
}
void write_preview(const char *path)
{
FILE *file = fopen(path, "wb");
assert(file);
fprintf(file, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
for (int i = 0; i < WIDTH * HEIGHT; ++i) {
u16 color = SCREEN[i];
fputc((color & 31) * 255 / 31, file);
fputc(((color >> 5) & 31) * 255 / 31, file);
fputc(((color >> 10) & 31) * 255 / 31, file);
}
assert(fclose(file) == 0);
}
/* Simulate the same row copies as hardware DMA, into a separate display. */
int pending_pixels(void)
{
int total = 0;
for (int y = 0; y < HEIGHT; ++y) {
assert(dirty_first[y] <= WIDTH && dirty_end[y] <= WIDTH);
if (dirty_first[y] < dirty_end[y]) total += dirty_end[y] - dirty_first[y];
}
return total;
}
void copy_pending_rows(u16 *visible)
{
for (int y = 0; y < HEIGHT; ++y) {
int first = dirty_first[y], end = dirty_end[y];
if (first < end)
memcpy(&visible[y * WIDTH + first], &SCREEN[y * WIDTH + first],
(size_t)(end - first) * sizeof(u16));
}
reset_dirty_rows();
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef TEST_SUPPORT_H
#define TEST_SUPPORT_H
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "../source/game.h"
void render_scene(int x, int y);
int reachable(int start_x, int start_y, int end_x, int end_y);
void check_art(const char *const *art, int rows, int colors);
void write_preview(const char *path);
int pending_pixels(void);
void copy_pending_rows(u16 *visible);
void check_buffered_walking(void);
void check_route_travel(void);
void check_mellon_visit(void);
void check_starter_gift(void);
void check_ball_gift(void);
void check_following_and_battle(void);
void check_wild_encounters(void);
void check_background_cache(void);
void check_sprite_rows(void);
void check_movement_clock_and_speed(void);
#endif