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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Add Donkey Kong engine and original pixel art by Bobydeluxe
Bobydeluxe522f21407475
diff --git a/games/donkeykong/src/assets.h b/games/donkeykong/src/assets.hnew file mode 100644index 00000000..98ba6c33--- /dev/null+++ b/games/donkeykong/src/assets.h@@ -0,0 +1,23 @@+/* SPDX-License-Identifier: GPL-3.0-only; generated by tools/assets.py */+typedef struct {uint8_t w,h;const uint8_t *p;} dk_sprite;+static const uint16_t palette[]={66,64168,65204,17403,65293,24996,43782,62828,65433,62261,30429,44031,58342,10665,48699,65535};+static const uint8_t pixels_mario[]={0,0,17,17,16,0,0,0,17,17,17,17,0,0,5,85,34,80,0,0,5,37,82,34,82,0,0,82,85,34,34,82,0,5,82,34,37,85,0,0,0,34,34,34,0,0,0,17,49,16,0,0,0,17,19,17,49,16,0,17,17,51,51,17,16,2,33,52,52,49,34,0,34,35,51,51,34,32,0,3,51,51,51,0,0,0,51,48,51,48,0,0,85,80,0,85,80,0,85,85,0,5,85,80};+static const dk_sprite spr_mario={13,16,pixels_mario};+static const uint8_t pixels_kong[]={0,0,0,0,5,85,85,85,85,80,0,0,0,0,0,0,0,5,86,102,102,102,101,85,0,0,0,0,0,0,0,86,102,102,102,102,102,102,80,0,0,0,0,0,5,102,103,119,119,119,118,102,101,0,0,0,0,0,86,102,119,119,119,119,119,102,102,80,0,0,0,5,102,103,117,85,119,85,87,118,102,101,0,0,0,5,102,103,117,136,119,88,135,118,102,101,0,0,0,86,102,103,117,87,119,117,87,118,102,102,80,0,0,86,102,102,119,119,119,119,119,102,102,102,80,0,5,102,102,102,119,117,85,87,119,102,102,102,101,0,5,102,102,102,119,120,136,135,119,102,102,102,101,0,86,102,102,102,103,119,119,119,102,102,102,102,101,0,86,102,102,102,102,119,119,118,102,102,102,102,101,0,86,102,101,102,119,119,119,119,102,86,102,102,101,0,86,102,85,103,119,119,119,119,118,85,102,102,101,0,86,102,85,103,119,119,119,119,118,85,102,102,101,0,86,102,85,103,119,119,119,119,118,85,102,102,101,0,87,119,85,103,119,119,119,119,118,85,119,119,101,0,87,119,85,103,119,119,119,119,118,85,119,119,101,0,87,119,85,102,119,119,119,119,102,85,119,119,101,0,5,85,85,102,103,119,119,118,102,85,85,85,80,0,0,0,5,102,102,102,102,102,102,101,0,0,0,0,0,0,5,102,102,96,6,102,102,101,0,0,0,0,0,0,87,119,119,80,5,119,119,119,80,0,0,0,0,5,119,119,119,80,5,119,119,119,117,0,0,0};+static const dk_sprite spr_kong={28,25,pixels_kong};+static const uint8_t pixels_pauline[]={0,4,68,64,0,0,69,85,85,0,0,82,37,37,0,0,82,34,37,0,0,85,34,85,0,0,9,34,144,0,0,41,153,146,0,2,41,153,146,32,2,9,153,144,32,0,9,153,144,0,0,153,153,153,0,0,153,153,153,0,9,153,153,153,144,9,153,153,153,144,0,0,34,0,0,0,2,32,32,0,0,9,144,153,0,0,153,144,153,144};+static const dk_sprite spr_pauline={10,18,pixels_pauline};+static const uint8_t pixels_barrel[]={0,204,204,204,0,12,71,119,116,192,199,71,119,71,76,199,116,116,119,76,199,119,71,119,76,199,116,116,119,76,199,71,119,71,76,12,71,119,116,192,0,204,204,204,0};+static const dk_sprite spr_barrel={10,9,pixels_barrel};+static const uint8_t pixels_fire[]={0,0,16,0,0,0,1,16,1,0,0,12,16,193,0,1,12,204,193,0,1,204,204,193,0,12,204,204,204,16,204,204,204,204,193,204,196,204,76,193,204,200,204,140,193,12,196,68,204,16,0,204,76,193,0,0,192,0,192,0};+static const dk_sprite spr_fire={10,12,pixels_fire};+static const uint8_t pixels_tub[]={14,238,238,238,224,237,221,221,221,222,236,204,204,204,206,14,204,204,204,224,14,204,204,204,224,0,238,238,238,0,0,208,0,13,0};+static const dk_sprite spr_tub={10,7,pixels_tub};+static const uint8_t pixels_hammer[]={14,238,238,238,14,238,238,238,224,238,238,238,238,0,238,238,238,224,0,0,119,0,0,0,7,112,0,0,0,119,0,0,0,7,112,0,0,0,119,0,0};+static const dk_sprite spr_hammer={9,9,pixels_hammer};+static const uint8_t pixels_bag[]={0,4,68,0,0,4,0,4,0,9,153,153,153,9,153,153,153,153,153,148,148,153,153,153,73,73,153,153,153,153,153,144,153,153,153,144};+static const dk_sprite spr_bag={9,8,pixels_bag};+static const uint8_t pixels_hat[]={0,153,153,144,0,9,153,153,0,9,153,153,153,4,68,68,68,68,153,153,153,153,144};+static const dk_sprite spr_hat={9,5,pixels_hat};+static const uint8_t pixels_parasol[]={0,0,176,0,0,11,187,187,0,11,185,185,187,11,185,187,155,187,0,0,64,0,0,0,4,0,0,0,0,64,0,0,4,4,0,0,0,4,0,0,0};+static const dk_sprite spr_parasol={9,9,pixels_parasol};diff --git a/games/donkeykong/src/font.h b/games/donkeykong/src/font.hnew file mode 100644index 00000000..21da4cdf--- /dev/null+++ b/games/donkeykong/src/font.h@@ -0,0 +1,19 @@+/* SPDX-License-Identifier: GPL-3.0-only; shared NumPlay 5x7 lettering. */+static const uint8_t alphabet[][5] = {+ {0x7e,0x11,0x11,0x11,0x7e},{0x7f,0x49,0x49,0x49,0x36},{0x3e,0x41,0x41,0x41,0x22},+ {0x7f,0x41,0x41,0x22,0x1c},{0x7f,0x49,0x49,0x49,0x41},{0x7f,0x09,0x09,0x09,0x01},+ {0x3e,0x41,0x49,0x49,0x7a},{0x7f,0x08,0x08,0x08,0x7f},{0x00,0x41,0x7f,0x41,0x00},+ {0x20,0x40,0x41,0x3f,0x01},{0x7f,0x08,0x14,0x22,0x41},{0x7f,0x40,0x40,0x40,0x40},+ {0x7f,0x02,0x0c,0x02,0x7f},{0x7f,0x04,0x08,0x10,0x7f},{0x3e,0x41,0x41,0x41,0x3e},+ {0x7f,0x09,0x09,0x09,0x06},{0x3e,0x41,0x51,0x21,0x5e},{0x7f,0x09,0x19,0x29,0x46},+ {0x46,0x49,0x49,0x49,0x31},{0x01,0x01,0x7f,0x01,0x01},{0x3f,0x40,0x40,0x40,0x3f},+ {0x1f,0x20,0x40,0x20,0x1f},{0x3f,0x40,0x38,0x40,0x3f},{0x63,0x14,0x08,0x14,0x63},+ {0x07,0x08,0x70,0x08,0x07},{0x61,0x51,0x49,0x45,0x43},+ {0x3e,0x51,0x49,0x45,0x3e},{0x00,0x42,0x7f,0x40,0x00},{0x62,0x51,0x49,0x49,0x46},+ {0x22,0x41,0x49,0x49,0x36},{0x18,0x14,0x12,0x7f,0x10},{0x27,0x45,0x45,0x45,0x39},+ {0x3c,0x4a,0x49,0x49,0x30},{0x01,0x71,0x09,0x05,0x03},{0x36,0x49,0x49,0x49,0x36},+ {0x06,0x49,0x49,0x29,0x1e},+ {0,0x08,0x3e,0x08,0},{0,0x08,0x08,0x08,0},{0,0x60,0x60,0,0},+ {0,0x36,0x36,0,0},{0x20,0x10,0x08,0x04,0x02},{0,0,0x5f,0,0},+ {0x08,0x14,0x22,0x41,0},{0x41,0x22,0x14,0x08,0},{0,0,0x7f,0,0}+};diff --git a/games/donkeykong/src/game.c b/games/donkeykong/src/game.cnew file mode 100644index 00000000..0e96823b--- /dev/null+++ b/games/donkeykong/src/game.c@@ -0,0 +1,384 @@+/* SPDX-License-Identifier: GPL-3.0-only+ * Native arcade adaptation. Integer physics, bounded entity pools, no heap.+ * Layouts are redrawn for 224x224; this does not emulate the arcade ROM. */+#include "game.h"+#include <stdlib.h>+#include <string.h>+static int clamp(int x,int a,int b) {return x<a?a:x>b?b:x;}+static uint32_t random32(dk_game *g) {+ uint32_t x=g->rng;x^=x<<13;x^=x>>17;x^=x<<5;return g->rng=x;+}+unsigned dk_difficulty(const dk_game *g) {return g->round>9?8:g->round-1;}+static void platform(dk_game *g,int a,int b,int y,int z,int type,int dir) {+ g->platform[g->platform_count++]=(dk_platform){a,b,y,z,type,dir};+}+static void ladder(dk_game *g,int x,int lower,int upper,int broken,int retract) {+ g->ladder[g->ladder_count++]=(dk_ladder){x,lower,upper,broken,retract};+}+int dk_surface(const dk_game *g,unsigned index,int x) {+ if(index>=g->platform_count)return -1;+ const dk_platform *p=&g->platform[index];+ if(x<p->x1||x>p->x2)return -1;+ if(g->board==3&&index>=1&&index<=4) {+ unsigned bit=(index-1)*2;+ if((g->rivets&(1u<<bit))&&abs(x-48)<4)return -1;+ if((g->rivets&(2u<<bit))&&abs(x-176)<4)return -1;+ }+ return p->y1+(p->y2-p->y1)*(x-p->x1)/(p->x2-p->x1);+}+bool dk_ladder_open(const dk_game *g,unsigned index) {+ if(index>=g->ladder_count||g->ladder[index].broken)return false;+ return !g->ladder[index].retract||g->age%240<150;+}+int dk_belt_direction(const dk_game *g,const dk_platform *p) {+ return p->dir*((g->age/360)&1?-1:1);+}+static dk_enemy *spawn_enemy(dk_game *g,int type,int row,int x,int dir) {+ for(unsigned i=0;i<DK_ENEMIES;++i)if(!g->enemy[i].active) {+ dk_enemy *e=&g->enemy[i];memset(e,0,sizeof *e);+ e->active=1;e->type=type;e->row=row;e->x=x*DK_Q;e->dir=dir;e->ladder=-1;+ int y=dk_surface(g,(unsigned)row,x);e->y=(y<0?176:y)*DK_Q;e->old_x=e->x;+ return e;+ }+ return NULL;+}+static void load_board(dk_game *g) {+ g->age=0;g->bonus_ticks=0;g->spawn=100;g->throws=0;g->rivets=0;+ g->platform_count=g->ladder_count=0;g->phase=DK_READY;g->timer=65;+ g->bonus=(uint16_t)(g->round<5?4000+g->round*1000:8000);+ memset(g->enemy,0,sizeof g->enemy);memset(g->item,0,sizeof g->item);+ memset(&g->player,0,sizeof g->player);+ dk_player *p=&g->player;p->x=18*DK_Q;p->face=1;p->ladder=-1;+ p->grounded=1;p->support=0;p->jump_id=1;+ if(g->board==0) {+ platform(g,8,216,214,207,0,0);platform(g,8,216,177,184,0,0);+ platform(g,8,216,154,147,0,0);platform(g,8,216,117,124,0,0);+ platform(g,8,216,94,87,0,0);platform(g,8,216,57,64,0,0);+ platform(g,104,184,25,25,0,0);+ ladder(g,196,0,1,0,0);ladder(g,78,0,1,1,0);+ ladder(g,32,1,2,0,0);ladder(g,126,1,2,1,0);+ ladder(g,188,2,3,0,0);ladder(g,100,2,3,0,0);+ ladder(g,36,3,4,0,0);ladder(g,152,3,4,1,0);+ ladder(g,184,4,5,0,0);ladder(g,84,4,5,0,0);ladder(g,138,5,6,0,0);+ g->item[0]=(dk_item){58,dk_surface(g,1,58)-10,1,1};+ g->item[1]=(dk_item){160,dk_surface(g,3,160)-10,1,1};+ } else if(g->board==1) {+ platform(g,8,216,212,212,0,0);+ platform(g,8,98,176,176,1,1);platform(g,126,216,176,176,1,1);+ platform(g,8,216,140,140,0,0);+ platform(g,8,98,104,104,1,-1);platform(g,126,216,104,104,1,-1);+ platform(g,8,216,68,68,1,1);platform(g,128,208,28,28,0,0);+ ladder(g,24,0,1,0,0);ladder(g,200,0,2,0,0);+ ladder(g,56,1,3,0,0);ladder(g,166,2,3,0,0);+ ladder(g,28,3,4,0,0);ladder(g,196,3,5,0,0);+ ladder(g,54,4,6,0,1);ladder(g,172,5,6,0,1);ladder(g,184,6,7,0,0);+ g->item[0]=(dk_item){76,166,1,1};g->item[1]=(dk_item){150,94,1,1};+ g->item[2]=(dk_item){204,203,2,1};g->item[3]=(dk_item){100,131,3,1};+ g->item[4]=(dk_item){18,59,4,1};+ } else if(g->board==2) {+ platform(g,8,46,212,212,0,0);platform(g,146,216,212,212,0,0);+ platform(g,140,216,172,172,0,0);platform(g,140,216,132,132,0,0);+ platform(g,140,216,94,94,0,0);platform(g,8,82,55,55,0,0);+ platform(g,136,216,55,55,0,0);+ platform(g,8,42,168,168,0,0);platform(g,8,42,126,126,0,0);platform(g,8,42,84,84,0,0);+ platform(g,144,214,24,24,0,0);+ for(int i=0;i<3;++i)platform(g,56,80,212-i*52,212-i*52,2,-1);+ for(int i=0;i<3;++i)platform(g,104,128,56+i*52,56+i*52,2,1);+ ladder(g,200,1,2,0,0);ladder(g,160,2,3,0,0);ladder(g,198,3,4,0,0);+ ladder(g,156,4,6,0,0);ladder(g,188,6,10,0,0);+ g->item[0]=(dk_item){22,159,2,1};g->item[1]=(dk_item){200,123,3,1};+ g->item[2]=(dk_item){150,85,4,1};g->spawn=80;+ } else {+ for(int i=0;i<5;++i)platform(g,16,208,210-i*36,210-i*36,0,0);+ platform(g,144,208,28,28,0,0);+ for(int i=0;i<4;++i){ladder(g,28,i,i+1,0,0);ladder(g,196,i,i+1,0,0);}+ ladder(g,112,0,1,0,0);ladder(g,112,2,3,0,0);+ g->item[0]=(dk_item){150,164,1,1};g->item[1]=(dk_item){74,92,1,1};+ g->item[2]=(dk_item){84,201,2,1};g->item[3]=(dk_item){200,129,3,1};+ g->item[4]=(dk_item){60,57,4,1};+ spawn_enemy(g,DK_FIRE,1,142,-1);spawn_enemy(g,DK_FIRE,3,82,1);+ g->spawn=300;+ }+ p->y=dk_surface(g,0,p->x/DK_Q)*DK_Q;p->fall_from=p->y;+}+void dk_new(dk_game *g,uint32_t seed) {+ memset(g,0,sizeof *g);g->rng=seed?seed:0x1981a11u;g->round=1;g->lives=3;+ load_board(g);+}+static void points(dk_game *g,dk_stats *s,unsigned score,int x,int y) {+ if(score>DK_MAX_SCORE-g->score)g->score=DK_MAX_SCORE;else g->score+=score;+ if(g->score>s->best)s->best=g->score;+ if(!g->extra_life&&g->score>=7000){g->extra_life=1;if(g->lives<9)++g->lives;}+ if(x>=0){g->popup_x=(int16_t)x;g->popup_y=(int16_t)y;g->popup_time=50;g->popup_value=(uint16_t)score;}+}+static void die(dk_game *g,unsigned reason) {+ if(g->phase!=DK_RUNNING)return;+ g->phase=DK_DYING;g->timer=70;g->reason=(uint8_t)reason;g->player.hammer=0;+}+static void clear(dk_game *g,dk_stats *s) {+ points(g,s,g->bonus,-1,0);g->phase=DK_CLEAR;g->timer=125;+ if(g->board==3&&s->rescues<UINT32_MAX)++s->rescues;+}+static void update_lifts(dk_game *g) {+ if(g->board!=2)return;+ for(unsigned i=11;i<17;++i) {+ dk_platform *p=&g->platform[i];int old=p->y1;+ unsigned travel=(g->age*112u/DK_Q+(i-11)%3*52)%156;+ p->y1=p->y2=(int16_t)(p->dir<0?212-(int)travel:56+(int)travel);+ if(g->player.grounded&&g->player.support==(int)i) {+ int delta=p->y1-old;+ if(abs(delta)>4)g->player.grounded=0,g->player.support=-1;+ else g->player.y+=delta*DK_Q,g->player.fall_from+=delta*DK_Q;+ }+ }+}+static void move_player(dk_game *g,unsigned input) {+ dk_player *p=&g->player;int steer=!!(input&DK_RIGHT)-!!(input&DK_LEFT);+ int vertical=!!(input&DK_DOWN)-!!(input&DK_UP);+ if(p->hammer)--p->hammer;+ if((input&DK_JUMP)&&!(g->previous_input&DK_JUMP))p->jump_buffer=7;+ else if(p->jump_buffer)--p->jump_buffer;+ if(p->grounded)p->coyote=6;else if(p->coyote)--p->coyote;+ if(!p->hammer&&vertical&&p->ladder<0) {+ for(unsigned i=0;i<g->ladder_count;++i)if(dk_ladder_open(g,i)) {+ dk_ladder *l=&g->ladder[i];int top=dk_surface(g,l->upper,l->x),bottom=dk_surface(g,l->lower,l->x);+ if(top<0||bottom<0||abs(p->x-l->x*DK_Q)>5*DK_Q)continue;+ if((vertical<0&&abs(p->y-bottom*DK_Q)<=3*DK_Q)||+ (vertical>0&&abs(p->y-top*DK_Q)<=3*DK_Q)) {+ p->ladder=(int8_t)i;p->x=l->x*DK_Q;p->grounded=0;p->vy=0;p->jump_buffer=0;break;+ }+ }+ }+ if(p->ladder>=0) {+ dk_ladder *l=&g->ladder[(unsigned)p->ladder];+ int top=dk_surface(g,l->upper,l->x),bottom=dk_surface(g,l->lower,l->x);+ p->y+=vertical*200;+ if(p->y<=top*DK_Q){p->y=top*DK_Q;p->grounded=1;p->support=l->upper;p->ladder=-1;}+ else if(p->y>=bottom*DK_Q){p->y=bottom*DK_Q;p->grounded=1;p->support=l->lower;p->ladder=-1;}+ p->fall_from=p->y;return;+ }+ if(steer)p->face=(int8_t)steer;+ int belt=0;+ if(p->grounded&&p->support>=0) {+ dk_platform *floor=&g->platform[(unsigned)p->support];+ if(floor->type==DK_BELT)belt=dk_belt_direction(g,floor)*74;+ }+ p->x=clamp(p->x+steer*272+belt,6*DK_Q,218*DK_Q);+ if(p->jump_buffer&&p->coyote&&!p->hammer) {+ p->vy=-620;p->grounded=0;p->support=-1;p->jump_buffer=0;p->coyote=0;+ p->fall_from=p->y;p->air_points=0;if(++p->jump_id==0)++p->jump_id;+ }+ if(p->grounded) {+ int ground=dk_surface(g,(unsigned)p->support,p->x/DK_Q);+ if(ground>=0){p->y=ground*DK_Q;p->fall_from=p->y;p->vy=0;return;}+ p->grounded=0;p->support=-1;p->vy=0;+ }+ int old=p->y;p->vy=clamp(p->vy+40,-2000,1400);p->y+=p->vy;+ if(p->vy>=0) {+ int best=DK_H+32,index=-1;+ for(unsigned i=0;i<g->platform_count;++i) {+ int y=dk_surface(g,i,p->x/DK_Q);+ if(y>=0&&y<best&&old<=(y+2)*DK_Q&&p->y>=y*DK_Q)best=y,index=(int)i;+ }+ if(index>=0) {+ p->y=best*DK_Q;p->vy=0;p->support=(int8_t)index;p->grounded=1;+ if(p->y-p->fall_from>28*DK_Q)die(g,DK_FALL);+ p->fall_from=p->y;+ }+ }+ if(p->y>(DK_H+14)*DK_Q)die(g,DK_FALL);+}+static void barrel(dk_game *g,dk_enemy *e) {+ unsigned difficulty=dk_difficulty(g);+ if(e->mode) {+ e->vy=clamp(e->vy+48,0,1300);e->y+=e->vy;+ int target=dk_surface(g,(unsigned)e->row,clamp(e->x/DK_Q,8,216));+ if(target>=0&&e->y>=target*DK_Q){e->y=target*DK_Q;e->vy=0;e->mode=0;}+ return;+ }+ e->x+=e->dir*(260+(int)difficulty*14);+ dk_platform *p=&g->platform[(unsigned)e->row];+ if(e->x<=p->x1*DK_Q||e->x>=p->x2*DK_Q) {+ e->x=clamp(e->x,p->x1*DK_Q,p->x2*DK_Q);+ if(e->row==0) {+ if(e->blue)spawn_enemy(g,DK_FIRE,0,30,1);+ e->active=0;return;+ }+ --e->row;e->dir=-e->dir;e->mode=1;e->vy=0;return;+ }+ e->y=dk_surface(g,(unsigned)e->row,e->x/DK_Q)*DK_Q;+ if(!e->timer) {+ for(unsigned i=0;i<g->ladder_count;++i) {+ dk_ladder *l=&g->ladder[i];+ if(l->upper==e->row&&abs(e->x-l->x*DK_Q)<2*DK_Q&&+ (random32(g)%8<(e->blue?4:1+difficulty/3))) {+ e->row=l->lower;e->x=l->x*DK_Q;e->mode=1;e->vy=160;+ e->dir=e->row&1?1:-1;e->timer=30;break;+ }+ }+ }+}+static void fire(dk_game *g,dk_enemy *e) {+ int speed=58+(int)dk_difficulty(g)*8;+ if(e->ladder>=0) {+ dk_ladder *l=&g->ladder[(unsigned)e->ladder];+ int y=dk_surface(g,(unsigned)e->row,l->x);+ e->y+=(y*DK_Q>e->y?1:-1)*100;+ if(abs(e->y-y*DK_Q)<=100){e->y=y*DK_Q;e->ladder=-1;e->timer=50;}+ return;+ }+ if(!e->timer) {+ e->dir=g->player.x>e->x?1:-1;e->timer=(int16_t)(70+random32(g)%90);+ }+ int next=e->x+e->dir*speed;+ const dk_platform *p=&g->platform[(unsigned)e->row];+ if(next<(p->x1+6)*DK_Q||next>(p->x2-6)*DK_Q||dk_surface(g,(unsigned)e->row,next/DK_Q)<0)e->dir=-e->dir;+ else e->x=next;+ int floor=dk_surface(g,(unsigned)e->row,e->x/DK_Q);+ if(floor>=0)e->y=floor*DK_Q;+ if(e->timer<40)for(unsigned i=0;i<g->ladder_count;++i)if(dk_ladder_open(g,i)) {+ dk_ladder *l=&g->ladder[i];int target=-1;+ if(l->lower==e->row&&g->player.y<e->y-10*DK_Q)target=l->upper;+ if(l->upper==e->row&&g->player.y>e->y+10*DK_Q)target=l->lower;+ if(target>=0&&abs(e->x-l->x*DK_Q)<3*DK_Q) {+ e->x=l->x*DK_Q;e->row=(int8_t)target;e->ladder=(int8_t)i;break;+ }+ }+}+static void generate(dk_game *g) {+ if(g->spawn){--g->spawn;return;}+ unsigned d=dk_difficulty(g);+ if(g->board==0) {+ dk_enemy *e=spawn_enemy(g,DK_BARREL,5,62,1);+ if(e)e->blue=(++g->throws%5)==0;+ g->spawn=(uint16_t)(130-d*7+random32(g)%35);+ } else if(g->board==1) {+ int row=(g->throws++&1)?4:2;+ dk_platform *p=&g->platform[row];int dir=dk_belt_direction(g,p);+ spawn_enemy(g,DK_TUB,row,dir>0?p->x1+8:p->x2-8,dir);+ /* Both sides get traffic; the centre gap is crossed by climbing around it. */+ row=row==4?5:1;p=&g->platform[row];dir=dk_belt_direction(g,p);+ spawn_enemy(g,DK_TUB,row,dir>0?p->x1+8:p->x2-8,dir);+ if(g->throws%3==0)spawn_enemy(g,DK_FIRE,3,112,(random32(g)&1)?1:-1);+ g->spawn=(uint16_t)(210-d*10);+ } else if(g->board==2) {+ spawn_enemy(g,DK_SPRING,5,45,1);g->spawn=(uint16_t)(115-d*5+random32(g)%25);+ } else {+ unsigned count=0;for(unsigned i=0;i<DK_ENEMIES;++i)count+=g->enemy[i].active;+ if(count<5) {+ int row=1+(int)(random32(g)%4),dir=random32(g)&1?1:-1;+ spawn_enemy(g,DK_FIRE,row,dir>0?24:200,dir);+ }+ g->spawn=(uint16_t)(290-d*15);+ }+}+static void items(dk_game *g,dk_stats *s) {+ dk_player *p=&g->player;+ for(unsigned i=0;i<5;++i) {+ dk_item *v=&g->item[i];if(!v->active)continue;+ if(abs(p->x/DK_Q-v->x)<9&&p->y/DK_Q>v->y-3&&p->y/DK_Q<v->y+21) {+ if(v->type==1){if(p->ladder>=0)continue;p->hammer=480;}+ else points(g,s,300,v->x,v->y-8);+ v->active=0;+ }+ }+ if(g->board==3&&p->grounded&&p->support>=1&&p->support<=4) {+ int left=abs(p->x/DK_Q-48),right=abs(p->x/DK_Q-176);+ if(left<=3||right<=3) {+ unsigned bit=(unsigned)(p->support-1)*2+(right<left);+ if(!(g->rivets&(1u<<bit))) {+ g->rivets|=(uint8_t)(1u<<bit);points(g,s,100,p->x/DK_Q,p->y/DK_Q-22);+ if(g->rivets==255)clear(g,s);+ }+ }+ }+}+static void enemies(dk_game *g,dk_stats *s,int old_player_x) {+ dk_player *p=&g->player;+ for(unsigned i=0;i<DK_ENEMIES&&g->phase==DK_RUNNING;++i) {+ dk_enemy *e=&g->enemy[i];if(!e->active)continue;+ e->old_x=e->x;if(e->timer>0)--e->timer;+ if(e->type==DK_BARREL)barrel(g,e);+ else if(e->type==DK_FIRE)fire(g,e);+ else if(e->type==DK_TUB) {+ e->dir=(int8_t)dk_belt_direction(g,&g->platform[(unsigned)e->row]);+ e->x+=e->dir*(145+(int)dk_difficulty(g)*10);+ dk_platform *floor=&g->platform[(unsigned)e->row];+ if(e->x<(floor->x1+3)*DK_Q||e->x>(floor->x2-3)*DK_Q)e->active=0;+ } else {+ if(!e->mode) {+ e->x+=640+(int)dk_difficulty(g)*12;+ unsigned phase=(g->age+i*7)%30;+ int bump=(int)(phase*(30-phase))/15;+ e->y=(55-bump)*DK_Q;+ if(e->x>=207*DK_Q)e->mode=1,e->vy=0;+ } else e->vy+=60,e->y+=e->vy;+ if(e->y>235*DK_Q)e->active=0;+ }+ if(!e->active)continue;+ int dx=(e->x-p->x)/DK_Q,dy=(e->y-p->y)/DK_Q;+ if(p->hammer&&e->type!=DK_SPRING&&dx*p->face>=-4&&dx*p->face<19&&dy>-14&&dy<20) {+ e->active=0;points(g,s,300,e->x/DK_Q,e->y/DK_Q-16);continue;+ }+ if(!p->grounded&&p->ladder<0&&!p->hammer&&e->jump_id!=p->jump_id&&+ dy>=7&&dy<=30&&((old_player_x-e->old_x<0&&p->x-e->x>=0)||+ (old_player_x-e->old_x>0&&p->x-e->x<=0))) {+ e->jump_id=p->jump_id;if(p->air_points<3)++p->air_points;+ }+ /* Body collision, with a two-pixel forgiving margin on the sprite edges. */+ int height=e->type==DK_FIRE?11:8;+ if(abs(dx)<8&&dy>-height+2&&dy<12)die(g,DK_CRASH);+ }+}+void dk_step(dk_game *g,dk_stats *s,uint8_t input) {+ if(g->phase==DK_OVER)return;+ ++g->tick;if(g->popup_time)--g->popup_time;+ if(g->round>s->best_round)s->best_round=g->round;+ if(g->phase!=DK_RUNNING) {+ if(g->timer)--g->timer;+ if(!g->timer) {+ if(g->phase==DK_READY)g->phase=DK_RUNNING;+ else if(g->phase==DK_DYING) {+ if(g->lives)--g->lives;+ if(!g->lives)g->phase=DK_OVER;else load_board(g);+ } else if(g->phase==DK_CLEAR) {+ if(++g->board==4){g->board=0;if(g->round<999)++g->round;}+ load_board(g);+ }+ }+ g->previous_input=input;return;+ }+ ++g->age;update_lifts(g);+ int old_x=g->player.x;uint8_t was_grounded=g->player.grounded;+ move_player(g,input);+ if(g->phase==DK_RUNNING) {+ items(g,s);+ if(g->phase==DK_RUNNING){generate(g);enemies(g,s,old_x);}+ if(!was_grounded&&g->player.grounded&&g->player.air_points) {+ points(g,s,g->player.air_points==1?100:g->player.air_points==2?300:500,+ g->player.x/DK_Q,g->player.y/DK_Q-21);g->player.air_points=0;+ }+ if(g->phase==DK_RUNNING) {+ int x=g->player.x/DK_Q,y=g->player.y/DK_Q;+ if(g->board!=3&&y<=29&&x>=132&&g->player.grounded)clear(g,s);+ if(g->board==1&&abs(x-112)<10&&y>153&&y<184)die(g,DK_CRASH);+ /* Kong rides harmlessly on the conveyor board, as in the arcade game. */+ if(g->board!=1) {+ int kx=g->board==3?112:34,ky=g->board==0?58:g->board==2?55:66;+ if(abs(x-kx)<15&&y>ky-26&&y<ky+9)die(g,DK_CRASH);+ }+ if(++g->bonus_ticks>=90){g->bonus_ticks=0;if(g->bonus>=100)g->bonus-=100;if(!g->bonus)die(g,DK_TIME);}+ }+ }+ g->previous_input=input;+}+bool dk_valid(const dk_game *g) {+ if(!g->round||g->round>999||g->board>3||g->phase>DK_OVER||g->lives>9||+ g->score>DK_MAX_SCORE||g->platform_count>DK_PLATFORMS||g->ladder_count>DK_LADDERS||+ g->player.x<0||g->player.x>DK_W*DK_Q||g->player.y< -64*DK_Q||g->player.y>512*DK_Q)return false;+ if(g->player.support< -1||g->player.support>=g->platform_count||+ g->player.ladder< -1||g->player.ladder>=g->ladder_count)return false;+ for(unsigned i=0;i<DK_ENEMIES;++i)if(g->enemy[i].active&&+ (g->enemy[i].type<1||g->enemy[i].type>4||g->enemy[i].row<0||g->enemy[i].row>=g->platform_count))return false;+ return true;+}diff --git a/games/donkeykong/src/game.h b/games/donkeykong/src/game.hnew file mode 100644index 00000000..46ff5c27--- /dev/null+++ b/games/donkeykong/src/game.h@@ -0,0 +1,62 @@+/* SPDX-License-Identifier: GPL-3.0-only */+#ifndef DK_GAME_H+#define DK_GAME_H+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>+#define DK_Q 256+#define DK_W 224+#define DK_H 224+#define DK_PLATFORMS 20+#define DK_LADDERS 18+#define DK_ENEMIES 16+#define DK_SAVE_BYTES 32+#define DK_MAX_SCORE 999999u+enum { DK_LEFT=1, DK_RIGHT=2, DK_UP=4, DK_DOWN=8, DK_JUMP=16 };+enum { DK_READY, DK_RUNNING, DK_DYING, DK_CLEAR, DK_OVER };+enum { DK_GIRDER, DK_BELT, DK_LIFT };+enum { DK_BARREL=1, DK_FIRE, DK_TUB, DK_SPRING };+enum { DK_CRASH, DK_FALL, DK_TIME };+typedef struct { int16_t x1,x2,y1,y2; int8_t type,dir; } dk_platform;+typedef struct { int16_t x; uint8_t lower,upper,broken,retract; } dk_ladder;+typedef struct {+ int32_t x,y,vy,fall_from;+ int8_t support,ladder,face;+ uint8_t grounded,coyote,jump_buffer,air_points;+ uint16_t hammer,jump_id;+} dk_player;+typedef struct {+ int32_t x,y,old_x,vy;+ int16_t timer;+ uint16_t jump_id;+ int8_t row,dir,ladder;+ uint8_t type,mode,blue,active;+} dk_enemy;+typedef struct { int16_t x,y; uint8_t type,active; } dk_item;+typedef struct {+ uint32_t best,runs,rescues;+ uint16_t best_round;+} dk_stats;+typedef struct {+ uint32_t score,rng,tick,age;+ uint16_t round,bonus,timer,bonus_ticks,spawn,throws;+ uint16_t popup_time,popup_value;+ int16_t popup_x,popup_y;+ uint8_t board,phase,lives,extra_life,rivets,previous_input,reason;+ uint8_t platform_count,ladder_count;+ dk_player player;+ dk_platform platform[DK_PLATFORMS];+ dk_ladder ladder[DK_LADDERS];+ dk_enemy enemy[DK_ENEMIES];+ dk_item item[5];+} dk_game;+void dk_new(dk_game *,uint32_t seed);+void dk_step(dk_game *,dk_stats *,uint8_t input);+int dk_surface(const dk_game *,unsigned platform,int x);+bool dk_ladder_open(const dk_game *,unsigned ladder);+int dk_belt_direction(const dk_game *,const dk_platform *);+unsigned dk_difficulty(const dk_game *);+bool dk_valid(const dk_game *);+void dk_encode_stats(const dk_stats *,uint8_t out[DK_SAVE_BYTES]);+bool dk_decode_stats(dk_stats *,const uint8_t *,size_t);+#endifdiff --git a/games/donkeykong/src/icon.png b/games/donkeykong/src/icon.pngnew file mode 100644index 00000000..283ca0f5Binary files /dev/null and b/games/donkeykong/src/icon.png differdiff --git a/games/donkeykong/src/main.c b/games/donkeykong/src/main.cnew file mode 100644index 00000000..98608707--- /dev/null+++ b/games/donkeykong/src/main.c@@ -0,0 +1,114 @@+/* SPDX-License-Identifier: GPL-3.0-only */+#include <eadk.h>+#include "render.h"+#include "../../common/epsilon_app.h"+#include "../../common/epsilon_files.h"+#ifdef __ELF__+const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name")))="Donkey Kong";+const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level")))=0;+#endif+#define KEY(k) (UINT64_C(1)<<(k))+static dk_game game;+static dk_stats stats;+static uint8_t save_data[DK_SAVE_BYTES];+static bool have_save,save_failed;+static uint32_t last_checksum;+uint32_t perf_frames,perf_max,perf_slow;+static void push(int x,int y,int w,int h,const uint16_t *p) {+ eadk_display_push_rect((eadk_rect_t){x,y,w,h},p);+}+static bool save(void) {+ dk_encode_stats(&stats,save_data);+ uint32_t hash=save_data[28]|(uint32_t)save_data[29]<<8|(uint32_t)save_data[30]<<16|(uint32_t)save_data[31]<<24;+ if(have_save&&hash==last_checksum&&!save_failed)return true;+ save_failed=!ef_write("donkeykong.sav",save_data,DK_SAVE_BYTES);+ if(!save_failed)have_save=true,last_checksum=hash;+ return !save_failed;+}+static void fresh(uint32_t seed) {+ if(stats.runs<UINT32_MAX)++stats.runs;+ dk_new(&game,seed^(stats.runs*0x9E3779B9u));+}+int main(void) {+ np_app_begin();+ uint32_t length=0;const uint8_t *data=ef_read("donkeykong.sav",&length);+ dk_decode_stats(&stats,data,length);dk_new(&game,1981);+ bool ongoing=false;+ unsigned view=DK_TITLE,selected=0,error_return=DK_TITLE,error_selection=0;+ uint64_t previous=eadk_keyboard_scan(),stale=previous;+ uint32_t now=(uint32_t)eadk_timing_millis(),last=now,draw_at=now,save_at=now,accum=0;+ dk_invalidate();+ for(;;) {+ now=(uint32_t)eadk_timing_millis();uint32_t elapsed=now-last;last=now;+ uint64_t keys=eadk_keyboard_scan();stale&=keys;+ uint64_t active=keys&~stale,hit=active&~previous;previous=active;+ bool ok=(hit&(KEY(eadk_key_ok)|KEY(eadk_key_exe)))!=0;+ bool back=(hit&KEY(eadk_key_back))!=0;+ bool next=(hit&(KEY(eadk_key_down)|KEY(eadk_key_right)))!=0;+ bool prev=(hit&(KEY(eadk_key_up)|KEY(eadk_key_left)))!=0;+ if(view!=DK_SAVE_ERROR&&(keys&(KEY(eadk_key_home)|KEY(eadk_key_on_off)))) {+ if(save())break;+ error_return=view==DK_PLAY?DK_PAUSE:view;error_selection=selected;+ view=DK_SAVE_ERROR;ok=back=next=prev=false;stale=keys;+ }+ if(view==DK_PLAY) {+ if(back)view=DK_PAUSE,selected=0,accum=0,save();+ else {+ unsigned input=0;+ if(active&(KEY(eadk_key_left)|KEY(eadk_key_four)))input|=DK_LEFT;+ if(active&(KEY(eadk_key_right)|KEY(eadk_key_six)))input|=DK_RIGHT;+ if(active&(KEY(eadk_key_up)|KEY(eadk_key_eight)))input|=DK_UP;+ if(active&(KEY(eadk_key_down)|KEY(eadk_key_two)))input|=DK_DOWN;+ if(active&(KEY(eadk_key_ok)|KEY(eadk_key_exe)|KEY(eadk_key_shift)))input|=DK_JUMP;+ if(elapsed>66)elapsed=66;+ accum+=elapsed*60;+ while(accum>=1000)dk_step(&game,&stats,(uint8_t)input),accum-=1000;+ if(game.phase==DK_OVER)view=DK_RESULT,selected=0,ongoing=false,save();+ if(now-save_at>=5000)save(),save_at=now;+ }+ } else {+ accum=0;+ if(view==DK_TITLE) {+ if(next)selected=(selected+1)%3;+ if(prev)selected=(selected+2)%3;+ if(back||(ok&&selected==2)) {+ if(save())break;+ error_return=DK_TITLE;error_selection=selected;view=DK_SAVE_ERROR;+ } else if(ok&&selected==0) {+ if(!ongoing)fresh(now),ongoing=true;+ game.previous_input=DK_JUMP;stale|=keys;view=DK_PLAY;+ } else if(ok&&selected==1)view=DK_HELP;+ } else if(view==DK_HELP) {+ if(ok||back)view=DK_TITLE,selected=1;+ } else if(view==DK_PAUSE) {+ if(next)selected=(selected+1)%3;+ if(prev)selected=(selected+2)%3;+ if(back||(ok&&selected==0))view=DK_PLAY,stale|=keys;+ else if(ok&&selected==1)view=DK_CONFIRM,selected=0;+ else if(ok&&selected==2)view=DK_TITLE,selected=0;+ } else if(view==DK_CONFIRM) {+ if(next||prev)selected=!selected;+ if(back||(ok&&selected==0))view=DK_PAUSE,selected=1;+ else if(ok)fresh(now),view=DK_PLAY,selected=0,stale|=keys;+ } else if(view==DK_RESULT) {+ if(next||prev)selected=!selected;+ if(back||(ok&&selected))view=DK_TITLE,selected=0;+ else if(ok)fresh(now),view=DK_PLAY,selected=0,ongoing=true,stale|=keys;+ } else if(view==DK_SAVE_ERROR) {+ if(back)break;+ if(ok&&save())break;+ if(hit&KEY(eadk_key_one))view=error_return,selected=error_selection;+ }+ }+ if(now-draw_at>=16||hit||!perf_frames) {+ uint32_t start=(uint32_t)eadk_timing_millis();+ dk_render(&game,&stats,view,selected,ongoing,save_failed,now,push);+ uint32_t spent=(uint32_t)eadk_timing_millis()-start;+ if(spent>perf_max)perf_max=spent;+ if(spent>17)++perf_slow;+ ++perf_frames;draw_at=now;+ }+ eadk_timing_msleep(2);+ }+ return np_app_end();+}diff --git a/games/donkeykong/src/render.c b/games/donkeykong/src/render.cnew file mode 100644index 00000000..3d3b4d81--- /dev/null+++ b/games/donkeykong/src/render.c@@ -0,0 +1,273 @@+/* SPDX-License-Identifier: GPL-3.0-only+ * Original pixel art, direct packed sprites, one 8-row RGB565 strip (5 KiB).+ * Only changed strips are sent to the LCD; no heap or full framebuffer. */+#include "render.h"+#include <stdlib.h>+#include <string.h>+#include "assets.h"+#include "font.h"+#define C(v) ((uint16_t)((((v)>>8)&0xF800)|(((v)>>5)&0x07E0)|(((v)>>3)&31)))+#define BLACK C(0x070B16)+#define PANEL C(0x111A2B)+#define DIM C(0x52617B)+#define STEEL C(0x29364F)+#define WHITE C(0xFFF0CD)+#define RED C(0xFF5545)+#define PINK C(0xF566AF)+#define GOLD C(0xFFE16B)+#define CYAN C(0x77DAEA)+static uint16_t band[320*8];+static uint32_t hashes[30];+static bool valid;+static int top;+static const dk_game *g;+static const dk_stats *s;+static unsigned view,selected;+static bool ongoing,failed;+static uint32_t clock_ms;+void dk_invalidate(void){valid=false;}+static void pixel(int x,int y,uint16_t color) {+ if(x>=0&&x<320&&y>=top&&y<top+8)band[(y-top)*320+x]=color;+}+static void rect(int x,int y,int w,int h,uint16_t color) {+ int l=x<0?0:x,r=x+w>320?320:x+w,t=y<top?top:y,b=y+h>top+8?top+8:y+h;+ if(l>=r||t>=b)return;+ for(int j=t;j<b;++j)for(int i=l;i<r;++i)band[(j-top)*320+i]=color;+}+static void line(int a,int b,int c,int d,uint16_t color) {+ if((b<top&&d<top)||(b>=top+8&&d>=top+8))return;+ int dx=abs(c-a),sx=a<c?1:-1,dy=-abs(d-b),sy=b<d?1:-1,e=dx+dy;+ for(;;){pixel(a,b,color);if(a==c&&b==d)break;int twice=e*2;+ if(twice>=dy)e+=dy,a+=sx;+ if(twice<=dx)e+=dx,b+=sy;}+}+static void frame(int x,int y,int w,int h,uint16_t color) {+ rect(x,y,w,1,color);rect(x,y+h-1,w,1,color);rect(x,y,1,h,color);rect(x+w-1,y,1,h,color);+}+static void text(int x,int y,const char *str,int scale,uint16_t color) {+ if(y>=top+8||y+7*scale<=top)return;+ for(;*str;++str,x+=6*scale) {+ unsigned ch=(unsigned char)*str,i;if(ch>='a'&&ch<='z')ch-=32;+ if(ch>='A'&&ch<='Z')i=ch-'A';+ else if(ch>='0'&&ch<='9')i=ch-'0'+26;+ else{static const char punctuation[]="+-.:/!<>|";const char *p=strchr(punctuation,(int)ch);+ if(!p||ch==' ')continue;+ i=36+(unsigned)(p-punctuation);}+ for(int col=0;col<5;++col)for(int row=0;row<7;++row)+ if(alphabet[i][col]&(1<<row))rect(x+col*scale,y+row*scale,scale,scale,color);+ }+}+static void center(int x,int y,const char *str,int scale,uint16_t color) {+ text(x-((int)strlen(str)*6*scale-scale)/2,y,str,scale,color);+}+static void number(int x,int y,uint32_t v,int scale,uint16_t color) {+ char str[11];unsigned n=0;do{str[n++]=(char)('0'+v%10);v/=10;}while(v);+ for(unsigned i=0;i<n/2;++i){char c=str[i];str[i]=str[n-i-1];str[n-i-1]=c;}str[n]=0;text(x,y,str,scale,color);+}+static void sprite(int x,int y,const dk_sprite *p,int face,int scale,bool blue) {+ if(y>=top+8||y+p->h*scale<=top)return;+ int first=(top-y)/scale;if(first<0)first=0;+ int last=(top+7-y)/scale;if(last>=p->h)last=p->h-1;+ for(int row=first;row<=last;++row)for(int col=0;col<p->w;++col) {+ unsigned i=(unsigned)(row*p->w+col),v=p->p[i/2];v=(i&1)?v&15:v>>4;+ if(v)rect(x+(face<0?p->w-col-1:col)*scale,y+row*scale,scale,scale,+ blue&&(v==12||v==7)?CYAN:palette[v]);+ }+}+static int wx(int x){return x+88;}+static int wy(int y){return y+8;}+static void heart(int x,int y) {+ rect(x,y,3,3,PINK);rect(x+5,y,3,3,PINK);rect(x-1,y+2,10,3,PINK);+ rect(x,y+5,8,1,PINK);rect(x+1,y+6,6,1,PINK);rect(x+2,y+7,4,1,PINK);rect(x+3,y+8,2,1,PINK);+}+static void mario(int x,int feet,int face,bool ladder,bool dead,int step,bool hammer) {+ if(dead) { // The silhouette tumbles once, then stays still during the death cue.+ sprite(x-6,feet-13,&spr_mario,face,1,false);line(x-6,feet-6,x+6,feet-1,WHITE);return;+ }+ sprite(x-6,feet-16,&spr_mario,face,1,false);+ if(ladder) {+ rect(x-6,feet-9,2,3,palette[2]);rect(x+5,feet-12,2,3,palette[2]);+ rect(x-4,feet-4,8,4,BLACK);rect(x-4,feet-(step?5:3),3,3,palette[3]);+ rect(x+2,feet-(step?3:5),3,3,palette[3]);+ } else if(step) {+ rect(x-6,feet-2,13,2,BLACK);rect(x-3,feet-2,4,2,palette[5]);rect(x+3,feet-4,4,2,palette[5]);+ }+ if(hammer) sprite(x+(face<0?-17:9),feet-23+(step?7:0),&spr_hammer,face,1,false);+}+static void kong(int x,int feet) {+ if(g->board==3&&g->phase==DK_CLEAR)feet+=(125-g->timer)/2;+ sprite(wx(x-13),wy(feet-25),&spr_kong,1,1,false);+ // Fists move with barrel throws; the tie is red, not another bitmap frame.+ if((g->tick/24)&1){rect(wx(x-15),wy(feet-17),4,5,palette[7]);rect(wx(x+12),wy(feet-17),4,5,palette[7]);}+ rect(wx(x-1),wy(feet-10),3,5,RED);rect(wx(x-2),wy(feet-6),5,3,RED);+}+static void girders(void) {+ for(unsigned i=0;i<g->platform_count;++i) {+ const dk_platform *p=&g->platform[i];+ int low=p->y1<p->y2?p->y1:p->y2,high=p->y1>p->y2?p->y1:p->y2;+ if(wy(low)>top+7||wy(high+5)<top)continue;+ uint16_t color=p->type==DK_LIFT?GOLD:PINK;+ for(int x=p->x1;x<=p->x2;++x) {+ int y=dk_surface(g,i,x);if(y<0)continue;+ pixel(wx(x),wy(y),color);pixel(wx(x),wy(y+4),color);+ if(p->type==DK_BELT) {+ pixel(wx(x),wy(y+2),STEEL);+ int offset=dk_belt_direction(g,p)*(int)(g->age/4);+ if(((x+offset)%8+8)%8<2)pixel(wx(x),wy(y+2),WHITE);+ } else {+ int k=(x-p->x1)%8;pixel(wx(x),wy(y+1+(k<4?k:7-k)),color);+ }+ }+ if(p->type==DK_BELT) {+ rect(wx(p->x1),wy(low),2,5,CYAN);rect(wx(p->x2-1),wy(high),2,5,CYAN);+ }+ }+ if(g->board==2)for(int x=60;x<=124;x+=56)line(wx(x),wy(57),wx(x),wy(216),STEEL);+}+static void ladders(void) {+ for(unsigned i=0;i<g->ladder_count;++i) {+ const dk_ladder *l=&g->ladder[i];+ int t=dk_surface(g,l->upper,l->x)+2,b=dk_surface(g,l->lower,l->x);+ bool open=dk_ladder_open(g,i);uint16_t color=open?CYAN:DIM;+ if(l->retract&&!open)t=b-7;+ for(int y=t;y<b;++y) {+ if(l->broken&&y>t+7&&y<b-7)continue;+ pixel(wx(l->x-3),wy(y),color);pixel(wx(l->x+3),wy(y),color);+ if((y-t)%4==0)rect(wx(l->x-3),wy(y),7,1,color);+ }+ }+}+static void world(void) {+ rect(87,7,226,226,BLACK);frame(86,6,228,228,STEEL);+ girders();ladders();+ for(unsigned i=0;i<5;++i)if(g->item[i].active) {+ const dk_item *v=&g->item[i];const dk_sprite *p=v->type==1?&spr_hammer:v->type==2?&spr_bag:v->type==3?&spr_hat:&spr_parasol;+ sprite(wx(v->x-p->w/2),wy(v->y-p->h/2),p,1,1,false);+ }+ if(g->board==3)for(int row=1;row<=4;++row)for(int side=0;side<2;++side)+ if(!(g->rivets&(1u<<((row-1)*2+side)))) {+ int x=side?176:48,y=g->platform[row].y1;+ rect(wx(x-2),wy(y-2),5,2,CYAN);rect(wx(x),wy(y),1,5,CYAN);+ }+ if(g->board==0||g->board==1) {+ int x=g->board==0?17:112,y=g->board==0?212:176;+ rect(wx(x-7),wy(y-13),14,13,palette[3]);rect(wx(x-8),wy(y-14),16,2,CYAN);+ if((g->age/8)&1)sprite(wx(x-5),wy(y-23),&spr_fire,1,1,true);+ center(wx(x),wy(y-8),"OIL",1,WHITE);+ }+ int kx=g->board==3?112:g->board==1?80+(int)(g->age/8%42):34;+ int ky=g->board==0?58:g->board==1?68:g->board==2?55:66;+ kong(kx,ky);+ int px=g->board==0?163:g->board==1?198:g->board==2?204:185;+ int py=g->board==0?25:g->board==1?28:g->board==2?24:28;+ sprite(wx(px-5),wy(py-18),&spr_pauline,1,1,false);+ if(g->phase==DK_CLEAR&&g->board==3)heart(wx(165),wy(5));+ else if((g->tick/45)&1)text(wx(px-36),wy(4),"HELP!",1,PINK);+ for(unsigned i=0;i<DK_ENEMIES;++i) {+ const dk_enemy *e=&g->enemy[i];if(!e->active)continue;+ int x=wx(e->x/DK_Q),y=wy(e->y/DK_Q);+ if(e->type==DK_SPRING) {+ rect(x-5,y-8,10,1,CYAN);rect(x-5,y-1,10,1,CYAN);+ for(int j=0;j<3;++j)line(x-5,y-7+j*2,x+4,y-5+j*2,WHITE);+ } else {+ const dk_sprite *p=e->type==DK_BARREL?&spr_barrel:e->type==DK_FIRE?&spr_fire:&spr_tub;+ sprite(x-p->w/2,y-p->h,p,e->dir,1,e->blue);+ if(e->type==DK_BARREL)line(x-3,y-5,x+3,y-5+(int)(g->age/6%3)-1,GOLD);+ }+ }+ const dk_player *p=&g->player;+ if(g->phase!=DK_DYING||(g->timer/7)&1)+ mario(wx(p->x/DK_Q),wy(p->y/DK_Q),p->face,p->ladder>=0,g->phase==DK_DYING,+ (int)(g->tick/7&1)&&((g->previous_input&(DK_LEFT|DK_RIGHT|DK_UP|DK_DOWN))!=0),p->hammer!=0);+ if(g->popup_time)number(wx(g->popup_x-8),wy(g->popup_y),g->popup_value,1,WHITE);+}+static void side(void) {+ text(8,12,"1UP",1,RED);number(8,24,g->score,1,WHITE);+ text(8,43,"HI SCORE",1,DIM);number(8,55,s->best,1,GOLD);+ rect(8,73,65,1,STEEL);+ text(8,84,"ROUND",1,DIM);number(48,84,g->round,1,WHITE);+ static const char *const stage[]={"25M","50M","75M","100M"};+ text(8,103,stage[g->board],2,CYAN);+ text(8,126,"BONUS",1,DIM);number(8,138,g->bonus,1,GOLD);+ text(8,160,"LIVES",1,DIM);+ for(unsigned i=0;i<g->lives&&i<4;++i)mario(14+(int)i*16,189,1,false,false,0,false);+ if(g->lives>4)number(61,181,g->lives,1,WHITE);+ if(g->player.hammer) { // eight seconds, shown without an extra HUD row+ rect(8,197,64,3,STEEL);rect(8,197,g->player.hammer*64/480,3,GOLD);+ }+ text(8,214,"BACK",1,DIM);text(8,226,"PAUSE",1,WHITE);+}+static void button(int x,int y,int w,const char *label,bool on) {+ rect(x,y,w,20,on?GOLD:PANEL);+ text(x+8,y+6,label,1,on?BLACK:WHITE);+ if(on)text(x+w-12,y+6,">",1,BLACK);+}+static void overlay(const char *title,int height) {+ int y=(240-height)/2;rect(94,y,210,height,PANEL);frame(94,y,210,height,GOLD);+ center(199,y+12,title,1,GOLD);+}+static void title(void) {+ // A vertical control panel next to a live-looking attract board.+ text(7,14,"DONKEY",2,RED);text(7,33,"KONG",2,GOLD);+ text(8,58,"ARCADE",1,CYAN);rect(8,74,65,1,STEEL);+ text(8,84,"BEST",1,DIM);number(8,96,s->best,1,WHITE);+ button(4,123,76,ongoing?"RESUME":"PLAY",selected==0);+ button(4,148,76,"HOW TO",selected==1);+ button(4,173,76,"EXIT",selected==2);+ text(8,211,"BY",1,DIM);text(8,224,"BOBYDELUXE",1,WHITE);+ overlay("HOW HIGH CAN YOU GET?",54);center(199,119,"FOUR BOARDS. ONE RESCUE.",1,WHITE);+ center(199,134,clock_ms/600%2?"PRESS OK":"",1,CYAN);+}+static void help(void) {+ rect(0,0,320,240,BLACK);rect(0,0,320,30,PANEL);text(12,10,"HOW TO PLAY",2,GOLD);+ text(12,43,"LEFT / RIGHT",1,CYAN);text(132,43,"MOVE",1,WHITE);+ text(12,59,"UP / DOWN",1,CYAN);text(132,59,"CLIMB LADDERS",1,WHITE);+ text(12,75,"OK / EXE / SHIFT",1,CYAN);text(132,75,"JUMP",1,WHITE);+ text(12,91,"BACK",1,CYAN);text(132,91,"PAUSE",1,WHITE);+ rect(12,107,296,1,STEEL);+ text(12,119,"25M JUMP BARRELS. CLIMB TO PAULINE.",1,WHITE);+ text(12,135,"50M WATCH THE BELTS AND OIL FIRE.",1,WHITE);+ text(12,151,"75M RIDE LIFTS. DODGE THE SPRINGS.",1,WHITE);+ text(12,167,"100M REMOVE ALL EIGHT BLUE RIVETS.",1,WHITE);+ text(12,186,"HAMMER: NO JUMPING OR CLIMBING.",1,GOLD);+ text(12,201,"LONG FALLS HURT. EXTRA LIFE: 7000.",1,DIM);+ text(12,223,"ARCADE ADAPTATION / BY BOBYDELUXE",1,CYAN);+ text(12,213,"ORIGINAL GAME: NINTENDO / 1981",1,DIM);+ text(278,12,"BACK",1,WHITE);+}+static void scene(void) {+ rect(0,top,320,8,BLACK);+ if(view==DK_HELP){help();return;}+ side();world();+ if(view==DK_TITLE) {rect(0,0,84,240,BLACK);title();}+ else if(view==DK_PLAY) {+ if(g->phase==DK_READY) {overlay("GET READY",52);center(199,123,"SAVE PAULINE!",1,WHITE);}+ if(g->phase==DK_CLEAR) {overlay(g->board==3?"PAULINE IS SAFE!":"BOARD CLEAR",42);center(199,123,"BONUS ADDED",1,WHITE);}+ if(g->phase==DK_DYING)center(199,15,g->reason==DK_TIME?"TIME UP!":"TRY AGAIN!",1,RED);+ } else if(view==DK_PAUSE) {+ overlay("PAUSED",116);button(108,91,182,"RESUME",selected==0);+ button(108,117,182,"RESTART",selected==1);button(108,143,182,"TITLE",selected==2);+ } else if(view==DK_CONFIRM) {+ overlay("RESTART THIS RUN?",94);button(108,105,182,"KEEP PLAYING",selected==0);+ button(108,131,182,"RESTART",selected==1);+ } else if(view==DK_RESULT) {+ overlay("GAME OVER",122);center(199,82,"FINAL SCORE",1,DIM);number(170,97,g->score,2,WHITE);+ button(108,121,182,"PLAY AGAIN",selected==0);button(108,147,182,"TITLE",selected==1);+ } else if(view==DK_SAVE_ERROR) {+ overlay("STORAGE FULL",108);center(199,96,"RECORD COULD NOT BE SAVED",1,WHITE);+ center(199,114,"OK: RETRY AND EXIT",1,GOLD);center(199,132,"1: KEEP PLAYING",1,WHITE);+ center(199,150,"BACK: EXIT WITHOUT SAVING",1,DIM);+ }+ if(failed&&view!=DK_SAVE_ERROR)rect(77,3,4,4,RED);+}+void dk_render(const dk_game *game,const dk_stats *stats,unsigned page,unsigned selection,+ bool continuing,bool save_failed,uint32_t ms,dk_push push) {+ g=game;s=stats;view=page;selected=selection;ongoing=continuing;failed=save_failed;clock_ms=ms;+ for(unsigned strip=0;strip<30;++strip) {+ top=(int)strip*8;scene();uint32_t hash=2166136261u;+ for(unsigned i=0;i<320*8;++i)hash=(hash^band[i])*16777619u;+ if(!valid||hashes[strip]!=hash)push(0,top,320,8,band);+ hashes[strip]=hash;+ }+ valid=true;+}diff --git a/games/donkeykong/src/render.h b/games/donkeykong/src/render.hnew file mode 100644index 00000000..6166bad3--- /dev/null+++ b/games/donkeykong/src/render.h@@ -0,0 +1,10 @@+/* SPDX-License-Identifier: GPL-3.0-only */+#ifndef DK_RENDER_H+#define DK_RENDER_H+#include "game.h"+enum { DK_TITLE, DK_PLAY, DK_PAUSE, DK_HELP, DK_CONFIRM, DK_RESULT, DK_SAVE_ERROR };+typedef void (*dk_push)(int,int,int,int,const uint16_t *);+void dk_render(const dk_game *,const dk_stats *,unsigned view,unsigned selection,+ bool continuing,bool save_failed,uint32_t ms,dk_push);+void dk_invalidate(void);+#endifdiff --git a/games/donkeykong/src/save.c b/games/donkeykong/src/save.cnew file mode 100644index 00000000..23defcbf--- /dev/null+++ b/games/donkeykong/src/save.c@@ -0,0 +1,16 @@+/* SPDX-License-Identifier: GPL-3.0-only */+#include "game.h"+#include <string.h>+static void put32(uint8_t *p,uint32_t x){for(unsigned i=0;i<4;++i)p[i]=(uint8_t)(x>>(i*8));}+static uint32_t get32(const uint8_t *p){return p[0]|(uint32_t)p[1]<<8|(uint32_t)p[2]<<16|(uint32_t)p[3]<<24;}+static uint32_t hash(const uint8_t *p){uint32_t h=2166136261u;for(unsigned i=0;i<28;++i)h=(h^p[i])*16777619u;return h;}+void dk_encode_stats(const dk_stats *s,uint8_t out[DK_SAVE_BYTES]) {+ memset(out,0,DK_SAVE_BYTES);memcpy(out,"DKA1",4);put32(out+4,s->best);put32(out+8,s->runs);+ put32(out+12,s->rescues);put32(out+16,s->best_round);put32(out+28,hash(out));+}+bool dk_decode_stats(dk_stats *s,const uint8_t *data,size_t size) {+ if(!data||size!=DK_SAVE_BYTES||memcmp(data,"DKA1",4)||get32(data+28)!=hash(data))return false;+ dk_stats next={get32(data+4),get32(data+8),get32(data+12),(uint16_t)get32(data+16)};+ if(next.best>DK_MAX_SCORE||get32(data+16)>999)return false;+ *s=next;return true;+}