nsnake
Classic snake game for the terminal
LayoutGame.cpp
1 #include <Interface/LayoutGame.hpp>
2 #include <Config/Globals.hpp>
3 #include <Misc/Utils.hpp>
4 
5 LayoutGame::LayoutGame(Game* game, int width, int height):
6  Layout(width, height),
7  game(game),
8  gamewin(NULL),
9  info(NULL),
10  pause(NULL),
11  help(NULL),
12  boardwin(NULL),
13  helpWindows(NULL)
14 {
15  this->windowsInit();
16 }
17 LayoutGame::~LayoutGame()
18 {
19  this->windowsExit();
20 }
21 void LayoutGame::windowsInit()
22 {
23  Layout::windowsInit();
24  this->main->setTitle("nsnake " VERSION);
25 
26  if (this->game->currentScore->level.empty())
27  this->main->setTitle("Arcade Mode", Window::TOP_RIGHT);
28  else
29  this->main->setTitle("Level " + this->game->board->getMetadata("name"), Window::TOP_RIGHT);
30 
31  // Leftmost window
32  this->gamewin = new Window(this->main,
33  WINDOW_FILL,
34  WINDOW_FILL,
35  WINDOW_FILL,
36  this->main->getH() - 3);
37 
38  this->info = new Window(this->main,
39  WINDOW_FILL,
40  this->main->getH() - 2,
41  WINDOW_FILL,
42  1);
43 
44  // Le pause window.
45  this->pause = new Window(this->main,
46  this->main->getW() / 4,
47  this->main->getH() / 2 - 1, //center
48  this->main->getW() / 2,
49  7);
50 
51  if (Globals::Screen::show_borders)
52  {
53  this->pause->borders(Globals::Screen::fancy_borders ?
54  Window::BORDER_FANCY :
55  Window::BORDER_REGULAR);
56  }
57  this->pause->setTitle("Paused");
58 
59  // Le help window.
60  this->help = new Window(this->main,
61  this->main->getW() / 4,
62  this->main->getH() / 4,
63  this->main->getW() / 2,
64  this->main->getH() / 2);
65 
66  if (Globals::Screen::show_borders)
67  {
68  this->help->borders(Globals::Screen::fancy_borders ?
69  Window::BORDER_FANCY :
70  Window::BORDER_REGULAR);
71  }
72  this->help->setTitle("Help");
73 
74  this->helpWindows = new WindowGameHelp();
75 }
76 void LayoutGame::windowsExit()
77 {
78  SAFE_DELETE(this->gamewin);
79  SAFE_DELETE(this->info);
80  SAFE_DELETE(this->pause);
81  SAFE_DELETE(this->help);
82  SAFE_DELETE(this->boardwin);
83  SAFE_DELETE(this->helpWindows);
84 
85  this->main->clear(); // clear() as in Window
86  this->main->refresh(); // clear() as in Window
87 
88  Layout::windowsExit();
89 }
90 void LayoutGame::draw(Menu* menu)
91 {
92  if (! this->game)
93  return;
94 
95  // This hack allows the game board to be centered
96  if (! this->boardwin)
97  {
98  // initializing for the first time
99  int x = this->gamewin->getW()/2 - this->game->board->getW()/2;
100  int y = this->gamewin->getH()/2 - this->game->board->getH()/2;
101  int w = this->game->board->getW();
102  int h = this->game->board->getH();
103 
104  boardwin = new Window(this->gamewin, x, y, w, h);
105  }
106 
107  this->main->clear();
108 
109  // Will only show the requested windows then exit.
110 
111  if (this->game->isPaused && this->game->showPauseMenu)
112  {
113  this->pause->clear();
114  menu->draw(this->pause);
115  this->pause->refresh();
116 
117  refresh();
118  return;
119  }
120 
121  if (this->game->showHelp)
122  {
123  this->helpWindows->run();
124  this->game->showHelp = false;
125 
126  // NCURSES NEEDS THIS
127  refresh();
128  return;
129  }
130 
131  // Statistics
132  // A mess of direct Ncurses calls - fix this later
133  this->info->clear();
134  this->info->print("a", 0, 0, 0);
135 
136  ColorPair hilite = Globals::Theme::hilite_text;
137 
138  this->info->print("Hi-Score", 0, 0, hilite);
139  this->info->print("Score", this->info->getW()/3, 0, hilite);
140  this->info->print("Speed", this->info->getW()/3 * 2, 0, hilite);
141 
142  // Default color
143  wattrset(this->info->win, COLOR_PAIR(0));
144 
145  if (this->game->scores->highScore)
146  mvwprintw(this->info->win, 0, 9, "%u", this->game->scores->highScore->points);
147 
148  mvwprintw(this->info->win, 0, this->info->getW()/3 + 6, "%u", this->game->currentScore->points);
149  mvwprintw(this->info->win, 0, this->info->getW()/3 * 2 + 6, "%d", this->game->currentScore->speed);
150 
151  // // Timer - how much time has passed since game start
152  // this->rightmost->print("Timer", 2, 10, hilite);
153 
154  // long delta_s = this->game->timer.delta_s();
155 
156  // int seconds = delta_s % 60;
157  // int minutes = (delta_s / 60) % 60;
158  // int hours = ((delta_s / 60) / 60) % 24;
159 
160  // wattrset(this->rightmost->win, COLOR_PAIR(0));
161 
162  // mvwprintw(this->rightmost->win, 10, 8, "%02d:%02d:%02d", hours, minutes, seconds);
163 
164  // // Delay
165  // this->rightmost->print("Delay", 2, 12, hilite);
166  // wattrset(this->rightmost->win, COLOR_PAIR(0));
167 
168  // mvwprintw(this->rightmost->win, 12, 8, "%dms", this->game->getDelay(this->game->score->level));
169 
170  // // Bottom line - version and Help
171  // this->rightmost->print("nsnake v" VERSION, 1, this->rightmost->getH() - 2, Colors::pair(COLOR_CYAN, COLOR_DEFAULT));
172 
173  // this->rightmost->print("H", this->rightmost->getW() - 5, this->rightmost->getH() - 2, Colors::pair(COLOR_YELLOW, COLOR_DEFAULT));
174  // this->rightmost->print("elp", this->rightmost->getW() - 4, this->rightmost->getH() - 2, Colors::pair(COLOR_CYAN, COLOR_DEFAULT));
175  // this->rightmost->refresh();
176  // }
177 
178  // Board and main game stuff
179  this->gamewin->clear();
180 
181  this->game->board->draw(boardwin);
182  this->game->player->draw(boardwin);
183  this->game->fruits->draw(boardwin);
184 
185  this->gamewin->refresh();
186  this->info->refresh();
187  this->main->refresh();
188 
189  // NCURSES NEEDS THIS
190  refresh();
191 }
192 
WINDOW * win
Ncurses&#39; internal data structure.
Definition: Window.hpp:85
A segment of the terminal screen (2D char matrix).
Definition: Window.hpp:16
Window * main
Layout&#39;s main Window, where all the others are inside.
Definition: Layout.hpp:33
Specific Window that shows Help and other info during Game.
Definition: Game.hpp:16
void draw(Window *window)
Draws the whole Menu on #window.
Definition: Menu.cpp:95
List of selectable items.
Definition: Menu.hpp:28
void setTitle(std::string title, WindowTitlePosition position=Window::TOP_LEFT)
Sets a text that will appear at the top of the Window.
Definition: Window.cpp:214
Interface for how the things are shown on the screen.
Definition: Layout.hpp:15
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