-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
215 lines (169 loc) · 5.5 KB
/
main.cpp
File metadata and controls
215 lines (169 loc) · 5.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Author: Sarthak Sharma [Sarthak950]
*
Making a sand simulator so there are 4 states of a cell
1. Empty
2. Rock which are immovable
3. Sand which falls down
4. Water which flows down and to the sides
*/
#include "raylib.h"
#include <bits/stdc++.h>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 800;
const int CELL_SIZE = 4;
const int GRID_SIZE = 800/CELL_SIZE;
int FPS = 400;
// not const
int grid[GRID_SIZE][GRID_SIZE];
int rockCount = 0;
int sandCount = 0;
int waterCount = 0;
#define EMPTY 0
#define ROCK 1
#define SAND 2
#define WATER 3
void InitializeGrid() {
// Set all cells to Empty
for (int i = 0; i < GRID_SIZE; i++)
for (int j = 0; j < GRID_SIZE; j++)
grid[i][j] = EMPTY;
// Border aroound the grid
for (int i = 0; i < GRID_SIZE; i++) {
grid[GRID_SIZE - 1][i] = ROCK;
grid[i][GRID_SIZE - 1] = ROCK;
grid[0][i] = ROCK;
grid[i][0] = ROCK;
}
}
void KeyPressHandeler () {
int mouseX = GetMouseX();
int mouseY = GetMouseY();
mouseX = mouseX / CELL_SIZE;
mouseY = mouseY / CELL_SIZE;
if (IsKeyDown(KEY_SPACE) ) {
grid[mouseX][mouseY] = ROCK;
}
else if (IsKeyDown(KEY_S) ) {
if (grid[mouseX][mouseY] == EMPTY) grid[mouseX][mouseY] = SAND;
}
else if (IsKeyDown(KEY_W) ) {
if (grid[mouseX][mouseY] == EMPTY) grid[mouseX][mouseY] = WATER;
}
else if (IsKeyDown(KEY_E) ) {
if (grid[mouseX][mouseY] == EMPTY) grid[mouseX][mouseY] = EMPTY;
}
else if (IsKeyPressed(KEY_BACKSPACE)) {
InitializeGrid();
}
}
std::string FormatText (const char *text, ...) {
char buffer[1024] = { 0 };
va_list args;
va_start(args, text);
vsprintf(buffer, text, args);
va_end(args);
return buffer;
}
void DisplayCount () {
rockCount = 0;
sandCount = 0;
waterCount = 0;
for (int i = 0; i < GRID_SIZE; i++)
for (int j = 0; j < GRID_SIZE; j++) {
if (grid[i][j] == ROCK) rockCount++;
else if (grid[i][j] == SAND) sandCount++;
else if (grid[i][j] == WATER) waterCount++;
}
std::string rockCountString = FormatText("Rocks: %i", rockCount);
std::string sandCountString = FormatText("Sand: %i", sandCount);
std::string waterCountString = FormatText("Water: %i", waterCount);
// Display the count
DrawText(rockCountString.c_str(), 10, 10, 20, BLACK);
DrawText(sandCountString.c_str(), 10, 30, 20, BLACK);
DrawText(waterCountString.c_str(), 10, 50, 20, BLACK);
}
void UpdateGrid() {
KeyPressHandeler();
DisplayCount();
for (int i= 0;i<GRID_SIZE;i++) {
for ( int j = GRID_SIZE;j>0;j--) {
if ( grid[i][j] == SAND) {
if (grid[i][j+1] == EMPTY) {
grid[i][j+1] = SAND;
grid[i][j] = EMPTY;
}
else if (grid[i-1][j+1] == EMPTY) {
grid[i-1][j+1] = SAND;
grid[i][j] = EMPTY;
}
else if (grid[i+1][j+1] == EMPTY) {
grid[i+1][j+1] = SAND;
grid[i][j] = EMPTY;
}
// add interaction with water
else if (grid[i][j+1] == WATER) {
grid[i][j+1] = SAND;
grid[i][j] = WATER;
}
else if (grid[i-1][j+1] == WATER) {
grid[i-1][j+1] = SAND;
grid[i][j] = WATER;
}
else if (grid[i+1][j+1] == WATER) {
grid[i+1][j+1] = SAND;
grid[i][j] = WATER;
}
}
else if (grid[i][j] == WATER) {
if (grid[i][j+1] == EMPTY) {
grid[i][j+1] = WATER;
grid[i][j] = EMPTY;
}
else if (grid[i+1][j+1] == EMPTY) {
grid[i+1][j+1] = WATER;
grid[i][j] = EMPTY;
}
else if (grid[i-1][j+1] == EMPTY) {
grid[i-1][j+1] = WATER;
grid[i][j] = EMPTY;
}
else if (grid[i+1][j] == EMPTY) {
grid[i+1][j] = WATER;
grid[i][j] = EMPTY;
}
else if (grid[i-1][j] == EMPTY) {
grid[i-1][j] = WATER;
grid[i][j] = EMPTY;
}
}
}
}
}
void DrawBox (int x, int y, int width, int height, int state) {
auto color = CLITERAL(Color){ 0, 0, 0, 255 };
if (state == EMPTY) color = CLITERAL(Color){ 243, 240, 202, 255 };
else if (state == ROCK) color = CLITERAL(Color){ 0, 0, 0, 255 };
else if (state == SAND) color = CLITERAL(Color){ 255, 170, 116, 255 };
else if (state == WATER) color = CLITERAL(Color){ 56, 118, 191, 255 };
DrawRectangle(x, y, width, height, color);
}
int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Sand Sim");
InitializeGrid();
SetTargetFPS(FPS);
while (!WindowShouldClose()) {
// Setup the canvas
BeginDrawing();
ClearBackground(RAYWHITE);
// Render the grid
for (int i = 0; i < GRID_SIZE; i++)
for (int j = 0; j < GRID_SIZE; j++)
DrawBox (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE, grid[i][j]);
// Update the grid
UpdateGrid();
EndDrawing();
}
CloseWindow();
return 0;
}