cgol

a cairo-based Game Of Life
Log | Files | Refs

commit 716f8e9da4dac2284e386ecf075e18dc9746a891
parent 86d0e88d78b87a1318c7728656f9ad26c5b847a8
Author: aabacchus <ben@bvnf.space>
Date:   Mon, 27 Sep 2021 02:40:47 +0100

start project proper

Diffstat:
A.gitignore | 3+++
MMakefile | 19++++++++++++-------
Acgol.h | 15+++++++++++++++
Aimage.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amain.c | 19+++++++++++++++++++
5 files changed, 107 insertions(+), 7 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +*.o +cgol +out.png diff --git a/Makefile b/Makefile @@ -2,17 +2,22 @@ DEBUG = -g XCFLAGS = $(CFLAGS) -Wall -Wextra -Wpedantic $(DEBUG) -CAIROFLAGS = $$(pkgconf --cflags --libs cairo) +CAIROCFLAGS = $$(pkgconf --cflags cairo) +CAIROLFLAGS = -lcairo -BINS = \ - stroke +OBJS = \ + image.o \ + main.o \ -all: $(BINS) +all: cgol -$(BINS): $(BINS:=.c) - $(CC) $(XCFLAGS) $(CAIROFLAGS) -o $@ $< +%.o: %.c cgol.h + $(CC) -c $(XCFLAGS) $(CAIROCFLAGS) $< + +cgol: $(OBJS) + $(CC) $(LDFLAGS) $(CAIROLFLAGS) $(OBJS) -o cgol clean: - rm -f $(BINS) + rm -f $(OBJS) cgol .PHONY: clean diff --git a/cgol.h b/cgol.h @@ -0,0 +1,15 @@ +#include <cairo.h> +#include <stdio.h> + +struct rgb { + double r, g, b; +}; + +struct imgdata { + double width, height; + int ny, nx; /* number of columns and rows */ + struct rgb a, i, b; /* RGB for active, inactive, background pixels */ +}; + +/* cells[i][j] should match i = img->ny, j = img->nx */ +int png(struct imgdata *img, short *cells, char *fname); diff --git a/image.c b/image.c @@ -0,0 +1,58 @@ +/* + * image.c + * Copyright (c) 2021 Ben Fuller + */ +#include "cgol.h" + +/* cells[i][j]; i = img->ny; j = img->nx */ +int +png(struct imgdata *img, short *cells, char *fname) +{ + double width, height, scale; + int nx, ny; + cairo_surface_t *surface; + cairo_t *cr; + scale = 1; + + width = img->width; + height = img->height; + nx = img->nx; + ny = img->ny; + + surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + cr = cairo_create(surface); + cairo_scale(cr, scale, scale); + + /* background fill */ + cairo_set_source_rgb(cr, img->b.r, img->b.g, img->b.b); + cairo_rectangle(cr, 0, 0, width/scale, height/scale); + cairo_fill(cr); + + for (int i = 0; i < ny; i++) { + for (int j = 0; j < nx; j++) { + if (*(cells + i * nx + j)) + cairo_set_source_rgb(cr, img->a.r, img->a.g, img->a.b); + else + cairo_set_source_rgb(cr, img->i.r, img->i.g, img->i.b); + cairo_rectangle(cr, j * width/nx/scale, i * height/ny/scale, + width/nx/scale * 0.9, + height/ny/scale * 0.9); + cairo_fill(cr); + } + } + cairo_status_t crs; + crs = cairo_status(cr); + if (crs != CAIRO_STATUS_SUCCESS) { + fprintf(stderr, "cairo: %s\n", cairo_status_to_string(crs)); + cairo_destroy(cr); + cairo_surface_destroy(surface); + return 1; + } + + /* Write output and clean up */ + cairo_surface_write_to_png(surface, fname); + cairo_destroy(cr); + cairo_surface_destroy(surface); + + return 0; +} diff --git a/main.c b/main.c @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Ben Fuller + */ +#include "cgol.h" + +int +main(void) { + struct imgdata img = { + 200, 200, + 11, 10, + { 1, 1, 1, }, + { 0.1, 0.1, 0.1 }, + { 0, 0, 0 } + }; + short cells[11][10] = { 0 }; + if (png(&img, &cells[0][0], "out.png") != 0) + return 1; + return 0; +}