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