-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
188 lines (169 loc) · 5.78 KB
/
main.c
File metadata and controls
188 lines (169 loc) · 5.78 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "raylib.h"
#define WIDTH 800
#define HEIGHT 600
#define AIR 0
#define SAND 1
#define WALL 2
void MouseInput(int rows, int cols, int grid[rows][cols], int w, int mouseSelect){
int mouseCol = GetMouseX() / w;
int mouseRow = GetMouseY() / w;
if(IsMouseButtonDown(MOUSE_LEFT_BUTTON)){
//Create stuff
int matrix = 5;
int extend = matrix / 2;
for(int i = -extend; i <= extend; i++){
for(int j = -extend; j <= extend; j++){
//Get a random value just for the sand to be random and not a block
if(GetRandomValue(1, 2) == 2 && mouseSelect == SAND){
int col = mouseCol + i;
int row = mouseRow + j;
if(row >= 0 && row < rows && col >= 0 && col < cols){
grid[row][col] = SAND;
}
} else if (mouseSelect == WALL){
int col = mouseCol + i;
int row = mouseRow + j;
if(row >= 0 && row < rows && col >= 0 && col < cols){
grid[row][col] = WALL;
}
}
}
}
} else if(IsMouseButtonDown(MOUSE_RIGHT_BUTTON)){
int matrix = 5;
int extend = matrix / 2;
for(int i = -extend; i <= extend; i++){
for(int j = -extend; j <= extend; j++){
int col = mouseCol + i;
int row = mouseRow + j;
if(row >= 0 && row < rows && col >= 0 && col < cols){
grid[row][col] = AIR;
}
}
}
}
}
void UpdateArray2D(int rows, int cols, int grid[rows][cols], int w){
//Drawing the Sand
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(grid[i][j] == SAND){
DrawRectangle(j * w, i * w, w, w, YELLOW);
}
if(grid[i][j] == WALL){
DrawRectangle(j * w, i * w, w, w, LIGHTGRAY);
}
}
}
//Create a 2DArray for next frame
int nextGrid[rows][cols];
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
nextGrid[i][j] = AIR;
}
}
//Check every Cell
for (int i = rows - 1; i >= 0; i--) { // From groundUp
for (int j = 0; j < cols; j++) {
int state = grid[i][j];
if (state == SAND) {
// Look Down if isn't the last row
if (i + 1 < rows) {
int below= grid[i + 1][j];
int belowRight = (j+1 < cols) ? grid[i + 1][j + 1] : 1;
int belowLeft = (j -1 >= 0) ? grid[i + 1][j - 1] : 1;
if (below == 0) {
nextGrid[i + 1][j] = state;
} else {
// Choose random direction
if (GetRandomValue(0, 1) == 0) {
// Right then Left
if (belowRight == 0) {
nextGrid[i + 1][j + 1] = state;
} else if (belowLeft == 0) {
nextGrid[i + 1][j - 1] = state;
} else {
nextGrid[i][j] = state;
}
} else {
// Left then Right
if (belowLeft == 0) {
nextGrid[i + 1][j - 1] = state;
} else if (belowRight == 0) {
nextGrid[i + 1][j + 1] = state;
} else {
nextGrid[i][j] = state;
}
}
}
} else {
//ON THE GROUND!!
nextGrid[i][j] = state;
}
} else {
nextGrid[i][j] = state;
}
}
}
//Update the grid with the nextGrid
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
grid[i][j] = nextGrid[i][j];
}
}
}
void Draw_SimpleUI(int mouseSelect, Rectangle sand, Rectangle wall){
DrawRectangleRec(sand, YELLOW);
DrawRectangleRec(wall, LIGHTGRAY);
switch (mouseSelect) {
case 1:
DrawRectangleLinesEx(sand, 2.5, WHITE);
break;
case 2:
DrawRectangleLinesEx(wall, 2.5, WHITE);
break;
default:
break;
}
}
int Update_SimpleUI(int mouseSelect, Rectangle sand, Rectangle wall){
Vector2 mousePosition = GetMousePosition();
if(CheckCollisionPointRec(mousePosition, sand) || IsKeyPressed(KEY_ONE)){
return SAND;
}
if(CheckCollisionPointRec(mousePosition, wall) || IsKeyPressed(KEY_TWO)){
return WALL;
}
return mouseSelect;
}
int main(){
//--SETUP--
InitWindow(WIDTH, HEIGHT, "Falling Sand in C & Raylib");
SetTargetFPS(60);
//Creating the Array2D
int w = 5;
int cols = WIDTH/ w;
int rows = HEIGHT / w;
int grid[rows][cols];
int mouseSelect = SAND;
Rectangle sandButton = {(int)(WIDTH / 2), 10, 40, 20};
Rectangle wallButton = {(int)(WIDTH / 2) + 50, 10, 40, 20};
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
grid[i][j] = AIR;
}
}
while(!WindowShouldClose()){
//--Input--
mouseSelect = Update_SimpleUI(mouseSelect, sandButton, wallButton);
MouseInput(rows, cols, grid, w, mouseSelect);
//---Draw---
BeginDrawing();
ClearBackground(BLACK);
UpdateArray2D(rows, cols, grid, w);
Draw_SimpleUI(mouseSelect, sandButton, wallButton);
EndDrawing();
}
CloseWindow();
return 0;
}