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.

2.0 KB
Raw Blame
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC optimize("Os")
#endif
/* Subway Surfers for the NumWorks calculator.
 *
 * Inspired by Subway Surfers by SYBO and Kiloo, not affiliated with them.
 * Made by Mason Chen as part of NumPlay. */
#include "app.h"
#include "plat.h"
#include "r3d.h"

#ifndef HOST
#ifndef NP_PORT /* (another target: platform/port.mk names the app) */
const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Subway Surfers";
const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;
#endif

uint32_t perf_frames __attribute__((used));   /* read by tools/emu.py */
uint32_t perf_max __attribute__((used));
uint32_t perf_slow __attribute__((used));

int main(void) {
  uint16_t band[SCREEN_W * BAND_H];   /* (here: on the stack, out of the RAM apps get) */
  r_set_band_memory(band);
  /* the renderer's lists: the RAM after the game's own data */
  extern char _heap_start[], _heap_end[];
  uintptr_t heap = ((uintptr_t)_heap_start + 7) & ~(uintptr_t)7;
  if ((uintptr_t)_heap_end < heap + r_work_size()) return 1;
  r_init((void *)heap);
  plat_begin();
  app_init();
  uint32_t prev = 0;
  /* the game's 60 steps a second, drawn when caught up (at most 4 steps a drawing: slower models skip) */
  uint32_t next = plat_millis() * 3;   /* in thirds of a millisecond: 50 a step */
  while (app_running) {
    int n = 0;
    uint32_t now = plat_millis() * 3, keys = plat_keys();
    while ((int32_t)(now - next) >= 0 && n < 4 && app_running) {
      app_step(keys, n ? 0 : keys & ~prev);
      next += 50;
      n++;
    }
    if (n) prev = keys;
    if (!app_running) break;
    if ((int32_t)(now - next) > 400) next = now;   /* far behind: do not rush */
    if (n) {
      uint32_t t0 = plat_millis();
      app_draw();
      uint32_t dt = plat_millis() - t0;
      if (dt > perf_max) perf_max = dt;
      if (dt > 33) perf_slow++;
      perf_frames++;
    } else
      plat_sleep(1);
  }
  return plat_end();
}
#endif