Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_live_x11_viewer_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11*/
12
25#include <cstdlib>
26#include <iostream>
27#include <limits>
28#include <utility>
29#include <vector>
30
31#include <ca-engine-utils.H>
32#include <ca-x11-viewer.H>
33#include <tpl_ca_lattice.H>
34#include <tpl_ca_storage.H>
35
36using namespace Aleph::CA;
37
38namespace
39{
41
42std::size_t parse_steps(const char *text, const std::size_t fallback)
43{
44 if (text == nullptr)
45 return fallback;
46 char *end = nullptr;
47 const unsigned long value = std::strtoul(text, &end, 10);
48 if (end == text or *end != '\0' or value == 0
49 or value > std::numeric_limits<std::size_t>::max())
50 return fallback;
51 return static_cast<std::size_t>(value);
52}
53
54void stamp(Grid &grid,
55 const ca_index_t row,
56 const ca_index_t col,
57 const std::vector<std::pair<int, int>> &cells)
58{
59 const ca_index_t rows = static_cast<ca_index_t>(grid.size(0));
60 const ca_index_t cols = static_cast<ca_index_t>(grid.size(1));
61 for (const auto &[dr, dc] : cells)
62 grid.set({(row + dr + rows) % rows, (col + dc + cols) % cols}, 1);
63}
64}
65
66int main(int argc, char **argv)
67{
68 const std::size_t steps = argc >= 2 ? parse_steps(argv[1], 400) : 400;
69
70 Grid grid({48, 80}, 0);
71 stamp(grid, 20, 30, {{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}});
72 stamp(grid, 10, 12, {{0, -1}, {0, 0}, {0, 1}});
73
74 auto engine = make_gol_engine(std::move(grid));
76 opts.cell_size = 10;
77 opts.delay_ms = 30;
78 opts.title = "Aleph::CA Game of Life";
79
80 X11_Frame_Sink viewer(Binary_RGB_Mapper<int>{0, {245, 247, 250}, {0, 40, 55}}, opts);
81 for (std::size_t i = 0; i <= steps; ++i)
82 {
83 viewer.accept(engine.steps_run(), engine.frame());
84 if (i != steps)
85 engine.step();
86 }
87 viewer.flush();
88
89 std::cout << "Ran " << steps << " Game-of-Life steps";
90 if (not viewer.active())
91 std::cout << " (viewer inactive; DISPLAY was probably unavailable)";
92 std::cout << '\n';
93 return 0;
94}
int main()
size_t steps
Definition ca-c-api.h:126
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t row
Definition ca-c-api.h:115
size_t * rows
Definition ca-c-api.h:112
size_t size_t col
Definition ca-c-api.h:116
size_t cols
Definition ca-c-api.h:105
Convenience builders for the Phase 3 synchronous engine.
Optional live X11 viewer sink for CA simulations.
Lattice that adds boundary-aware access on top of a storage.
Live X11 frame sink.
void accept(const std::size_t step, const Lattice &frame)
Draw one frame, or no-op when disabled/headless.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
Game_Of_Life_Engine make_gol_engine(ca_size_t rows, ca_size_t cols)
Build a Conway's Game of Life engine of the given shape.
Default binary RGB mapper.
Definition ca-io.H:245
The lattice wraps around on every axis.
Definition ca-traits.H:124
Options for the live X11 viewer.
unsigned cell_size
pixels per CA cell
static mt19937 engine
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).