~l3kn/quad

4ede712d28d8203bd9e3c1a040a631dd4e6f1769 — Leon Rische 5 months ago 8183975
Extend to 8 rules
2 files changed, 27 insertions(+), 45 deletions(-)

M README.md
M quad.c
M README.md => README.md +5 -4
@@ 6,7 6,8 @@ Needs the `sdl2-dev` package, run with `make`.

## Controls

- `q` to quit
- `r` to save as bitmap (`quad-render.bpm`)
- `o` to toggle outlines
- `c` to reset rules
- `q` quit
- `r` save as bitmap (`quad-render.bpm`)
- `o` toggle outlines
- `c` reset rules
- `i` invert colors

M quad.c => quad.c +22 -41
@@ 9,7 9,7 @@

#define PADDING 20
#define SIZE 1024
#define LIMIT 1
#define LIMIT 8
#define SCALE 2
#define MENU_SIZE 64
#define MENU_WIDTH 640


@@ 27,7 27,7 @@ SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;

Node selection;
Rule rules[4];
Rule rules[8];
int colors[2] = { COLOR_BLACK, COLOR_WHITE };

int outline = 1;


@@ 91,21 91,17 @@ void saveBMP() {

void drawNumber(int x, int y, int n) {
  int h = MENU_SIZE / 2;
  int s = h / 2;
  int o = s / 2;
  int o = MENU_SIZE / 8;
  int pattern = (n >= 4) ? WHITE : BLACK;

  if (n == 0) {
    drawSquare(x + o, y + o, s, WHITE, 1);
  if (n & 2) {
    y += h;
  }
  if (n == 1) {
    drawSquare(x + h + o, y + o, s, WHITE, 1);
  }
  if (n == 2) {
    drawSquare(x + o, y + h + o, s, WHITE, 1);
  }
  if (n == 3) {
    drawSquare(x + h + o, y + h + o, s, WHITE, 1);
  if (n & 1) {
    x += h;
  }

  drawSquare(x + o, y + o, h / 2, pattern, 1);
}

void drawNode(int x, int y, Node node) {


@@ 144,17 140,12 @@ void drawMenu() {
    }
  }

  for (int i = 0; i < 4; i++) {
    int x = i * MENU_SIZE + MENU_X;
    int y = 3 * MENU_SIZE + MENU_Y;
    drawSquare(x, y, MENU_SIZE, 0, 1);
    drawNumber(x, y, i);
  for (i = 0; i < 8; i++) {
    drawRule(MENU_X + (i % 2) * 6 * MENU_SIZE,
             (5 + 3 * (i / 2)) * MENU_SIZE + MENU_Y, i);
  }
}

  drawRule(MENU_X, 5 * MENU_SIZE + MENU_Y, 0);
  drawRule(MENU_X, 8 * MENU_SIZE + MENU_Y, 1);
  drawRule(MENU_X, 11 * MENU_SIZE + MENU_Y, 2);
  drawRule(MENU_X, 14 * MENU_SIZE + MENU_Y, 3);
void setColor(int color) {
  int r = color >> 16;
  int g = (color >> 8) & 255;


@@ 219,13 210,17 @@ void handleMouse(SDL_Event* event) {

  switch (event->type) {
  case SDL_MOUSEBUTTONUP:
    if (tx >= 0 && tx <= 4 &&
        ty >= 5 && ty <= 15 &&
    if (tx >= 0 && tx < 10 &&
        ty >= 5 && ty < 16 &&
        selection.type != NONE) {

      ty -= 5;
      int rule = ty / 3;

      int rule = 2 * (ty / 3);
      ty %= 3;
      if (tx >= 6) {
        tx -= 6;
        rule += 1;
      }

      if (tx == 0 && ty == 1 && selection.type == PATTERN) {
        rules[rule].fallback = selection;


@@ 264,19 259,6 @@ void handleMouse(SDL_Event* event) {
      selection.type = NONE;
    }
    break;
  case SDL_MOUSEMOTION:
    break;
  }
}

void reset() {
  struct node pat0 = { PATTERN, 0 };
  for (int i = 0; i < 4; i++) {
    rules[i].tl = pat0;
    rules[i].tr = pat0;
    rules[i].bl = pat0;
    rules[i].br = pat0;
    rules[i].fallback = pat0;
  }
}



@@ 308,7 290,6 @@ int main(int argc, char* argv[]) {
        break;
      case SDL_MOUSEBUTTONDOWN:
      case SDL_MOUSEBUTTONUP:
      case SDL_MOUSEMOTION:
        handleMouse(&event);
        break;
      case SDL_WINDOWEVENT: