~l3kn/quad

7deec99fcc42a0d37ca709f806f2186acce74353 — Leon Rische 5 months ago ece2998
Scale interface based on display size
1 files changed, 33 insertions(+), 28 deletions(-)

M quad.c
M quad.c => quad.c +33 -28
@@ 7,17 7,11 @@
#include "quad.h"
#include "patterns.h"

int width, height, size, menu_size, menu_width;
#define PADDING 20
#define SIZE 1024
#define SCALE 2
#define MENU_SIZE 64
#define MENU_WIDTH 640
#define WIDTH MENU_WIDTH + MENU_SIZE + SIZE + 2 * PADDING
#define HEIGHT SIZE + 2 * PADDING
#define CANVAS_X PADDING + MENU_WIDTH + MENU_SIZE
#define CANVAS_X PADDING + menu_width + menu_size
#define CANVAS_Y PADDING
#define MENU_X PADDING
#define MENU_Y PADDING

#define COLOR_BLACK 0x000000
#define COLOR_WHITE 0xFFFFFF


@@ 90,8 84,8 @@ void saveBMP() {
}

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

  if (n & 2) {


@@ 107,11 101,11 @@ void drawNumber(int x, int y, int n) {
void drawNode(int x, int y, Node node) {
  switch (node.type) {
  case RULE:
    drawSquare(x, y, MENU_SIZE, BLACK, 1);
    drawSquare(x, y, menu_size, BLACK, 1);
    drawNumber(x, y, node.index);
    break;
  case PATTERN:
    drawSquare(x, y, MENU_SIZE, patterns[node.index], 1);
    drawSquare(x, y, menu_size, patterns[node.index], 1);
    break;
  default:
    break;


@@ 122,27 116,27 @@ void drawRule(int x, int y, int n) {
  drawNumber(x, y, n);
  Rule rule = rules[n];

  drawNode(x, y + MENU_SIZE, rule.fallback);
  drawNode(x, y + menu_size, rule.fallback);

  drawNode(x + 2 * MENU_SIZE, y, rule.quads[0]);
  drawNode(x + 3 * MENU_SIZE, y, rule.quads[1]);
  drawNode(x + 2 * MENU_SIZE, y + MENU_SIZE, rule.quads[2]);
  drawNode(x + 3 * MENU_SIZE, y + MENU_SIZE, rule.quads[3]);
  drawNode(x + 2 * menu_size, y, rule.quads[0]);
  drawNode(x + 3 * menu_size, y, rule.quads[1]);
  drawNode(x + 2 * menu_size, y + menu_size, rule.quads[2]);
  drawNode(x + 3 * menu_size, y + menu_size, rule.quads[3]);
}

void drawMenu() {
  int i, j, x, y;
  for (i = 0; i < 10; i++) {
    x = i * MENU_SIZE + MENU_X;
    x = i * menu_size + PADDING;
    for (j = 0; j < 4; j ++) {
      y = j * MENU_SIZE + MENU_Y;
      drawSquare(x, y, MENU_SIZE, patterns[i + 10 * j], 1);
      y = j * menu_size + PADDING;
      drawSquare(x, y, menu_size, patterns[i + 10 * j], 1);
    }
  }

  for (i = 0; i < 8; i++) {
    drawRule(MENU_X + (i % 2) * 6 * MENU_SIZE,
             (5 + 3 * (i / 2)) * MENU_SIZE + MENU_Y, i);
    drawRule(PADDING + (i % 2) * 6 * menu_size,
             (5 + 3 * (i / 2)) * menu_size + PADDING, i);
  }
}



@@ 159,8 153,8 @@ void draw() {
  setColor(colors[1]);

  drawMenu();
  drawSquare(CANVAS_X, CANVAS_Y, SIZE, BLACK, 1);
  expandRule(0, 0, SIZE, &rules[0]);
  drawSquare(CANVAS_X, CANVAS_Y, size, BLACK, 1);
  expandRule(0, 0, size, &rules[0]);

  SDL_RenderPresent(renderer);
}


@@ 200,7 194,7 @@ void handleKey(SDL_Event* event) {
    break;
  }
  case SDLK_1: {
    if (limit < SIZE / 2) {
    if (limit < size / 2) {
      limit <<= 1;
      draw();
    }


@@ 219,8 213,8 @@ void handleKey(SDL_Event* event) {
void handleMouse(SDL_Event* event) {
  int x_ = event->motion.x;
  int y_ = event->motion.y;
  int tx = (x_ - MENU_X) / MENU_SIZE;
  int ty = (y_ - MENU_Y) / MENU_SIZE;
  int tx = (x_ - PADDING) / menu_size;
  int ty = (y_ - PADDING) / menu_size;

  switch (event->type) {
  case SDL_MOUSEBUTTONUP:


@@ 300,7 294,18 @@ int main(int argc, char* argv[]) {
    return 1;
  }

  if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) != 0) {
  SDL_DisplayMode dm;
  SDL_GetCurrentDisplayMode(0, &dm);
  int display_width = dm.w;
  int display_height = dm.h;

  size = 1 << (int)(log2(display_height - 2 * PADDING));
  menu_size = size / 16;
  menu_width = menu_size * 10;
  width = menu_width + menu_size + size + 2 * PADDING;
  height = size + 2 * PADDING;

  if (SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer) != 0) {
    printf("Error creating window & renderer: %s\n", SDL_GetError());
    return 1;
  }