diff --git a/examples/game-of-life-png/Makefile b/examples/game-of-life-png/Makefile index d6554b52..ac2e5160 100644 --- a/examples/game-of-life-png/Makefile +++ b/examples/game-of-life-png/Makefile @@ -7,16 +7,16 @@ META_FILES = $(addsuffix .meta, $(SCM_LIBS)) GENC_FILES = $(addsuffix .c, $(SCM_LIBS)) COBJECTS=$(SLD_FILES:.sld=.o) -all: $(SCM_PROGRAM) write-png +all: $(SCM_PROGRAM) #write-png %.o: %.sld cyclone $< -$(SCM_PROGRAM): $(SCM_FILE) $(COBJECTS) +$(SCM_PROGRAM): $(SCM_FILE) $(COBJECTS) write-png.o cyclone $< -write-png: write-png.o - gcc -o write-png write-png.o -lpng +#write-png: write-png.o +# gcc -o write-png write-png.o -lpng write-png.o: write-png.c write-png.h gcc -c write-png.c diff --git a/examples/game-of-life-png/example/life.sld b/examples/game-of-life-png/example/life.sld index 6df7a0c1..1a43dc80 100644 --- a/examples/game-of-life-png/example/life.sld +++ b/examples/game-of-life-png/example/life.sld @@ -20,23 +20,30 @@ ((3) #t) ((2) (ref grid i j)) (else #f))) - (define (clear-vt100) - (display - (string - (integer->char #x1B) - #\[ - #\H - (integer->char #x1B) - #\[ - #\J))) - (define (life-print grid) - (clear-vt100) - (each grid - (lambda (i j v) - (display (if v "*" " ")) - (if (= j (- (cols grid) 1)) - ;(when (= j (- (cols grid) 1)) - (newline))))) +; (define (clear-vt100) +; (display +; (string +; (integer->char #x1B) +; #\[ +; #\H +; (integer->char #x1B) +; #\[ +; #\J))) + (define (life-print grid iteration) + ;(clear-vt100) + (let ((img (png:init 100 100)) + (path (string-append "life-" (number->string iteration) ".png"))) + (each grid + (lambda (i j v) + ;(display (if v "*" " ")) + ;(when (= j (- (cols grid) 1)) + ; (newline)) + (if v + (png:set! i j 0 250 0)) + )) + (png:save img path) + (png:free img) + )) (define (life grid iterations) (do ((i 0 (+ i 1)) (grid0 grid grid1) @@ -48,4 +55,35 @@ (let ((a (life-alive? grid0 j k))) (put! grid1 j k a)))) ;(set! grid1 j k a)))) - (life-print grid1))))) + (life-print grid1 i))) + (define-c png:init + "(void *data, int argc, closure _, object width, object height)" + " RGBBitmap *img = malloc(sizeof(RGBBitmap)); + make_c_opaque(opq, (void *)img); + bitmap_init(img, (int)(unbox_number(width)), (int)(unbox_number(height))); + return_closcall1(data, k, &opq); + ") + (define-c png:free + "(void *data, int argc, closure _, object opq)" + " RGBBitmap *img = opaque_ptr(opq); + free(img->pixels); + free(img); + return_closcall1(data, k, boolean_t); + ") + (define-c png:set! + "(void *data, int argc, closure _, object opq, object x, object y, object r, object g, object b)" + " RGBBitmap *img = opaque_ptr(opq); + bitmap_set(img, + ((int)(unbox_number(x))), + ((int)(unbox_number(y))), + ((int)(unbox_number(r))), + ((int)(unbox_number(g))), + ((int)(unbox_number(b)))); + return_closcall1(data, k, boolean_t); ") + (define-c png:save + "(void *data, int argc, closure _, object opq, object path)" + " RGBBitmap *img = opaque_ptr(opq); + bitmap_save_to_png(img, string_str(path)); + return_closcall1(data, k, boolean_t); + ") +)) diff --git a/examples/game-of-life-png/write-png.c b/examples/game-of-life-png/write-png.c index c17bf9b4..59dadf99 100644 --- a/examples/game-of-life-png/write-png.c +++ b/examples/game-of-life-png/write-png.c @@ -96,29 +96,29 @@ int bitmap_set(RGBBitmap *img, int x, int y, int r, int g, int b) return 0; } -int main() -{ - const char path[] = "test.png"; - int status = 0, x, y; - RGBBitmap img; - - bitmap_init(&img, 100, 100); - for (y = 0; y < img.height; y++) { - for (x = 0; x < img.height; x++) { - bitmap_set(&img, x, y, 255, 255, 255); - } - } - bitmap_set(&img, 50, 50, 0, 0, 255); - bitmap_set(&img, 0, 0, 0, 0, 255); - bitmap_set(&img, 99, 0, 0, 0, 255); - bitmap_set(&img, 0, 99, 0, 0, 255); - bitmap_set(&img, 99, 99, 0, 0, 255); - - status = bitmap_save_to_png(&img, path); - if (!status){ - printf("Successfully saved %s\n", path); - } else { - printf("Unable to save %s\n", path); - } - return status; -} +//int main() +//{ +// const char path[] = "test.png"; +// int status = 0, x, y; +// RGBBitmap img; +// +// bitmap_init(&img, 100, 100); +// for (y = 0; y < img.height; y++) { +// for (x = 0; x < img.height; x++) { +// bitmap_set(&img, x, y, 255, 255, 255); +// } +// } +// bitmap_set(&img, 50, 50, 0, 0, 255); +// bitmap_set(&img, 0, 0, 0, 0, 255); +// bitmap_set(&img, 99, 0, 0, 0, 255); +// bitmap_set(&img, 0, 99, 0, 0, 255); +// bitmap_set(&img, 99, 99, 0, 0, 255); +// +// status = bitmap_save_to_png(&img, path); +// if (!status){ +// printf("Successfully saved %s\n", path); +// } else { +// printf("Unable to save %s\n", path); +// } +// return status; +//}