testutils.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright 1997-2022 Sam Lantinga
  3. Copyright 2022 Collabora Ltd.
  4. SPDX-License-Identifier: Zlib
  5. */
  6. #include "testutils.h"
  7. #if defined(SDL_FILESYSTEM_OS2) || defined(SDL_FILESYSTEM_WINDOWS)
  8. static const char pathsep[] = "\\";
  9. #elif defined(SDL_FILESYSTEM_RISCOS)
  10. static const char pathsep[] = ".";
  11. #else
  12. static const char pathsep[] = "/";
  13. #endif
  14. /*
  15. * Return the absolute path to def in the SDL_GetBasePath() if possible, or
  16. * the relative path to def on platforms that don't have a working
  17. * SDL_GetBasePath(). Free the result with SDL_free.
  18. *
  19. * Fails and returns NULL if out of memory.
  20. */
  21. char *
  22. GetNearbyFilename(const char *file)
  23. {
  24. char *base;
  25. char *path;
  26. base = SDL_GetBasePath();
  27. if (base != NULL) {
  28. size_t len = SDL_strlen(base) + SDL_strlen(pathsep) + SDL_strlen(file) + 1;
  29. path = SDL_malloc(len);
  30. if (path == NULL) {
  31. SDL_free(base);
  32. SDL_OutOfMemory();
  33. return NULL;
  34. }
  35. SDL_snprintf(path, len, "%s%s%s", base, pathsep, file);
  36. SDL_free(base);
  37. } else {
  38. path = SDL_strdup(file);
  39. if (path == NULL) {
  40. SDL_OutOfMemory();
  41. }
  42. }
  43. return path;
  44. }
  45. /*
  46. * If user_specified is non-NULL, return a copy of it. Free with SDL_free.
  47. *
  48. * Otherwise, return the absolute path to def in the SDL_GetBasePath() if
  49. * possible, or the relative path to def on platforms that don't have a
  50. * working SDL_GetBasePath(). Free the result with SDL_free.
  51. *
  52. * Fails and returns NULL if out of memory.
  53. */
  54. char *
  55. GetResourceFilename(const char *user_specified, const char *def)
  56. {
  57. if (user_specified != NULL) {
  58. char *ret = SDL_strdup(user_specified);
  59. if (ret == NULL) {
  60. SDL_OutOfMemory();
  61. }
  62. return ret;
  63. } else {
  64. return GetNearbyFilename(def);
  65. }
  66. }
  67. /*
  68. * Load the .bmp file whose name is file, from the SDL_GetBasePath() if
  69. * possible or the current working directory if not.
  70. *
  71. * If transparent is true, set the transparent colour from the top left pixel.
  72. *
  73. * If width_out is non-NULL, set it to the texture width.
  74. *
  75. * If height_out is non-NULL, set it to the texture height.
  76. */
  77. SDL_Texture *
  78. LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent,
  79. int *width_out, int *height_out)
  80. {
  81. SDL_Surface *temp = NULL;
  82. SDL_Texture *texture = NULL;
  83. char *path;
  84. path = GetNearbyFilename(file);
  85. if (path != NULL) {
  86. file = path;
  87. }
  88. temp = SDL_LoadBMP(file);
  89. if (temp == NULL) {
  90. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  91. } else {
  92. /* Set transparent pixel as the pixel at (0,0) */
  93. if (transparent) {
  94. if (temp->format->palette) {
  95. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
  96. } else {
  97. switch (temp->format->BitsPerPixel) {
  98. case 15:
  99. SDL_SetColorKey(temp, SDL_TRUE,
  100. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  101. break;
  102. case 16:
  103. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  104. break;
  105. case 24:
  106. SDL_SetColorKey(temp, SDL_TRUE,
  107. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  108. break;
  109. case 32:
  110. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  111. break;
  112. }
  113. }
  114. }
  115. if (width_out != NULL) {
  116. *width_out = temp->w;
  117. }
  118. if (height_out != NULL) {
  119. *height_out = temp->h;
  120. }
  121. texture = SDL_CreateTextureFromSurface(renderer, temp);
  122. if (!texture) {
  123. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  124. }
  125. }
  126. if (temp) {
  127. SDL_FreeSurface(temp);
  128. }
  129. if (path) {
  130. SDL_free(path);
  131. }
  132. return texture;
  133. }