🛠️🐜 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.

158 lines
4.2 KiB

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