-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraylib_app.c
242 lines (202 loc) · 9.48 KB
/
raylib_app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "raylib_app.h"
#include <stdlib.h>
void DrawUI()
{
const int padding = 10;
const int buttonWidth = 100;
const int buttonHeight = 30;
Vector2 colorButtonPos = {padding + 10, padding + 10};
DrawRectangleRec((Rectangle){colorButtonPos.x, colorButtonPos.y, buttonWidth, buttonHeight}, snowColor);
DrawText("Snow Color", colorButtonPos.x + buttonWidth / 2 - MeasureText("Snow Color", 10) / 2, colorButtonPos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 sizeButtonPos = {padding + 10, padding + 50};
DrawRectangle(sizeButtonPos.x, sizeButtonPos.y, buttonWidth, buttonHeight, GRAY);
DrawText(TextFormat("Drop Size: %02d", snowDropSize), sizeButtonPos.x + buttonWidth / 2 - MeasureText(TextFormat("Drop Size: %02d", snowDropSize), 10) / 2, sizeButtonPos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 densityButtonPos = {padding + 10, padding + 90};
DrawRectangle(densityButtonPos.x, densityButtonPos.y, buttonWidth, buttonHeight, GRAY);
DrawText(TextFormat("Density: %03d", snowDensity), densityButtonPos.x + buttonWidth / 2 - MeasureText(TextFormat("Density: %03d", snowDensity), 10) / 2, densityButtonPos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 windSpeedButtonPos = {padding + 10, padding + 130};
DrawRectangle(windSpeedButtonPos.x, windSpeedButtonPos.y, buttonWidth, buttonHeight, GRAY);
DrawText("Wind Speed", windSpeedButtonPos.x + buttonWidth / 2 - MeasureText("Wind Speed", 10) / 2, windSpeedButtonPos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 playMusicPos = {padding + 10, padding + 170};
DrawRectangle(playMusicPos.x, playMusicPos.y, buttonWidth, buttonHeight, GRAY);
DrawText("Toggle Music", playMusicPos.x + buttonWidth / 2 - MeasureText("Toggle Music", 10) / 2, playMusicPos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 uniqueColorsPos = {padding + 10, padding + 210};
DrawRectangle(uniqueColorsPos.x, uniqueColorsPos.y, buttonWidth, buttonHeight, snowColor);
DrawText("Fun Colors", uniqueColorsPos.x + buttonWidth / 2 - MeasureText("Fun Colors", 10) / 2, uniqueColorsPos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 blizzardModePos = {padding + 10, padding + 250};
DrawRectangle(blizzardModePos.x, blizzardModePos.y, buttonWidth, buttonHeight, GRAY);
DrawText("Blizzard!", blizzardModePos.x + buttonWidth / 2 - MeasureText("Blizzard!", 10) / 2, blizzardModePos.y + buttonHeight / 2 - 5, 10, BLACK);
Vector2 backgroundColorsPos = {padding + 10, padding + 290};
DrawRectangle(backgroundColorsPos.x, backgroundColorsPos.y, buttonWidth, buttonHeight, currentBackgroundColor);
DrawText("Background Color", backgroundColorsPos.x + buttonWidth / 2 - MeasureText("Background Color", 10) / 2, backgroundColorsPos.y + buttonHeight / 2 - 5, 10, RAYWHITE);
/*
Make sure this is always on the bottom of the UI.
The reset button makes sense on the bottom to me.
*/
Vector2 resetButtonPos = {padding + 10, padding + 330};
DrawRectangle(resetButtonPos.x, resetButtonPos.y, buttonWidth, buttonHeight, GRAY);
DrawText("Reset", resetButtonPos.x + buttonWidth / 2 - MeasureText("Reset", 10) / 2, resetButtonPos.y + buttonHeight / 2 - 5, 10, RAYWHITE);
}
int main(void)
{
const int screenWidth = 854;
const int screenHeight = 480;
Color backgroundColors[] = {
LIGHTGRAY, GRAY, DARKGRAY, SKYBLUE,
BROWN, MAGENTA, RAYWHITE, BEIGE, BLACK};
InitWindow(screenWidth, screenHeight, "Relaxing Snow Application");
InitAudioDevice();
Sound sound = LoadSound("mp3/christmas.mp3");
SetSoundVolume(sound, 10);
ToggleFullscreen();
Snowdrop drops[MAX_DROPS];
for (int i = 0; i < MAX_DROPS; i++)
{
drops[i].position.x = GetRandomValue(0, screenWidth);
drops[i].position.y = GetRandomValue(-screenHeight, 0);
drops[i].speed = (float)GetRandomValue(50, 100) / 100.0f;
drops[i].windSpeed = (float)GetRandomValue(-50, 50) / 100.0f;
drops[i].gravity = (float)GetRandomValue(90, 110) / 100.0f;
}
SetTargetFPS(60);
while (!WindowShouldClose())
{
for (int i = 0; i < MAX_DROPS; i++)
{
drops[i].position.y += drops[i].speed * drops[i].gravity;
drops[i].position.x += drops[i].windSpeed;
// If a snowdrop reaches the bottom, reset its position to the top
if (drops[i].position.y > screenHeight)
{
drops[i].position.y = GetRandomValue(-screenHeight, 0);
drops[i].position.x = GetRandomValue(0, screenWidth);
}
}
// user input
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Vector2 mousePos = GetMousePosition();
Rectangle colorButton = {10, 10, 75, 30};
if (CheckCollisionPointRec(mousePos, colorButton))
{
snowColor.r = (snowColor.r + 50 > 255) ? 100 : snowColor.r + 50;
snowColor.g = snowColor.r;
snowColor.b = snowColor.r;
}
Rectangle sizeButton = {10, 50, 75, 30};
if (CheckCollisionPointRec(mousePos, sizeButton))
{
snowDropSize = (snowDropSize + 2) % 20;
}
Rectangle densityButton = {10, 90, 75, 30};
if (CheckCollisionPointRec(mousePos, densityButton))
{
snowDensity = (snowDensity + 25) % (MAX_DROPS + 1);
}
Rectangle windSpeedButton = {10, 130, 75, 30};
if (CheckCollisionPointRec(mousePos, windSpeedButton))
{
for (int i = 0; i < MAX_DROPS; i++)
{
drops[i].windSpeed = (float)GetRandomValue(-100, 100) / 100.0f;
}
}
Rectangle playMusicButton = {10, 170, 75, 30};
if (CheckCollisionPointRec(mousePos, playMusicButton))
{
if (soundPlaying)
{
soundPlaying = false;
StopSound(sound);
}
else
{
soundPlaying = true;
PlaySound(sound);
}
}
Rectangle uniqueColorsPosButton = {10, 210, 75, 30};
if (CheckCollisionPointRec(mousePos, uniqueColorsPosButton))
{
int colorSize = sizeof(colors) / sizeof(colors[0]);
snowColor = colors[colorIndex];
colorIndex++;
if (colorIndex >= colorSize)
{
colorIndex = 0;
}
}
Rectangle blizzardButton = {10, 250, 75, 30};
if (CheckCollisionPointRec(mousePos, blizzardButton))
{
blizzardToggle = !blizzardToggle;
for (int i = 0; i < MAX_DROPS; i++)
{
if (blizzardToggle)
{
drops[i].speed = 1.35f;
drops[i].gravity = 1.35f;
}
else
{
drops[i].speed = (float)GetRandomValue(50, 100) / 100.0f;
drops[i].gravity = (float)GetRandomValue(90, 110) / 100.0f;
}
}
}
Rectangle backgroundColorButton = {10, 290, 75, 30};
if (CheckCollisionPointRec(mousePos, backgroundColorButton))
{
int backgroundColorSize = sizeof(backgroundColors) / sizeof(backgroundColors[0]);
backgroundIndex = (backgroundIndex + 1) % backgroundColorSize;
ClearBackground(backgroundColors[backgroundIndex]);
if (backgroundIndex >= backgroundColorSize)
{
backgroundIndex = 0;
}
}
/*
Make sure this is always on the bottom.
The second integer in the Rectangle struct
is the height!
Also, this logic needs to get expand upon whenever
there is a new feature that effects
the Snowflake polygon.
*/
Rectangle resetButton = {10, 330, 75, 30};
if (CheckCollisionPointRec(mousePos, resetButton))
{
if (soundPlaying == true)
{
StopSound(sound);
soundPlaying = false;
}
snowDropSize = 10;
snowDensity = MAX_DROPS;
snowColor = (Color){255, 255, 255, 200};
for (int i = 0; i < MAX_DROPS; i++)
{
drops[i].windSpeed = (float)GetRandomValue(-50, 50) / 100.0f;
drops[i].position.x = GetRandomValue(0, screenWidth);
drops[i].position.y = GetRandomValue(-screenHeight, 0);
drops[i].speed = (float)GetRandomValue(50, 100) / 100.0f;
drops[i].gravity = (float)GetRandomValue(90, 110) / 100.0f;
drops[i].position.x += drops[i].windSpeed;
}
}
}
// Draw
BeginDrawing();
ClearBackground(backgroundColors[backgroundIndex - 1]);
for (int i = 0; i < snowDensity; i++)
{
DrawSnowflake(drops[i].position, snowDropSize, snowColor);
}
DrawUI();
EndDrawing();
}
UnloadSound(sound);
CloseAudioDevice();
CloseWindow();
return 0;
}