🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

151 lines
3.9 KiB

  1. /*
  2. Copyright 1997-2022 Sam Lantinga
  3. Copyright 2022 Collabora Ltd.
  4. SPDX-License-Identifier: Zlib
  5. */
  6. #include "testutils.h"
  7. /*
  8. * Return the absolute path to def in the SDL_GetBasePath() if possible, or
  9. * the relative path to def on platforms that don't have a working
  10. * SDL_GetBasePath(). Free the result with SDL_free.
  11. *
  12. * Fails and returns NULL if out of memory.
  13. */
  14. char *
  15. GetNearbyFilename(const char *file)
  16. {
  17. char *base;
  18. char *path;
  19. base = SDL_GetBasePath();
  20. if (base != NULL) {
  21. SDL_RWops *rw;
  22. size_t len = SDL_strlen(base) + SDL_strlen(file) + 1;
  23. path = SDL_malloc(len);
  24. if (path == NULL) {
  25. SDL_free(base);
  26. SDL_OutOfMemory();
  27. return NULL;
  28. }
  29. SDL_snprintf(path, len, "%s%s", base, file);
  30. SDL_free(base);
  31. rw = SDL_RWFromFile(path, "rb");
  32. if (rw) {
  33. SDL_RWclose(rw);
  34. return path;
  35. }
  36. /* Couldn't find the file in the base path */
  37. SDL_free(path);
  38. }
  39. path = SDL_strdup(file);
  40. if (path == NULL) {
  41. SDL_OutOfMemory();
  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. SDL_FreeSurface(temp);
  127. if (path) {
  128. SDL_free(path);
  129. }
  130. return texture;
  131. }