From 72999caa545c18fbbb84c567af4138fa988ec6c9 Mon Sep 17 00:00:00 2001 From: Spencer Vaughn Date: Fri, 29 Sep 2023 00:16:31 -0500 Subject: [PATCH] got filepath working for macos --- src/filehelper.c | 69 ++++++++++++++++++++++++++++-------------- src/fileselectscreen.c | 17 +++++++++++ src/main.c | 17 +++++++---- 3 files changed, 75 insertions(+), 28 deletions(-) diff --git a/src/filehelper.c b/src/filehelper.c index b869f6f..2f28cb7 100644 --- a/src/filehelper.c +++ b/src/filehelper.c @@ -1,35 +1,53 @@ +#include +#include +#include +#include +#include #include "filehelper.h" +#include // For dirname function +#include // For _NSGetExecutablePath function char *resolvedPath = NULL; char *absolutePath = NULL; -void getAbsolutePath(const char* basePath, const char* fileName) { - // Combine the base path and file name - char fullPath[strlen(basePath) + 1 + strlen(fileName) + 1]; - sprintf(fullPath, "%s/%s", basePath, fileName); - - // Get the absolute path - resolvedPath = realpath(fullPath, NULL); - if (resolvedPath) { - // Allocate memory for the absolute path - absolutePath = (char*)malloc(strlen(resolvedPath) + 1); - if (absolutePath) { - strcpy(absolutePath, resolvedPath); - } - } -} +// This will only work on macOS void get_save_files(struct SaveFileData *save_data) { DIR *dir; struct dirent *entry; - const char *saveDir = save_data->saveDir; - char **saves_file_path = save_data->saves_file_path; - int *numSaves = &(save_data->numSaves); + char *saveDir = save_data->saveDir; + int numSaves = 0; + + char *executablePath = NULL; + uint32_t size = 0; + int result = 0; + + // Get the path of the executable + result = _NSGetExecutablePath(NULL, &size); + if (result == -1) + { + executablePath = malloc(size); + result = _NSGetExecutablePath(executablePath, &size); + } + + // Get the directory of the executable + char *executableDir = dirname(executablePath); + + // Combine the executable directory and save directory + char *saveDirPath = malloc(strlen(executableDir) + 1 + strlen(saveDir) + 1); + sprintf(saveDirPath, "%s/%s", executableDir, saveDir); + + // Get the absolute path + absolutePath = realpath(saveDirPath, NULL); + if (absolutePath) { + saveDir = absolutePath; + } dir = opendir(saveDir); if (dir == NULL) { - perror("Error opening directory"); + perror("Error opening directory: "); + printf("Path: %s\n", saveDir); return; } @@ -38,15 +56,22 @@ void get_save_files(struct SaveFileData *save_data) // Check if the entry is a regular file with a .sav extension if (entry->d_type == DT_REG && strstr(entry->d_name, ".sav")) { - getAbsolutePath(saveDir, entry->d_name); + // Combine the base path and file name + char fullPath[strlen(saveDir) + 1 + strlen(entry->d_name) + 1]; + sprintf(fullPath, "%s/%s", saveDir, entry->d_name); + + // Get the absolute path + absolutePath = realpath(fullPath, NULL); if (absolutePath) { - saves_file_path[*numSaves] = absolutePath; - (*numSaves)++; + save_data->saves_file_path[numSaves] = absolutePath; + numSaves++; } } } closedir(dir); + save_data->numSaves = numSaves; + free(saveDirPath); } void free_filehelper_pointers(void) { diff --git a/src/fileselectscreen.c b/src/fileselectscreen.c index a544e18..0c42c5a 100644 --- a/src/fileselectscreen.c +++ b/src/fileselectscreen.c @@ -83,4 +83,21 @@ void DrawFileSelectScreen(struct pksav_gen2_save *save_player1, struct pksav_gen save_player2 = NULL; } } + + // change directory button between back and next buttons in line horizontally centered vertically + int button_width = MeasureText("Change Save Directory", 20) + 20; + DrawText("Change Save Directory", SCREEN_WIDTH / 2 - button_width / 2, BACK_BUTTON_Y, 20, BLACK); + if (CheckCollisionPointRec(GetMousePosition(), (Rectangle){SCREEN_WIDTH / 2 - button_width / 2 - 10, BACK_BUTTON_Y - 30, button_width, BUTTON_HEIGHT})) + { + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + { + *current_screen = SCREEN_FILE_EDIT; + selected_saves_index[0] = -1; + selected_saves_index[1] = -1; + trainer1->trainer_id = 0; + trainer2->trainer_id = 0; + save_player1 = NULL; + save_player2 = NULL; + } + } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 27f3717..2a32329 100644 --- a/src/main.c +++ b/src/main.c @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) char inputText[MAX_INPUT_CHARS + 1] = "\0"; // Input text buffer int textSize = 0; // Current text size - Rectangle inputBox = {SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - 20, 200, 40}; + Rectangle inputBox = { 50, SCREEN_HEIGHT / 2 - 20, SCREEN_WIDTH - 100, 40}; bool editingText = false; // Flag to indicate if the text is being edited while (!should_close_window && !WindowShouldClose()) @@ -121,6 +121,12 @@ int main(int argc, char *argv[]) } break; case SCREEN_FILE_EDIT: + // Placeholder Text + if (!editingText && textSize == 0) { + strcpy(inputText, save_file_data.saveDir); + textSize = strlen(inputText); + } + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { // Check if the mouse is clicked within the input box @@ -157,8 +163,8 @@ int main(int argc, char *argv[]) editingText = false; } } - DrawText("Specify folder name containing saves", 190, 100, 20, BLACK); - DrawText("Saves folder must be in same folder as application: ", 190, 200, 20, BLACK); + DrawText("Specify folder name containing saves", 50, SCREEN_HEIGHT / 2 - 75, 20, BLACK); + DrawText("relative to executable (e.g. \"../my_saves\" or \"saves\")", 50, SCREEN_HEIGHT / 2 - 50, 20, BLACK); // Draw the input box DrawRectangleRec(inputBox, WHITE); @@ -175,14 +181,13 @@ int main(int argc, char *argv[]) } // Draw the save button - DrawText("Save", NEXT_BUTTON_X, NEXT_BUTTON_Y, 20, BLACK); + DrawText("Save!", NEXT_BUTTON_X, NEXT_BUTTON_Y, 20, textSize ? BLACK : LIGHTGRAY); if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { - if (CheckCollisionPointRec(GetMousePosition(), (Rectangle){NEXT_BUTTON_X - 15, NEXT_BUTTON_Y - 30, BUTTON_WIDTH, BUTTON_HEIGHT})) + if (CheckCollisionPointRec(GetMousePosition(), (Rectangle){NEXT_BUTTON_X - 15, NEXT_BUTTON_Y - 30, BUTTON_WIDTH, BUTTON_HEIGHT}) && textSize > 0) { save_file_data.saveDir = inputText; - printf("Save directory changed to %s\n", save_file_data.saveDir); save_file_data.numSaves = 0; *save_file_data.saves_file_path = NULL; get_save_files(&save_file_data);