~l3kn/quad

0df581a812911417ede2c3ebcb4712d765546daf — Leon Rische 6 months ago 652e49a
Rewrite main drawing code
1 files changed, 30 insertions(+), 59 deletions(-)

M quad.c
M quad.c => quad.c +30 -59
@@ 12,11 12,8 @@
#include "menu.h"

SDL_Window* window = NULL;
SDL_Renderer* mainRenderer = NULL;
SDL_Renderer* windowRenderer = NULL;

SDL_Surface* grammarSurface = NULL;
SDL_Renderer* grammarRenderer = NULL;
SDL_Texture* grammarTexture = NULL;
int currentRule = 0;
int currentQuad = 0;



@@ 50,31 47,24 @@ void saveBMP() {
  SDL_FreeSurface(surface);
}

void renderRule(SDL_Renderer* renderer) {
  setColor(renderer, colors[0]);
  SDL_RenderClear(renderer);
  setColor(renderer, colors[1]);
  expandRule(renderer, 0, 0, SIZE, currentRule, rules);
  grammarTexture = SDL_CreateTextureFromSurface(mainRenderer, grammarSurface);
}

void draw() {
  setColor(mainRenderer, colors[0]);
  SDL_RenderClear(mainRenderer);
  setColor(mainRenderer, colors[1]);
void draw(bool expand) {
  SDL_Rect rect = makeRect(0, 0, CANVAS_X, HEIGHT);
  setColor(windowRenderer, colors[0]);
  SDL_RenderFillRect(windowRenderer, &rect);
  setColor(windowRenderer, colors[1]);

  drawSquare(mainRenderer, CANVAS_X-1, CANVAS_Y-1, SIZE+2, BLACK, true);
  drawMenu(mainRenderer, PADDING, PADDING, &patterns, &rules);

  SDL_Rect src = makeRect(0, 0, SIZE, SIZE);
  SDL_Rect target = makeRect(CANVAS_X, CANVAS_Y, SIZE, SIZE);
  if (expand) {
    drawSquare(windowRenderer, CANVAS_X-1, CANVAS_Y-1, SIZE+2, BLACK, true);
    expandRule(windowRenderer, CANVAS_X, CANVAS_Y, SIZE, currentRule, rules);
  }

  SDL_RenderCopy(mainRenderer, grammarTexture, &src, &target);
  drawMenu(windowRenderer, MENU_X, MENU_Y, patterns, rules);

  if (selector) {
    drawSelector(mainRenderer, selectorX, selectorY, &patterns);
    drawSelector(windowRenderer, selectorX, selectorY, patterns);
  }
  SDL_RenderPresent(mainRenderer);

  SDL_RenderPresent(windowRenderer);
}

void reset() {


@@ 85,46 75,45 @@ void reset() {
    }
    rules[i].fallback = pat0;
  }
  renderRule(mainRenderer);
  draw();
  draw(true);
}

void invertColors() {

  int tmp = colors[0];
  colors[0] = colors[1];
  colors[1] = tmp;
  renderRule(grammarRenderer);
  draw();

  setColor(windowRenderer, colors[0]);
  SDL_RenderClear(windowRenderer);
  setColor(windowRenderer, colors[1]);

  draw(true);
}

void toggleOutline() {
  outline = !outline;
  renderRule(grammarRenderer);
  draw();
  draw(true);
}

void decreaseLimit() {
  if (limit < SIZE / 2) {
    limit <<= 1;
    renderRule(grammarRenderer);
    draw();
    draw(true);
  }
}

void increaseLimit() {
  if (limit > 1) {
    limit >>= 1;
    renderRule(grammarRenderer);
    draw();
    draw(true);
  }
}

void selectRule(int n) {
  if (n >= 0 && n < NRULES) {
    currentRule = n;
    renderRule(grammarRenderer);
    draw();
    draw(true);
  }
}



@@ 243,30 232,14 @@ int main(int argc, char* argv[]) {
    return 1;
  }

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

  grammarSurface = SDL_CreateRGBSurface(0, SIZE, SIZE, 32, 0, 0, 0, 0);
  /* grammarSurface = SDL_GetWindowSurface(window); */
  if (grammarSurface == NULL) {
    SDL_Log("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
    exit(1);
  }

  grammarRenderer = SDL_CreateSoftwareRenderer(grammarSurface);

  if (!grammarRenderer) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n", SDL_GetError());
    return 1;
  }

  selection.type = PATTERN;
  selection.index = 11; // White pattern

  reset();
  draw();
  draw(true);

  while (!quit) {
    SDL_Event event;


@@ 284,7 257,7 @@ int main(int argc, char* argv[]) {
        break;
      case SDL_WINDOWEVENT:
        if (event.window.event == SDL_WINDOWEVENT_EXPOSED) {
          draw();
          draw(true);
        }
      }
    }


@@ 293,10 266,8 @@ int main(int argc, char* argv[]) {
    SDL_Delay(1000 / 60);
  }

  SDL_DestroyRenderer(mainRenderer);
  SDL_DestroyRenderer(grammarRenderer);

  SDL_FreeSurface(grammarSurface);
  SDL_DestroyRenderer(windowRenderer);
  SDL_DestroyWindow(window);
  SDL_Quit();