#include #include #include #include #include #include #include #include #include #include #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 //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){ (*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; iobj.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; iobj.cells[i]=-1; } for (int i=0; iobj.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; kobj.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; iobj.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