Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created December 19, 2024 13:41
Show Gist options
  • Save ProfAndreaPollini/537218f762028a456f1f8425685a05e7 to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/537218f762028a456f1f8425685a05e7 to your computer and use it in GitHub Desktop.
Simulazione di neve utilizzando raylib e C++ . Automi cellulari
#include <vector>
#include "raylib.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 450
#define CELL_SIZE 2
#define GRID_WIDTH (SCREEN_WIDTH / CELL_SIZE)
#define GRID_HEIGHT (SCREEN_HEIGHT / CELL_SIZE)
void DrawWorld(const std::vector<bool> &world,Color c) {
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
if (world[y * GRID_WIDTH + x]) {
//DrawRectangle(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE, LIGHTGRAY);
DrawCircle(x * CELL_SIZE, y * CELL_SIZE, 4, c);
}
}
}
}
void UpdateWorld(std::vector<bool>& world)
{
for (int y = GRID_HEIGHT - 1; y >= 0; y--)
{
for (int x = 0; x < GRID_WIDTH; x++)
{
if (world[y * GRID_WIDTH + x])
{
if (y < GRID_HEIGHT - 1)
{
if (!world[(y + 1) * GRID_WIDTH + x])
{
int dx = 0;
if (GetRandomValue(1,10) >6) {
dx = GetRandomValue(-3, 3);
}
// world[y * GRID_WIDTH + x] = false;
// world[(y + 1) * GRID_WIDTH + x] = true;
if (x + dx >= 0 && x + dx < GRID_WIDTH && !world[(y + 1) * GRID_WIDTH + x + dx])
{
world[y * GRID_WIDTH + x] = false;
world[(y + 1) * GRID_WIDTH + x + dx] = true;
}
}
else
{
int dx = GetRandomValue(-1, 1);
if (x + dx >= 0 && x + dx < GRID_WIDTH && !world[(y + 1) * GRID_WIDTH + x + dx])
{
world[y * GRID_WIDTH + x] = false;
world[(y + 1) * GRID_WIDTH + x + dx] = true;
}
}
}
}
}
}
}
void AddSand(std::vector<bool> &world, int mouseX, int mouseY) {
int x = mouseX / CELL_SIZE;
int y = mouseY / CELL_SIZE;
if (x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) {
world[y * GRID_WIDTH + x] = true;
}
}
int main() {
std::vector<bool> world(GRID_WIDTH * GRID_HEIGHT, false);
std::vector<bool> world2(GRID_WIDTH * GRID_HEIGHT, false);
std::vector<int> generator_x;
for (int i = 0; i < 10; ++i) {
generator_x.push_back(GetRandomValue(0, SCREEN_WIDTH-1));
}
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Falling Sand Simulation");
SetTargetFPS(30); // Set our game to run at 60 frames-per-second
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
AddSand(world, GetMouseX(), GetMouseY());
}
//for (auto x: generator_x) {
if(GetRandomValue(1,10)>5) {
AddSand(world, GetRandomValue(0,SCREEN_WIDTH-1),0);
}
if(GetRandomValue(1,10)>5) {
AddSand(world2, GetRandomValue(0,SCREEN_WIDTH-1),0);
}
//}
UpdateWorld(world);
UpdateWorld(world2);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(DARKGRAY);
DrawWorld(world,RED);
DrawWorld(world2,WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment