nsnake
Classic snake game for the terminal
LayoutGame.cpp
1 #include <Display/Layouts/LayoutGame.hpp>
2 #include <Engine/EngineGlobals.hpp>
3 #include <Engine/Helpers/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, WINDOW_FILL,
34  WINDOW_FILL, this->main->getH() - 3);
35 
36  this->gamewin->borders(Window::BORDER_NONE);
37 
38  this->info = new Window(this->main,
39  WINDOW_FILL, this->main->getH() - 2,
40  WINDOW_FILL, 1);
41 
42  this->info->borders(Window::BORDER_NONE);
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  this->pause->setTitle("Paused");
52 
53  // Le help window.
54  this->help = new Window(this->main,
55  this->main->getW() / 4,
56  this->main->getH() / 4,
57  this->main->getW() / 2,
58  this->main->getH() / 2);
59 
60  this->help->setTitle("Help");
61 
62  this->helpWindows = new WindowGameHelp();
63 }
64 void LayoutGame::windowsExit()
65 {
66  SAFE_DELETE(this->gamewin);
67  SAFE_DELETE(this->info);
68  SAFE_DELETE(this->pause);
69  SAFE_DELETE(this->help);
70  SAFE_DELETE(this->boardwin);
71  SAFE_DELETE(this->helpWindows);
72 
73  this->main->clear(); // clear() as in Window
74  this->main->refresh(); // clear() as in Window
75 
76  Layout::windowsExit();
77 }
78 void LayoutGame::draw(Menu* menu)
79 {
80  if (! this->game)
81  return;
82 
83  // This hack allows the game board to be centered
84  if (! this->boardwin)
85  {
86  // initializing for the first time
87  int x = this->gamewin->getW()/2 - this->game->board->getW()/2;
88  int y = this->gamewin->getH()/2 - this->game->board->getH()/2;
89  int w = this->game->board->getW();
90  int h = this->game->board->getH();
91 
92  boardwin = new Window(this->gamewin, x, y, w, h);
93  }
94 
95  this->main->clear();
96 
97  // Will only show the requested windows then exit.
98 
99  if (this->game->isPaused && this->game->showPauseMenu)
100  {
101  this->pause->clear();
102  menu->draw(this->pause);
103  this->pause->refresh();
104 
105  refresh();
106  return;
107  }
108 
109  if (this->game->showHelp)
110  {
111  this->helpWindows->run();
112  this->game->showHelp = false;
113 
114  // NCURSES NEEDS THIS
115  refresh();
116  return;
117  }
118 
119  // Statistics
120  // A mess of direct Ncurses calls - fix this later
121  this->info->clear();
122  this->info->print("a", 0, 0);
123 
124  ColorPair hilite = EngineGlobals::Theme::hilite_text;
125 
126  this->info->print("Hi-Score", 0, 0, hilite);
127  this->info->print("Score", this->info->getW()/3, 0, hilite);
128  this->info->print("Speed", this->info->getW()/3 * 2, 0, hilite);
129 
130  if (this->game->scores->highScore)
131  {
132  std::string points = Utils::String::toString(this->game->scores->highScore->points);
133 
134  this->info->print(points, 9, 0, EngineGlobals::Theme::text);
135  }
136 
137  std::string points = Utils::String::toString(this->game->currentScore->points);
138  this->info->print(points,
139  this->info->getW()/3 + 6,
140  0,
141  EngineGlobals::Theme::text);
142 
143  std::string speed = Utils::String::toString(this->game->currentScore->speed);
144  this->info->print(speed,
145  this->info->getW()/3 * 2 + 6,
146  0,
147  EngineGlobals::Theme::text);
148 
149  // // Timer - how much time has passed since game start
150  // this->rightmost->print("Timer", 2, 10, hilite);
151 
152  // long delta_s = this->game->timer.delta_s();
153 
154  // int seconds = delta_s % 60;
155  // int minutes = (delta_s / 60) % 60;
156  // int hours = ((delta_s / 60) / 60) % 24;
157 
158  // wattrset(this->rightmost->win, COLOR_PAIR(0));
159 
160  // mvwprintw(this->rightmost->win, 10, 8, "%02d:%02d:%02d", hours, minutes, seconds);
161 
162  // // Delay
163  // this->rightmost->print("Delay", 2, 12, hilite);
164  // wattrset(this->rightmost->win, COLOR_PAIR(0));
165 
166  // mvwprintw(this->rightmost->win, 12, 8, "%dms", this->game->getDelay(this->game->score->level));
167 
168  // // Bottom line - version and Help
169  // this->rightmost->print("nsnake v" VERSION, 1, this->rightmost->getH() - 2, Colors::pair(COLOR_CYAN, COLOR_DEFAULT));
170 
171  // this->rightmost->print("H", this->rightmost->getW() - 5, this->rightmost->getH() - 2, Colors::pair(COLOR_YELLOW, COLOR_DEFAULT));
172  // this->rightmost->print("elp", this->rightmost->getW() - 4, this->rightmost->getH() - 2, Colors::pair(COLOR_CYAN, COLOR_DEFAULT));
173  // this->rightmost->refresh();
174  // }
175 
176  // Board and main game stuff
177  this->gamewin->clear();
178 
179  this->game->board->draw(boardwin);
180  this->game->player->draw(boardwin);
181  this->game->fruits->draw(boardwin);
182 
183  this->gamewin->refresh();
184  this->info->refresh();
185  this->main->refresh();
186 
187  // NCURSES NEEDS THIS
188  refresh();
189 }
190 
ScoreEntry * highScore
Maximum high score obtained for the current game.
Definition: ScoreFile.hpp:148
bool showHelp
If it's showing the help screen.
Definition: Game.hpp:98
std::string level
On which level the user made this score.
Definition: ScoreFile.hpp:37
std::string getMetadata(std::string name)
Gets a meta information from this level.
Definition: Board.cpp:166
Specific Window that shows Help and other info during Game.
Definition: Game.hpp:16
Window * help
Contains the help screen.
Definition: LayoutGame.hpp:45
Window * pause
Contains the pause menu.
Definition: LayoutGame.hpp:42
unsigned int points
How many points the user got.
Definition: ScoreFile.hpp:30
ScoreEntry * currentScore
Current score for this level.
Definition: Game.hpp:66
unsigned int speed
Under which game speed the score was made.
Definition: ScoreFile.hpp:33
void run()
Updates and draws all tabs.
bool isPaused
If the game is paused.
Definition: Game.hpp:90
ScoreFile * scores
All the current level's score.
Definition: Game.hpp:59