CARDS.DLL loader for SDL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1KB

  1. /* Copyright (C) 2019 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef ASSET_H
  17. #define ASSET_H
  18. #include <SDL_filesystem.h>
  19. #include <SDL_rwops.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. struct Paths {
  23. char *base;
  24. char *pref;
  25. };
  26. static inline int assetPaths(struct Paths *paths) {
  27. paths->base = SDL_GetBasePath();
  28. if (!paths->base) return -1;
  29. paths->pref = SDL_GetPrefPath("Causal Agency", "Entertainment Pack");
  30. if (!paths->pref) return -1;
  31. return 0;
  32. }
  33. static inline SDL_RWops *
  34. assetOpen(const struct Paths *paths, const char *names[], size_t len) {
  35. const char *dirs[3] = { paths->pref, paths->base, "" };
  36. for (size_t i = 0; i < 3; ++i) {
  37. for (size_t j = 0; j < len; ++j) {
  38. char path[1024];
  39. snprintf(path, sizeof(path), "%s%s", dirs[i], names[j]);
  40. SDL_RWops *rw = SDL_RWFromFile(path, "rb");
  41. if (rw) return rw;
  42. }
  43. }
  44. return NULL;
  45. }
  46. static inline SDL_RWops *assetOpenCards(const struct Paths *paths) {
  47. const char *names[3] = { "CARDS.DLL", "SOL.EXE", "cards.dll" };
  48. SDL_RWops *rw = assetOpen(paths, names, 3);
  49. if (rw) return rw;
  50. char msg[4096];
  51. snprintf(
  52. msg, sizeof(msg), "%s or %s not found in:\n%s\n%s",
  53. names[0], names[1], paths->pref, paths->base
  54. );
  55. SDL_ShowSimpleMessageBox(
  56. SDL_MESSAGEBOX_ERROR, "Entertainment Pack", msg, NULL
  57. );
  58. return NULL;
  59. }
  60. static inline SDL_RWops *assetOpenFreeCell(const struct Paths *paths) {
  61. const char *names[2] = { "FREECELL.EXE", "freecell.exe" };
  62. return assetOpen(paths, names, 2);
  63. }
  64. #endif