wm: dino

ref: 80efdd120f2b5feed3aeb46dd212521230d5f1dc
dir: /graph.c/

View raw version
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <time.h>

#include <errno.h>
#include <fcntl.h>
#include <local/colors.h>
#include <signal.h>
#include <sys/ioctl.h>

#define DINASOUR_HEIGHT 15
#define DINASOUR_WIDTH 15
#define SUN_HEIGHT 5
#define SUN_WIDTH 5
#define DELAY 10000

void die(const char *msg) {
  perror(msg);
  exit(EXIT_FAILURE);
}

char *fbuffer = NULL; // should be point_t* buffer
uint64_t fbuffer_size = 0;
static volatile int cursor = 0;
int max_y;
int max_x;
static volatile bool resize = false;
typedef struct {
  bool *matrix;
  int height;
  int width;
  int x;
  int y;
} shape_t;
// a hash tables suits better no?
// keys will be memory address of shapes.
// and vals will be the number of times they collided
// and um, we also need to store coordinates of collisions?
// maybe we can use an observer pattern here!\
// each shape subscribes to collisions,...
typedef struct collision_t {
  shape_t *shape;
  struct collision_t *next;
} collision_t;

typedef struct {
  char *escapes;
  char charachter;
  // collision_t collisions; // it's actually better to have a global collisions
  // (hash table or linked list and update it every tflush()).
  int x;
  int y;
  bool written;
} point_t;






char *colors[] = {

black_fg,  
red_fg, 	  
green_fg , 
yellow_fg ,
blue_fg   ,
magenta_fg,
cyan_fg   ,
white_fg  ,
};






void alloc_fbuf();
void tflush();
void gotoxy(int x, int y) {
  if (x >= max_x || y >= max_y)
    return;
  int new_cursor = (max_x * y) + x;
  cursor = (max_x * y) + x;
}
void _gotoxy(int x, int y) { printf("%c[%d;%df", 0x1B, y, x); }
void max_xy(int signo) {
  (void)signo;
  struct winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

  max_x = w.ws_col;
  max_y = w.ws_row;
  resize = true;
  // alloc_fbuf();
}

void tprint(const char c) {
  if (cursor >= fbuffer_size || cursor < 0)
    return;
  fbuffer[cursor++] = c;
}

void draw_line(point_t a, point_t b) {
  int slope = (b.y - a.y) / (b.x - a.x);
  while (true) {
    a.x++;
    a.y += slope;
    if (a.x == b.x || a.y == b.y)
      break;
    // printf("newx = %d, newy = %d\n", a.x, a.y);
    for (int i = 0; i < slope; i++) {
      gotoxy(a.x, a.y + i);
      tprint('*');
    }
    // gotoxy(a.x, a.y);
    // tprint('*');
    tflush();
  }
}

void draw_shape(shape_t *shape, const char *color) {
  bool print_newline = false;
  // gotoxy(shape->x, shape->y);

  for (int i = 0; i < shape->height; i++) {
    gotoxy(shape->x, shape->y + i);
    for (int j = 0; j < shape->width; j++) {
      if (shape->matrix[i * shape->height + j]) {
        print_newline = true;
        // printf("%s%s%s", inverse_off ,  color, " " reset );
        tprint('*');
      } else {
        // printf(" ");
        tprint(' ');
      }
    }
    if (print_newline)
      printf("\r");
    // fbuffer[cursor] = '\n'
    tprint('\n');
  }
}

shape_t *init_shape(int width, int height) {
  shape_t *ret = calloc(width * height, sizeof(bool));
  return ret;
}

void free_shape(shape_t *shape) { free(shape); }

void tflush() {
  for (uint64_t row = 0; row < max_y; ++row) {
    printf("\n");
    for (uint64_t col = 0; col < max_x; ++col) {
      if (fbuffer[row * max_x + col] == '*') {
        _gotoxy(col, row);
        printf("%s%s%c%s", inverse, colors[rand() % sizeof(colors)/sizeof(*colors)],  /*fbuffer[row * max_x + col]*/ ' ',
               reset);

      } else
        printf(reset " " reset);
    }
  }
  // write(STDOUT_FILENO, fbuffer, max_x * max_y); // drawing will be very
  // smooth this way.
}

void alloc_fbuf() {
  if (max_x * max_y > fbuffer_size) {
    fbuffer_size = max_y * max_x; //* sizeof(char);
    fbuffer = realloc(fbuffer, fbuffer_size);
    assert(fbuffer);
  }
}

bool dinasour_frame1[DINASOUR_WIDTH][DINASOUR_HEIGHT] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0},
    {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
    {1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0},
    {1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0},
    {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0},
};

bool dinasour_frame2[DINASOUR_WIDTH][DINASOUR_HEIGHT] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0},
    {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
    {1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0},
    {1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0},
    {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
};

bool dinasour_frame3[DINASOUR_WIDTH][DINASOUR_HEIGHT] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0},
    {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
    {1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0},
    {1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0},
    {1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
};

void clear_screen() {
  memset(fbuffer, 0, fbuffer_size);
  printf("\033c");
}

int main(int argc, char *argv[]) {
  max_xy(0);
  alloc_fbuf();
  shape_t dinasour_f1 = {.matrix = &dinasour_frame1[0][0],
                         .width = DINASOUR_WIDTH,
                         .height = DINASOUR_HEIGHT,
                         .x = max_x / 2,
                         .y = max_y / 2};

  shape_t dinasour_f2 = {.matrix = &dinasour_frame2[0][0],
                         .height = DINASOUR_HEIGHT,
                         .width = DINASOUR_WIDTH,
                         .x = max_x/2,
                         .y = max_y/2};

  shape_t dinasour_f3 = {
      .matrix = &dinasour_frame3[0][0],
      .height = DINASOUR_HEIGHT,
      .width = DINASOUR_WIDTH,
      .x = max_x/2,
      .y = max_y/2,
  };

  shape_t dinasour2_f1 = {.matrix = &dinasour_frame1[0][0],
                          .width = DINASOUR_WIDTH,
                          .height = DINASOUR_HEIGHT,
                          .x = 10,
                          .y = 10};

  shape_t dinasour2_f2 = {.matrix = &dinasour_frame2[0][0],
                          .height = DINASOUR_HEIGHT,
                          .width = DINASOUR_WIDTH,
                          .x = 10,
                          .y = 10};

  shape_t dinasour2_f3 = {
      .matrix = &dinasour_frame3[0][0],
      .height = DINASOUR_HEIGHT,
      .width = DINASOUR_WIDTH,
      .x = 10,
      .y = 10 + DINASOUR_HEIGHT + 2,
  };

  bool sun_matrix[SUN_HEIGHT][SUN_WIDTH] = {
      {0, 0, 1, 0, 0}, {0, 1, 0, 1, 0}, {1, 0, 0, 0, 1},
      {0, 1, 0, 1, 0}, {0, 0, 1, 0, 0},
  };
  shape_t sun = {.matrix = &sun_matrix[0][0],
                 .height = SUN_HEIGHT,
                 .width = SUN_WIDTH,
                 .x = (float)max_x / 2,
                 .y = 10};

  struct sigaction sa;
  sa.sa_handler = max_xy;
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);
  sigaction(SIGWINCH, &sa, NULL);

  int randx = 0;
  int randy = 0;
  clear_screen();
  shape_t *arr[3] = {&dinasour_f1, &dinasour_f2, &dinasour_f3};
  shape_t *arr2[3] = {&dinasour2_f1, &dinasour2_f2, &dinasour2_f3};
  unsigned int index = 0;
  srand(time(NULL));
  unsigned int t = 1;

  while (true) {
	  //clear_screen();
	  /*for (int y = 0; y < max_y; ++y) {
		  for (int x = 0; x < max_x; ++x) {
			  //gotoxy(x, y);
			  //tprint('*');

		  }
	  }*/
	  tflush();
	  //usleep(DELAY);
	  if (resize) { 
		  alloc_fbuf();
		  clear_screen();
		  resize = false;
	  }
	  draw_shape(arr[index], white_fg);
	  for (int i = 0; i < 3; ++i) {
		  arr[index]->x = max_x/2;
		  arr[index]->y = max_y/2;
	  }
	  index = (index + 1) % 3;
  }
  free(fbuffer);
  return 0;
}