-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
158 lines (125 loc) · 3.64 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <string.h>
#include "raylib.h"
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define COLS 40
#define ROWS 40
#define BACKGROUND_COLOR { 23, 23, 26, 0 }
#define PINK_RED { 246, 64, 112, 255 }
#define SAND_YELLOW { 211, 169, 108, 255 }
const int screenWidth = 600;
const int screenHeight = 900;
const int cellWidth = screenWidth / COLS;
const int cellHeight = screenHeight / ROWS;
const Color background = BACKGROUND_COLOR;
const Color pinkishRed = PINK_RED;
const Color sandYellow = SAND_YELLOW;
typedef struct Cell {
int i;
int j;
int value;
// Color color;
} Cell;
Cell grid[COLS][ROWS];
int GenerateRandomNumber(int min, int max) {
return rand() % (max - min + 1) + min;
}
void PopulateGrid(Cell (*grid)[COLS]) {
for(int i = 0; i < COLS; i++) {
for(int j = 0; j < ROWS; j++) {
// float hue = ((float)rand() / RAND_MAX) * 360.0f;
grid[i][j] = (Cell) {
.i = i,
.j = j,
// .color = ColorFromHSV(hue, 1.0f, 1.0f),
.value = 0,
};
}
}
}
void DrawCell(Cell cell) {
if(cell.value == 1) {
DrawRectangle(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, sandYellow);
}
// draw grid lines
// DrawRectangleLines(cell.i * cellWidth, cell.j * cellHeight, cellWidth, cellHeight, WHITE);
}
void DrawGridArea() {
Cell newGrid[COLS][ROWS];
PopulateGrid(newGrid);
for(int i = 0; i < COLS; i++) {
for(int j = 0; j < ROWS; j++) {
// Color currentColor = grid[i][j].color;
// Vector3 hsv = ColorToHSV(currentColor);
// float hue = hsv.x;
// float saturation = hsv.y;
// float value = hsv.z;
// hue += 1.0f;
// hue = fmodf(hue, 360.0f);
// newGrid[i][j].color = currentColor;
int currentCell = grid[i][j].value;
// TODO: FIX THIS PLEASE;
if(currentCell == 1) {
int cellUnderCurrent = grid[i][j+1].value;
int cellUnderRight = grid[i + 1][j + 1].value;
int cellUnderLeft = grid[i - 1][j + 1].value;
if(cellUnderCurrent == 0 && j < ROWS - 1) {
newGrid[i][j].value = 0;
newGrid[i][j+1].value = 1;
}
else if (cellUnderRight == 0 && j < ROWS - 1) {
newGrid[i][j].value = 0;
newGrid[i+1][j+1].value = 1;
}
else if (cellUnderLeft == 0 && j < ROWS - 1) {
newGrid[i][j].value = 0;
newGrid[i-1][j+1].value = 1;
}
else {
newGrid[i][j].value = 1;
}
}
}
}
memcpy(grid, newGrid, sizeof(grid));
for(int i = 0; i < COLS; i++) {
for(int j = 0; j < ROWS; j++) {
DrawCell(grid[i][j]);
}
}
}
bool IsClickValid(int i, int j) {
return i >= 0 && i < COLS && j>= 0 && j < ROWS;
}
void DrawCellColor(int i, int j) {
grid[i][j].value = 1;
}
void CheckIsClicked() {
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
Vector2 mousePos = GetMousePosition();
int indexI = mousePos.x / cellWidth;
int indexJ = mousePos.y / cellHeight;
if(IsClickValid(indexI, indexJ)) {
DrawCellColor(indexI, indexJ);
}
}
}
void gameLoop() {
CheckIsClicked();
BeginDrawing();
ClearBackground(background);
DrawGridArea();
EndDrawing();
}
int main(void) {
InitWindow(screenWidth, screenHeight, "Falling Sand Simulator");
SetTargetFPS(60);
PopulateGrid(grid);
while(!WindowShouldClose()) {
gameLoop();
}
CloseWindow();
return 0;
}