1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- //
- // Rainbows (backgrounds)
- //
- #include "raylib.h"
- #include <math.h>
- int main(void)
- {
- // Initialization
- //--------------------------------------------------------------------------------------
- const int screenWidth = 800;
- const int screenHeight = 450;
- InitWindow(screenWidth, screenHeight, "raylib example.");
-
- SetTargetFPS(60); // Set our game to run at 60 frames-per-second
- //--------------------------------------------------------------------------------------
- float debug=0;
- Color col1 = (Color){100,0 ,200,255};
- Color col2 = (Color){255,0 ,100,255};
- float size=1;
- int fresh=0;
- // Main game loop
- while (!WindowShouldClose()) // Detect window close button or ESC key
- {
- // Update
- //----------------------------------------------------------------------------------
- fresh++;
- if(IsKeyPressed(KEY_SPACE)==true || fresh>30){
- fresh=0;
- col1 = (Color){GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(0,255),255};
- col2 = (Color){GetRandomValue(0,255),GetRandomValue(0,255),GetRandomValue(0,255),255};
- size = GetRandomValue(1,32);
- }
- // Find the step value between the colors so it flows from color to color.
- // We divide it by the screenheight. screenheight is divided by the bar size of each color.
- float stepr = abs(col1.r-col2.r)/(float)(screenHeight/size);
- float stepg = abs(col1.g-col2.g)/(float)(screenHeight/size);
- float stepb = abs(col1.b-col2.b)/(float)(screenHeight/size);
- // Make sure the color goes from one value to another(negatives/positives and step values.)
- if(col1.r>col2.r)stepr=-stepr;
- if(col1.g>col2.g)stepg=-stepg;
- if(col1.b>col2.b)stepb=-stepb;
-
-
- //----------------------------------------------------------------------------------
- // Draw
- //----------------------------------------------------------------------------------
- BeginDrawing();
- ClearBackground(RAYWHITE);
-
- // Here we draw the rainbow...
- Color col=col1;
- float r=col.r;
- float g=col.g;
- float b=col.b;
- int cnt=0;
- for(int i=0;i<screenHeight;i++){
- // Draw a line.
- DrawLine(0,i,screenWidth,i,col);
- // This is for the bar height...
- if(cnt>=size){
- r+=stepr;
- g+=stepg;
- b+=stepb;
- col.r = r;
- col.g = g;
- col.b = b;
- cnt=0;
- }
- cnt++;
- }
-
- DrawText("Rainbow Backgrounds..",0,0,20,WHITE);
- EndDrawing();
- //----------------------------------------------------------------------------------
- }
- // De-Initialization
- //--------------------------------------------------------------------------------------
- CloseWindow(); // Close window and OpenGL context
- //--------------------------------------------------------------------------------------
- return 0;
- }
|