-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl.c
180 lines (167 loc) · 5.94 KB
/
sdl.c
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
175
176
177
178
179
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#define EVER ;;
#define WIDTH 1669
#define HEIGHT 1045
#define SQSIZE 5
#define COLCELLS 278
#define ROWCELLS 174
struct cell{
int alive;
SDL_Rect rect;
};
int main (int argc, char *argv[]){
if(SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Failed to init SDL: %s \n", SDL_GetError());
return 1;
}
if(strcmp(argv[1], "-h") == 0){
fprintf(stderr, "To start random world generation give 2 arguments.\nFirst is refresh rate in ms.\nSecond is %% change to fill cell.\nExample: ./conway 100 30\nStart with 1(refresh rate ms) or without argument(s) and click with mouse to spawn alive cells\nThen enter to start");
return 1;
}
SDL_Window *window = SDL_CreateWindow("conway", 0, 0, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL ){
printf("Window could not be created!: %s \n", SDL_GetError());
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
if(renderer == NULL ){
printf("Renderer could not be created!: %s \n", SDL_GetError());
return 1;
}
int sleepms = 100;
int pct;
if(argc > 2){
sleepms = atoi(argv[1]);
pct = atoi(argv[2]);
}else if (argc == 1){
sleepms = atoi(argv[1]);
}
srand(time(0));
Uint32 fps_frames = 0;
SDL_Event event;
struct cell cells[ROWCELLS][COLCELLS];
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
int cellsAliveTmp[ROWCELLS][COLCELLS];
float frameTime = 0;
int prevTime = 0;
int currentTime = 0;
float deltaTime = 0;
for(int i = 0, y = 1; i < ROWCELLS; i++, y+=6){
for(int j = 0, x = 1; j < COLCELLS; j++, x+=6){
cells[i][j].rect.x = x;
cells[i][j].rect.y = y;
cells[i][j].rect.w = cells[i][j].rect.h = SQSIZE;
if(argc == 3){
int n = rand() % 100;
if(n < pct) {
cells[i][j].alive = 1;
}
}
}
}
while(argc < 3){
if(SDL_WaitEvent(&event)){
if(event.type == SDL_QUIT){
break;
}
else if (event.type == SDL_MOUSEBUTTONDOWN){
cells[event.button.y/6-1][event.button.x/6-1].alive = cells[event.button.y/6-1][event.button.x/6-1].alive ? 0 : 1;
if(cells[event.button.y/6-1][event.button.x/6-1].alive == 0){
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderFillRect(renderer, &cells[event.button.y/6-1][event.button.x/6-1].rect);
} else {
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_RenderFillRect(renderer, &cells[event.button.y/6-1][event.button.x/6-1].rect);
}
}
SDL_RenderPresent(renderer);
}
}
while(1){
prevTime = currentTime;
currentTime = SDL_GetTicks();
deltaTime = (currentTime - prevTime);
if(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
break;
}
else if (event.type == SDL_MOUSEBUTTONDOWN){
cells[event.button.y/6-1][event.button.x/6-1].alive = cells[event.button.y/6-1][event.button.x/6-1].alive ? 0 : 1;
}
}
frameTime += deltaTime;
if ( frameTime >= sleepms){
frameTime = 0;
for(int i = 0; i < ROWCELLS; i++){
for(int j = 0; j < COLCELLS; j++){
if(cells[i][j].alive == 0){
SDL_SetRenderDrawColor(renderer, 0,0,0,0);
SDL_RenderFillRect(renderer, &cells[i][j].rect);
} else {
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_RenderFillRect(renderer, &cells[i][j].rect);
}
}
}
// change cellsAliveTmp state
for (int i = 0; i < ROWCELLS; ++i) {
for (int j = 0; j < COLCELLS; ++j) {
// get number of neighbors around
int nb = 0;
for (int y = -1; y < 2 ; ++y) {
for (int x = -1; x < 2 ; ++x) {
if(y == 0 && x == 0) {// skip self
continue;
}
if(i+y < 0 || j+x < 0 || i+y >= ROWCELLS || j+x >= COLCELLS)
continue;
if(cells[i+y][j+x].alive == 1){
nb++;
}
}
}
// conway's rules:
if(cells[i][j].alive == 1) {
if(nb < 2 || nb > 3) {
cellsAliveTmp[i][j] = 0;
} else {
cellsAliveTmp[i][j] = 1;
}
}else if (nb == 3) {
cellsAliveTmp[i][j] = 1;
}
}
}
// change world state
for (int i = 0; i < ROWCELLS; ++i) {
for (int j = 0; j < COLCELLS; ++j) {
cells[i][j].alive = cellsAliveTmp[i][j];
}
}
fps_frames++;
}
SDL_RenderPresent(renderer);
//if(currentTime > 30000){
// break;
// }
}
SDL_DestroyRenderer(renderer);
SDL_Quit();
printf("%d", fps_frames);
return 0;
}