| 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <SDL/SDL.h> |
| 4 | #include <SDL/SDL_image.h> |
| 5 | #include "files.h" |
| 6 | |
| 7 | static char *data_dir = NULL; |
| 8 | |
| 9 | // Find a file's full path based on the name of a file |
| 10 | char *Find_Data_File(char *filename) |
| 11 | { |
| 12 | char *location; |
| 13 | FILE *my_file; |
| 14 | |
| 15 | location = (char *) calloc( strlen(filename) + 64, sizeof(char) ); |
| 16 | if (! location) |
| 17 | return filename; |
| 18 | |
| 19 | // maybe we already know the directory and don't have to search |
| 20 | if (data_dir) |
| 21 | { |
| 22 | sprintf(location, "%s/%s", data_dir, filename); |
| 23 | return location; |
| 24 | } |
| 25 | |
| 26 | sprintf(location, "%s/%s", DATADIR, filename); |
| 27 | my_file = fopen(location, "r"); |
| 28 | if (my_file) |
| 29 | { |
| 30 | fclose(my_file); |
| 31 | data_dir = DATADIR; |
| 32 | return location; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | This is the old way of guessing one location at a time. Now we |
| 37 | can use the DATADIR definition to make sure we check the correct |
| 38 | place. |
| 39 | |
| 40 | sprintf(location, "/usr/local/share/games/openssn/%s", filename); |
| 41 | my_file = fopen(location, "r"); |
| 42 | if (my_file) |
| 43 | { |
| 44 | fclose(my_file); |
| 45 | data_dir = "/usr/local/share/games/openssn"; |
| 46 | return location; |
| 47 | } |
| 48 | |
| 49 | sprintf(location, "/usr/share/games/openssn/%s", filename); |
| 50 | my_file = fopen(location, "r"); |
| 51 | if (my_file) |
| 52 | { |
| 53 | fclose(my_file); |
| 54 | data_dir = "/usr/share/games/openssn"; |
| 55 | return location; |
| 56 | } |
| 57 | */ |
| 58 | |
| 59 | // last chance, try OPTDIR |
| 60 | sprintf(location, "%s/%s", OPTDIR, filename); |
| 61 | my_file = fopen(location, "r"); |
| 62 | if (my_file) |
| 63 | { |
| 64 | fclose(my_file); |
| 65 | data_dir = OPTDIR; |
| 66 | return location; |
| 67 | } |
| 68 | data_dir = "."; |
| 69 | return filename; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | SDL_Surface *Load_Image(char *filename) |
| 74 | { |
| 75 | char *file_name; |
| 76 | SDL_Surface *my_image = NULL; |
| 77 | |
| 78 | file_name = Find_Data_File(filename); |
| 79 | if (file_name) |
| 80 | my_image = IMG_Load(file_name); |
| 81 | if ( (file_name) && (file_name != filename) ) |
| 82 | free(file_name); |
| 83 | return my_image; |
| 84 | |
| 85 | } |
| 86 | |
| 87 |