Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

119 linhas
5.2 KiB

  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-2024 Wilhem Barbier (@nounoursheureux) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define NUM_TEXTURES 9 // Currently we have 8 generation algorithms but some have multiple purposes (Linear and Square Gradients)
  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 = GenImageGradientLinear(screenWidth, screenHeight, 0, RED, BLUE);
  26. Image horizontalGradient = GenImageGradientLinear(screenWidth, screenHeight, 90, RED, BLUE);
  27. Image diagonalGradient = GenImageGradientLinear(screenWidth, screenHeight, 45, RED, BLUE);
  28. Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, WHITE, BLACK);
  29. Image squareGradient = GenImageGradientSquare(screenWidth, screenHeight, 0.0f, WHITE, BLACK);
  30. Image checked = GenImageChecked(screenWidth, screenHeight, 32, 32, RED, BLUE);
  31. Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f);
  32. Image perlinNoise = GenImagePerlinNoise(screenWidth, screenHeight, 50, 50, 4.0f);
  33. Image cellular = GenImageCellular(screenWidth, screenHeight, 32);
  34. Texture2D textures[NUM_TEXTURES] = { 0 };
  35. textures[0] = LoadTextureFromImage(verticalGradient);
  36. textures[1] = LoadTextureFromImage(horizontalGradient);
  37. textures[2] = LoadTextureFromImage(diagonalGradient);
  38. textures[3] = LoadTextureFromImage(radialGradient);
  39. textures[4] = LoadTextureFromImage(squareGradient);
  40. textures[5] = LoadTextureFromImage(checked);
  41. textures[6] = LoadTextureFromImage(whiteNoise);
  42. textures[7] = LoadTextureFromImage(perlinNoise);
  43. textures[8] = LoadTextureFromImage(cellular);
  44. // Unload image data (CPU RAM)
  45. UnloadImage(verticalGradient);
  46. UnloadImage(horizontalGradient);
  47. UnloadImage(diagonalGradient);
  48. UnloadImage(radialGradient);
  49. UnloadImage(squareGradient);
  50. UnloadImage(checked);
  51. UnloadImage(whiteNoise);
  52. UnloadImage(perlinNoise);
  53. UnloadImage(cellular);
  54. int currentTexture = 0;
  55. SetTargetFPS(60);
  56. //---------------------------------------------------------------------------------------
  57. // Main game loop
  58. while (!WindowShouldClose())
  59. {
  60. // Update
  61. //----------------------------------------------------------------------------------
  62. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_RIGHT))
  63. {
  64. currentTexture = (currentTexture + 1)%NUM_TEXTURES; // Cycle between the textures
  65. }
  66. //----------------------------------------------------------------------------------
  67. // Draw
  68. //----------------------------------------------------------------------------------
  69. BeginDrawing();
  70. ClearBackground(RAYWHITE);
  71. DrawTexture(textures[currentTexture], 0, 0, WHITE);
  72. DrawRectangle(30, 400, 325, 30, Fade(SKYBLUE, 0.5f));
  73. DrawRectangleLines(30, 400, 325, 30, Fade(WHITE, 0.5f));
  74. DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, WHITE);
  75. switch(currentTexture)
  76. {
  77. case 0: DrawText("VERTICAL GRADIENT", 560, 10, 20, RAYWHITE); break;
  78. case 1: DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE); break;
  79. case 2: DrawText("DIAGONAL GRADIENT", 540, 10, 20, RAYWHITE); break;
  80. case 3: DrawText("RADIAL GRADIENT", 580, 10, 20, LIGHTGRAY); break;
  81. case 4: DrawText("SQUARE GRADIENT", 580, 10, 20, LIGHTGRAY); break;
  82. case 5: DrawText("CHECKED", 680, 10, 20, RAYWHITE); break;
  83. case 6: DrawText("WHITE NOISE", 640, 10, 20, RED); break;
  84. case 7: DrawText("PERLIN NOISE", 640, 10, 20, RED); break;
  85. case 8: DrawText("CELLULAR", 670, 10, 20, RAYWHITE); break;
  86. default: break;
  87. }
  88. EndDrawing();
  89. //----------------------------------------------------------------------------------
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. // Unload textures data (GPU VRAM)
  94. for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]);
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }