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
/* Whole-number math, randomness and the buttons. */
#include "ce.h"
int fdiv(int a, int b) {
int q = a / b;
return (a % b != 0 && a < 0) ? q - 1 : q;
}
/* Math.Round(movementCounter): the counter c (MU) in whole pixels. Celeste's frame is 0.0166667 s, a hair longer than
* 1/60, so what moves goes that much further: the float counter is ahead of the exact one by a little more than a
* millionth of all it moved since it was last zeroed. A counter exactly between two pixels (which whole numbers hit and
* floats almost never do) rounds the way that sum goes (dir: it; 0: to even, as .NET does). */
int round_mu(int c, int dir) {
int n = fdiv(c + MU / 2, MU); /* half up */
if ((c + MU / 2) - n * MU == 0) { /* exactly halfway between n - 1 and n */
if (dir < 0) n--;
else if (dir == 0 && (n & 1)) n--;
}
return n;
}
int bias_add(int b, int amount) {
b += amount;
return b > 4000000 ? 4000000 : b < -4000000 ? -4000000 : b;
}
int isqrt(int32_t v) {
if (v <= 0) return 0;
uint32_t r = 0, b = 1ul << 30, x = (uint32_t)v;
while (b > x) b >>= 2;
while (b) {
if (x >= r + b) x -= r + b, r = (r >> 1) + b;
else r >>= 1;
b >>= 2;
}
return (int)r;
}
/* sin over a quarter turn (256 steps), 32767 at the top */
static const uint16_t SIN[257] = {
0, 201, 402, 603, 804, 1005, 1206, 1407, 1608, 1809, 2009, 2210, 2410, 2611, 2811, 3012, 3212, 3412, 3612, 3811,
4011, 4210, 4410, 4609, 4808, 5007, 5205, 5404, 5602, 5800, 5998, 6195, 6393, 6590, 6786, 6983, 7179, 7375, 7571,
7767, 7962, 8157, 8351, 8545, 8739, 8933, 9126, 9319, 9512, 9704, 9896, 10087, 10278, 10469, 10659, 10849, 11039,
11228, 11417, 11605, 11793, 11980, 12167, 12353, 12539, 12725, 12910, 13094, 13279, 13462, 13645, 13828, 14010,
14191, 14372, 14553, 14732, 14912, 15090, 15269, 15446, 15623, 15800, 15976, 16151, 16325, 16499, 16673, 16846,
17018, 17189, 17360, 17530, 17700, 17869, 18037, 18204, 18371, 18537, 18703, 18868, 19032, 19195, 19357, 19519,
19680, 19841, 20000, 20159, 20317, 20475, 20631, 20787, 20942, 21096, 21250, 21403, 21554, 21705, 21856, 22005,
22154, 22301, 22448, 22594, 22739, 22884, 23027, 23170, 23311, 23452, 23592, 23731, 23870, 24007, 24143, 24279,
24413, 24547, 24680, 24811, 24942, 25072, 25201, 25329, 25456, 25582, 25708, 25832, 25955, 26077, 26198, 26319,
26438, 26556, 26674, 26790, 26905, 27019, 27133, 27245, 27356, 27466, 27575, 27683, 27790, 27896, 28001, 28105,
28208, 28310, 28411, 28510, 28609, 28706, 28803, 28898, 28992, 29085, 29177, 29268, 29358, 29447, 29534, 29621,
29706, 29791, 29874, 29956, 30037, 30117, 30195, 30273, 30349, 30424, 30498, 30571, 30643, 30714, 30783, 30852,
30919, 30985, 31050, 31113, 31176, 31237, 31297, 31356, 31414, 31470, 31526, 31580, 31633, 31685, 31736, 31785,
31833, 31880, 31926, 31971, 32014, 32057, 32098, 32137, 32176, 32213, 32250, 32285, 32318, 32351, 32382, 32412,
32441, 32469, 32495, 32521, 32545, 32567, 32589, 32609, 32628, 32646, 32663, 32678, 32692, 32705, 32717, 32728,
32737, 32745, 32752, 32757, 32761, 32765, 32766, 32767};
int sin15(int a) {
a &= 1023;
if (a < 256) return SIN[a];
if (a < 512) return SIN[512 - a];
if (a < 768) return -(int)SIN[a - 512];
return -(int)SIN[1024 - a];
}
static uint32_t seed = 0x2F6E2B1u;
uint32_t rnd(void) { /* xorshift */
seed ^= seed << 13, seed ^= seed >> 17, seed ^= seed << 5;
return seed;
}
/* ---------------------------------------------------------------- the buttons: Celeste's 0.08 s press buffer */
Input g_in;
static int last_x, last_y;
static void button(Button *b, bool down, bool was) {
b->consumed = false;
if (b->buffer > 0) b->buffer = (int8_t)(b->buffer - DTU);
b->edge = down && !was;
b->check = down;
if (b->edge) b->buffer = (int8_t)TIM(.08f);
if (!down) b->buffer = 0;
}
void input_update(unsigned keys) {
unsigned prev = g_in.keys;
g_in.prev = prev;
g_in.keys = keys;
button(&g_in.jump, keys & K_JUMP, prev & K_JUMP);
button(&g_in.dash, keys & K_DASH, prev & K_DASH);
button(&g_in.grab, keys & K_GRAB, prev & K_GRAB);
button(&g_in.pause, keys & K_PAUSE, prev & K_PAUSE);
button(&g_in.ok, keys & K_OK, prev & K_OK);
button(&g_in.back, keys & K_BACK, prev & K_BACK);
button(&g_in.talk, keys & K_TALK, prev & K_TALK);
bool l = keys & K_LEFT, r = keys & K_RIGHT, u = keys & K_UP, d = keys & K_DOWN;
if (l && !(prev & K_LEFT)) last_x = -1;
if (r && !(prev & K_RIGHT)) last_x = 1;
if (u && !(prev & K_UP)) last_y = -1;
if (d && !(prev & K_DOWN)) last_y = 1;
g_in.move_x = l && r ? last_x : l ? -1 : r ? 1 : 0;
g_in.move_y = u && d ? last_y : u ? -1 : d ? 1 : 0;
g_in.aim_x = g_in.move_x, g_in.aim_y = g_in.move_y;
}