From 93bba30c3aea778344ad1e735e015977510dcbc8 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Wed, 21 Sep 2016 02:40:23 -0400 Subject: SDL2 level renderer in C --- graftest/Makefile | 23 +++ graftest/blob2c.c | 117 ++++++++++++ graftest/graftest.c | 537 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 677 insertions(+) create mode 100644 graftest/Makefile create mode 100644 graftest/blob2c.c create mode 100644 graftest/graftest.c (limited to 'graftest') diff --git a/graftest/Makefile b/graftest/Makefile new file mode 100644 index 0000000..e16ae6d --- /dev/null +++ b/graftest/Makefile @@ -0,0 +1,23 @@ +HOSTCC=gcc +CC=gcc + +CFLAGS=-Wall -O2 +HOSTCFLAGS=-Wall -O2 + +all: rom.c + gcc -O2 -Wall -o graftest graftest.c rom.c `pkg-config sdl2 --cflags --libs` + +rom.c: blob2c jumpmanjr.rom + ./blob2c jumpmanjr.rom > rom.c 2> rom.h + +blob2c: blob2c.c + $(HOSTCC) $(HOSTCFLAGS) -o blob2c blob2c.c + +jumpmanjr.rom: + ln -s ../jumpmanjr.rom . + +test: all + ./graftest + +clean: + rm -f *.o graftest core blob2c jumpmanjr.rom rom.h rom.c diff --git a/graftest/blob2c.c b/graftest/blob2c.c new file mode 100644 index 0000000..779b79f --- /dev/null +++ b/graftest/blob2c.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include + +/* since we're emitting C source, the comments containing the + ASCII dump better not contain C comment markers! */ +void fixasc(char *asc, int len) { + int j; + + for(j=0; j<(len-1); j++) { + /* slash-star or slash-slash become slash-dot */ + if(asc[j] == '/' && (asc[j+1] == '*' || asc[j+1] == '/')) + asc[j+1] = '.'; + /* star-slash becomes dot-slash */ + else if(asc[j] == '*' && asc[j+1] == '/') + asc[j] = '.'; + } +} + +int main(int argc, char **argv) { + int i, j, count = 0; + char *p, asc[9]; + FILE *in; + + if(argc != 2) { + fprintf(stderr, + "#error usage: %s blobfile >blobfile.c 2>blobfile.h\n", argv[0]); + exit(1); + } + + if( !(in = fopen(argv[1], "rb")) ) { + fprintf(stderr, "/* %s: %s */\n", argv[1], strerror(errno)); + exit(1); + } + + printf("/* C source created by blob2c from input file %s */\n\n", argv[1]); + fprintf(stderr, + "/* C header created by blob2c from input file %s */\n\n", argv[1]); + + for(p = argv[1]; *p; p++) + if(!isalnum(*p)) *p = '_'; + + fprintf(stderr, + "#ifndef %s_H\n" + "#define %s_H\n\n", + argv[1], argv[1]); + + /* start array definition */ + printf("unsigned char %s[] = {", argv[1]); + + asc[8] = '\0'; + + /* read/process each input byte in loop, until EOF */ + while( (i = getc(in)) != EOF ) { + if(count) putchar(','); + + if(count % 8 == 0) { + if(count) { + /* fix & print ASCII dump */ + fixasc(asc, 8); + printf(" /* %s */", asc); + } + + /* start next line */ + printf("\n\t/* %6d */ ", count); + } + + /* store this byte of ASCII dump */ + asc[count % 8] = isprint(i) ? i : '.'; + + /* print this byte of hex data */ + printf("0x%02x", i); + + count++; + } + + /* fix ASCII dump for last line */ + i = count % 8; + if(!i) i = 8; + fixasc(asc, i); + asc[i] = '\0'; + + /* line it up with the previous lines */ + j = (8 - i) * 5; + for(i=0; i