-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleTools.cpp
129 lines (109 loc) · 2.29 KB
/
ConsoleTools.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
//
// ConsoleTools.cpp
// TankGame
//
// Created by Jacob Gonzalez on 11/04/2015.
// Copyright (c) 2015 Jacob Gonzalez. All rights reserved.
//
#include <unistd.h>
#include <sys/ioctl.h>
#include <cstdlib>
#include <fstream>
#include "ConsoleTools.h"
bool ConsoleTools::audio = true;
bool ConsoleTools::replay = false;
std::string ConsoleTools::get_line()
{
std::string input;
getline(std::cin, input);
return input;
}
void ConsoleTools::set_title(char const *title)
{
printf("\x1b]2;%s\x07", title);
}
void ConsoleTools::set_fcolor(ConsoleTools::Color color)
{
printf("\033[%dm", color);
}
void ConsoleTools::set_bcolor(ConsoleTools::Color color)
{
printf("\033[%dm", color+10);
}
void ConsoleTools::set_color(ConsoleTools::Color fcolor, ConsoleTools::Color bcolor)
{
printf("\033[%d;%dm", fcolor, bcolor+10);
}
void ConsoleTools::end_color()
{
printf("\033[%dm", endc);
}
void ConsoleTools::hide_text()
{
printf("\033[8m");
}
void ConsoleTools::reverse_color()
{
printf("\033[7m");
}
void ConsoleTools::clear_screen()
{
printf("\033[2J");
set_cursor(0, 0);
}
void ConsoleTools::clear_line()
{
printf("\033[K");
}
void ConsoleTools::set_cursor(int x, int y)
{
printf("\033[%d;%dH", y, x);
}
void ConsoleTools::set_cursor(Point<int> pos)
{
printf("\033[%d;%dH", pos.y, pos.x);
}
void ConsoleTools::move_cursor(ConsoleTools::Direction dir, int dist)
{
printf("\033[%d%c", dist, static_cast<char>(dir));
}
Point<int> ConsoleTools::get_terminal_size()
{
winsize win;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
Point<int> p(win.ws_col, win.ws_row);
return p;
}
void ConsoleTools::stop_music()
{
if (audio == false)
{
return;
}
#ifdef __APPLE__
system("killall afplay");
#elif __linux__
system("killall aplay");
#endif
}
void ConsoleTools::play_music(std::string filename)
{
if (audio == false)
{
return;
}
std::ifstream file(filename.c_str(), std::ios::binary);
if (file.good() == true)
{
#ifdef __APPLE__
system(std::string("afplay " + filename + "&").c_str());
#elif __linux__
system(std::string("aplay -q " + filename + " &").c_str());
#endif
}
else
{
audio = false;
}
file.close();
}