nsnake
Classic snake game for the terminal
WindowGameHelp.cpp
1 #include <Display/WindowGameHelp.hpp>
2 #include <Engine/Graphics/Layout.hpp>
3 #include <Engine/Graphics/Colors.hpp>
4 #include <Engine/EngineGlobals.hpp>
5 #include <Engine/InputManager.hpp>
6 #include <Engine/Helpers/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, windowy, width, height);
17 
18  Window* win;
19 
20  // Help
21  win = new Window(this->main, 0, 0, WINDOW_FILL, WINDOW_FILL);
22  win->borders(Window::BORDER_NONE);
23 
24  this->windows.push_back(win);
25 
26  win = new Window(this->main, 0, 0, WINDOW_FILL, WINDOW_FILL);
27  win->borders(Window::BORDER_NONE);
28  this->windows.push_back(win);
29 }
31 {
32  int activatedIndex = 0;
33 
34  while (true)
35  {
36  // Refreshing Windows
37  this->main->clear();
38  this->windows[activatedIndex]->clear();
39 
40  this->main->print(((activatedIndex == 0) ?
41  "(Help)" :
42  " Help "),
43  2,
44  0,
45  ((activatedIndex == 0) ?
46  EngineGlobals::Theme::textbox :
47  EngineGlobals::Theme::hilite_text));
48 
49  this->main->print(((activatedIndex == 1) ?
50  "(Credits)" :
51  " Credits "),
52  12,
53  0,
54  ((activatedIndex == 1) ?
55  EngineGlobals::Theme::textbox :
56  EngineGlobals::Theme::hilite_text));
57 
58  // This is a HACK to avoid the borders from being
59  // bold. Apparently I need to activate the default
60  // ColorPair before continuing.
61  //
62  // TODO: Find out about this mistery.
63  this->main->print("", 0, 0, EngineGlobals::Theme::text);
64 
65  // Help Window
66  if (activatedIndex == 0)
67  {
68  this->windows[0]->print("In-game controls:\n",
69  0, 0,
70  EngineGlobals::Theme::hilite_text);
71 
72  this->windows[0]->print(Utils::String::split("Move up\n"
73  "Move down\n"
74  "Move left\n"
75  "Move right\n"
76  "Pause game\n"
77  "Quit anytime\n"
78  "Show help", '\n'),
79  1, 1,
80  EngineGlobals::Theme::hilite_text);
81 
82  this->windows[0]->print(Utils::String::split(InputManager::keyToString(InputManager::getBind("up")) + "\n" +
83  InputManager::keyToString(InputManager::getBind("down")) + "\n" +
84  InputManager::keyToString(InputManager::getBind("left")) + "\n" +
85  InputManager::keyToString(InputManager::getBind("right")) + "\n" +
86  InputManager::keyToString(InputManager::getBind("pause")) + "\n" +
87  InputManager::keyToString(InputManager::getBind("quit")) + "\n" +
88  InputManager::keyToString(InputManager::getBind("help")), '\n'),
89  14, 1,
90  EngineGlobals::Theme::text);
91 
92  this->windows[0]->print("Menu controls:\n",
93  0, 9,
94  EngineGlobals::Theme::hilite_text);
95 
96  this->windows[0]->print(Utils::String::split("First item\n"
97  "Last item", '\n'),
98  1, 10,
99  EngineGlobals::Theme::hilite_text);
100 
101  this->windows[0]->print(Utils::String::split("page up\n"
102  "page down", '\n'),
103  14, 10,
104  EngineGlobals::Theme::text);
105 
106  this->windows[0]->print(Utils::String::split(" Settings and scores are stored at:\n"
107  " `~/.local/share/nsnake/`", '\n'),
108  0, 13,
109  EngineGlobals::Theme::text);
110  }
111  //
112  // Credits
113  else if (activatedIndex == 1)
114  {
115  this->windows[1]->print(Utils::String::split(" _ __ _ __ _ ____ \n"
116  "| |\\ | ( (` | |\\ | / /\\ | |_/ | |_ \n"
117  "|_| \\| _)_) |_| \\| /_/--\\ |_| \\ |_|__", '\n'),
118  0, 0, Colors::pair("blue", "default", true));
119 
120  this->windows[1]->print(" v" VERSION " (built " DATE ")",
121  0, 3,
122  Colors::pair("green", "default", true));
123 
124  this->windows[1]->print(Utils::String::split("Try `nsnake --help` and `man nsnake`\n"
125  "\n"
126  "Game made by Alexandre Dantas,\n"
127  "contact him at <eu@alexdantas.net>\n"
128  "Thanks for playing this game :)\n"
129  "\n"
130  "Homepage:\n"
131  " http://nsnake.alexdantas.net/\n"
132  "Source Code:\n"
133  " https://github.com/alexdantas/nsnake/", '\n'),
134  0, 5, EngineGlobals::Theme::text);
135  }
136 
137  this->windows[activatedIndex]->refresh();
138  this->main->refresh();
139  refresh();
140 
141  // Getting Input
142  InputManager::update();
143 
144  if (InputManager::isPressed("left") || // user-defined
145  InputManager::isPressed(KEY_LEFT))
146  {
147  activatedIndex--;
148  if (activatedIndex < 0)
149  activatedIndex = 0;
150  }
151  else if (InputManager::isPressed("right") || // user-defined
152  InputManager::isPressed(KEY_RIGHT))
153  {
154  activatedIndex++;
155  if (activatedIndex > 1)
156  activatedIndex = 1;
157  }
158  else if (InputManager::isPressed("quit") ||
159  InputManager::isPressed(KEY_ENTER) ||
160  InputManager::isPressed('\n'))
161  return;
162  }
163 }
164 
void run()
Updates and draws all tabs.