The user can now click to create a cell anywhere. Terrible main loop for drawing the cells but good enough for quick and dirty testing.

This commit is contained in:
2022-06-06 22:57:22 -05:00
parent 813b0407a4
commit 03883a04de
3 changed files with 62 additions and 13 deletions
+4 -2
View File
@@ -1,7 +1,7 @@
#include "cell.h"
#include <stdlib.h>
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;
}
+4 -1
View File
@@ -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
+53 -9
View File
@@ -2,6 +2,7 @@
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_hints.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_thread.h>
@@ -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];
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;
@@ -128,3 +168,7 @@ void HandleInput(void) {
int GetIndex(int x, int y, int columns) {
return x + y * columns;
}
int GetNotSoRandomNumber(int max) {
return rand() % (max + 1);
}