nsnake
Classic snake game for the terminal
WindowGameHelp.cpp
1 #include <Interface/WindowGameHelp.hpp>
2 #include <Interface/Layout.hpp>
3 #include <Interface/Colors.hpp>
4 #include <Config/Globals.hpp>
5 #include <Flow/InputManager.hpp>
6 #include <Misc/Utils.hpp>
7 
8 WindowGameHelp::WindowGameHelp()
9 {
10  int width = 40;
11  int height = 17;
12 
13  int windowx = Layout::screenWidth/2 - width/2;
14  int windowy = Layout::screenHeight/2 - height/2;
15 
16  this->main = new Window(windowx,
17  windowy,
18  width,
19  height);
20 
21  if (Globals::Screen::show_borders)
22  {
23  this->main->borders(Globals::Screen::fancy_borders ?
24  Window::BORDER_FANCY :
25  Window::BORDER_REGULAR);
26  }
27 
28  Window* win;
29 
30  // Help
31  win = new Window(this->main, 0, 0, WINDOW_FILL, WINDOW_FILL);
32  this->windows.push_back(win);
33 
34  win = new Window(this->main, 0, 0, WINDOW_FILL, WINDOW_FILL);
35  this->windows.push_back(win);
36 }
38 {
39  int activatedIndex = 0;
40 
41  while (true)
42  {
43  // Refreshing Windows
44  this->main->clear();
45  this->windows[activatedIndex]->clear();
46 
47  this->main->print(((activatedIndex == 0) ?
48  "(Help)" :
49  " Help "),
50  2,
51  0,
52  ((activatedIndex == 0) ?
53  Globals::Theme::textbox :
54  Globals::Theme::hilite_text));
55 
56  this->main->print(((activatedIndex == 1) ?
57  "(Credits)" :
58  " Credits "),
59  12,
60  0,
61  ((activatedIndex == 1) ?
62  Globals::Theme::textbox :
63  Globals::Theme::hilite_text));
64 
65  // HACK TO AVOID THE BORDERS FROM BEING
66  // BOLD. GOTTA SOLVE THIS MISTERY.
67  Colors::pairActivate(this->main->win, Globals::Theme::text);
68 
69  // Help Window
70  if (activatedIndex == 0)
71  {
72  this->windows[0]->print("In-game controls:\n",
73  0, 0,
74  Globals::Theme::hilite_text);
75 
76  this->windows[0]->print(Utils::String::split("Move up\n"
77  "Move down\n"
78  "Move left\n"
79  "Move right\n"
80  "Pause game\n"
81  "Quit anytime\n"
82  "Show help", '\n'),
83  1, 1,
84  Globals::Theme::hilite_text);
85 
86  this->windows[0]->print(Utils::String::split(InputManager::keyToString(InputManager::getBind("up")) + "\n" +
87  InputManager::keyToString(InputManager::getBind("down")) + "\n" +
88  InputManager::keyToString(InputManager::getBind("left")) + "\n" +
89  InputManager::keyToString(InputManager::getBind("right")) + "\n" +
90  InputManager::keyToString(InputManager::getBind("pause")) + "\n" +
91  InputManager::keyToString(InputManager::getBind("quit")) + "\n" +
92  InputManager::keyToString(InputManager::getBind("help")), '\n'),
93  14, 1,
94  Globals::Theme::text);
95 
96  this->windows[0]->print("Menu controls:\n",
97  0, 9,
98  Globals::Theme::hilite_text);
99 
100  this->windows[0]->print(Utils::String::split("First item\n"
101  "Last item", '\n'),
102  1, 10,
103  Globals::Theme::hilite_text);
104 
105  this->windows[0]->print(Utils::String::split("page up\n"
106  "page down", '\n'),
107  14, 10,
108  Globals::Theme::text);
109 
110  this->windows[0]->print(Utils::String::split(" Settings and scores are stored at:\n"
111  " `~/.local/share/nsnake/`", '\n'),
112  0, 13,
113  Globals::Theme::text);
114  }
115  //
116  // Credits
117  else if (activatedIndex == 1)
118  {
119  this->windows[1]->print(Utils::String::split(" _ __ _ __ _ ____ \n"
120  "| |\\ | ( (` | |\\ | / /\\ | |_/ | |_ \n"
121  "|_| \\| _)_) |_| \\| /_/--\\ |_| \\ |_|__", '\n'),
122  0, 0, Colors::pair(COLOR_BLUE, COLOR_DEFAULT, true));
123 
124  this->windows[1]->print(" v" VERSION " (built " DATE ")",
125  0, 3,
126  Colors::pair(COLOR_GREEN, COLOR_DEFAULT, true));
127 
128  this->windows[1]->print(Utils::String::split("Try `nsnake --help` and `man nsnake`\n"
129  "\n"
130  "Game made by Alexandre Dantas,\n"
131  "contact him at <eu@alexdantas.net>\n"
132  "Thanks for playing this game :)\n"
133  "\n"
134  "Homepage:\n"
135  " http://nsnake.alexdantas.net/\n"
136  "Source Code:\n"
137  " https://github.com/alexdantas/nsnake/", '\n'),
138  0, 5, Globals::Theme::text);
139  }
140 
141  this->windows[activatedIndex]->refresh();
142  this->main->refresh();
143  refresh();
144 
145  // Getting Input
146  InputManager::update();
147 
148  if (InputManager::isPressed("left") || // user-defined
149  InputManager::isPressed(KEY_LEFT))
150  {
151  activatedIndex--;
152  if (activatedIndex < 0)
153  activatedIndex = 0;
154  }
155  else if (InputManager::isPressed("right") || // user-defined
156  InputManager::isPressed(KEY_RIGHT))
157  {
158  activatedIndex++;
159  if (activatedIndex > 1)
160  activatedIndex = 1;
161  }
162  else if (InputManager::isPressed("quit") ||
163  InputManager::isPressed(KEY_ENTER) ||
164  InputManager::isPressed('\n'))
165  return;
166  }
167 }
168 
WINDOW * win
Ncurses&#39; internal data structure.
Definition: Window.hpp:85
A segment of the terminal screen (2D char matrix).
Definition: Window.hpp:16
static int screenWidth
Full width of the terminal right now.
Definition: Layout.hpp:19
void print(std::string str, int x, int y, ColorPair pair=0)
Shows text #str at #x #y on the window with color #pair.
Definition: Window.cpp:94
static int screenHeight
Full height of the terminal right now.
Definition: Layout.hpp:22
void run()
Updates and draws all tabs.