notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

hello.c (1199B)


      1 /*
      2  * Minimal notedeck WASM app.
      3  *
      4  * Build:
      5  *   clang --target=wasm32 -nostdlib \
      6  *     -Wl,--no-entry -Wl,--export=nd_update -Wl,--allow-undefined \
      7  *     -o hello.wasm hello.c
      8  */
      9 
     10 #include "../api/notedeck_api.h"
     11 
     12 static int count = 0;
     13 
     14 /* simple int-to-string, returns length written */
     15 static int itoa_simple(int n, char *buf, int buf_size) {
     16     if (buf_size < 2) return 0;
     17     if (n == 0) { buf[0] = '0'; return 1; }
     18 
     19     int neg = 0;
     20     if (n < 0) { neg = 1; n = -n; }
     21 
     22     int len = 0;
     23     char tmp[16];
     24     while (n > 0 && len < 16) {
     25         tmp[len++] = '0' + (n % 10);
     26         n /= 10;
     27     }
     28     if (neg && len < 16) tmp[len++] = '-';
     29 
     30     if (len > buf_size) len = buf_size;
     31     for (int i = 0; i < len; i++) {
     32         buf[i] = tmp[len - 1 - i];
     33     }
     34     return len;
     35 }
     36 
     37 void nd_update(void) {
     38     nd_heading("Hello from WASM!", 16);
     39     nd_add_space(8.0f);
     40 
     41     if (nd_button("Click me", 8)) {
     42         count++;
     43     }
     44 
     45     nd_add_space(4.0f);
     46 
     47     char buf[48];
     48     /* "Clicks: " prefix */
     49     buf[0]='C'; buf[1]='l'; buf[2]='i'; buf[3]='c';
     50     buf[4]='k'; buf[5]='s'; buf[6]=':'; buf[7]=' ';
     51     int num_len = itoa_simple(count, buf + 8, 40);
     52     nd_label(buf, 8 + num_len);
     53 }