textures_image_generation.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Procedural images generation
  4. *
  5. * Example originally created with raylib 1.8, last time updated with raylib 1.8
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2O17-2023 Wilhem Barbier (@nounoursheureux) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define NUM_TEXTURES 6 // Currently we have 7 generation algorithms
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation");
  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 cellular = GenImageCellular(screenWidth, screenHeight, 32);
  31. Texture2D textures[NUM_TEXTURES] = { 0 };
  32. textures[0] = LoadTextureFromImage(verticalGradient);
  33. textures[1] = LoadTextureFromImage(horizontalGradient);
  34. textures[2] = LoadTextureFromImage(radialGradient);
  35. textures[3] = LoadTextureFromImage(checked);
  36. textures[4] = LoadTextureFromImage(whiteNoise);
  37. textures[5] = LoadTextureFromImage(cellular);
  38. // Unload image data (CPU RAM)
  39. UnloadImage(verticalGradient);
  40. UnloadImage(horizontalGradient);
  41. UnloadImage(radialGradient);
  42. UnloadImage(checked);
  43. UnloadImage(whiteNoise);
  44. UnloadImage(cellular);
  45. int currentTexture = 0;
  46. SetTargetFPS(60);
  47. //---------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose())
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_RIGHT))
  54. {
  55. currentTexture = (currentTexture + 1)%NUM_TEXTURES; // Cycle between the textures
  56. }
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawTexture(textures[currentTexture], 0, 0, WHITE);
  63. DrawRectangle(30, 400, 325, 30, Fade(SKYBLUE, 0.5f));
  64. DrawRectangleLines(30, 400, 325, 30, Fade(WHITE, 0.5f));
  65. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, WHITE);
  66. switch(currentTexture)
  67. {
  68. case 0: DrawText("VERTICAL GRADIENT", 560, 10, 20, RAYWHITE); break;
  69. case 1: DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE); break;
  70. case 2: DrawText("RADIAL GRADIENT", 580, 10, 20, LIGHTGRAY); break;
  71. case 3: DrawText("CHECKED", 680, 10, 20, RAYWHITE); break;
  72. case 4: DrawText("WHITE NOISE", 640, 10, 20, RED); break;
  73. case 5: DrawText("CELLULAR", 670, 10, 20, RAYWHITE); break;
  74. default: break;
  75. }
  76. EndDrawing();
  77. //----------------------------------------------------------------------------------
  78. }
  79. // De-Initialization
  80. //--------------------------------------------------------------------------------------
  81. // Unload textures data (GPU VRAM)
  82. for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]);
  83. CloseWindow(); // Close window and OpenGL context
  84. //--------------------------------------------------------------------------------------
  85. return 0;
  86. }