aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 0f311dc5021b125da7eef79067ab15e0e0925fa2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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;
}