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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
#include <algolib.c>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <vectorlib.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_video.h>
#define PROJECT_NAME "circuit"
#define grid_cols 50
#define grid_rows grid_cols
#define obj_n grid_cols * grid_rows
#define frame_delay 0
// CONFIG
#define random_chance 0 //chance of picking a new random spot
#define random_direction 30 //chance of turning a line
//not finished
#define DIRS 4 //set to 8 for 8 directions. set to 4 for 4 directions
// END CONFIG
typedef struct CELL{
int i;
int j;
struct CELL* next;
struct CELL* prev;
bool circle;
bool visited;
} CELL;
typedef struct OBJECT {
CELL items[obj_n];
CELL* current;
int cells[obj_n];
int cells_len;
int w;
int h;
} OBJECT;
#include <init.h>
//TODO: make it wrap?
int get_index(int i, int j){
if (i < 0 || j < 0 || i > grid_cols-1 || j > grid_rows-1)
return -1;
return (j * grid_cols + i);
}
void cell_constructor(CELL* cell,int i,int j){
cell->i = i;
cell->j = j;
cell->visited=false;
cell->next = NULL;
cell->prev = NULL;
cell->circle = false;
}
void push_cell(CELL* arr[], CELL* cell, int count){
for (int i=0; i<count; i++){
if(!arr[i]){
arr[i]=cell;
return;
}
}
}
CELL* pop_cell(CELL* arr[], int count){
CELL* ret_cell;
for (int i=0; i<count; i++){
if(!arr[i]){
ret_cell = arr[i-1];
arr[i-1]=NULL;
return ret_cell;
} else if (i == count){
ret_cell = arr[i];
arr[i]=NULL;
return ret_cell;
}
}
return NULL;
}
void slice_match(int(* cells)[],int num,int count){
bool found=false;
for (int i=0; i<count; i++){
if((*cells)[i] == num)
found = true;
if(found==true){
if(i+1>=count){
(*cells)[i] = -1;
return;
}
if((*cells)[i+1] == -1){
(*cells)[i] = -1;
return;
}
(*cells)[i] = (*cells)[i+1];
}
}
(*cells)[count] = -1;
return;
}
void push_int(int(*cells)[],int ins, int count){
for (int i=0; i<count; i++){
if((*cells)[i] == -1){
(*cells)[i]=ins;
return;
}
}
}
void init_stuffs(int w, int h, STUFFS* stuff)
{
if (DIRS != 8 && DIRS != 4){
//TODO: you still need to free sdl
//required editing init.c and updating ever other file.
fprintf(stderr,"DIRS must be 4 or 8");
exit(-1);
}
stuff->obj.w = stuff->width / grid_cols;
stuff->obj.h = stuff->height / grid_rows;
for (int j = 0; j < grid_rows; j++) {
for (int i = 0; i < grid_cols; i++) {
int index = j * grid_cols + i;
cell_constructor(&stuff->obj.items[index],i,j);
}
}
stuff->obj.cells_len=obj_n;
//TODO: optomize this. tiz slow at big grids
int ints_len=obj_n;
int ints[obj_n];
for(int i=0; i<obj_n; i++){
ints[i]=i;
stuff->obj.cells[i]=-1;
}
for (int i=0; i<obj_n; i++){
int selected = (int)RandomFloat(0,ints_len);
push_int(&(stuff->obj.cells),ints[selected],obj_n);
slice_match(&ints,ints[selected],obj_n);
ints_len--;
}
stuff->obj.current = &stuff->obj.items[(int)RandomFloat(0,obj_n)];
stuff->obj.current->circle=true;
}
void pick_random(STUFFS* stuff){
stuff->obj.current->circle=true;
int selected=(int)RandomFloat(0,stuff->obj.cells_len);
stuff->obj.current=&stuff->obj.items[stuff->obj.cells[selected]];
stuff->obj.current->circle=true;
slice_match(&stuff->obj.cells,stuff->obj.cells[selected],obj_n);
stuff->obj.cells_len--;
}
void random_dir(STUFFS* stuff, int* index){
int not_checked[DIRS]={0,1,2,3,4,5,6,7};
int randdir=-1;
for (int k=0; k<DIRS; k++){
randdir = (int)RandomFloat(0,DIRS-1-k);
slice_match(¬_checked,not_checked[randdir],DIRS);
switch(randdir){
case 0:
*index = get_index(stuff->obj.current->i,stuff->obj.current->j-1);
break;
case 1:
*index = get_index(stuff->obj.current->i+1,stuff->obj.current->j);
break;
case 2:
*index = get_index(stuff->obj.current->i,stuff->obj.current->j+1);
break;
case 3:
*index = get_index(stuff->obj.current->i-1,stuff->obj.current->j);
break;
case 4:
*index = get_index(stuff->obj.current->i-1,stuff->obj.current->j+1);
break;
case 5:
*index = get_index(stuff->obj.current->i+1,stuff->obj.current->j+1);
break;
case 6:
*index = get_index(stuff->obj.current->i+1,stuff->obj.current->j-1);
break;
case 7:
*index = get_index(stuff->obj.current->i-1,stuff->obj.current->j-1);
break;
}
if(*index==-1) continue;
if (stuff->obj.items[*index].visited==false){ break; }
}
}
void step_next(STUFFS* stuff){
int index = -1;
int doranddir=(int)RandomFloat(0,100);
if(doranddir > random_direction && stuff->obj.current->prev){
int tmp_index=0;
int x = stuff->obj.current->i - stuff->obj.current->prev->i + stuff->obj.current->i;
int y = stuff->obj.current->j - stuff->obj.current->prev->j + stuff->obj.current->j;
tmp_index = get_index(x,y);
if(tmp_index == -1 || stuff->obj.items[tmp_index].visited==true){
random_dir(stuff,&index);
} else {
index=tmp_index;
}
} else {
random_dir(stuff,&index);
}
if(index != -1 && stuff->obj.items[index].visited==false){
stuff->obj.items[index].prev=stuff->obj.current;
stuff->obj.current->next=(struct CELL*)&stuff->obj.items[index];
stuff->obj.current=&stuff->obj.items[index];
slice_match(&stuff->obj.cells,index,obj_n);
stuff->obj.cells_len--;
} else {
pick_random(stuff);
}
}
void draw_cell(CELL* cell, STUFFS* stuff)
{
int w = stuff->obj.w;
int x = cell->i * w;
int y = cell->j * w;
SDL_SetRenderDrawColor(stuff->renderer, 255, 255, 255, 255);
if (cell->next){
int x2 = cell->next->i * w;
int y2 = cell->next->j * w;
thickLineRGBA(stuff->renderer,x + w/2,y + w/2,x2 + w/2,y2 + w/2, w/3,255,255,255,255);
if(cell->next->circle==true){
filledCircleRGBA(stuff->renderer,x2 + w/2, y2 + w/2, w/2.5, 255, 255, 255, 255);
filledCircleRGBA(stuff->renderer,x2 + w/2, y2 + w/2, w/3.5, 20, 20, 20, 255);
}
}
if(cell->circle==true){
filledCircleRGBA(stuff->renderer,x + w/2, y + w/2, w/2.5, 255, 255, 255, 255);
filledCircleRGBA(stuff->renderer,x + w/2, y + w/2, w/3.5, 20, 20, 20, 255);
}
}
void draw_highlight(CELL* cell, STUFFS* stuff){
int w = stuff->obj.w;
int x = cell->i * w;
int y = cell->j * w;
SDL_SetRenderDrawColor(stuff->renderer, 255, 0, 0, 200);
SDL_RenderFillRect(stuff->renderer, &(SDL_Rect) { x, y, w, w });
}
void draw(SDL_Window* window, STUFFS* stuff)
{
static bool done = false;
if (done == false) {
SDL_SetRenderDrawColor(stuff->renderer, 20, 20, 20, 255);
// SDL_RenderClear(renderer);
SDL_RenderFillRect(stuff->renderer, &(SDL_Rect) { 0, 0, stuff->width, stuff->height });
for (int i=0; i<obj_n; i++){
draw_cell(&stuff->obj.items[i],stuff);
}
stuff->obj.current->visited=true;
if(stuff->obj.cells_len > 0){
float step = RandomFloat(0,100);
if (step > random_chance){
step_next(stuff);
} else {
pick_random(stuff);
}
} else {
done=true;
}
if(done==false)
draw_highlight(stuff->obj.current,stuff);
SDL_RenderPresent(stuff->renderer);
}
}
void on_end(STUFFS* stuff)
{
}
#include <init.c>
|