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

@@ -82,6 +82,15 @@ int main(int ac, char **av) {
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"],
&av[0][2]);

View File

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