nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
sounds_main.cc
Go to the documentation of this file.
1// Each sound the game makes, to listen to one at a time, outside the game:
2// up and down pick one, and Space plays a new take of it and draws it.
3#include <raylib.h>
4
5#include <algorithm>
6#include <array>
7#include <cstddef>
8#include <cstdint>
9#include <functional>
10#include <iostream>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include "absl/status/statusor.h"
16#include "absl/strings/str_cat.h"
17#include "client/audio.h"
18#include "client/characters.h"
19#include "client/foley.h"
20#include "client/synth.h"
21#include "client/window.h"
22#include "content/content.h"
23#include "terrain/lands.h"
24
25namespace {
26
27struct Entry {
28 std::string name;
29 /// A take of the sound, from its seed.
30 std::function<nanommo::Samples(uint32_t)> make;
31};
32
33constexpr std::array<const char*, 7> kGroundNames = {
34 "grass", "sand", "rock", "snow", "ash", "stone", "water"};
35
36std::vector<Entry> Entries(const nanommo::SkillTable& skills) {
37 std::vector<Entry> entries;
38 entries.reserve(nanommo::kGrounds.size() + skills.by_id.size() + 1 +
39 nanommo::kLands.size());
40 for (const nanommo::Ground ground : nanommo::kGrounds) {
41 entries.push_back(
42 {absl::StrCat("Step on ", kGroundNames[static_cast<int>(ground)]),
43 [ground](uint32_t seed) { return nanommo::Step(ground, seed); }});
44 }
45 for (const auto& [id, skill] : skills.by_id) {
46 const nanommo::skills::Look look = skill.look();
47 // As cast at a monster, so that its fx at the target sound too.
48 entries.push_back({skill.name(), [look](uint32_t seed) {
49 return nanommo::SkillSound(look, seed, true);
50 }});
51 }
52 entries.push_back({"Going through a gate",
53 [](uint32_t seed) { return nanommo::Departure(seed); }});
54 for (int land = 0; land < static_cast<int>(nanommo::kLands.size()); ++land) {
55 entries.push_back(
56 {absl::StrCat("Arriving in the ", nanommo::kLands[land].name),
57 [land](uint32_t) { return nanommo::Arrival(land); }});
58 }
59 return entries;
60}
61
62// The sound's shape: the highest and lowest sample under each column.
63void DrawWave(const nanommo::Samples& samples, Rectangle box, Color color) {
64 DrawRectangleLinesEx(box, 1.0f, DARKGRAY);
65 const float middle = box.y + box.height / 2.0f;
66 DrawLineV({box.x, middle}, {box.x + box.width, middle}, DARKGRAY);
67 if (samples.empty()) {
68 return;
69 }
70 const int columns = static_cast<int>(box.width);
71 for (int x = 0; x < columns; ++x) {
72 const std::size_t from = samples.size() * x / columns;
73 const std::size_t to =
74 std::max(samples.size() * (x + 1) / columns, from + 1);
75 float low = 0.0f;
76 float high = 0.0f;
77 for (std::size_t i = from; i < to && i < samples.size(); ++i) {
78 low = std::min(low, samples[i]);
79 high = std::max(high, samples[i]);
80 }
81 DrawLineV({box.x + x, middle - high * box.height / 2.0f},
82 {box.x + x, middle - low * box.height / 2.0f}, color);
83 }
84}
85
86} // namespace
87
88int main() {
89 const absl::StatusOr<nanommo::Content> content =
91 if (!content.ok()) {
92 std::cerr << "nanommo sounds: " << content.status() << "\n";
93 return 1;
94 }
95 absl::StatusOr<std::unique_ptr<nanommo::Window>> window =
96 nanommo::Window::Create("nanoMMO sounds", 960, 540);
97 if (!window.ok()) {
98 std::cerr << "nanommo sounds: " << window.status() << "\n";
99 return 1;
100 }
101 absl::StatusOr<std::unique_ptr<nanommo::AudioDevice>> audio =
103 if (!audio.ok()) {
104 std::cerr << "nanommo sounds: " << audio.status() << "\n";
105 }
106
107 const std::vector<Entry> entries = Entries(content->skills);
108 int picked = 0;
109 uint32_t seed = 0;
110 nanommo::Samples shown;
111 std::unique_ptr<nanommo::Clip> playing;
112 while (!(*window)->ShouldClose()) {
113 if (IsKeyPressed(KEY_DOWN)) {
114 picked = (picked + 1) % static_cast<int>(entries.size());
115 }
116 if (IsKeyPressed(KEY_UP)) {
117 picked = (picked + static_cast<int>(entries.size()) - 1) %
118 static_cast<int>(entries.size());
119 }
120 // Each press is a take of its own.
121 if (IsKeyPressed(KEY_SPACE)) {
122 shown = entries[picked].make(++seed);
123 if (audio.ok()) {
124 playing = nanommo::Clip::Load(**audio, shown, 1);
125 playing->Play(1.0f, 0.0f, 1.0f);
126 }
127 }
128
129 BeginDrawing();
130 ClearBackground({18, 20, 26, 255});
131 for (std::size_t i = 0; i < entries.size(); ++i) {
132 const int y = 20 + 22 * static_cast<int>(i);
133 const bool here = static_cast<int>(i) == picked;
134 if (here) {
135 DrawRectangle(12, y - 3, 300, 22, {179, 18, 42, 255});
136 }
137 DrawText(entries[i].name.c_str(), 20, y, 18, here ? WHITE : LIGHTGRAY);
138 }
139 DrawWave(shown, {340.0f, 60.0f, 600.0f, 420.0f}, {255, 140, 90, 255});
140 DrawText(absl::StrCat(entries[picked].name, ", take ", seed).c_str(), 340,
141 20, 20, WHITE);
142 DrawText(absl::StrCat(static_cast<float>(shown.size()) / nanommo::kRate,
143 " seconds")
144 .c_str(),
145 340, 492, 18, GRAY);
146 EndDrawing();
147 }
148 return 0;
149}
static absl::StatusOr< std::unique_ptr< AudioDevice > > Create()
Definition audio.cc:75
static std::unique_ptr< Clip > Load(const AudioDevice &device, const Samples &samples, int voices)
Definition audio.cc:109
static absl::StatusOr< std::unique_ptr< Window > > Create(const std::string &title, int width, int height, Visibility visibility=Visibility::kShown)
Definition window.cc:13
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
Samples SkillSound(const skills::Look &look, uint32_t seed, bool target)
Definition foley.cc:540
Place Arrival(const Gate &gate, float yaw)
Definition lands.cc:43
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
Ground
What a foot comes down on, which is how the step sounds.
Definition foley.h:15
Samples Step(Ground ground, uint32_t seed)
Definition foley.cc:122
std::string ContentFolder()
constexpr std::array< Ground, 7 > kGrounds
Definition foley.h:17
constexpr int kRate
Definition synth.h:11
Samples Departure(uint32_t seed)
Going through a gate: a rush up, as the screen covers the world.
Definition foley.cc:583
absl::StatusOr< Content > ReadContent(const std::string &folder)
Reads each file in the folder, or says which file is wrong, and where.
Definition content.cc:64
int main()
std::map< uint32_t, skills::Skill > by_id
Definition skills.h:17