Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

new test fix

Lily Pitiot2ffe6192786b
diff --git a/games/dinorun/src/main.c b/games/dinorun/src/main.c
index 63ee55d0..c3a87014 100644
--- a/games/dinorun/src/main.c
+++ b/games/dinorun/src/main.c
@@ -39,6 +39,12 @@ const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level")))
#define GAME_FRAME_MS 100
#define DRAW_FRAME_MS 33
#define MAX_CATCHUP_STEPS 12
+/* Transferring an entire screen 30 times/second saturated the NumWorks
+ * display link. Compose bands as before, but upload only changed 32x16 tiles.
+ * This is presentation-only: it does not modify any gameplay/physics timing. */
+#define DIRTY_TILE_W 32
+#define DIRTY_COLS (W/DIRTY_TILE_W)
+#define DIRTY_ROWS (H/BAND)
#define SAVE_NAME "dinorun.sav"
typedef uint16_t color;
@@ -48,6 +54,11 @@ enum { TITLE, HELP, PLAY, PAUSED, DEAD };
enum { CACTUS, LARGE_CACTUS, BIRD };
static color pixels[W * BAND];
+/* Just 600 B of hashes instead of a second 150 KB screen buffer.
+ * Staging one band lets us merge adjacent changed tiles into one transfer. */
+static uint32_t previous_hashes[DIRTY_ROWS][DIRTY_COLS];
+static color upload_pixels[W * BAND];
+static bool display_cache_valid;
static int band_y, scene;
static color bg, ink, faint;
static obstacle obstacles[12];
@@ -350,6 +361,51 @@ static void update(bool jump,bool down) {
bird_meter++;cloud_clock++;dust_clock++;pebble_clock++;
ticks++;
}
+/* Hash each 32x16 tile of the freshly rendered RGB565 band. Only pixels
+ * that differ from the last displayed frame need to cross the EADK bus.
+ * The hash is over actual pixels, so it handles spawned/removed objects,
+ * night/day changes, pause overlays and HUD changes without special cases. */
+static uint32_t tile_hash(int x) {
+ uint32_t h=2166136261u;
+ for(int y=0;y<BAND;y++) {
+ const color *p=pixels+y*W+x;
+ for(int dx=0;dx<DIRTY_TILE_W;dx++) {
+ h^=p[dx];
+ h*=16777619u;
+ }
+ }
+ return h;
+}
+static void upload_changed_band(int y) {
+ int run_start=-1;
+ const int row=y/BAND;
+ for(int col=0;col<=DIRTY_COLS;col++) {
+ bool changed=false;
+ if(col<DIRTY_COLS) {
+ uint32_t hash=tile_hash(col*DIRTY_TILE_W);
+ changed=!display_cache_valid || previous_hashes[row][col]!=hash;
+ previous_hashes[row][col]=hash;
+ }
+ if(changed && run_start<0)run_start=col;
+ if(!changed && run_start>=0) {
+ int x=run_start*DIRTY_TILE_W;
+ int width=(col-run_start)*DIRTY_TILE_W;
+ if(width==W) {
+ eadk_display_push_rect((eadk_rect_t){0,(uint16_t)y,W,BAND},pixels);
+ } else {
+ /* EADK expects tightly packed rows, unlike the W-stride band. */
+ for(int yy=0;yy<BAND;yy++) {
+ const color *src=pixels+yy*W+x;
+ color *dst=upload_pixels+yy*width;
+ for(int xx=0;xx<width;xx++)dst[xx]=src[xx];
+ }
+ eadk_display_push_rect((eadk_rect_t){(uint16_t)x,(uint16_t)y,
+ (uint16_t)width,BAND},upload_pixels);
+ }
+ run_start=-1;
+ }
+ }
+}
static void frame(void) {
bool night=((score/300)&1)!=0;
/* Exact daytime/nighttime palette from the Python version. */
@@ -377,6 +433,8 @@ static void frame(void) {
/* The original title character is the separately encoded 3x sprite. */
SPRITE(DINO_MENU_LARGE,120,150,ink);
centered(40,"CHROME DINO",3,ink);
+ /* Small build marker: confirms the newly compiled app is installed. */
+ text_at(302,230,"R2",1,faint);
centered(202,scene==HELP?"OK TO JUMP":"PRESS OK TO PLAY",2,ink);
centered(225,scene==HELP?"DOWN DUCK CLEAR PAUSE":"SHIFT FOR HELP",1,faint);
if(scene!=HELP) {text_at(9,230,"BEST",1,faint);number(46,230,high_score,5,1,ink);}
@@ -392,8 +450,9 @@ static void frame(void) {
centered(115,"CLEAR TO RESUME",1,ink);
centered(129,"BACK TO EXIT",1,ink);
}
- eadk_display_push_rect((eadk_rect_t){0,(uint16_t)band_y,W,BAND},pixels);
+ upload_changed_band(band_y);
}
+ display_cache_valid=true;
}
/* Separated out so native host tests can verify controls as well as physics.
diff --git a/games/dinorun/tests/test_dino.c b/games/dinorun/tests/test_dino.c
index 7d52800f..752df8da 100644
--- a/games/dinorun/tests/test_dino.c
+++ b/games/dinorun/tests/test_dino.c
@@ -2,15 +2,20 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#define main dino_app_main
#include "../src/main.c"
#undef main
static uint16_t screen[W*H];
+static uint64_t pixels_uploaded, pushes;
+
uint64_t eadk_keyboard_scan(void) {return 0;}
uint32_t eadk_timing_millis(void) {return 0;}
void eadk_timing_msleep(uint32_t ms) {(void)ms;}
void eadk_display_push_rect(eadk_rect_t r,const uint16_t *px) {
- assert(r.x+r.width<=W && r.y+r.height<=H);
+ assert(r.width>0 && r.height>0 && r.x+r.width<=W && r.y+r.height<=H);
+ pixels_uploaded+=(uint64_t)r.width*r.height;
+ pushes++;
for(int y=0;y<r.height;y++) for(int x=0;x<r.width;x++)
screen[(y+r.y)*W+x+r.x]=px[y*r.width+x];
}
@@ -22,6 +27,15 @@ static int bird_screen_left(void) {
if(screen[y*W+x]==RGB(0x535353))return x;
return -1;
}
+/* A dirty-only frame MUST leave exactly the same framebuffer as full
+ * repaint. This catches leftover pixels when sprites move or disappear. */
+static uint16_t screen_copy[W*H];
+static void verify_dirty_matches_full(void) {
+ memcpy(screen_copy,screen,sizeof screen);
+ display_cache_valid=false; /* Force the following draw to upload all tiles. */
+ frame();
+ assert(memcmp(screen_copy,screen,sizeof screen)==0);
+}
static void screenshot(const char *f) {
FILE *fp=fopen(f,"wb"); assert(fp);
fprintf(fp,"P6\n%d %d\n255\n",W,H);
@@ -37,7 +51,46 @@ static void screenshot(const char *f) {
int main(int argc,char **argv) {
scene=TITLE; dino_y=DY*256;score=0;
frame();
+ assert(pixels_uploaded==(uint64_t)W*H); /* First frame covers entire screen. */
if(argc>=2) screenshot(argv[1]);
+ pixels_uploaded=pushes=0;
+ frame();
+ assert(pixels_uploaded==0 && pushes==0); /* Do NOT resend the same pixels. */
+
+ /* A single moving bird changes just a few tiles, not 320x240 pixels.
+ * This is the hardware-performance regression the old tests missed. */
+ start_game();
+ frame(); /* Draw the otherwise empty game screen. */
+ pixels_uploaded=pushes=0;
+ frame();
+ assert(pixels_uploaded==0 && pushes==0);
+ obstacles[0]=(obstacle){220,165,21,18,BIRD};count=1;
+ pixels_uploaded=pushes=0;
+ frame();
+ assert(pixels_uploaded>0 && pixels_uploaded<(uint64_t)W*H/4);
+ assert(pushes>0);
+ uint64_t first_bird_pixels=pixels_uploaded;
+ verify_dirty_matches_full();
+ render_ms=70;
+ pixels_uploaded=pushes=0;
+ frame();
+ assert(pixels_uploaded>0 && pixels_uploaded<(uint64_t)W*H/4);
+ uint64_t next_bird_pixels=pixels_uploaded;
+ verify_dirty_matches_full();
+ printf("Screen upload check: %llu pixels for bird appearance, %llu for bird movement, vs %d previously every frame.\n",
+ (unsigned long long)first_bird_pixels,(unsigned long long)next_bird_pixels,W*H);
+ obstacles[0].x=100;render_ms=24;
+ frame();verify_dirty_matches_full();
+ count=0;frame();verify_dirty_matches_full(); /* Object removal clears pixels. */
+ /* Dark-to-light transition legitimately repaints the full background. */
+ score=300;
+ pixels_uploaded=pushes=0;
+ frame();
+ assert(pixels_uploaded>=(uint64_t)W*H*3/4);
+ verify_dirty_matches_full();
+ score=0;
+ frame();
+
/* Reference: original Python H=-13, gravity=+2, 49 px at frame 7. */
start_game();assert(scene==PLAY && score==0 && count==0);
@@ -157,6 +210,6 @@ int main(int argc,char **argv) {
clouds[0]=(decoration){170,70};cloud_count=1;
frame();
if(argc>=3)screenshot(argv[2]);
- printf("Host tests passed: original physics, birds moving left EVERY drawn frame, 30fps forward motion, slow-frame catch-up, controls.\n");
+ printf("Host tests passed: dirty tile transfers, physics, birds left every frame, 30fps forward motion, slow-frame catch-up, controls.\n");
return 0;
}