From 8edec29c784f2ac4d1ab276e4bc901af924ebd56 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 28 Mar 2019 20:41:12 -0400 Subject: Factor out path searching --- freecell.c | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) (limited to 'freecell.c') diff --git a/freecell.c b/freecell.c index e8b41f6..f480bda 100644 --- a/freecell.c +++ b/freecell.c @@ -22,6 +22,7 @@ #include "cards.h" #include "layout.h" +#include "path.h" #include "stack.h" #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) @@ -274,7 +275,6 @@ static bool mouseButtonUp(SDL_MouseButtonEvent button) { return false; } -static SDL_Window *window; static SDL_Renderer *render; static void renderOutlines(void) { @@ -309,7 +309,7 @@ static void renderList(SDL_Texture **textures, const struct List *list) { static void err(const char *title) { int error = SDL_ShowSimpleMessageBox( - SDL_MESSAGEBOX_ERROR, title, SDL_GetError(), window + SDL_MESSAGEBOX_ERROR, title, SDL_GetError(), NULL ); if (error) fprintf(stderr, "%s\n", SDL_GetError()); exit(EXIT_FAILURE); @@ -319,32 +319,14 @@ int main(void) { if (SDL_Init(SDL_INIT_VIDEO) < 0) err("SDL_Init"); atexit(SDL_Quit); - char *prefPath = SDL_GetPrefPath("Causal Agency", "Cards"); - if (!prefPath) err("SDL_GetPrefPath"); - char *basePath = SDL_GetBasePath(); if (!basePath) err("SDL_GetBasePath"); - const char *paths[] = { prefPath, basePath, "" }; - const char *names[] = { "CARDS.DLL", "SOL.EXE", "cards.dll" }; + char *prefPath = SDL_GetPrefPath("Causal Agency", "Cards"); + if (!prefPath) err("SDL_GetPrefPath"); - SDL_RWops *rw = NULL; - for (uint i = 0; !rw && i < ARRAY_LEN(paths); ++i) { - for (uint j = 0; !rw && j < ARRAY_LEN(names); ++j) { - char path[1024]; - snprintf(path, sizeof(path), "%s%s", paths[i], names[j]); - rw = SDL_RWFromFile(path, "rb"); - } - } - if (!rw) { - char msg[4096]; - snprintf( - msg, sizeof(msg), "CARDS.DLL or SOL.EXE not found in:\n%s\n%s", - prefPath, basePath - ); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Cards", msg, NULL); - return EXIT_FAILURE; - } + SDL_RWops *rw = pathCards(basePath, prefPath); + if (!rw) return EXIT_FAILURE; SDL_Surface *surfaces[Cards_Empty]; int error = Cards_LoadCards( @@ -354,6 +336,7 @@ int main(void) { if (error) err("Cards_LoadCards"); SDL_RWclose(rw); + SDL_Window *window; error = SDL_CreateWindowAndRenderer( WindowWidth, WindowHeight, SDL_WINDOW_ALLOW_HIGHDPI, &window, &render -- cgit 1.4.1