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