simulations

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit c82fa96c12be89cb267b1cf25d881a8e36e2d92e
Author: Damian <damianhsimon@gmail.com>
Date:   Sat, 24 Oct 2026 16:14:19 +0200

init

Diffstat:
A.gitignore | 1+
ACMakeLists.txt | 20++++++++++++++++++++
Amain.cpp | 228+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest | 0
4 files changed, 249 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.20) + +project(myproject LANGUAGES C CXX) + +set(CMAKE_CXX_STANDARD 20) + +include(FetchContent) + +FetchContent_Declare( + raylib + GIT_REPOSITORY https://github.com/raysan5/raylib.git + GIT_TAG 6.0 + GIT_SHALLOW TRUE +) + +FetchContent_MakeAvailable(raylib) + +add_executable(myproject main.cpp) + +target_link_libraries(myproject PRIVATE raylib) diff --git a/main.cpp b/main.cpp @@ -0,0 +1,228 @@ +#include <iostream> +#include <sys/types.h> +#include <vector> +#include <array> +#include <string_view> + +#include <raylib.h> + +int N = 100; + +void place_r_pentomino(std::vector<u_int8_t>& grid, int x, int y, int N) { + grid[y*N + x+1] = 1; + grid[y*N + x+2] = 1; + grid[(y+1)*N + x] = 1; + grid[(y+1)*N + x+1] = 1; + grid[(y+2)*N + x+1] = 1; +} +void place_lwss(std::vector<u_int8_t>& grid, int x, int y, int N) { + grid[y*N + x+1] = 1; + grid[y*N + x+4] = 1; + + grid[(y+1)*N + x] = 1; + + grid[(y+2)*N + x] = 1; + grid[(y+2)*N + x+4] = 1; + + for (int dx = 0; dx < 4; ++dx) + grid[(y+3)*N + x+dx] = 1; +} + + +void place_pulsar(std::vector<u_int8_t>& grid, int x, int y, int N) { + constexpr std::array<std::string_view, 13> pattern = { + "..###...###..", + ".............", + "#....#.#....#", + "#....#.#....#", + "#....#.#....#", + "..###...###..", + ".............", + "..###...###..", + "#....#.#....#", + "#....#.#....#", + "#....#.#....#", + ".............", + "..###...###.." + }; + + for (int dy = 0; dy < 13; ++dy) { + for (int dx = 0; dx < 13; ++dx) { + if (pattern[dy][dx] == '#') + grid[(y + dy)*N + x + dx] = 1; + } + } +} +void place_glider(std::vector<u_int8_t>& grid, int x, int y, int N) { + grid[y*N + x+1] = 1; + grid[(y+1)*N + x +2] = 1; + grid[(y+2)*N + x+0] = 1; + grid[(y+2)*N + x+1] = 1; + grid[(y+2)*N + x+2] = 1; +} + +void populate_grid(std::vector<u_int8_t>& grid, int N) { + for (int y = 5, row = 0; y + 13 <= N; y += 24, ++row) { + for (int x = 5, col = 0; x + 13 <= N; x += 24, ++col) { + + switch ((row + col) % 4) { + case 0: + place_glider(grid, x, y, N); + break; + case 1: + place_r_pentomino(grid, x, y, N); + break; + case 2: + place_lwss(grid, x, y, N); + break; + case 3: + place_pulsar(grid, x, y, N); + break; + } + + // Add some extra gliders + if ((row + col) % 2 == 0 && + x + 18 < N && y + 18 < N) { + place_glider(grid, x + 16, y + 16, N); + } + } + } +} + +void update_grid(std::vector<u_int8_t>& grid) { + std::vector<u_int8_t> next(N * N, 0); + + for (int i=0; i < N*N; i++) { + int count = 0; + int y = i / N; + int x = i - y *N; + + constexpr std::array<std::pair<int, int>, 8> neighbours = {{ + { 0, 1}, + { 1, 1}, + { 1, 0}, + { 1, -1}, + { 0, -1}, + {-1, -1}, + {-1, 0}, + {-1, 1} + }}; + + for (const auto& [dx, dy] : neighbours) { + + int nx = x + dx; + int ny = y + dy; + + if (nx < 0 || nx > N-1 || ny < 0 || ny > N-1) { + continue; + } + + if (grid[ny * N + nx]) { + count += 1; + } + } + + if (grid[i]) { + if (count == 2 || count == 3) { + next[i] = 1; + } else { + next[i] = 0; + } + } else { + if (count == 3) { + next[i] = 1; + } else { + next[i] = 0; + } + } + + // std::cout << y << ", " << x << ", " << count << "\n"; + + } + + grid.swap(next); + return; +} + +void update_texture( + const std::vector<u_int8_t>& cells, + std::vector<Color>& pixels, + Texture2D texture +) { + for (std::size_t i = 0; i < cells.size(); ++i) { + pixels[i] = cells[i] ? WHITE : BLACK; + } + + UpdateTexture(texture, pixels.data()); +} + +int main() { + int CELL_SIZE = 5; + int SCALE = 8; + + std::vector<u_int8_t> grid(N*N, 0); + std::vector<Color> pixels(N*N); + + for (int y=0; y < 20; y++) { + for(int x=0; x < 20; x++) { + int index = y * N + x; + grid[index] = 1; + } + } + + + populate_grid(grid, N); + + InitWindow(800,600, "lol"); + SetTargetFPS(4.0); + + // 3. Create a 100x100 texture + Image image = GenImageColor(N, N, BLACK); + Texture2D texture = LoadTextureFromImage(image); + UnloadImage(image); + + // Keep pixels sharp when scaling up + SetTextureFilter(texture, TEXTURE_FILTER_POINT); + + // 4. Map simulation cells to pixel colors + for (int i = 0; i < N * N; ++i) { + pixels[i] = grid[i] ? WHITE : BLACK; + } + + // 5. Upload the pixels to the GPU + UpdateTexture(texture, pixels.data()); + + + for (int i = 0; i<N*N; i++) { + pixels[i] = grid[i] ? WHITE : BLACK; + } + + UpdateTexture(texture, pixels.data()); + + + while(!WindowShouldClose()) { + BeginDrawing(); + ClearBackground(BLACK); + + update_grid(grid); + update_texture(grid, pixels, texture); + + DrawTextureEx( + texture, + Vector2{0, 0}, + 0.0f, + static_cast<float>(SCALE), + WHITE + ); + + + EndDrawing(); + } + + CloseWindow(); + + + + return 0; +} + diff --git a/test b/test Binary files differ.