-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
78 lines (60 loc) · 1.9 KB
/
main.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
#include <array>
#include <iostream>
#include <exception>
#include <iomanip>
#include <Windows.h>
#include "Piano.h"
#include "MidiFile.h"
#include "Options.h"
#include <boost/program_options.hpp>
bool hasEnding (std::string const &fullString, std::string const &ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
int main(const int argc, const char* argv[]) {
try {
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("loop", "Play song on repeat");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (argc < 2 || vm.count("help")) {
puts("Usage: <input file>");
std::cout << desc << "\n";
return 1;
}
HWND hWindowHandle = FindWindow(nullptr, "Garry's Mod");
if (!hWindowHandle) {
std::cerr << "Failed to acquire handle\n";
return 1;
}
Piano piano(hWindowHandle);
const std::string fileName = argv[1];
try {
if (hasEnding(fileName, ".mid")) {
piano.loadMidi(fileName.c_str());
} else {
piano.loadText(fileName.c_str());
}
} catch (std::ifstream::failure &ex) {
std::cerr << "Failed to read file\n";
std::cerr << ex.what() << '\n'; // not very useful
return 1;
}
const bool loop = vm.count("loop");
do {
piano.play();
Sleep(1000);
} while (loop);
} catch (std::exception& ex) {
std::cout << ex.what() << '\n';
return 1;
}
return 0;
}