aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xa.outbin40608 -> 0 bytes
-rw-r--r--main.c129
2 files changed, 129 insertions, 0 deletions
diff --git a/a.out b/a.out
deleted file mode 100755
index 5fec095..0000000
--- a/a.out
+++ /dev/null
Binary files differ
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..0f311dc
--- /dev/null
+++ b/main.c
@@ -0,0 +1,129 @@
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_error.h>
+#include <SDL2/SDL_events.h>
+#include <SDL2/SDL_keycode.h>
+#include <SDL2/SDL_mouse.h>
+#include <SDL2/SDL_rect.h>
+#include <SDL2/SDL_surface.h>
+#include <SDL2/SDL_video.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define DEFAULT_WIDTH 900
+#define DEFAULT_HEIGHT 900
+#define COLOR 0x9AC2B3
+#define EMPTY_COLOR 0x000000
+
+static const int dx[], dy[] = {-1, 0, 1};
+
+typedef struct pedi {
+ int **matrix;
+ int width, height;
+} pedi;
+
+typedef struct SDL_info {
+ SDL_Window *w;
+ SDL_Surface *s;
+ SDL_Event *e;
+} SDL_info;
+
+void init_cell(pedi *p, SDL_Event e) {
+ int mouse_x, mouse_y, cell_x, cell_y = 0;
+
+ SDL_GetMouseState(&mouse_x, &mouse_y);
+ cell_x = mouse_x / 10;
+ cell_y = mouse_y / 10;
+
+ if (e.button.button == SDL_BUTTON_LEFT) {
+ printf("[MOUSE_LEFT] cell_x: %d, cell_y: %d\n", cell_x, cell_y);
+ p->matrix[cell_x][cell_y] = 1;
+ } else if (e.button.button == SDL_BUTTON_RIGHT) {
+ printf("[MOUSE_RIGHT] cell_x: %d, cell_y: %d\n", cell_x, cell_y);
+ p->matrix[cell_x][cell_y] = 0;
+ }
+}
+
+void game_update(SDL_Window *pwindow, pedi *p) {
+ if (!p) {
+ return;
+ }
+
+ SDL_UpdateWindowSurface(pwindow);
+}
+
+void draw_cells(pedi *p, SDL_Rect *cells, SDL_Surface *s) {
+ int m = 0;
+ for (int i = 0; i < p->height; i++) {
+ for (int j = 0; j < p->width; j++) {
+ cells[m].h = 10;
+ cells[m].w = 10;
+ if (p->matrix[i][j] == 1) {
+ cells[m].x = i * 10;
+ cells[m].y = j * 10;
+ SDL_FillRect(s, &cells[m], COLOR);
+ } else {
+ SDL_FillRect(s, &cells[m], EMPTY_COLOR);
+ }
+ m++;
+ }
+ }
+}
+
+pedi *create_pedi(int width, int height) {
+ pedi *p = malloc(sizeof(pedi));
+ p->width = width;
+ p->height = height;
+ p->matrix = (int **)malloc(sizeof(int *) * width);
+ for (int i = 0; i < width; i++) {
+ p->matrix[i] = (int *)calloc(height, sizeof(int));
+ }
+ return p;
+}
+
+int main(int argc, char **argv) {
+ SDL_Window *pwindow = NULL;
+ SDL_Rect *cells;
+ pedi *p;
+ if (argc != 1) {
+ pwindow = SDL_CreateWindow(
+ "Conway's Game of Life", SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED, atoi(argv[1]), atoi(argv[2]), 0);
+ int width = atoi(argv[1]);
+ int height = atoi(argv[2]);
+ p = create_pedi(width, height);
+ cells = malloc(sizeof(SDL_Rect) * (width * height));
+ } else {
+ pwindow = SDL_CreateWindow(
+ "Conway's Game of Life", SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0);
+ p = create_pedi(DEFAULT_WIDTH, DEFAULT_HEIGHT);
+ cells = malloc(sizeof(SDL_Rect) * (DEFAULT_WIDTH * DEFAULT_HEIGHT));
+ }
+
+ SDL_Surface *psurface = SDL_GetWindowSurface(pwindow);
+
+ int app_running = 1;
+ int cell_lock = 1;
+ while (app_running) {
+ SDL_Event e;
+
+ while (SDL_PollEvent(&e)) {
+ if (e.type == SDL_QUIT)
+ app_running = 0;
+ else if ((e.type == SDL_MOUSEBUTTONDOWN) && cell_lock)
+ init_cell(p, e);
+ else if (e.type == SDL_KEYDOWN && cell_lock &&
+ e.key.keysym.sym == SDLK_RETURN) {
+ cell_lock = 0;
+ }
+ }
+ draw_cells(p, cells, psurface);
+ game_update(pwindow, p);
+ }
+
+ free(p->matrix);
+ free(p);
+
+ return 0;
+}