Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123 rindas
5.4 KiB

pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Procedural images generation
  4. *
  5. * Example complexity rating: [] 2/4
  6. *
  7. * Example originally created with raylib 1.8, last time updated with raylib 1.8
  8. *
  9. * Example contributed by Wilhem Barbier (@nounoursheureux) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2017-2025 Wilhem Barbier (@nounoursheureux) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define NUM_TEXTURES 9 // Currently we have 8 generation algorithms but some have multiple purposes (Linear and Square Gradients)
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation");
  29. Image verticalGradient = GenImageGradientLinear(screenWidth, screenHeight, 0, RED, BLUE);
  30. Image horizontalGradient = GenImageGradientLinear(screenWidth, screenHeight, 90, RED, BLUE);
  31. Image diagonalGradient = GenImageGradientLinear(screenWidth, screenHeight, 45, RED, BLUE);
  32. Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, WHITE, BLACK);
  33. Image squareGradient = GenImageGradientSquare(screenWidth, screenHeight, 0.0f, WHITE, BLACK);
  34. Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE);
  35. Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f);
  36. Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 50, 50, 4.0f);
  37. Image cellular = GenImageCellular(screenWidth, screenHeight, 32);
  38. Texture2D textures[NUM_TEXTURES] = { 0 };
  39. textures[0] = LoadTextureFromImage(verticalGradient);
  40. textures[1] = LoadTextureFromImage(horizontalGradient);
  41. textures[2] = LoadTextureFromImage(diagonalGradient);
  42. textures[3] = LoadTextureFromImage(radialGradient);
  43. textures[4] = LoadTextureFromImage(squareGradient);
  44. textures[5] = LoadTextureFromImage(checked);
  45. textures[6] = LoadTextureFromImage(whiteNoise);
  46. textures[7] = LoadTextureFromImage(perlinNoise);
  47. textures[8] = LoadTextureFromImage(cellular);
  48. // Unload image data (CPU RAM)
  49. UnloadImage(verticalGradient);
  50. UnloadImage(horizontalGradient);
  51. UnloadImage(diagonalGradient);
  52. UnloadImage(radialGradient);
  53. UnloadImage(squareGradient);
  54. UnloadImage(checked);
  55. UnloadImage(whiteNoise);
  56. UnloadImage(perlinNoise);
  57. UnloadImage(cellular);
  58. int currentTexture = 0;
  59. SetTargetFPS(60);
  60. //---------------------------------------------------------------------------------------
  61. // Main game loop
  62. while (!WindowShouldClose())
  63. {
  64. // Update
  65. //----------------------------------------------------------------------------------
  66. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_RIGHT))
  67. {
  68. currentTexture = (currentTexture + 1)%NUM_TEXTURES; // Cycle between the textures
  69. }
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. DrawTexture(textures[currentTexture], 0, 0, WHITE);
  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. switch (currentTexture)
  80. {
  81. case 0: DrawText("VERTICAL GRADIENT", 560, 10, 20, RAYWHITE); break;
  82. case 1: DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE); break;
  83. case 2: DrawText("DIAGONAL GRADIENT", 540, 10, 20, RAYWHITE); break;
  84. case 3: DrawText("RADIAL GRADIENT", 580, 10, 20, LIGHTGRAY); break;
  85. case 4: DrawText("SQUARE GRADIENT", 580, 10, 20, LIGHTGRAY); break;
  86. case 5: DrawText("CHECKED", 680, 10, 20, RAYWHITE); break;
  87. case 6: DrawText("WHITE NOISE", 640, 10, 20, RED); break;
  88. case 7: DrawText("PERLIN NOISE", 640, 10, 20, RED); break;
  89. case 8: DrawText("CELLULAR", 670, 10, 20, RAYWHITE); break;
  90. default: break;
  91. }
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. // Unload textures data (GPU VRAM)
  98. for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]);
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }