-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathExample_-_Rainbow_Backgrounds.c
92 lines (76 loc) · 3.18 KB
/
Example_-_Rainbow_Backgrounds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// 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;
}