juegos

Juegos de terminal de Pancho: Catan (TUI, GUI, web, servidor, PicoCalc), ajedrez, calculadora y minijuegos
git clone https://git.lu3dhn.xyz/juegos.git
Log | Files | Refs

app.c (1405B)


      1 /* app.c - la calculadora en la PicoCalc (y en el simulador, con hal_sim.c). */
      2 #include "hal.h"
      3 #include "calc_gfx.h"
      4 
      5 static Calc C;
      6 static char save_buf[16000];
      7 
      8 static void save(void)
      9 {
     10     int n = persist_save(&C, save_buf, sizeof save_buf);
     11     if (n > 0) hal_store_save(save_buf, n);
     12     C.dirty_save = false;
     13 }
     14 
     15 void app_main(void)
     16 {
     17     hal_keys_ext = true;               /* Supr, F1-F10, Inicio/Fin y codigos crudos */
     18     hal_init();
     19     calc_init(&C);
     20     C.rand = hal_random;
     21     int n = hal_store_load(save_buf, sizeof save_buf - 1);
     22     if (n > 0) { save_buf[n] = 0; persist_load(&C, save_buf, n); }
     23     bool dirty = true;
     24     uint32_t last_change = 0, last_bat = 0;
     25     int bat = hal_battery();
     26     for (;;) {
     27         int k = hal_key();
     28         if (k == 0) dirty = true;
     29         else if (k > 0) {
     30             if ((k == K_UP || k == K_DOWN) && calc_vert_px(&C, k == K_UP ? -1 : 1)) dirty = true;
     31             else if (calc_key(&C, k)) dirty = true;
     32             last_change = hal_ms();
     33         }
     34         /* la SD es lenta: se guarda 2 s despues del ultimo cambio */
     35         if (C.dirty_save && hal_ms() - last_change > 2000) save();
     36         if (hal_ms() - last_bat > 30000) { bat = hal_battery(); last_bat = hal_ms(); dirty = true; }
     37         if (dirty) {
     38             calc_prepare(&C, bat);
     39             hal_draw(calc_paint, &C);
     40             dirty = false;
     41         } else hal_sleep(5);
     42     }
     43 }