textures_formats_loading.lua 9.0 KB

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