-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.cpp
277 lines (227 loc) · 6.03 KB
/
old.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <raylib.h>
#include <raymath.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <math.h>
#define RAYGUI_IMPLEMENTATION
#include <raygui.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
struct Note
{
Sound sound;
Rectangle rec;
Color color;
bool played = false;
};
std::vector<Note> notes;
const int WIDTH = 800;
const int HEIGHT = 450;
bool playing = false;
Rectangle playing_line = {50, 50, 5, HEIGHT-50};
int line_delay = 0;
void UpdateDrawFrame();
Camera2D camera = { 0 };
const int CUBE_SIZE = 50;
Vector2 mouse_pos;
Sound beep;
struct Instrument
{
Sound sound;
std::string icon;
Rectangle row;
Color row_clr;
};
std::vector<Instrument> instruments;
std::vector<std::string> sound_icons = {
"#124#",
"#0#",
"#147#",
"#220#",
"#222#",
"#223#",
"#152#",
"#221#"
};
FilePathList sound_files;
size_t getLength(char **array) {
size_t length = 0;
while (array[length] != nullptr) {
length++;
}
return length;
}
int tempo = 30;
int main(void)
{
InitWindow(WIDTH+50, HEIGHT, "raylib [core] example - basic window");
InitAudioDevice();
GuiLoadIcons("iconset.rgi", true);
sound_files = LoadDirectoryFiles("sounds");
for (int i = 0; i < sound_files.count; i++)
{
// Wave w = LoadWave(sound_files.paths[i]);
// std::string p = std::string(sound_files.paths[i]) + ".h";
// ExportWaveAsCode(w, p.c_str());
Instrument instrument = {LoadSound(sound_files.paths[i]), sound_icons[i].c_str()};
instrument.row_clr = {
(unsigned char) GetRandomValue(0, 255),
(unsigned char) GetRandomValue(0, 255),
(unsigned char) GetRandomValue(0, 255),
255
};
instruments.push_back(instrument);
}
camera.zoom = 1.f;
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
//SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame();
}
#endif
CloseWindow();
return 0;
}
float mapValue(float mouse_pos_y, float min_value = 0.1f, float max_value = 2.0f) {
float clamped_y = std::clamp(mouse_pos_y, 0.0f, static_cast<float>(WIDTH));
return min_value + (clamped_y / WIDTH) * (max_value - min_value);
}
void Reset()
{
line_delay = 0;
playing = !playing;
for (Note& n : notes) n.played = false;
playing_line.x = CUBE_SIZE;
}
bool value_box_select = false;
void UpdateDrawFrame()
{
// camera.offset.x = Clamp(camera.offset.x, 0, INFINITY);
mouse_pos = GetScreenToWorld2D(GetMousePosition(), camera);
BeginDrawing();
ClearBackground(RAYWHITE);
std::cout << camera.offset.x << '\n';
std::string camera_view_text = "x: " + std::to_string((int)(camera.offset.x / 50)*-1) + ", y: " + std::to_string((int)(camera.offset.y / 50));
DrawText(camera_view_text.c_str(), 250, 0, 20, BLACK);
if (camera.offset.x > 0) camera.offset.x = 0;
for (int x = 0; x < WIDTH/CUBE_SIZE; x++)
{
for (int y = 0; y < HEIGHT/CUBE_SIZE; y++)
{
Rectangle rec = {50+(float)x*CUBE_SIZE, 50+(float)y*CUBE_SIZE, CUBE_SIZE, CUBE_SIZE};
DrawRectangleLinesEx(rec, 1.f, GRAY);
if (CheckCollisionPointRec(mouse_pos, rec))
{
mouse_pos = {rec.x+CUBE_SIZE/2, rec.y+CUBE_SIZE/2};
}
}
}
for (int y = 0; y < instruments.size(); y++)
{
Rectangle rect = {0, 50+(float)y*CUBE_SIZE, CUBE_SIZE, CUBE_SIZE};
instruments[y].row = {50, 50+(float)y*CUBE_SIZE, WIDTH, 50};
DrawRectangleLinesEx(instruments[y].row, 1.f, RED);
if (GuiButton(rect, instruments[y].icon.c_str()))
{
PlaySound(instruments[y].sound);
}
}
int min = 30;
int max = INFINITY;
if (GuiValueBox({0, 0, 100, 50}, "TEMPO", &tempo, min, max, value_box_select))
{
value_box_select = !value_box_select;
}
// NOTE: STATIC THINGS ARE DRAWN OUTSIDE CAMERA MODE
BeginMode2D(camera);
if (IsKeyPressed(KEY_RIGHT))
{
camera.offset.x -= 50;
}
if (IsKeyPressed(KEY_LEFT))
{
camera.offset.x += 50;
}
for (Note& note : notes)
{
DrawRectangleRec(note.rec, note.color);
}
if (!playing)
{
DrawCircleV(mouse_pos, CUBE_SIZE/2, Color{0, 0, 0, 100});
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && mouse_pos.x >= 50)
{
//SetSoundPitch(beep, mapValue(mouse_pos.y, 0.f, 2.f));
//SetAudioStreamPitch(beep.stream, 5.f);
//std::cout << mapValue(mouse_pos.y, 0.f, 2.f) << '\n';
for (Instrument& instrument : instruments)
{
if (CheckCollisionPointRec(mouse_pos, instrument.row))
{
bool on_note = false;
int note_i = NULL;
for (int i = 0; i < notes.size(); i++)
{
Note note = notes[i];
if (CheckCollisionPointRec(mouse_pos, note.rec))
{
on_note = true;
note_i = i;
}
}
if (!on_note)
{
notes.push_back(Note{
instrument.sound,
Rectangle { mouse_pos.x-CUBE_SIZE/2, mouse_pos.y-CUBE_SIZE/2, CUBE_SIZE, CUBE_SIZE },
instrument.row_clr
});
PlaySound(instrument.sound);
}
else
{
notes.erase(notes.begin() + note_i);
}
}
}
//SetAudioStreamPitch(beep.stream, 1.f);
}
}
if (playing)
{
float frames_per_beat = (GetFPS() * 60.0f) / tempo;
float seconds_per_beat = 60.0f / tempo;
float speed_per_frame = CUBE_SIZE / seconds_per_beat;
playing_line.x += speed_per_frame * GetFrameTime();
if (playing_line.x > WIDTH+CUBE_SIZE)
{
Reset();
playing = true;
}
for (Note& note : notes)
{
if (CheckCollisionRecs(note.rec, playing_line) && !note.played && playing)
{
PlaySound(note.sound);
note.played = true;
}
}
}
if (IsKeyPressed(KEY_SPACE))
{
Reset();
}
if (playing) DrawRectangleRec(playing_line, BLACK);
EndMode2D();
EndDrawing();
}