This code is hot garabage, but just testing things. Trying a really terrible way to do 'Updates' on the cells' statuses. Not really working but its a start of the checking the rules for the game.
This commit is contained in:
+6
-2
@@ -4,13 +4,17 @@
|
||||
Cell* CreateCell(int x, int y) {
|
||||
Cell* newCell = calloc(1, sizeof(Cell));
|
||||
|
||||
if (!newCell) {
|
||||
printf("Faield to allocate cell\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!newCell) {
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newCell->Visited = 0;
|
||||
newCell->Status = Alive;
|
||||
newCell->Status = Dead;
|
||||
newCell->x = x;
|
||||
newCell->y = y;
|
||||
|
||||
|
||||
+17
-12
@@ -8,22 +8,27 @@ typedef enum {
|
||||
Dead
|
||||
} State;
|
||||
|
||||
typedef struct cell {
|
||||
struct cell* TopLeft;
|
||||
struct cell* Top;
|
||||
struct cell* TopRight;
|
||||
struct cell* Left;
|
||||
struct cell* Right;
|
||||
struct cell* BottomLeft;
|
||||
struct cell* Botttom;
|
||||
struct cell* BottomRight;
|
||||
State Status;
|
||||
SDL_Rect Hitbox;
|
||||
int Visited;
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
} Point;
|
||||
|
||||
typedef struct cell {
|
||||
// Point TopLeft;
|
||||
// Point Top;
|
||||
// Point TopRight;
|
||||
// Point Left;
|
||||
// Point Right;
|
||||
// Point BottomLeft;
|
||||
// Point Botttom;
|
||||
// Point BottomRight;
|
||||
int living;
|
||||
State Status;
|
||||
int y;
|
||||
int x;
|
||||
} Cell;
|
||||
|
||||
Cell* CreateCell(int x, int y);
|
||||
void UpdateCell(Cell*);
|
||||
|
||||
#endif
|
||||
+89
-12
@@ -10,6 +10,7 @@
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "cell.h"
|
||||
|
||||
@@ -18,6 +19,7 @@ const int SCREEN_HEIGHT = 720;
|
||||
const int CELL_SIZE = 15;
|
||||
const int MAX_X = SCREEN_WIDTH / CELL_SIZE - 1;
|
||||
const int MAX_Y = SCREEN_HEIGHT / CELL_SIZE - 1;
|
||||
int Start = 0;
|
||||
|
||||
typedef struct {
|
||||
SDL_Window *window;
|
||||
@@ -79,9 +81,16 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
GlobalList = calloc(1, sizeof(CellList));
|
||||
GlobalList->cells = calloc(GetIndex(MAX_X, MAX_Y, MAX_X), sizeof(Cell*));
|
||||
GlobalList->cells = calloc(GetIndex(MAX_X, MAX_Y, MAX_X) + 1, sizeof(Cell*));
|
||||
GlobalList->capcity = GetIndex(MAX_X, MAX_Y, MAX_X);
|
||||
GlobalList->size = 0;
|
||||
|
||||
for(int x = 0; x < MAX_X; x++) {
|
||||
for(int y = 0; y < MAX_Y; y++) {
|
||||
GlobalList->cells[GetIndex(x, y, MAX_X)] = CreateCell(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
GlobalList->cells[GetIndex(MAX_X + 1, MAX_Y, MAX_X)] = NULL;
|
||||
|
||||
SDL_Rect* rect = malloc(sizeof(SDL_Rect));
|
||||
rect->h = CELL_SIZE;
|
||||
@@ -90,8 +99,7 @@ int main(int argc, char** argv) {
|
||||
// rect->y = 0;//MAX_Y * CELL_SIZE;
|
||||
// Cell* cell = CreateCell(*rect);
|
||||
|
||||
while(1){
|
||||
|
||||
while(!Start){
|
||||
HandleInput();
|
||||
|
||||
SDL_Delay(128);
|
||||
@@ -110,7 +118,68 @@ int main(int argc, char** argv) {
|
||||
rect->x = cell->x * CELL_SIZE;
|
||||
rect->y = cell->y * CELL_SIZE;
|
||||
|
||||
SDL_RenderFillRect(app.renderer, rect);
|
||||
if (cell->Status == Alive) SDL_RenderFillRect(app.renderer, rect);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(app.renderer);
|
||||
}
|
||||
|
||||
while(1){
|
||||
|
||||
HandleInput();
|
||||
|
||||
SDL_Delay(128);
|
||||
|
||||
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 255);
|
||||
|
||||
SDL_RenderClear(app.renderer);
|
||||
|
||||
SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255);
|
||||
|
||||
for(int i = 0; i < GlobalList->capcity; i++) {
|
||||
// if (GlobalList->cells[i] == NULL) {
|
||||
// Cell* newCell = CreateCell(int x, int y)
|
||||
// }
|
||||
|
||||
Cell* cell = GlobalList->cells[i];
|
||||
|
||||
if (!cell) continue;
|
||||
|
||||
int count = 0;
|
||||
|
||||
Cell* topLeft = GlobalList->cells[GetIndex(cell->x - 1, cell->y - 1, MAX_X)];
|
||||
if (topLeft && topLeft->Status == Alive) count++;
|
||||
// Point Top;
|
||||
Cell* top = GlobalList->cells[GetIndex(cell->x, cell->y - 1, MAX_X)];
|
||||
if (top && top->Status == Alive) count++;
|
||||
// Point TopRight;
|
||||
Cell* topRight = GlobalList->cells[GetIndex(cell->x + 1, cell->y - 1, MAX_X)];
|
||||
if (topRight && topRight->Status == Alive) count++;
|
||||
// Point Left;
|
||||
Cell* left = GlobalList->cells[GetIndex(cell->x - 1, cell->y, MAX_X)];
|
||||
if (left && left->Status == Alive) count++;
|
||||
// Point Right;
|
||||
Cell* right = GlobalList->cells[GetIndex(cell->x + 1, cell->y, MAX_X)];
|
||||
if (right && right->Status == Alive) count++;
|
||||
// Point BottomLeft;
|
||||
Cell* bottomLeft = GlobalList->cells[GetIndex(cell->x + 1, cell->y - 1, MAX_X)];
|
||||
if (bottomLeft && bottomLeft->Status == Alive) count++;
|
||||
// Point Botttom;
|
||||
Cell* bottom = GlobalList->cells[GetIndex(cell->x, cell->y + 1, MAX_X)];
|
||||
if (bottom && bottom->Status == Alive) count++;
|
||||
// Point BottomRight;
|
||||
Cell* bottomRight = GlobalList->cells[GetIndex(cell->x - 1, cell->y - 1, MAX_X)];
|
||||
if (bottomRight && bottomRight->Status == Alive) count++;
|
||||
|
||||
rect->x = cell->x * CELL_SIZE;
|
||||
rect->y = cell->y * CELL_SIZE;
|
||||
|
||||
if (cell->Status == Alive) {
|
||||
if (count < 2 || count > 3) cell->Status = Dead;
|
||||
}
|
||||
else if (cell->Status == Dead && count == 3) cell->Status = Alive;
|
||||
|
||||
if (cell->Status == Alive) SDL_RenderFillRect(app.renderer, rect);
|
||||
}
|
||||
|
||||
// rect->x += CELL_SIZE;
|
||||
@@ -139,13 +208,16 @@ 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);
|
||||
}
|
||||
GlobalList->cells[GetIndex(x, y, MAX_X)]->Status = Alive;
|
||||
|
||||
printf("Cell at [%d, %d] marked as Alive.\n", x, y);
|
||||
// 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) {
|
||||
@@ -159,6 +231,7 @@ void HandleInput(void) {
|
||||
if (event.button.button == SDL_BUTTON_RIGHT) exit(0);
|
||||
else Thing(event.button.x, event.button.y);
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.sym == SDLK_s) Start = 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -166,6 +239,10 @@ void HandleInput(void) {
|
||||
}
|
||||
|
||||
int GetIndex(int x, int y, int columns) {
|
||||
if ( x < 0 || y < 0 || x > MAX_X || y > MAX_Y) {
|
||||
return MAX_X + MAX_Y * MAX_X + 1;
|
||||
}
|
||||
|
||||
return x + y * columns;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user