main.cpp (5159B)
1 #include <iostream> 2 #include <sys/types.h> 3 #include <vector> 4 #include <array> 5 #include <string_view> 6 7 #include <raylib.h> 8 9 int N = 100; 10 11 void place_r_pentomino(std::vector<u_int8_t>& grid, int x, int y, int N) { 12 grid[y*N + x+1] = 1; 13 grid[y*N + x+2] = 1; 14 grid[(y+1)*N + x] = 1; 15 grid[(y+1)*N + x+1] = 1; 16 grid[(y+2)*N + x+1] = 1; 17 } 18 void place_lwss(std::vector<u_int8_t>& grid, int x, int y, int N) { 19 grid[y*N + x+1] = 1; 20 grid[y*N + x+4] = 1; 21 22 grid[(y+1)*N + x] = 1; 23 24 grid[(y+2)*N + x] = 1; 25 grid[(y+2)*N + x+4] = 1; 26 27 for (int dx = 0; dx < 4; ++dx) 28 grid[(y+3)*N + x+dx] = 1; 29 } 30 31 32 void place_pulsar(std::vector<u_int8_t>& grid, int x, int y, int N) { 33 constexpr std::array<std::string_view, 13> pattern = { 34 "..###...###..", 35 ".............", 36 "#....#.#....#", 37 "#....#.#....#", 38 "#....#.#....#", 39 "..###...###..", 40 ".............", 41 "..###...###..", 42 "#....#.#....#", 43 "#....#.#....#", 44 "#....#.#....#", 45 ".............", 46 "..###...###.." 47 }; 48 49 for (int dy = 0; dy < 13; ++dy) { 50 for (int dx = 0; dx < 13; ++dx) { 51 if (pattern[dy][dx] == '#') 52 grid[(y + dy)*N + x + dx] = 1; 53 } 54 } 55 } 56 void place_glider(std::vector<u_int8_t>& grid, int x, int y, int N) { 57 grid[y*N + x+1] = 1; 58 grid[(y+1)*N + x +2] = 1; 59 grid[(y+2)*N + x+0] = 1; 60 grid[(y+2)*N + x+1] = 1; 61 grid[(y+2)*N + x+2] = 1; 62 } 63 64 void populate_grid(std::vector<u_int8_t>& grid, int N) { 65 for (int y = 5, row = 0; y + 13 <= N; y += 24, ++row) { 66 for (int x = 5, col = 0; x + 13 <= N; x += 24, ++col) { 67 68 switch ((row + col) % 4) { 69 case 0: 70 place_glider(grid, x, y, N); 71 break; 72 case 1: 73 place_r_pentomino(grid, x, y, N); 74 break; 75 case 2: 76 place_lwss(grid, x, y, N); 77 break; 78 case 3: 79 place_pulsar(grid, x, y, N); 80 break; 81 } 82 83 // Add some extra gliders 84 if ((row + col) % 2 == 0 && 85 x + 18 < N && y + 18 < N) { 86 place_glider(grid, x + 16, y + 16, N); 87 } 88 } 89 } 90 } 91 92 void update_grid(std::vector<u_int8_t>& grid) { 93 std::vector<u_int8_t> next(N * N, 0); 94 95 for (int i=0; i < N*N; i++) { 96 int count = 0; 97 int y = i / N; 98 int x = i - y *N; 99 100 constexpr std::array<std::pair<int, int>, 8> neighbours = {{ 101 { 0, 1}, 102 { 1, 1}, 103 { 1, 0}, 104 { 1, -1}, 105 { 0, -1}, 106 {-1, -1}, 107 {-1, 0}, 108 {-1, 1} 109 }}; 110 111 for (const auto& [dx, dy] : neighbours) { 112 113 int nx = x + dx; 114 int ny = y + dy; 115 116 if (nx < 0 || nx > N-1 || ny < 0 || ny > N-1) { 117 continue; 118 } 119 120 if (grid[ny * N + nx]) { 121 count += 1; 122 } 123 } 124 125 if (grid[i]) { 126 if (count == 2 || count == 3) { 127 next[i] = 1; 128 } else { 129 next[i] = 0; 130 } 131 } else { 132 if (count == 3) { 133 next[i] = 1; 134 } else { 135 next[i] = 0; 136 } 137 } 138 139 // std::cout << y << ", " << x << ", " << count << "\n"; 140 141 } 142 143 grid.swap(next); 144 return; 145 } 146 147 void update_texture( 148 const std::vector<u_int8_t>& cells, 149 std::vector<Color>& pixels, 150 Texture2D texture 151 ) { 152 for (std::size_t i = 0; i < cells.size(); ++i) { 153 pixels[i] = cells[i] ? WHITE : BLACK; 154 } 155 156 UpdateTexture(texture, pixels.data()); 157 } 158 159 int main() { 160 int CELL_SIZE = 5; 161 int SCALE = 8; 162 163 std::vector<u_int8_t> grid(N*N, 0); 164 std::vector<Color> pixels(N*N); 165 166 for (int y=0; y < 20; y++) { 167 for(int x=0; x < 20; x++) { 168 int index = y * N + x; 169 grid[index] = 1; 170 } 171 } 172 173 174 populate_grid(grid, N); 175 176 InitWindow(800,600, "lol"); 177 SetTargetFPS(4.0); 178 179 // 3. Create a 100x100 texture 180 Image image = GenImageColor(N, N, BLACK); 181 Texture2D texture = LoadTextureFromImage(image); 182 UnloadImage(image); 183 184 // Keep pixels sharp when scaling up 185 SetTextureFilter(texture, TEXTURE_FILTER_POINT); 186 187 // 4. Map simulation cells to pixel colors 188 for (int i = 0; i < N * N; ++i) { 189 pixels[i] = grid[i] ? WHITE : BLACK; 190 } 191 192 // 5. Upload the pixels to the GPU 193 UpdateTexture(texture, pixels.data()); 194 195 196 for (int i = 0; i<N*N; i++) { 197 pixels[i] = grid[i] ? WHITE : BLACK; 198 } 199 200 UpdateTexture(texture, pixels.data()); 201 202 203 while(!WindowShouldClose()) { 204 BeginDrawing(); 205 ClearBackground(BLACK); 206 207 update_grid(grid); 208 update_texture(grid, pixels, texture); 209 210 DrawTextureEx( 211 texture, 212 Vector2{0, 0}, 213 0.0f, 214 static_cast<float>(SCALE), 215 WHITE 216 ); 217 218 219 EndDrawing(); 220 } 221 222 CloseWindow(); 223 224 225 226 return 0; 227 } 228