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:
+4
-2
@@ -1,7 +1,7 @@
|
|||||||
#include "cell.h"
|
#include "cell.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
Cell* CreateCell(SDL_Rect rect) {
|
Cell* CreateCell(int x, int y) {
|
||||||
Cell* newCell = calloc(1, sizeof(Cell));
|
Cell* newCell = calloc(1, sizeof(Cell));
|
||||||
|
|
||||||
if (!newCell) {
|
if (!newCell) {
|
||||||
@@ -9,8 +9,10 @@ Cell* CreateCell(SDL_Rect rect) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newCell->Visited = 0;
|
||||||
newCell->Status = Alive;
|
newCell->Status = Alive;
|
||||||
newCell->Hitbox = rect;
|
newCell->x = x;
|
||||||
|
newCell->y = y;
|
||||||
|
|
||||||
return newCell;
|
return newCell;
|
||||||
}
|
}
|
||||||
+4
-1
@@ -19,8 +19,11 @@ typedef struct cell {
|
|||||||
struct cell* BottomRight;
|
struct cell* BottomRight;
|
||||||
State Status;
|
State Status;
|
||||||
SDL_Rect Hitbox;
|
SDL_Rect Hitbox;
|
||||||
|
int Visited;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
} Cell;
|
} Cell;
|
||||||
|
|
||||||
Cell* CreateCell(SDL_Rect);
|
Cell* CreateCell(int x, int y);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+54
-10
@@ -2,6 +2,7 @@
|
|||||||
#include <SDL2/SDL_events.h>
|
#include <SDL2/SDL_events.h>
|
||||||
#include <SDL2/SDL_hints.h>
|
#include <SDL2/SDL_hints.h>
|
||||||
#include <SDL2/SDL_keycode.h>
|
#include <SDL2/SDL_keycode.h>
|
||||||
|
#include <SDL2/SDL_mouse.h>
|
||||||
#include <SDL2/SDL_rect.h>
|
#include <SDL2/SDL_rect.h>
|
||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <SDL2/SDL_thread.h>
|
#include <SDL2/SDL_thread.h>
|
||||||
@@ -24,11 +25,20 @@ typedef struct {
|
|||||||
TTF_Font* font;
|
TTF_Font* font;
|
||||||
} App;
|
} App;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Cell** cells;
|
||||||
|
int size;
|
||||||
|
unsigned int capcity;
|
||||||
|
} CellList;
|
||||||
|
|
||||||
|
CellList* GlobalList;
|
||||||
|
|
||||||
App app;
|
App app;
|
||||||
|
|
||||||
void Cleanup(void);
|
void Cleanup(void);
|
||||||
void HandleInput(void);
|
void HandleInput(void);
|
||||||
int GetIndex(int x, int y, int columns);
|
int GetIndex(int x, int y, int columns);
|
||||||
|
int GetNotSoRandomNumber(int);
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
memset(&app, 0, sizeof(app));
|
memset(&app, 0, sizeof(app));
|
||||||
@@ -68,12 +78,17 @@ int main(int argc, char** argv) {
|
|||||||
return -1;
|
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));
|
SDL_Rect* rect = malloc(sizeof(SDL_Rect));
|
||||||
rect->h = CELL_SIZE;
|
rect->h = CELL_SIZE;
|
||||||
rect->w = CELL_SIZE;
|
rect->w = CELL_SIZE;
|
||||||
rect->x = 0;//MAX_X * CELL_SIZE;
|
// rect->x = 0;//MAX_X * CELL_SIZE;
|
||||||
rect->y = 0;//MAX_Y * CELL_SIZE;
|
// rect->y = 0;//MAX_Y * CELL_SIZE;
|
||||||
Cell* cell = CreateCell(*rect);
|
// Cell* cell = CreateCell(*rect);
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
|
|
||||||
@@ -87,18 +102,29 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255);
|
SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255);
|
||||||
|
|
||||||
rect->x += CELL_SIZE;
|
for(int i = 0; i < GlobalList->capcity; i++) {
|
||||||
rect->y += CELL_SIZE;
|
if (GlobalList->cells[i] == NULL) continue;
|
||||||
|
|
||||||
if (rect->x / CELL_SIZE > MAX_X) rect->x = 0;
|
Cell* cell = GlobalList->cells[i];
|
||||||
if (rect->y / CELL_SIZE > MAX_Y) rect->y = 0;
|
|
||||||
|
|
||||||
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);
|
SDL_RenderPresent(app.renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(cell);
|
//free(cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cleanup(void) {
|
void Cleanup(void) {
|
||||||
@@ -109,6 +135,19 @@ void Cleanup(void) {
|
|||||||
SDL_Quit();
|
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) {
|
void HandleInput(void) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while(SDL_PollEvent(&event)) {
|
while(SDL_PollEvent(&event)) {
|
||||||
@@ -117,7 +156,8 @@ void HandleInput(void) {
|
|||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
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:
|
case SDL_KEYDOWN:
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -127,4 +167,8 @@ void HandleInput(void) {
|
|||||||
|
|
||||||
int GetIndex(int x, int y, int columns) {
|
int GetIndex(int x, int y, int columns) {
|
||||||
return x + y * columns;
|
return x + y * columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetNotSoRandomNumber(int max) {
|
||||||
|
return rand() % (max + 1);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user