1 | /******************************************************************************************* |
2 | * |
3 | * raylib [textures] example - Procedural images generation |
4 | * |
5 | * This example has been created using raylib 1.8 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Copyright (c) 2O17 Wilhem Barbier (@nounoursheureux) |
9 | * |
10 | ********************************************************************************************/ |
11 | |
12 | #include "raylib.h" |
13 | |
14 | #define NUM_TEXTURES 7 // Currently we have 7 generation algorithms |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation" ); |
24 | |
25 | Image verticalGradient = GenImageGradientV(screenWidth, screenHeight, RED, BLUE); |
26 | Image horizontalGradient = GenImageGradientH(screenWidth, screenHeight, RED, BLUE); |
27 | Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, WHITE, BLACK); |
28 | Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE); |
29 | Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f); |
30 | Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 50, 50, 4.0f); |
31 | Image cellular = GenImageCellular(screenWidth, screenHeight, 32); |
32 | |
33 | Texture2D textures[NUM_TEXTURES] = { 0 }; |
34 | |
35 | textures[0] = LoadTextureFromImage(verticalGradient); |
36 | textures[1] = LoadTextureFromImage(horizontalGradient); |
37 | textures[2] = LoadTextureFromImage(radialGradient); |
38 | textures[3] = LoadTextureFromImage(checked); |
39 | textures[4] = LoadTextureFromImage(whiteNoise); |
40 | textures[5] = LoadTextureFromImage(perlinNoise); |
41 | textures[6] = LoadTextureFromImage(cellular); |
42 | |
43 | // Unload image data (CPU RAM) |
44 | UnloadImage(verticalGradient); |
45 | UnloadImage(horizontalGradient); |
46 | UnloadImage(radialGradient); |
47 | UnloadImage(checked); |
48 | UnloadImage(whiteNoise); |
49 | UnloadImage(perlinNoise); |
50 | UnloadImage(cellular); |
51 | |
52 | int currentTexture = 0; |
53 | |
54 | SetTargetFPS(60); |
55 | //--------------------------------------------------------------------------------------- |
56 | |
57 | // Main game loop |
58 | while (!WindowShouldClose()) |
59 | { |
60 | // Update |
61 | //---------------------------------------------------------------------------------- |
62 | if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsKeyPressed(KEY_RIGHT)) |
63 | { |
64 | currentTexture = (currentTexture + 1)%NUM_TEXTURES; // Cycle between the textures |
65 | } |
66 | //---------------------------------------------------------------------------------- |
67 | |
68 | // Draw |
69 | //---------------------------------------------------------------------------------- |
70 | BeginDrawing(); |
71 | |
72 | ClearBackground(RAYWHITE); |
73 | |
74 | DrawTexture(textures[currentTexture], 0, 0, WHITE); |
75 | |
76 | DrawRectangle(30, 400, 325, 30, Fade(SKYBLUE, 0.5f)); |
77 | DrawRectangleLines(30, 400, 325, 30, Fade(WHITE, 0.5f)); |
78 | DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES" , 40, 410, 10, WHITE); |
79 | |
80 | switch(currentTexture) |
81 | { |
82 | case 0: DrawText("VERTICAL GRADIENT" , 560, 10, 20, RAYWHITE); break; |
83 | case 1: DrawText("HORIZONTAL GRADIENT" , 540, 10, 20, RAYWHITE); break; |
84 | case 2: DrawText("RADIAL GRADIENT" , 580, 10, 20, LIGHTGRAY); break; |
85 | case 3: DrawText("CHECKED" , 680, 10, 20, RAYWHITE); break; |
86 | case 4: DrawText("WHITE NOISE" , 640, 10, 20, RED); break; |
87 | case 5: DrawText("PERLIN NOISE" , 630, 10, 20, RAYWHITE); break; |
88 | case 6: DrawText("CELLULAR" , 670, 10, 20, RAYWHITE); break; |
89 | default: break; |
90 | } |
91 | |
92 | EndDrawing(); |
93 | //---------------------------------------------------------------------------------- |
94 | } |
95 | |
96 | // De-Initialization |
97 | //-------------------------------------------------------------------------------------- |
98 | |
99 | // Unload textures data (GPU VRAM) |
100 | for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]); |
101 | |
102 | CloseWindow(); // Close window and OpenGL context |
103 | //-------------------------------------------------------------------------------------- |
104 | |
105 | return 0; |
106 | } |
107 | |