cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

gprintf.c (1491B)


      1#include <stdint.h>
      2#include <stdarg.h>
      3#include <gb/drawing.h>
      4
      5int8_t gprintf(char *fmt, ...) NONBANKED
      6{
      7  va_list ap;
      8  int8_t nb = 0;
      9
     10  va_start(ap, fmt);
     11  for(; *fmt; fmt++)
     12    if(*fmt == '%') {
     13      switch(*++fmt) {
     14      case 'c': {
     15	/* char */
     16	  char c = va_arg(ap, char);
     17	  wrtchr(c);
     18	  break;
     19      }
     20      case 'd': {
     21	/* decimal int */
     22	  int8_t b = va_arg(ap, int8_t);
     23	  gprintn(b, 10, SIGNED);
     24	  break;
     25      }
     26      case 'u': {
     27	  /* unsigned int */
     28	  int8_t b = (int8_t)va_arg(ap, int);
     29	  gprintn(b, 10, UNSIGNED);
     30	  break;
     31      }
     32      case 'o': {
     33	  /* octal int */
     34	  int8_t b = va_arg(ap, int8_t);
     35	  gprintn(b, 8, UNSIGNED);
     36	  break;
     37      }
     38      case 'x': {
     39	  /* hexadecimal int */
     40	  int8_t b = va_arg(ap, int8_t);
     41	  gprintn(b, 16, UNSIGNED);
     42	  break;
     43      }
     44      case 's': {
     45	  /* string */
     46	  char *s = va_arg(ap, char *);
     47	  gprint(s);
     48	  break;
     49      }
     50#if 0
     51      case 'l':
     52	/* long */
     53	switch(*++fmt) {
     54	case 'd':
     55	  /* decimal long */
     56	  gprintln(va_arg(ap, int16_t), 10, SIGNED);
     57	  break;
     58	case 'u':
     59	  /* unsigned long */
     60	  gprintln(va_arg(ap, int16_t), 10, UNSIGNED);
     61	  break;
     62	case 'o':
     63	  /* octal long */
     64	  gprintln(va_arg(ap, int16_t), 8, UNSIGNED);
     65	  break;
     66	case 'x':
     67	  /* hexadecimal long */
     68	  gprintln(va_arg(ap, int16_t), 16, UNSIGNED);
     69	  break;
     70	}
     71	break;
     72#endif
     73      case '%':
     74	/* % */
     75	wrtchr(*fmt);
     76	break;
     77      default:
     78	return -1;
     79      }
     80      nb++;
     81    } else
     82      wrtchr(*fmt);
     83  va_end(ap);
     84
     85  return nb;
     86}