You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.3 KiB

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