textures_formats_loading.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - texture formats loading (compressed and uncompressed)
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3+ or ES2 versions for compressed textures,
  6. * OpenGL 1.1 does not support compressed textures, only uncompressed ones.
  7. *
  8. * This example has been created using raylib 1.3 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #define NUM_TEXTURES 24
  16. typedef enum {
  17. PNG_R8G8B8A8 = 0,
  18. PVR_GRAYSCALE,
  19. PVR_GRAY_ALPHA,
  20. PVR_R5G6B5,
  21. PVR_R5G5B5A1,
  22. PVR_R4G4B4A4,
  23. DDS_R5G6B5,
  24. DDS_R5G5B5A1,
  25. DDS_R4G4B4A4,
  26. DDS_R8G8B8A8,
  27. DDS_DXT1_RGB,
  28. DDS_DXT1_RGBA,
  29. DDS_DXT3_RGBA,
  30. DDS_DXT5_RGBA,
  31. PKM_ETC1_RGB,
  32. PKM_ETC2_RGB,
  33. PKM_ETC2_EAC_RGBA,
  34. KTX_ETC1_RGB,
  35. KTX_ETC2_RGB,
  36. KTX_ETC2_EAC_RGBA,
  37. ASTC_4x4_LDR,
  38. ASTC_8x8_LDR,
  39. PVR_PVRT_RGB,
  40. PVR_PVRT_RGBA
  41. } TextureFormats;
  42. static const char *formatText[] = {
  43. "PNG_R8G8B8A8",
  44. "PVR_GRAYSCALE",
  45. "PVR_GRAY_ALPHA",
  46. "PVR_R5G6B5",
  47. "PVR_R5G5B5A1",
  48. "PVR_R4G4B4A4",
  49. "DDS_R5G6B5",
  50. "DDS_R5G5B5A1",
  51. "DDS_R4G4B4A4",
  52. "DDS_R8G8B8A8",
  53. "DDS_DXT1_RGB",
  54. "DDS_DXT1_RGBA",
  55. "DDS_DXT3_RGBA",
  56. "DDS_DXT5_RGBA",
  57. "PKM_ETC1_RGB",
  58. "PKM_ETC2_RGB",
  59. "PKM_ETC2_EAC_RGBA",
  60. "KTX_ETC1_RGB",
  61. "KTX_ETC2_RGB",
  62. "KTX_ETC2_EAC_RGBA",
  63. "ASTC_4x4_LDR",
  64. "ASTC_8x8_LDR",
  65. "PVR_PVRT_RGB",
  66. "PVR_PVRT_RGBA"
  67. };
  68. int main()
  69. {
  70. // Initialization
  71. //--------------------------------------------------------------------------------------
  72. int screenWidth = 800;
  73. int screenHeight = 450;
  74. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture formats loading");
  75. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  76. Texture2D sonic[NUM_TEXTURES];
  77. sonic[PNG_R8G8B8A8] = LoadTexture("resources/texture_formats/sonic.png");
  78. // Load UNCOMPRESSED PVR texture data
  79. sonic[PVR_GRAYSCALE] = LoadTexture("resources/texture_formats/sonic_GRAYSCALE.pvr");
  80. sonic[PVR_GRAY_ALPHA] = LoadTexture("resources/texture_formats/sonic_L8A8.pvr");
  81. sonic[PVR_R5G6B5] = LoadTexture("resources/texture_formats/sonic_R5G6B5.pvr");
  82. sonic[PVR_R5G5B5A1] = LoadTexture("resources/texture_formats/sonic_R5G5B5A1.pvr");
  83. sonic[PVR_R4G4B4A4] = LoadTexture("resources/texture_formats/sonic_R4G4B4A4.pvr");
  84. // Load UNCOMPRESSED DDS texture data
  85. sonic[DDS_R5G6B5] = LoadTexture("resources/texture_formats/sonic_R5G6B5.dds");
  86. sonic[DDS_R5G5B5A1] = LoadTexture("resources/texture_formats/sonic_A1R5G5B5.dds");
  87. sonic[DDS_R4G4B4A4] = LoadTexture("resources/texture_formats/sonic_A4R4G4B4.dds");
  88. sonic[DDS_R8G8B8A8] = LoadTexture("resources/texture_formats/sonic_A8R8G8B8.dds");
  89. // Load COMPRESSED DXT DDS texture data (if supported)
  90. sonic[DDS_DXT1_RGB] = LoadTexture("resources/texture_formats/sonic_DXT1_RGB.dds");
  91. sonic[DDS_DXT1_RGBA] = LoadTexture("resources/texture_formats/sonic_DXT1_RGBA.dds");
  92. sonic[DDS_DXT3_RGBA] = LoadTexture("resources/texture_formats/sonic_DXT3_RGBA.dds");
  93. sonic[DDS_DXT5_RGBA] = LoadTexture("resources/texture_formats/sonic_DXT5_RGBA.dds");
  94. // Load COMPRESSED ETC texture data (if supported)
  95. sonic[PKM_ETC1_RGB] = LoadTexture("resources/texture_formats/sonic_ETC1_RGB.pkm");
  96. sonic[PKM_ETC2_RGB] = LoadTexture("resources/texture_formats/sonic_ETC2_RGB.pkm");
  97. sonic[PKM_ETC2_EAC_RGBA] = LoadTexture("resources/texture_formats/sonic_ETC2_EAC_RGBA.pkm");
  98. sonic[KTX_ETC1_RGB] = LoadTexture("resources/texture_formats/sonic_ETC1_RGB.ktx");
  99. sonic[KTX_ETC2_RGB] = LoadTexture("resources/texture_formats/sonic_ETC2_RGB.ktx");
  100. sonic[KTX_ETC2_EAC_RGBA] = LoadTexture("resources/texture_formats/sonic_ETC2_EAC_RGBA.ktx");
  101. // Load COMPRESSED ASTC texture data (if supported)
  102. sonic[ASTC_4x4_LDR] = LoadTexture("resources/texture_formats/sonic_ASTC_4x4_ldr.astc");
  103. sonic[ASTC_8x8_LDR] = LoadTexture("resources/texture_formats/sonic_ASTC_8x8_ldr.astc");
  104. // Load COMPRESSED PVR texture data (if supported)
  105. sonic[PVR_PVRT_RGB] = LoadTexture("resources/texture_formats/sonic_PVRT_RGB.pvr");
  106. sonic[PVR_PVRT_RGBA] = LoadTexture("resources/texture_formats/sonic_PVRT_RGBA.pvr");
  107. int selectedFormat = PNG_R8G8B8A8;
  108. Rectangle selectRecs[NUM_TEXTURES];
  109. for (int i = 0; i < NUM_TEXTURES; i++)
  110. {
  111. if (i < NUM_TEXTURES/2) selectRecs[i] = (Rectangle){ 40, 30 + 32*i, 150, 30 };
  112. else selectRecs[i] = (Rectangle){ 40 + 152, 30 + 32*(i - NUM_TEXTURES/2), 150, 30 };
  113. }
  114. // Texture sizes in KB
  115. float textureSizes[NUM_TEXTURES] = {
  116. 512*512*32/8/1024, //PNG_R8G8B8A8 (32 bpp)
  117. 512*512*8/8/1024, //PVR_GRAYSCALE (8 bpp)
  118. 512*512*16/8/1024, //PVR_GRAY_ALPHA (16 bpp)
  119. 512*512*16/8/1024, //PVR_R5G6B5 (16 bpp)
  120. 512*512*16/8/1024, //PVR_R5G5B5A1 (16 bpp)
  121. 512*512*16/8/1024, //PVR_R4G4B4A4 (16 bpp)
  122. 512*512*16/8/1024, //DDS_R5G6B5 (16 bpp)
  123. 512*512*16/8/1024, //DDS_R5G5B5A1 (16 bpp)
  124. 512*512*16/8/1024, //DDS_R4G4B4A4 (16 bpp)
  125. 512*512*32/8/1024, //DDS_R8G8B8A8 (32 bpp)
  126. 512*512*4/8/1024, //DDS_DXT1_RGB (4 bpp) -Compressed-
  127. 512*512*4/8/1024, //DDS_DXT1_RGBA (4 bpp) -Compressed-
  128. 512*512*8/8/1024, //DDS_DXT3_RGBA (8 bpp) -Compressed-
  129. 512*512*8/8/1024, //DDS_DXT5_RGBA (8 bpp) -Compressed-
  130. 512*512*4/8/1024, //PKM_ETC1_RGB (4 bpp) -Compressed-
  131. 512*512*4/8/1024, //PKM_ETC2_RGB (4 bpp) -Compressed-
  132. 512*512*8/8/1024, //PKM_ETC2_EAC_RGBA (8 bpp) -Compressed-
  133. 512*512*4/8/1024, //KTX_ETC1_RGB (4 bpp) -Compressed-
  134. 512*512*4/8/1024, //KTX_ETC2_RGB (4 bpp) -Compressed-
  135. 512*512*8/8/1024, //KTX_ETC2_EAC_RGBA (8 bpp) -Compressed-
  136. 512*512*8/8/1024, //ASTC_4x4_LDR (8 bpp) -Compressed-
  137. 512*512*2/8/1024, //ASTC_8x8_LDR (2 bpp) -Compressed-
  138. 512*512*4/8/1024, //PVR_PVRT_RGB (4 bpp) -Compressed-
  139. 512*512*4/8/1024, //PVR_PVRT_RGBA (4 bpp) -Compressed-
  140. };
  141. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  142. //---------------------------------------------------------------------------------------
  143. // Main game loop
  144. while (!WindowShouldClose()) // Detect window close button or ESC key
  145. {
  146. // Update
  147. //----------------------------------------------------------------------------------
  148. if (IsKeyPressed(KEY_DOWN))
  149. {
  150. selectedFormat++;
  151. if (selectedFormat >= NUM_TEXTURES) selectedFormat = 0;
  152. }
  153. else if (IsKeyPressed(KEY_UP))
  154. {
  155. selectedFormat--;
  156. if (selectedFormat < 0) selectedFormat = NUM_TEXTURES - 1;
  157. }
  158. else if (IsKeyPressed(KEY_RIGHT))
  159. {
  160. if (selectedFormat < NUM_TEXTURES/2) selectedFormat += NUM_TEXTURES/2;
  161. }
  162. else if (IsKeyPressed(KEY_LEFT))
  163. {
  164. if (selectedFormat >= NUM_TEXTURES/2) selectedFormat -= NUM_TEXTURES/2;
  165. }
  166. //----------------------------------------------------------------------------------
  167. // Draw
  168. //----------------------------------------------------------------------------------
  169. BeginDrawing();
  170. ClearBackground(RAYWHITE);
  171. // Draw rectangles
  172. for (int i = 0; i < NUM_TEXTURES; i++)
  173. {
  174. if (i == selectedFormat)
  175. {
  176. DrawRectangleRec(selectRecs[i], SKYBLUE);
  177. DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, BLUE);
  178. DrawText(formatText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(formatText[i], 10)/2, selectRecs[i].y + 11, 10, DARKBLUE);
  179. }
  180. else
  181. {
  182. DrawRectangleRec(selectRecs[i], LIGHTGRAY);
  183. DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, GRAY);
  184. DrawText(formatText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(formatText[i], 10)/2, selectRecs[i].y + 11, 10, DARKGRAY);
  185. }
  186. }
  187. // Draw selected texture
  188. if (sonic[selectedFormat].id != 0)
  189. {
  190. DrawTexture(sonic[selectedFormat], 350, -10, WHITE);
  191. }
  192. else
  193. {
  194. DrawRectangleLines(488, 165, 200, 110, DARKGRAY);
  195. DrawText("FORMAT", 550, 180, 20, MAROON);
  196. DrawText("NOT SUPPORTED", 500, 210, 20, MAROON);
  197. DrawText("ON YOUR GPU", 520, 240, 20, MAROON);
  198. }
  199. DrawText("Select texture format (use cursor keys):", 40, 10, 10, DARKGRAY);
  200. DrawText("Required GPU memory size (VRAM):", 40, 427, 10, DARKGRAY);
  201. DrawText(FormatText("%4.0f KB", textureSizes[selectedFormat]), 240, 420, 20, DARKBLUE);
  202. EndDrawing();
  203. //----------------------------------------------------------------------------------
  204. }
  205. // De-Initialization
  206. //--------------------------------------------------------------------------------------
  207. for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(sonic[i]);
  208. CloseWindow(); // Close window and OpenGL context
  209. //--------------------------------------------------------------------------------------
  210. return 0;
  211. }