use non-blocking (multithreaded) cin, update sprites and welcome chime

This commit is contained in:
2025-01-26 02:20:08 +01:00
parent d3daa584e9
commit 74062f164f
7 changed files with 45 additions and 25 deletions

View File

@@ -14,11 +14,11 @@ void Board::initVariables() {
// Wczytaj tło - bitmapę planszy. // Wczytaj tło - bitmapę planszy.
// loadFromFile() zwraca True w przypadku pomyślnego // loadFromFile() zwraca True w przypadku pomyślnego
// wczytania tekstury. // 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: // Jeśli nie uda nam się wczytać tekstury:
std::cerr << "Uwaga: Nie udalo sie wczytac " 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, " "Upewnij sie, ze plik .exe jest we wlasciwym katalogu, "
"a gra zostala w pelni wypakowana wraz z folderem \"res\".\n"; "a gra zostala w pelni wypakowana wraz z folderem \"res\".\n";
@@ -34,7 +34,7 @@ void Board::initWindow() {
sf::Vector2u textureSize = this->boardTexture.getSize(); sf::Vector2u textureSize = this->boardTexture.getSize();
// Aby okno zmieściło się na większości wyświetlaczy, // 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. // czyli dokładnie połowę z rozdzielczości tekstury planszy.
this->videoMode.width = textureSize.x / 2; this->videoMode.width = textureSize.x / 2;
this->videoMode.height = textureSize.y / 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();}); std::cin >> result;
//this->threads.push_back(std::thread([this]() { this->updateAndRender(); })); text = result;
// this->thread = std::thread(&Board::updateAndRender, this); done = true;
//this->threads.front().launch(); }
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;
} }

View File

@@ -17,6 +17,8 @@ class Board {
sf::Texture boardTexture; sf::Texture boardTexture;
sf::Sprite boardSprite; sf::Sprite boardSprite;
void _thread_wait_for_str_cin(bool &done, std::string &text);
public: public:
// Aby przekazać je do Engine // Aby przekazać je do Engine
@@ -39,10 +41,6 @@ class Board {
void run(); void run();
bool manualUpdate(); bool manualUpdate();
sf::Event lastEvent; sf::Event lastEvent;
//std::vector<std::thread> threads;
//std::vector<sf::Thread> threads;
//sf::Thread thread;
void runAsThread();
// Sterowanie oknem // Sterowanie oknem
void initVariables(); void initVariables();
@@ -51,5 +49,6 @@ class Board {
void initView(); void initView();
void smartSleep(int millis); void smartSleep(int millis);
void handleResize(unsigned int newWidth, unsigned int newHeight); void handleResize(unsigned int newWidth, unsigned int newHeight);
std::string asyncStrCin();
}; };

View File

@@ -68,7 +68,7 @@ void Engine::nextTurn() {
} else if (currentPlayer.pawnsActive > 0 && currentPlayer.pawnsActive < 4) { } else if (currentPlayer.pawnsActive > 0 && currentPlayer.pawnsActive < 4) {
// Może wyjść z bazy albo ruszyć pionek // Może wyjść z bazy albo ruszyć pionek
char choice; std::string choice;
std::cout << "Co chcesz zrobic?\n"; std::cout << "Co chcesz zrobic?\n";
std::cout << "a) Wyjsc pionkiem z bazy\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 // 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ł // białym a przed \n - aby np. żaden z użytkowników nie mógł
// napisać "a a" i potencjalnie zadecydować o ruchu oponenta. // 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; // std::cin >> choice;
choice |= 32; choice = board.asyncStrCin();
choice[0] |= 32;
if (choice == 'a') { if (choice[0] == 'a') {
pickAPlace = false; pickAPlace = false;
std::cout << currentPlayer.getName() << " wychodzi " std::cout << currentPlayer.getName() << " wychodzi "
@@ -96,7 +97,7 @@ void Engine::nextTurn() {
this->board.smartSleep(2000); this->board.smartSleep(2000);
break; break;
} else if (choice == 'b') { } else if (choice[0] == 'b') {
if (currentPlayer.pawnsActive > 1) { if (currentPlayer.pawnsActive > 1) {
pickAPlace = true; pickAPlace = true;
@@ -158,10 +159,12 @@ void Engine::nextTurn() {
break; break;
case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonReleased:
std::cout << "pretend i'm releasing a piece\n"; if (pickStatus == 4) {
this->pawnmoveBuffer.play(); std::cout << "pretend i'm releasing a piece\n";
this->board.smartSleep(1000); this->pawnmoveBuffer.play();
if (pickStatus == 4) pickStatus = 8; this->board.smartSleep(1000);
pickStatus = 8;
}
break; break;
case sf::Event::Resized: case sf::Event::Resized:

View File

@@ -222,7 +222,8 @@ void Game::run() {
int numPlayers = 2; int numPlayers = 2;
std::cout << "Podaj prosze liczbe graczy (2-4):\n> "; std::cout << "Podaj prosze liczbe graczy (2-4):\n> ";
std::cin >> numPlayers; // std::cin >> numPlayers;
numPlayers = stoi(board.asyncStrCin());
std::cout << "\n"; std::cout << "\n";
if (numPlayers < 2) numPlayers = 2; if (numPlayers < 2) numPlayers = 2;
@@ -236,7 +237,8 @@ void Game::run() {
std::string str_seed = std::to_string(std::time(nullptr)); // czas std::string str_seed = std::to_string(std::time(nullptr)); // czas
std::cout << "Wpisz nazwe gracza " << (i + 1) << " (" << colorNames[i] << "): "; 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 // Zignoruj zawartość bufora do \n, aby zapobiec nadpisywaniu ziarna
// poprzez podanie nazwy ze znakami białymi. // 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 // Jeżeli nie została zdefiniowana flaga do ignorowania ziarna użytkownika
// (deterministyczne losowanie), to pozwól na wprowadzanie ziaren. // (deterministyczne losowanie), to pozwól na wprowadzanie ziaren.
std::cout << "Wpisz ziarno gracza " << (i + 1) << " (" << colorNames[i] << "): "; std::cout << "Wpisz ziarno gracza " << (i + 1) << " (" << colorNames[i] << "): ";
std::cin >> str_seed; str_seed = board.asyncStrCin();
// std::cin >> str_seed;
#endif #endif
std::cout << "\n"; std::cout << "\n";

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB