Example_-_Rainbow_Backgrounds.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Rainbows (backgrounds)
  3. //
  4. #include "raylib.h"
  5. #include <math.h>
  6. int main(void)
  7. {
  8. // Initialization
  9. //--------------------------------------------------------------------------------------
  10. const int screenWidth = 800;
  11. const int screenHeight = 450;
  12. InitWindow(screenWidth, screenHeight, "raylib example.");
  13. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  14. //--------------------------------------------------------------------------------------
  15. float debug=0;
  16. Color col1 = (Color){100,0 ,200,255};
  17. Color col2 = (Color){255,0 ,100,255};
  18. float size=1;
  19. int fresh=0;
  20. // Main game loop
  21. while (!WindowShouldClose()) // Detect window close button or ESC key
  22. {
  23. // Update
  24. //----------------------------------------------------------------------------------
  25. fresh++;
  26. if(IsKeyPressed(KEY_SPACE)==true || fresh>30){
  27. fresh=0;
  28. col1 = (Color){GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(0,255),255};
  29. col2 = (Color){GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(0,255),255};
  30. size = GetRandomValue(1,32);
  31. }
  32. // Find the step value between the colors so it flows from color to color.
  33. // We divide it by the screenheight. screenheight is divided by the bar size of each color.
  34. float stepr = abs(col1.r-col2.r)/(float)(screenHeight/size);
  35. float stepg = abs(col1.g-col2.g)/(float)(screenHeight/size);
  36. float stepb = abs(col1.b-col2.b)/(float)(screenHeight/size);
  37. // Make sure the color goes from one value to another(negatives/positives and step values.)
  38. if(col1.r>col2.r)stepr=-stepr;
  39. if(col1.g>col2.g)stepg=-stepg;
  40. if(col1.b>col2.b)stepb=-stepb;
  41. //----------------------------------------------------------------------------------
  42. // Draw
  43. //----------------------------------------------------------------------------------
  44. BeginDrawing();
  45. ClearBackground(RAYWHITE);
  46. // Here we draw the rainbow...
  47. Color col=col1;
  48. float r=col.r;
  49. float g=col.g;
  50. float b=col.b;
  51. int cnt=0;
  52. for(int i=0;i<screenHeight;i++){
  53. // Draw a line.
  54. DrawLine(0,i,screenWidth,i,col);
  55. // This is for the bar height...
  56. if(cnt>=size){
  57. r+=stepr;
  58. g+=stepg;
  59. b+=stepb;
  60. col.r = r;
  61. col.g = g;
  62. col.b = b;
  63. cnt=0;
  64. }
  65. cnt++;
  66. }
  67. DrawText("Rainbow Backgrounds..",0,0,20,WHITE);
  68. EndDrawing();
  69. //----------------------------------------------------------------------------------
  70. }
  71. // De-Initialization
  72. //--------------------------------------------------------------------------------------
  73. CloseWindow(); // Close window and OpenGL context
  74. //--------------------------------------------------------------------------------------
  75. return 0;
  76. }