Commit 51a81155 authored by Konrad McClure's avatar Konrad McClure
Browse files

Added main menu, currently can select 2048 (successfully) and Snake (nothing there)

parent 8f728c2e
...@@ -11,7 +11,7 @@ void Display::number (int value) { ...@@ -11,7 +11,7 @@ void Display::number (int value) {
} }
void Display::showGame(const vector<vector<int>>& board, const int& score, const int& highScore) { void Display::showGame(const vector<vector<int>>& board, const int& score, const int& highScore) {
cout << setw(64) << "For help press: h" << endl << endl; cout << endl << setw(64) << "For help press: h" << endl << endl;
showScore(score, highScore, board.size()); showScore(score, highScore, board.size());
for (const vector<int>& colums: board ) { for (const vector<int>& colums: board ) {
for(int value: colums){ for(int value: colums){
......
...@@ -43,6 +43,8 @@ void Game2048::Shift(char input){ ...@@ -43,6 +43,8 @@ void Game2048::Shift(char input){
case 'd': case 'd':
movement.right(*board, score); movement.right(*board, score);
break; break;
default:
break;
} }
} }
......
...@@ -6,7 +6,7 @@ C2048GameState C2048GameState::m_2048GameState; ...@@ -6,7 +6,7 @@ C2048GameState C2048GameState::m_2048GameState;
void C2048GameState::Init() void C2048GameState::Init()
{ {
cout << " 2048GameState Init" << endl; // cout << " 2048GameState Init" << endl;
int highScore = 1000; int highScore = 1000;
game2048 = new Game2048(4, highScore); game2048 = new Game2048(4, highScore);
game2048->testInit(); game2048->testInit();
...@@ -15,24 +15,24 @@ void C2048GameState::Init() ...@@ -15,24 +15,24 @@ void C2048GameState::Init()
void C2048GameState::Pause() void C2048GameState::Pause()
{ {
cout << " 2048GameState Pause" << endl; //cout << " 2048GameState Pause" << endl;
} }
void C2048GameState::Resume() void C2048GameState::Resume()
{ {
cout << " 2048GameState Resume" << endl; //cout << " 2048GameState Resume" << endl;
} }
void C2048GameState::Cleanup() void C2048GameState::Cleanup()
{ {
cout << " 2048GameState Cleanup" << endl; //cout << " 2048GameState Cleanup" << endl;
delete game2048; delete game2048;
game2048 = nullptr; game2048 = nullptr;
} }
void C2048GameState::HandleEvents(CGameEngine* game) void C2048GameState::HandleEvents(CGameEngine* game)
{ {
cout << " 2048GameState Handle Events" << endl; // cout << " 2048GameState Handle Events" << endl;
// switch (runtimes) // switch (runtimes)
// { // {
...@@ -61,11 +61,19 @@ void C2048GameState::HandleEvents(CGameEngine* game) ...@@ -61,11 +61,19 @@ void C2048GameState::HandleEvents(CGameEngine* game)
// break; // break;
// } // }
input = game->PopInput(); while (game->newInput == false)
{
if (game->newInput)
{
break;
}
}
input = game->input;
game->newInput = false;
if (input == 'q') if (input == 'q')
{ {
game->Quit(); game->PopState();
} }
runtimes++; runtimes++;
...@@ -73,7 +81,7 @@ void C2048GameState::HandleEvents(CGameEngine* game) ...@@ -73,7 +81,7 @@ void C2048GameState::HandleEvents(CGameEngine* game)
void C2048GameState::Update(CGameEngine* game) void C2048GameState::Update(CGameEngine* game)
{ {
cout << " 2048GameState Update" << endl; // cout << " 2048GameState Update" << endl;
game2048->Shift(input); game2048->Shift(input);
if (!game2048->addNumber()) { if (!game2048->addNumber()) {
...@@ -83,7 +91,9 @@ void C2048GameState::Update(CGameEngine* game) ...@@ -83,7 +91,9 @@ void C2048GameState::Update(CGameEngine* game)
void C2048GameState::Draw(CGameEngine* game) void C2048GameState::Draw(CGameEngine* game)
{ {
cout << " 2048GameState Draw" << endl; // cout << " 2048GameState Draw" << endl;
system("stty cooked");
game2048->Draw(); game2048->Draw();
system("stty raw");
} }
...@@ -92,8 +92,9 @@ void CGameEngine::Update() ...@@ -92,8 +92,9 @@ void CGameEngine::Update()
void CGameEngine::Draw() void CGameEngine::Draw()
{ {
system("stty cooked"); system("stty cooked");
cout << "\e[2J\e[0;0H";
states.back()->Draw(this); states.back()->Draw(this);
system("stty raw"); // system("stty raw");
} }
void CGameEngine::PushInput(char input) void CGameEngine::PushInput(char input)
......
...@@ -31,6 +31,8 @@ class CGameEngine ...@@ -31,6 +31,8 @@ class CGameEngine
void PushInput(char input); void PushInput(char input);
char PopInput(); char PopInput();
bool newInput = false;
char input;
private: private:
// Gamestate Stack // Gamestate Stack
vector<CGameState*> states; vector<CGameState*> states;
......
#include <iostream> #include <iostream>
#include "MainMenuState.h" #include "MainMenuState.h"
// #include "TestState2.h"
// #include "TestState3.h"
#include "2048GameState.h" #include "2048GameState.h"
#include "SnakeState.h"
#include <string>
#include <fstream>
#include <streambuf>
using namespace std;
CMainMenuState CMainMenuState::m_MainMenuState; CMainMenuState CMainMenuState::m_MainMenuState;
void CMainMenuState::Init() void CMainMenuState::Init()
{ {
cout << " MainMenuState Init" << endl; // Load in main menu title
ifstream fs("mainmenu.txt");
fs.seekg(0, ios::end);
menuTitle.reserve(fs.tellg());
fs.seekg(0, ios::beg);
menuTitle.assign((istreambuf_iterator<char>(fs)),
istreambuf_iterator<char>());
// Add menu options
menuOptions.push_back("2048");
menuOptions.push_back("Snake");
menuOptions.push_back("Quit");
stateName = "MainMenuState"; stateName = "MainMenuState";
input = ' ';
Draw(nullptr);
} }
void CMainMenuState::Pause() void CMainMenuState::Pause()
{ {
cout << " MainMenuState Pause" << endl;
} }
void CMainMenuState::Resume() void CMainMenuState::Resume()
{ {
cout << " MainMenuState Resume" << endl; input = 0;
menuPos = 0;
} }
void CMainMenuState::Cleanup() void CMainMenuState::Cleanup()
{ {
cout << " MainMenuState Cleanup" << endl;
} }
void CMainMenuState::HandleEvents(CGameEngine* game) void CMainMenuState::HandleEvents(CGameEngine* game)
{ {
cout << " MainMenuState Handle Events" << endl; while (game->newInput == false)
{
input = ' ';
}
input = game->input;
game->newInput = false;
} }
void CMainMenuState::Update(CGameEngine* game) void CMainMenuState::Update(CGameEngine* game)
{ {
cout << " MainMenuState Update" << endl; switch(input)
{
case 'w':
menuPos--;
break;
case 's':
menuPos++;
break;
case 'd':
{
switch (menuPos)
{
case 0: //2048
game->PushState(C2048GameState::Instance());
break;
case 1: //Snake
game->PushState(CSnakeState::Instance());
break;
case 2: //Quit
game->Quit();
break;
}
break;
}
default:
break;
}
if (menuPos < 0)
{
menuPos = 0;
}
game->ChangeState(C2048GameState::Instance()); if (menuPos >= menuOptions.size())
{
menuPos = menuOptions.size() - 1;
}
} }
void CMainMenuState::Draw(CGameEngine* game) void CMainMenuState::Draw(CGameEngine* game)
{ {
cout << " MainMenuState Draw" << endl; system("stty cooked");
cout << menuTitle << endl;
for (int i = 0; i < menuOptions.size(); i++)
{
if (i == menuPos)
{
cout << " * " << menuOptions.at(i) << endl;
}
else
{
cout << " " << menuOptions.at(i) << endl;
}
}
system("stty raw");
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "GameState.h" #include "GameState.h"
#include <iostream> #include <iostream>
#include <string>
class CMainMenuState : public CGameState class CMainMenuState : public CGameState
{ {
...@@ -24,7 +25,10 @@ class CMainMenuState : public CGameState ...@@ -24,7 +25,10 @@ class CMainMenuState : public CGameState
private: private:
static CMainMenuState m_MainMenuState; static CMainMenuState m_MainMenuState;
int runtimes = 0; char input;
int menuPos;
string menuTitle;
vector<string> menuOptions;
}; };
#endif #endif
\ No newline at end of file
g++ -std=c++11 -pthread *.cpp ../2048/*.cpp ../Testings/Snake/ReadInput.cpp
./a.out
\ No newline at end of file
#include "GameEngine.h" #include "GameEngine.h"
#include "TestState1.h" #include "MainMenuState.h"
#include "../Testings/Nelson's YEET/ReadInput.h" #include "../Testings/Snake/ReadInput.h"
#include <thread> #include <thread>
#include <iostream>
// Primary Loop to be threaded // Primary Loop to be threaded
void MainLoop(CGameEngine game); void MainLoop(CGameEngine *game);
void MainInput(CGameEngine *game);
int main() int main()
{ {
CGameEngine game; CGameEngine* game = new CGameEngine;
game.Init(); game->Init();
game.ChangeState(CTestState1::Instance()); game->ChangeState(CMainMenuState::Instance());
// while (game.isRunning()) // while (game.isRunning())
// { // {
...@@ -22,30 +24,34 @@ int main() ...@@ -22,30 +24,34 @@ int main()
// } // }
thread gameloop(MainLoop, game); thread gameloop(MainLoop, game);
thread inputloop(MainInput, game);
gameloop.join(); gameloop.join();
inputloop.join();
game.Cleanup(); game->Cleanup();
return 0; return 0;
} }
// Primary Loop to be threaded // Primary Loop to be threaded
void MainLoop(CGameEngine game) void MainLoop(CGameEngine* game)
{ {
while (game.isRunning()) while (game->isRunning())
{ {
game.HandleEvents(); game->HandleEvents();
game.Update(); game->Update();
game.Draw(); game->Draw();
} }
} }
void MainInput(CGameEngine game) void MainInput(CGameEngine* game)
{ {
ReadInput reader; ReadInput reader;
while (game.isRunning()) while (game->isRunning())
{ {
game.PushInput(reader.collectInput()); // game.PushInput(reader.collectInput());
game->input = reader.collectInput();
game->newInput = true;
} }
} }
\ No newline at end of file
=============================================================
|| _____ _____ _ ||
|| / ____| / ____| | | ||
|| | | __ __ _ _ __ ___ ___| | ___ _ __ | |_ ___ _ __ ||
|| | | |_ |/ _` | '_ ` _ \ / _ \ | / _ \ '_ \| __/ _ \ '__| ||
|| | |__| | (_| | | | | | | __/ |___| __/ | | | || __/ | ||
|| \_____|\__,_|_| |_| |_|\___|\_____\___|_| |_|\__\___|_| ||
|| ||
=============================================================
\ No newline at end of file
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include "ReadInput.h" #include "ReadInput.h"
using namespace std;
char ReadInput::collectInput (){ char ReadInput::collectInput (){
system("stty raw"); system("stty raw");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment