-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.swift
118 lines (98 loc) · 2.96 KB
/
App.swift
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
import RaylibKit
@main struct PlayingSoundsExample: Applet {
var circles: [CircleWave]
let music: Music
var pause = false
var pitch: Float = 1
var progress: Float = 0
init() throws {
//TODO: Review View/Window flags
ConfigurationFlags.msaa4x.configure()
Window.create(800, by: 450, title: "Example - Audio - Module Playing")
Application.target(fps: 60)
AudioDevice.initialize()
circles = []
circles.reserveCapacity(CircleWave.max)
for _ in 0..<CircleWave.max {
circles.append(.random())
}
music = try Music(at: "mini1111.xm", bundle: .module)
music.isLooping = false;
music.play()
}
mutating func update() {
music.update()
// restart music
if Keyboard.space.isPressed {
music.restart()
pause = false
}
// pause/resume music
if Keyboard.p.isPressed {
pause = !pause;
music.paused(is: pause)
}
if Keyboard.down.isDown {
pitch -= 0.01
} else if Keyboard.up.isDown {
pitch += 0.01
}
music.set(pitch: pitch)
// scale the time played to the bar's dimensions
progress = (music.played / music.length) * (Window.width - 40).toFloat
// animate the color circles
if !pause {
for i in circles.indices {
var wave = circles[i]
wave.alpha += wave.speed
wave.circle.radius += wave.speed * 10
if wave.alpha > 1 {
wave.speed *= -1
} else if wave.alpha <= 0 {
wave = .random()
}
circles[i] = wave
}
}
}
func draw() {
for wave in circles {
Renderer2D.circle(wave.circle, color: wave.color.faded(to: wave.alpha))
}
// draw the progress bar
let y = Window.height - 20 - 12
Renderer2D.rectangle(at: 20, y, size: Window.width - 40, 12, color: .lightGray)
Renderer2D.rectangle(at: 20, y, size: progress.toInt, 12, color: .maroon)
WireRenderer2D.rectangle(at: 20, y, size: Window.width - 40, 12, color: .gray)
// Draw help instructions
Renderer2D.rectangle(at: 20, 20, size: 425, 145, color: .white)
WireRenderer2D.rectangle(at: 20, 20, size: 425, 145, color: .gray)
Renderer.pointSize = 20
Renderer2D.text("PRESS SPACE TO RESTART MUSIC", at: 40, 40)
Renderer2D.text("PRESS P TO PAUSE/RESUME", at: 40, 70)
Renderer2D.text("PRESS UP/DOWN TO CHANGE SPEED", at: 40, 100)
Renderer2D.text("SPEED: \(pitch, decimals: 2)", at: 40, 130, color: .maroon)
}
func unload() {
AudioDevice.close()
}
}
struct CircleWave {
static let max = 64
static let colors = [Color.orange, .red, .gold, .lime, .blue, .violet, .brown, .lightGray, .pink, .yellow, .green, .skyBlue, .purple, .beige]
var circle: Circle
var alpha: Float = 0
var speed: Float
var color: Color
static func random() -> CircleWave {
let radius = Random.between(10, and: 40)
let x = Random.between(radius, and: Window.width)
let y = Random.between(radius, and: Window.height)
let speed = Random.between(1, and: 100).toFloat / 2000
let color = Random.element(in: CircleWave.colors)
return CircleWave(
circle: Circle(at: x.toFloat, y.toFloat, radius: radius.toFloat),
speed: speed,
color: color)
}
}