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.9 KB
Raw Blame
/* Snow Rider 3D for the NumWorks calculator.
 *
 * A port of Snow Rider 3D, its world read from the game's own Unity project (tools/gen.py) and its scripts
 * rewritten in C. Not affiliated with the makers of Snow Rider 3D. Made as part of NumPlay. */
#include "sr.h"
#include "r3d.h"
#include "gfx.h"

/* (gfx.c's own strips and arena are not used: it draws into the renderer's bands) */
void np_push(int x, int y, int w, int h, const color_t *px) { plat_push(x, y, w, h, px); }
#ifndef NP_PORT
void *np_alloc(uint32_t size) { (void)size; return 0; }
#endif

#ifndef HOST
#ifdef SR_BENCH
#include "../tests/auto.h"
#endif
#ifndef NP_PORT /* (another target: platform/port.mk names the app) */
const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Snow Rider 3D";
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));
uint32_t perf_draw __attribute__((used));   /* (milliseconds spent drawing) */
uint32_t perf_step __attribute__((used));   /* (and in the game's steps) */
uint32_t host_god __attribute__((used));    /* (set by tools/bench.py: the sled goes through everything) */

int main(void) {
  uint16_t band[SCREEN_W * BAND_H];   /* (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();
  draw_init();
  game_init();
  uint32_t prev = plat_keys();   /* (keys still down from the launcher wait to be let go) */
  /* 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 (game_running) {
    int n = 0;
    uint32_t now = plat_millis() * 3, keys = plat_keys();
#ifdef SR_BENCH
    /* (timing: two steps a drawing and the autopilot, the same frames whatever the speed) */
    while (n < 2 && game_running) {
      if (mode == M_PLAY) keys = (keys & ~(SK_LEFT | SK_RIGHT | SK_UP)) | autopilot();
#else
    while ((int32_t)(now - next) >= 0 && n < 4 && game_running) {
#endif
      uint32_t s0 = plat_millis();
      game_step(keys, n ? 0 : keys & ~prev);
      perf_step += plat_millis() - s0;
      next += 50;
      n++;
    }
    if (n) prev = keys;
    if (!game_running) break;
    if ((int32_t)(now - next) > 400) next = now;   /* far behind: do not rush */
    if (n) {
      uint32_t t0 = plat_millis();
      draw_frame();
      uint32_t dt = plat_millis() - t0;
      perf_draw += dt;
      if (dt > perf_max) perf_max = dt;
      if (dt > 33) perf_slow++;
      perf_frames++;
    } else
      plat_sleep(1);
  }
  return plat_end();
}
#endif