diff --git a/src/cell.c b/src/cell.c index 0a4adcb..a0574ef 100644 --- a/src/cell.c +++ b/src/cell.c @@ -1,7 +1,7 @@ #include "cell.h" #include -Cell* CreateCell(SDL_Rect rect) { +Cell* CreateCell(int x, int y) { Cell* newCell = calloc(1, sizeof(Cell)); if (!newCell) { @@ -9,8 +9,10 @@ Cell* CreateCell(SDL_Rect rect) { return NULL; } + newCell->Visited = 0; newCell->Status = Alive; - newCell->Hitbox = rect; + newCell->x = x; + newCell->y = y; return newCell; } \ No newline at end of file diff --git a/src/cell.h b/src/cell.h index 371633b..1906bb6 100644 --- a/src/cell.h +++ b/src/cell.h @@ -19,8 +19,11 @@ typedef struct cell { struct cell* BottomRight; State Status; SDL_Rect Hitbox; + int Visited; + int x; + int y; } Cell; -Cell* CreateCell(SDL_Rect); +Cell* CreateCell(int x, int y); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6eee2cc..1d840e9 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -24,11 +25,20 @@ typedef struct { TTF_Font* font; } App; +typedef struct { + Cell** cells; + int size; + unsigned int capcity; +} CellList; + +CellList* GlobalList; + App app; void Cleanup(void); void HandleInput(void); int GetIndex(int x, int y, int columns); +int GetNotSoRandomNumber(int); int main(int argc, char** argv) { memset(&app, 0, sizeof(app)); @@ -68,12 +78,17 @@ int main(int argc, char** argv) { return -1; } + GlobalList = calloc(1, sizeof(CellList)); + GlobalList->cells = calloc(GetIndex(MAX_X, MAX_Y, MAX_X), sizeof(Cell*)); + GlobalList->capcity = GetIndex(MAX_X, MAX_Y, MAX_X); + GlobalList->size = 0; + SDL_Rect* rect = malloc(sizeof(SDL_Rect)); rect->h = CELL_SIZE; rect->w = CELL_SIZE; - rect->x = 0;//MAX_X * CELL_SIZE; - rect->y = 0;//MAX_Y * CELL_SIZE; - Cell* cell = CreateCell(*rect); + // rect->x = 0;//MAX_X * CELL_SIZE; + // rect->y = 0;//MAX_Y * CELL_SIZE; + // Cell* cell = CreateCell(*rect); while(1){ @@ -87,18 +102,29 @@ int main(int argc, char** argv) { SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255); - rect->x += CELL_SIZE; - rect->y += CELL_SIZE; + for(int i = 0; i < GlobalList->capcity; i++) { + if (GlobalList->cells[i] == NULL) continue; - if (rect->x / CELL_SIZE > MAX_X) rect->x = 0; - if (rect->y / CELL_SIZE > MAX_Y) rect->y = 0; + Cell* cell = GlobalList->cells[i]; - SDL_RenderFillRect(app.renderer, rect); + rect->x = cell->x * CELL_SIZE; + rect->y = cell->y * CELL_SIZE; + + SDL_RenderFillRect(app.renderer, rect); + } + + // rect->x += CELL_SIZE; + // rect->y += CELL_SIZE; + + // if (rect->x / CELL_SIZE > MAX_X) rect->x = 0; + // if (rect->y / CELL_SIZE > MAX_Y) rect->y = 0; + + // SDL_RenderFillRect(app.renderer, rect); SDL_RenderPresent(app.renderer); } - free(cell); + //free(cell); } void Cleanup(void) { @@ -109,6 +135,19 @@ void Cleanup(void) { SDL_Quit(); } +void Thing(int x, int y) { + x = x / CELL_SIZE; + y = y /CELL_SIZE; + + if (GlobalList->cells[GetIndex(x, y, MAX_X)] == NULL) { + GlobalList->cells[GetIndex(x, y, MAX_X)] = CreateCell(x, y); + GlobalList->size++; + } + else { + printf("Cell already at [%d, %d]\n", x, y); + } +} + void HandleInput(void) { SDL_Event event; while(SDL_PollEvent(&event)) { @@ -117,7 +156,8 @@ void HandleInput(void) { exit(0); break; case SDL_MOUSEBUTTONDOWN: - exit(0); + if (event.button.button == SDL_BUTTON_RIGHT) exit(0); + else Thing(event.button.x, event.button.y); case SDL_KEYDOWN: default: break; @@ -127,4 +167,8 @@ void HandleInput(void) { int GetIndex(int x, int y, int columns) { return x + y * columns; +} + +int GetNotSoRandomNumber(int max) { + return rand() % (max + 1); } \ No newline at end of file