textureloader.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef TEXTURELOADER_H
  19. #define TEXTURELOADER_H
  20. #if defined(_MSC_VER)
  21. #pragma once
  22. #endif
  23. #include "always.h"
  24. #include "texture.h"
  25. class StringClass;
  26. struct IDirect3DTexture8;
  27. class TextureLoadTaskClass;
  28. class TextureLoader
  29. {
  30. static void Init_Load_Task(TextureClass* tc);
  31. static bool Load_Uncompressed_Mipmap_Levels_From_TGA(TextureLoadTaskClass* texture);
  32. public:
  33. static void Init();
  34. static void Deinit();
  35. // Modify given texture size to nearest valid size on current hardware.
  36. static void Validate_Texture_Size(unsigned& width, unsigned& height);
  37. // Adds a loading task to the system. The task if processed in a separate
  38. // thread as soon as possible. The task will appear in finished tasks list
  39. // when it's been completed. The texture will be refreshed on the next
  40. // update call after appearing to the finished tasks list.
  41. static void Add_Load_Task(TextureClass* tc);
  42. static IDirect3DTexture8* Load_Thumbnail(
  43. const StringClass& filename,
  44. WW3DFormat texture_format); // Pass WW3D_FORMAT_UNKNOWN if you don't care
  45. static void Load_Mipmap_Levels(TextureLoadTaskClass* texture);
  46. static IDirect3DSurface8* Load_Surface_Immediate(
  47. const StringClass& filename,
  48. WW3DFormat surface_format, // Pass WW3D_FORMAT_UNKNOWN if you don't care
  49. bool allow_compression);
  50. // Textures can only be created and locked by the main thread so this function sends a request to the texture
  51. // handling system to load the texture immediatelly next time it enters the main thread. If this function
  52. // is called from the main thread the texture is loaded immediatelly.
  53. static void Request_High_Priority_Loading(
  54. TextureClass* texture,
  55. TextureClass::MipCountType mip_level_count);
  56. static void Request_Thumbnail(TextureClass* tc);
  57. static void Update();
  58. static void Flush_Pending_Load_Tasks();
  59. static IDirect3DTexture8* Generate_Bumpmap(TextureClass* texture);
  60. };
  61. // ----------------------------------------------------------------------------
  62. //
  63. // Texture loader task handler
  64. //
  65. // ----------------------------------------------------------------------------
  66. class TextureLoadTaskClass : public W3DMPO
  67. {
  68. W3DMPO_GLUE(TextureLoadTaskClass)
  69. static TextureLoadTaskClass* FreeTaskListHead;
  70. TextureClass* Texture;
  71. IDirect3DTexture8 *D3DTexture;
  72. unsigned Width;
  73. unsigned Height;
  74. WW3DFormat Format;
  75. unsigned char* LockedSurfacePtr[TextureClass::MIP_LEVELS_MAX];
  76. unsigned LockedSurfacePitch[TextureClass::MIP_LEVELS_MAX];
  77. unsigned MipLevelCount;
  78. unsigned Reduction;
  79. TextureLoadTaskClass* Succ;
  80. bool IsLoading;
  81. bool HasFailed;
  82. bool HighPriorityRequested;
  83. ~TextureLoadTaskClass();
  84. TextureLoadTaskClass();
  85. public:
  86. static TextureLoadTaskClass* Get_Instance(TextureClass* tc, bool high_priority);
  87. static void Release_Instance(TextureLoadTaskClass* task);
  88. static void shutdown(void) {TextureLoadTaskClass *pT; while (FreeTaskListHead) {pT = FreeTaskListHead; FreeTaskListHead = pT->Peek_Succ(); pT->Set_Succ(NULL); delete pT;} };
  89. void Init(TextureClass* tc,bool high_priority);
  90. void Deinit();
  91. unsigned Get_Mip_Level_Count() const { return MipLevelCount; }
  92. unsigned Get_Width() const { return Width; }
  93. unsigned Get_Height() const { return Height; }
  94. WW3DFormat Get_Format() const { return Format; }
  95. unsigned Get_Reduction() const { return Reduction; }
  96. unsigned char* Get_Locked_Surface_Ptr(unsigned level);
  97. unsigned Get_Locked_Surface_Pitch(unsigned level) const;
  98. bool Has_Failed() const { return HasFailed; }
  99. void Set_Fail(bool b) { HasFailed=b; }
  100. // Init the task or put it to a deferred init list if called from outside the main thread
  101. void Begin_Texture_Load();
  102. void Begin_Thumbnail_Load();
  103. void End_Load(); // Deinit must be called before Applying()
  104. void Apply(bool initialize);
  105. TextureLoadTaskClass* Peek_Succ() { return Succ; }
  106. void Set_Succ(TextureLoadTaskClass* succ);
  107. TextureClass* Peek_Texture() { return Texture; }
  108. IDirect3DTexture8* Peek_D3D_Texture() { return D3DTexture; }
  109. void Set_D3D_Texture(IDirect3DTexture8* texture);
  110. };
  111. #endif