context: Add program directory's path

This commit is contained in:
2026-01-13 15:30:30 +01:00
parent 8f7b735820
commit c4725847cc
3 changed files with 36 additions and 30 deletions

View File

@@ -33,6 +33,7 @@ typedef struct ctx {
std::shared_ptr<Selection> selection = nullptr; std::shared_ptr<Selection> selection = nullptr;
std::shared_ptr<PaternsMenu> paterns_menu = nullptr; std::shared_ptr<PaternsMenu> paterns_menu = nullptr;
nlohmann::json config_json; nlohmann::json config_json;
std::filesystem::path program_dir;
} ctx; } ctx;
} // namespace gol } // namespace gol

View File

@@ -82,6 +82,15 @@ int main(int ac, char **av) {
context->config_json["fps"] = 800; context->config_json["fps"] = 800;
} }
// Get program directory
char path_buf[1024];
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1);
if (len == -1) {
std::cerr << "Failed to determine program directory" << std::endl;
return 1;
}
context->program_dir = std::filesystem::path(path_buf).parent_path();
InitWindow(context->config_json["screen_width"], context->config_json["screen_height"], InitWindow(context->config_json["screen_width"], context->config_json["screen_height"],
&av[0][2]); &av[0][2]);

View File

@@ -62,41 +62,37 @@ void SelectionMenu::display() {
rlImGuiImageSize(&selectionTexture_.texture, 200, 200); rlImGuiImageSize(&selectionTexture_.texture, 200, 200);
ImGui::InputText("patern_name", patern_name_, 255); ImGui::InputText("patern_name", patern_name_, 255);
if (ImGui::Button("Save")) { if (ImGui::Button("Save")) {
char path_buf[1024]; // Create paterns dir if not present
ssize_t len = readlink("/proc/self/exe", path_buf, sizeof(path_buf)-1); std::filesystem::path paterns_dir = context_->program_dir / "paterns";
if (len != -1) { if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) {
// Create paterns dir if not present std::cerr << "Failed to create paterns directory" << std::endl;
std::filesystem::path paterns_dir = std::filesystem::path(path_buf).parent_path() / "paterns"; } else { // Could be optimized by early returning in a function
if (!std::filesystem::exists(paterns_dir) && !std::filesystem::create_directory(paterns_dir)) { std::ofstream patern_file;
std::cerr << "Failed to create paterns directory" << std::endl; paterns_dir += '/';
} else { // Could be optimized by early returning in a function paterns_dir += patern_name_;
std::ofstream patern_file; patern_file.open(paterns_dir);
paterns_dir += '/'; if (!patern_file) {
paterns_dir += patern_name_; std::cerr << "Failed to create the patern file" << std::endl;
patern_file.open(paterns_dir); } else {
if (!patern_file) { auto sel_it = sel_data_.begin();
std::cerr << "Failed to create the patern file" << std::endl; sel_it += 2; // skip dimensions
} else { patern_file << sel_data_[0];
auto sel_it = sel_data_.begin(); patern_file << "|"; // Separator needed to split as ascii values
sel_it += 2; // skip dimensions patern_file << sel_data_[1];
patern_file << sel_data_[0]; patern_file << "|"; // Separator needed to split as ascii values
patern_file << "|"; // Separator needed to split as ascii values for (int j = 0; j < sel_data_[1]; j++) {
patern_file << sel_data_[1]; for (int i = 0; i < sel_data_[0]; i++) {
patern_file << "|"; // Separator needed to split as ascii values patern_file << std::to_string(*sel_it);
for (int j = 0; j < sel_data_[1]; j++) { if (*sel_it == 1) {
for (int i = 0; i < sel_data_[0]; i++) {
patern_file << std::to_string(*sel_it);
if (*sel_it == 1) {
}
sel_it++;
} }
sel_it++;
} }
patern_file << std::flush;
patern_file.close();
} }
patern_file << std::flush;
patern_file.close();
} }
sel_ctrl_ = false;
} }
sel_ctrl_ = false;
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Discard")) { if (ImGui::Button("Discard")) {