Skip to content

Commit

Permalink
Merge pull request #2 from Lioydiano/music
Browse files Browse the repository at this point in the history
Music
  • Loading branch information
FLAK-ZOSO authored Aug 13, 2024
2 parents 88eeb3c + 7a14f76 commit def451d
Show file tree
Hide file tree
Showing 49 changed files with 149 additions and 31 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Dodas

Dodas stands for *"de dödas angrepp"*, literally *"attack of the dead"* and is a terminal videogame.

Dodas is written in C++ and uses the [`Sista`](https://github.com/Lioydiano/Dodas/blob/b32347f3daeb863757ac8ba256a22257d2bd6932/dodas.cpp#L256) library for terminal graphics.
Expand Down Expand Up @@ -63,7 +64,7 @@ dodas

If you want your record to be considered, you must run `./dodas` with no `-U` flag and without editing constants such as `START_AMMONITION`.

If you want the game to be pausable, you must run `./dodas -U` or `./dodas -unofficial`.
If you want the game to be pausable without the number of elapsed frames being incremented, you must run `./dodas -U` or `./dodas -unofficial` and this will make an eventual record not be considered.

If you don't want the music to be played, you must run `./dodas -M` or `./dodas --music-off`.

Expand Down Expand Up @@ -174,7 +175,7 @@ Despite not being intelligent in any way, her movements are unpredictable.
- Luca Corradin for the idea of the game
- Francesco Corradin for beta testing and suggestions
- FLAK-ZOSO for writing the code
- Il Pensionato Sentenzioso ([Karmolupe](https://karmolupe.bandcamp.com/)) for the music, still in development
- Il Pensionato Sentenzioso ([Karmolupe](https://karmolupe.bandcamp.com/)) for the music

# Records

Expand Down
Binary file removed audio/B1.mp3
Binary file not shown.
Binary file removed audio/B1.ogg
Binary file not shown.
Binary file removed audio/B1.wav
Binary file not shown.
Binary file removed audio/B2.mp3
Binary file not shown.
Binary file removed audio/B2.ogg
Binary file not shown.
Binary file removed audio/B2.wav
Binary file not shown.
Binary file added audio/MH1.mp3
Binary file not shown.
Binary file added audio/MH1.ogg
Binary file not shown.
Binary file added audio/MH1.wav
Binary file not shown.
Binary file added audio/MH2.mp3
Binary file not shown.
Binary file added audio/MH2.ogg
Binary file not shown.
Binary file added audio/MH2.wav
Binary file not shown.
Binary file added audio/MH3.mp3
Binary file not shown.
Binary file added audio/MH3.ogg
Binary file not shown.
Binary file added audio/MH3.wav
Binary file not shown.
Binary file added audio/MH4.mp3
Binary file not shown.
Binary file added audio/MH4.ogg
Binary file not shown.
Binary file added audio/MH4.wav
Binary file not shown.
Binary file added audio/ML1.mp3
Binary file not shown.
Binary file added audio/ML1.ogg
Binary file not shown.
Binary file added audio/ML1.wav
Binary file not shown.
Binary file added audio/ML2.mp3
Binary file not shown.
Binary file added audio/ML2.ogg
Binary file not shown.
Binary file added audio/ML2.wav
Binary file not shown.
Binary file added audio/ML3.mp3
Binary file not shown.
Binary file added audio/ML3.ogg
Binary file not shown.
Binary file added audio/ML3.wav
Binary file not shown.
Binary file added audio/ML4.mp3
Binary file not shown.
Binary file added audio/ML4.ogg
Binary file not shown.
Binary file added audio/ML4.wav
Binary file not shown.
Binary file added audio/P1.mp3
Binary file not shown.
Binary file added audio/P1.ogg
Binary file not shown.
Binary file added audio/P1.wav
Binary file not shown.
Binary file added audio/P2.mp3
Binary file not shown.
Binary file added audio/P2.ogg
Binary file not shown.
Binary file added audio/P2.wav
Binary file not shown.
Binary file added audio/P3.mp3
Binary file not shown.
Binary file added audio/P3.ogg
Binary file not shown.
Binary file added audio/P3.wav
Binary file not shown.
Binary file added audio/P4.mp3
Binary file not shown.
Binary file added audio/P4.ogg
Binary file not shown.
Binary file added audio/P4.wav
Binary file not shown.
80 changes: 80 additions & 0 deletions debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Before music thread
Frame: 0
Before genre
Genre: 2
n: 2
Frame: 1
Frame: 2
Frame: 3
Frame: 4
Frame: 5
Frame: 6
Frame: 7
Frame: 8
Frame: 9
After PlaySound
Frame: 10
Frame: 11
Frame: 12
Frame: 13
Frame: 14
Frame: 15
Frame: 16
Frame: 17
Frame: 18
Frame: 28
Frame: 29
Frame: 30
Frame: 31
Frame: 32
Frame: 33
Frame: 34
Frame: 35
Frame: 36
Frame: 37
Frame: 38
Frame: 39
Frame: 40
Frame: 41
Frame: 51
Frame: 52
Frame: 53
Frame: 54
Frame: 55
Frame: 56
Frame: 57
Frame: 58
Frame: 59
Frame: 60
Already slept for 5920 seconds
Pausing
Paused
Frame: 68
Before genre
Genre: 2
n: 4
Frame: 69
Frame: 70
After PlaySound
Frame: 71
Frame: 72
Frame: 73
Frame: 74
Frame: 81
Frame: 82
Frame: 83
Frame: 84
Frame: 85
Frame: 89
Frame: 90
Frame: 91
Frame: 92
Frame: 97
Frame: 98
Frame: 99
Frame: 100
Frame: 101
Frame: 102
Already slept for 5920 seconds
Pausing
Paused
Binary file modified dodas
Binary file not shown.
93 changes: 64 additions & 29 deletions dodas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,42 @@ int main(int argc, char** argv) {
});
std::thread music_th;
if (music) {
const unsigned tracks = 2; // The number of tracks in the audio folder
const int length[] = {16, 16};
const int genresProbSize = 3; // The number of genres
int genresProb[] = {3, 6, 1}; // The probability of each genre
int genresSize_[] = {0, 4, 4, 4}; // The number of tracks for each genre
std::discrete_distribution<int> genresDistribution(genresProb, genresProb + genresProbSize);
std::string genres[] = {"F", "MH", "ML", "P"}; // The genres of the music
std::unordered_map<std::string, int> length_ = { // The length of each track
{"MH1", 10}, {"MH2", 16}, {"MH3", 16}, {"MH4", 8},
{"ML1", 4}, {"ML2", 6}, {"ML3", 6}, {"ML4", 6},
{"P1", 4}, {"P2", 8}, {"P3", 6}, {"P4", 6}
};
int length[4][5] = {
{0, 0, 0, 0, 0},
{0, 10, 16, 16, 8},
{0, 4, 6, 6, 6},
{0, 4, 8, 6, 6}
};
debug << "Before music thread" << std::endl;
music_th = std::thread([&]() {
int n;
int n, genre;
#ifdef __APPLE__
std::vector<int> extendedProb;
for (unsigned i=0; i<genresProbSize; i++) {
for (unsigned j=0; j<genresProb[i]; j++) {
extendedProb.push_back(i+1);
}
}
while (!end) {
n = rand() % tracks + 1;
genre = extendedProb[rand() % extendedProb.size()];
debug << "Genre: " << genre << std::endl;
n = (rand() % genresSize[genre]) + 1;
debug << "n: " << n << std::endl;
std::string track = genres[genre] + std::to_string(n);
debug << "Playing " << track << std::endl;
try {
char buf[1024];
snprintf(buf, 1024, "afplay \"audio/B%d.mp3\"", n);
snprintf(buf, 1024, "afplay \"audio/%s.mp3\"", track.c_str());
system(buf);
} catch (std::exception& e) {
return; // If the music can't be played, the thread ends
Expand All @@ -179,49 +205,58 @@ int main(int argc, char** argv) {
}
}
#elif _WIN32
for (unsigned i=0; i<tracks; i++) {
debug << "audio/B" << i+1 << ".wav" << std::endl;
std::string track = "audio/B" + std::to_string(i+1) + ".wav";
debug << track << std::endl;
// LPCSTR track_ = track.c_str();
debug << "Before mciSendString" << std::endl;
mciSendString((LPCSTR)("open " + track + " type waveaudio alias B" + std::to_string(i+1)).c_str(), NULL, 0, NULL);
debug << "After mciSendString" << std::endl;
}
while (!end) {
n = rand() % tracks + 1;
debug << "Before genre" << std::endl;
genre = genresDistribution(rng);
debug << "Genre: " << genre << std::endl;
n = (rand() % genresSize_[genre]) + 1;
debug << "n: " << n << std::endl;
std::string track = genres[genre] + std::to_string(n);
try {
debug << "audio/B" << n << ".wav" << std::endl;
// std::string track = "\"audio/B" + std::to_string(n) + ".wav\"";
// debug << track << std::endl;
// LPCSTR track_ = track.c_str();
// debug << "Before PlaySound" << std::endl;
// PlaySound(TEXT(track_), NULL, SND_ASYNC);
// mciSendString(track_, NULL, 0, NULL);
mciSendString((LPCSTR)("play B" + std::to_string(n)).c_str(), NULL, 0, NULL);
mciSendString((LPCSTR)("play audio/" + track + ".wav").c_str(), NULL, 0, NULL);
debug << "After PlaySound" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(length[n-1]));
debug << "Already slept for length[" << n-1 << "] = " << length[n-1] << " seconds" << std::endl;
int wait = length[genre][n];
wait *= 1000;
wait -= WIN_API_MUSIC_DELAY; // Some time is wasted in API calls, so we have to compensate for that
std::this_thread::sleep_for(std::chrono::milliseconds(wait));
debug << "Already slept for " << wait << " seconds" << std::endl;
} catch (std::exception& e) {
debug << "Exception" << std::endl;
debug << e.what() << std::endl;
return; // If the music can't be played, the thread ends
}
if (pause_) {
debug << "Pausing" << std::endl;
PlaySound(NULL, 0, 0);
debug << "Paused" << std::endl;
break;
// break;
}
while (pause_) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
#elif __linux__
while (!end) {
n = (rand() % tracks) + 1;
// debug << "audio/B" << n << ".ogg" << std::endl;
genre = genresDistribution(rng);
// debug << "Genre: " << genre << std::endl;
n = (rand() % genresSize[genre]) + 1;
// debug << "n: " << n << std::endl;
std::string track = ((std::string)"audio/") + genres[genre] + std::to_string(n) + (std::string)".ogg";
// switch (genre) {
// case 1:
// track = "audio/MH" + std::to_string(n) + ".ogg";
// break;
// case 2:
// track = "audio/ML" + std::to_string(n) + ".ogg";
// break;
// case 3:
// track = "audio/P" + std::to_string(n) + ".ogg";
// break;
// default:
// break;
// }
try {
system(("canberra-gtk-play -f audio/B" + std::to_string(n) + ".ogg").c_str());
system(("canberra-gtk-play -f " + track).c_str());
// debug << "After canberra-gtk-play" << std::endl;
} catch (std::exception& e) {
debug << e.what() << std::endl;
Expand Down
Binary file modified dodas.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions dodas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define VERSION "0.5.0"
#define DATE "2024/07/27"

#define WIN_API_MUSIC_DELAY 80

void printIntro();

enum Type {
Expand Down
Binary file added dodasApple
Binary file not shown.

0 comments on commit def451d

Please sign in to comment.