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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#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 <string.h>
#include <time.h>
#include <unistd.h>
#include <wctype.h>
#define DEFAULT_WIDTH 900
#define DEFAULT_HEIGHT 900
#define COLOR 0x9AC2B3
#define EMPTY_COLOR 0x000000
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 life(pedi *p) {
int **rd_only_matrix = (int **)malloc(sizeof(int *) * p->width);
for (int i = 0; i < p->width; i++) {
rd_only_matrix[i] = (int *)malloc(sizeof(int) * p->height);
memcpy(rd_only_matrix[i], p->matrix[i], sizeof(int) * p->height);
}
int dirs[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
{0, 1}, {1, -1}, {1, 0}, {1, 1}};
for (int i = 0; i < p->width; i++) {
for (int j = 0; j < p->height; j++) {
int live_nei = 0;
for (int d = 0; d < 8; d++) {
int nx = i + dirs[d][0];
int ny = j + dirs[d][1];
if (nx >= 0 && nx < p->width && ny >= 0 && ny < p->height) {
if (rd_only_matrix[nx][ny] == 1) {
live_nei++;
printf("(%d, %d)\n ", nx, ny);
}
}
}
int cur_is_alive = rd_only_matrix[i][j];
if (cur_is_alive == 1) {
if (live_nei < 2 || live_nei > 3) {
p->matrix[i][j] = 0;
}
} else {
if (live_nei == 3) {
p->matrix[i][j] = 1;
}
}
}
}
}
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) {
SDL_UpdateWindowSurface(pwindow);
}
void draw_cells(pedi *p, SDL_Rect *cells, SDL_Surface *s) {
int m = 0;
for (int i = 0; i < p->width; i++) {
for (int j = 0; j < p->height; j++) {
cells[m].h = 10;
cells[m].w = 10;
cells[m].x = i * 10;
cells[m].y = j * 10;
if (p->matrix[i][j] == 1) {
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;
}
}
if (!cell_lock) {
life(p);
}
draw_cells(p, cells, psurface);
if (!cell_lock) {
struct timespec req = {0, 300000000}; // 0秒 + 500,000,000纳秒
nanosleep(&req, NULL);
}
game_update(pwindow, p);
}
free(p->matrix);
free(p);
return 0;
}
|