use non-blocking (multithreaded) cin, update sprites and welcome chime
This commit is contained in:
@@ -14,11 +14,11 @@ void Board::initVariables() {
|
||||
// Wczytaj tło - bitmapę planszy.
|
||||
// loadFromFile() zwraca True w przypadku pomyślnego
|
||||
// wczytania tekstury.
|
||||
if (!this->boardTexture.loadFromFile("res/sprites/board.png")) {
|
||||
if (!this->boardTexture.loadFromFile("res/sprites/board2.png")) {
|
||||
|
||||
// Jeśli nie uda nam się wczytać tekstury:
|
||||
std::cerr << "Uwaga: Nie udalo sie wczytac "
|
||||
"wymaganej tekstury planszy z \"res/sprites/board.png\"!\n"
|
||||
"wymaganej tekstury planszy z \"res/sprites/board2.png\"!\n"
|
||||
"Upewnij sie, ze plik .exe jest we wlasciwym katalogu, "
|
||||
"a gra zostala w pelni wypakowana wraz z folderem \"res\".\n";
|
||||
|
||||
@@ -34,7 +34,7 @@ void Board::initWindow() {
|
||||
sf::Vector2u textureSize = this->boardTexture.getSize();
|
||||
|
||||
// Aby okno zmieściło się na większości wyświetlaczy,
|
||||
// sprawmy, aby zajmowało ono około 450x450 pikseli,
|
||||
// sprawmy, aby zajmowało ono około 550x550 pikseli,
|
||||
// czyli dokładnie połowę z rozdzielczości tekstury planszy.
|
||||
this->videoMode.width = textureSize.x / 2;
|
||||
this->videoMode.height = textureSize.y / 2;
|
||||
@@ -167,12 +167,27 @@ void Board::smartSleep(int millis) {
|
||||
}
|
||||
}
|
||||
|
||||
void Board::runAsThread() {
|
||||
void Board::_thread_wait_for_str_cin(bool &done, std::string &text) {
|
||||
std::string result;
|
||||
|
||||
//this->thread = std::thread([this]() {this->updateAndRender();});
|
||||
//this->threads.push_back(std::thread([this]() { this->updateAndRender(); }));
|
||||
// this->thread = std::thread(&Board::updateAndRender, this);
|
||||
//this->threads.front().launch();
|
||||
std::cin >> result;
|
||||
text = result;
|
||||
done = true;
|
||||
}
|
||||
|
||||
std::string Board::asyncStrCin() {
|
||||
|
||||
bool done = false;
|
||||
std::string text;
|
||||
std::thread t(&Board::_thread_wait_for_str_cin, Board(), std::ref(done), std::ref(text));
|
||||
|
||||
while(t.joinable()) {
|
||||
this->update();
|
||||
this->render();
|
||||
if (done) t.join();
|
||||
}
|
||||
|
||||
return text;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ class Board {
|
||||
sf::Texture boardTexture;
|
||||
sf::Sprite boardSprite;
|
||||
|
||||
void _thread_wait_for_str_cin(bool &done, std::string &text);
|
||||
|
||||
public:
|
||||
|
||||
// Aby przekazać je do Engine
|
||||
@@ -39,10 +41,6 @@ class Board {
|
||||
void run();
|
||||
bool manualUpdate();
|
||||
sf::Event lastEvent;
|
||||
//std::vector<std::thread> threads;
|
||||
//std::vector<sf::Thread> threads;
|
||||
//sf::Thread thread;
|
||||
void runAsThread();
|
||||
|
||||
// Sterowanie oknem
|
||||
void initVariables();
|
||||
@@ -51,5 +49,6 @@ class Board {
|
||||
void initView();
|
||||
void smartSleep(int millis);
|
||||
void handleResize(unsigned int newWidth, unsigned int newHeight);
|
||||
std::string asyncStrCin();
|
||||
|
||||
};
|
||||
@@ -68,7 +68,7 @@ void Engine::nextTurn() {
|
||||
} else if (currentPlayer.pawnsActive > 0 && currentPlayer.pawnsActive < 4) {
|
||||
|
||||
// Może wyjść z bazy albo ruszyć pionek
|
||||
char choice;
|
||||
std::string choice;
|
||||
|
||||
std::cout << "Co chcesz zrobic?\n";
|
||||
std::cout << "a) Wyjsc pionkiem z bazy\n"
|
||||
@@ -80,12 +80,13 @@ void Engine::nextTurn() {
|
||||
// Czyścimy bufor, aby zignorować cokolwiek, co pojawiło się po znaku
|
||||
// białym a przed \n - aby np. żaden z użytkowników nie mógł
|
||||
// napisać "a a" i potencjalnie zadecydować o ruchu oponenta.
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
|
||||
std::cin >> choice;
|
||||
choice |= 32;
|
||||
// std::cin >> choice;
|
||||
choice = board.asyncStrCin();
|
||||
choice[0] |= 32;
|
||||
|
||||
if (choice == 'a') {
|
||||
if (choice[0] == 'a') {
|
||||
|
||||
pickAPlace = false;
|
||||
std::cout << currentPlayer.getName() << " wychodzi "
|
||||
@@ -96,7 +97,7 @@ void Engine::nextTurn() {
|
||||
this->board.smartSleep(2000);
|
||||
break;
|
||||
|
||||
} else if (choice == 'b') {
|
||||
} else if (choice[0] == 'b') {
|
||||
|
||||
if (currentPlayer.pawnsActive > 1) {
|
||||
pickAPlace = true;
|
||||
@@ -158,10 +159,12 @@ void Engine::nextTurn() {
|
||||
break;
|
||||
|
||||
case sf::Event::MouseButtonReleased:
|
||||
if (pickStatus == 4) {
|
||||
std::cout << "pretend i'm releasing a piece\n";
|
||||
this->pawnmoveBuffer.play();
|
||||
this->board.smartSleep(1000);
|
||||
if (pickStatus == 4) pickStatus = 8;
|
||||
pickStatus = 8;
|
||||
}
|
||||
break;
|
||||
|
||||
case sf::Event::Resized:
|
||||
|
||||
@@ -222,7 +222,8 @@ void Game::run() {
|
||||
|
||||
int numPlayers = 2;
|
||||
std::cout << "Podaj prosze liczbe graczy (2-4):\n> ";
|
||||
std::cin >> numPlayers;
|
||||
// std::cin >> numPlayers;
|
||||
numPlayers = stoi(board.asyncStrCin());
|
||||
std::cout << "\n";
|
||||
|
||||
if (numPlayers < 2) numPlayers = 2;
|
||||
@@ -236,7 +237,8 @@ void Game::run() {
|
||||
std::string str_seed = std::to_string(std::time(nullptr)); // czas
|
||||
|
||||
std::cout << "Wpisz nazwe gracza " << (i + 1) << " (" << colorNames[i] << "): ";
|
||||
std::cin >> name;
|
||||
//std::cin >> name;
|
||||
name = board.asyncStrCin();
|
||||
|
||||
// Zignoruj zawartość bufora do \n, aby zapobiec nadpisywaniu ziarna
|
||||
// poprzez podanie nazwy ze znakami białymi.
|
||||
@@ -246,7 +248,8 @@ void Game::run() {
|
||||
// Jeżeli nie została zdefiniowana flaga do ignorowania ziarna użytkownika
|
||||
// (deterministyczne losowanie), to pozwól na wprowadzanie ziaren.
|
||||
std::cout << "Wpisz ziarno gracza " << (i + 1) << " (" << colorNames[i] << "): ";
|
||||
std::cin >> str_seed;
|
||||
str_seed = board.asyncStrCin();
|
||||
// std::cin >> str_seed;
|
||||
#endif
|
||||
std::cout << "\n";
|
||||
|
||||
|
||||
Binary file not shown.
BIN
Chinczyk188/res/sprites/board2.png
Normal file
BIN
Chinczyk188/res/sprites/board2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.2 KiB |
BIN
Chinczyk188/res/sprites/board2_placesOverlay.png
Normal file
BIN
Chinczyk188/res/sprites/board2_placesOverlay.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 104 KiB |
Reference in New Issue
Block a user