remove unused parts of code, use sleep function which updates screen

This commit is contained in:
2025-01-15 03:33:44 +01:00
parent 25377a10d7
commit d82860f843
5 changed files with 27 additions and 149 deletions

View File

@@ -1,4 +1,6 @@
#include "Board.hpp" #include "Board.hpp"
#include <SFML/System/Sleep.hpp>
#include <SFML/System/Time.hpp>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>
@@ -20,7 +22,7 @@ void Board::initVariables() {
"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";
sf::sleep(sf::seconds(10)); this->smartSleep(10000);
this->window->close(); this->window->close();
} }
@@ -157,6 +159,14 @@ void Board::updateAndRender() {
} }
void Board::smartSleep(int millis) {
for (int i = 0; i < millis / 10; i++) {
sf::sleep(sf::milliseconds(10));
this->update();
this->render();
}
}
void Board::runAsThread() { void Board::runAsThread() {
//this->thread = std::thread([this]() {this->updateAndRender();}); //this->thread = std::thread([this]() {this->updateAndRender();});

View File

@@ -49,6 +49,7 @@ class Board {
void initWindow(); void initWindow();
void initBoard(); void initBoard();
void initView(); void initView();
void smartSleep(int millis);
void handleResize(unsigned int newWidth, unsigned int newHeight); void handleResize(unsigned int newWidth, unsigned int newHeight);
}; };

View File

@@ -52,7 +52,7 @@ void Engine::nextTurn() {
int diceRoll = rollDice(currentPlayer.getSeed(), currentPlayer.getRollCount()); int diceRoll = rollDice(currentPlayer.getSeed(), currentPlayer.getRollCount());
std::cout << "Wylosowano " << diceRoll << ".\n"; std::cout << "Wylosowano " << diceRoll << ".\n";
currentPlayer.incrementRollCount(); currentPlayer.incrementRollCount();
sf::sleep(sf::milliseconds(1000)); this->board.smartSleep(1000);
// Wybieranie pionka // Wybieranie pionka
bool pickAPlace = false; bool pickAPlace = false;
@@ -64,7 +64,7 @@ void Engine::nextTurn() {
currentPlayer.pawnsAtBase--; currentPlayer.pawnsAtBase--;
currentPlayer.pawnsActive++; currentPlayer.pawnsActive++;
this->pawnmoveBuffer.play(); this->pawnmoveBuffer.play();
sf::sleep(sf::milliseconds(2000)); this->board.smartSleep(2000);
} 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
@@ -87,7 +87,7 @@ void Engine::nextTurn() {
currentPlayer.pawnsAtBase--; currentPlayer.pawnsAtBase--;
currentPlayer.pawnsActive++; currentPlayer.pawnsActive++;
this->pawnmoveBuffer.play(); this->pawnmoveBuffer.play();
sf::sleep(sf::milliseconds(2000)); this->board.smartSleep(2000);
break; break;
} else if (choice == 'b') { } else if (choice == 'b') {
@@ -98,7 +98,7 @@ void Engine::nextTurn() {
std::cout << currentPlayer.getName() << " rusza jedyny pionek " std::cout << currentPlayer.getName() << " rusza jedyny pionek "
<< diceRoll << " pol do przodu.\n"; << diceRoll << " pol do przodu.\n";
this->pawnmoveBuffer.play(); this->pawnmoveBuffer.play();
sf::sleep(sf::milliseconds(2000)); this->board.smartSleep(2000);
} }
break; break;
@@ -120,7 +120,7 @@ void Engine::nextTurn() {
std::cout << currentPlayer.getName() << " rusza jedyny pionek " std::cout << currentPlayer.getName() << " rusza jedyny pionek "
<< diceRoll << " pol do przodu.\n"; << diceRoll << " pol do przodu.\n";
this->pawnmoveBuffer.play(); this->pawnmoveBuffer.play();
sf::sleep(sf::milliseconds(2000)); this->board.smartSleep(2000);
} }
} }
@@ -137,7 +137,7 @@ void Engine::nextTurn() {
while (board.manualUpdate()) { while (board.manualUpdate()) {
sf::Event currentEvent = board.lastEvent; sf::Event currentEvent = board.lastEvent;
std::cout << "this-evtype: " << currentEvent.type << "\n"; //std::cout << "currentEvent.type: " << currentEvent.type << "\n";
switch (currentEvent.type) { switch (currentEvent.type) {
case sf::Event::Closed: case sf::Event::Closed:
@@ -154,7 +154,7 @@ void Engine::nextTurn() {
case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonReleased:
std::cout << "pretend i'm releasing a piece\n"; std::cout << "pretend i'm releasing a piece\n";
this->pawnmoveBuffer.play(); this->pawnmoveBuffer.play();
sf::sleep(sf::seconds(1.0f)); this->board.smartSleep(1000);
if (pickStatus == 4) pickStatus = 8; if (pickStatus == 4) pickStatus = 8;
break; break;
@@ -183,7 +183,7 @@ void Engine::nextTurn() {
currentPlayerIndex = (currentPlayerIndex + 1) % players.size(); currentPlayerIndex = (currentPlayerIndex + 1) % players.size();
} }
sf::sleep(sf::milliseconds(1000)); this->board.smartSleep(1000);
std::cout << "\n"; std::cout << "\n";
return; return;
} }

View File

@@ -162,24 +162,8 @@ void Game::playStartTune() {
void Game::initVariables() { void Game::initVariables() {
// Ustaw wartości na domyślne. // Ustaw wartości na domyślne.
this->window = nullptr;
this->leaderboardLocation = LEADERBOARD_FILE; this->leaderboardLocation = LEADERBOARD_FILE;
// Wczytaj tło - bitmapę planszy.
// loadFromFile() zwraca True w przypadku pomyślnego
// wczytania tekstury.
if (!this->boardTexture.loadFromFile("res/sprites/board.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"
"Upewnij sie, ze plik .exe jest we wlasciwym katalogu, "
"a gra zostala w pelni wypakowana wraz z folderem \"res\".\n";
sf::sleep(sf::seconds(10));
this->window->close();
}
// Spróbuj wczytać listę wyników z pliku // Spróbuj wczytać listę wyników z pliku
this->readLeaderboards(leaderboardLocation); this->readLeaderboards(leaderboardLocation);
// Zapisz po cichu (stąd false) kopię zapasową listy wyników // Zapisz po cichu (stąd false) kopię zapasową listy wyników
@@ -187,63 +171,6 @@ void Game::initVariables() {
} }
void Game::initWindow() {
// Dwuwymiarowy, wektor typu (u)nsigned int.
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,
// czyli dokładnie połowę z rozdzielczości tekstury planszy.
this->videoMode.width = textureSize.x / 2;
this->videoMode.height = textureSize.y / 2;
// Stwórz okno.
this->window = new sf::RenderWindow(
this->videoMode,
"Okno planszy - Chinczyk188",
sf::Style::Default);
}
void Game::initBoard() {
// Wczytaj wymaganą teksturę planszy
this->boardSprite.setTexture(this->boardTexture);
// Ustaw pozycję grafiki.
this->boardSprite.setPosition(0.0f, 0.0f);
}
void Game::initView() {
// Ze względu na to, że długość krawędzi okna jest dwa razy mniejsza
// od tekstury planszy, powinniśmy to zrównoważyć powiększając
// widok o tą samą wartość.
sf::Vector2f viewSize(
static_cast<float>(2 * this->videoMode.width),
static_cast<float>(2 * this->videoMode.height)
);
this->view.setSize(viewSize);
this->view.setCenter(viewSize.x / 2.0f, viewSize.y / 2.0f);
// Ustawiamy widok na wyznaczone wartości.
this->window->setView(this->view);
}
void Game::handleResize(unsigned int newWidth, unsigned int newHeight) {
// Aby zapobiec sytuacji, w której po zmianie rozmiaru
// okno byłoby większe od rozdzielczości ekranu,
// niech krawędź okna wynosi minimum z nowej szerokości i wysokości,
// zachowując przy tym proporcję 1:1 planszy.
unsigned int newSize = std::min(newWidth, newHeight);
this->window->setSize(sf::Vector2u(newSize, newSize));
}
Game::Game() { Game::Game() {
//: //:
// thread(std::thread([this]() { // thread(std::thread([this]() {
@@ -256,6 +183,7 @@ Game::Game() {
// Konstruktor klasy. Klasę Game tworzymy poprzez // Konstruktor klasy. Klasę Game tworzymy poprzez
// ustawienie domyślnych wartości i stworzenie okna. // ustawienie domyślnych wartości i stworzenie okna.
this->initVariables();
this->board.initVariables(); this->board.initVariables();
this->board.initWindow(); this->board.initWindow();
this->board.initBoard(); this->board.initBoard();
@@ -273,7 +201,6 @@ Game::~Game() {
// W destruktorze zapisujemy dane i usuwamy okno. // W destruktorze zapisujemy dane i usuwamy okno.
this->dumpLeaderboards(); this->dumpLeaderboards();
delete this->window; // alternatywnie: this->window->close();
} }
@@ -285,55 +212,6 @@ const bool Game::running() const {
} }
void Game::pollEvents() {
while (this->window->pollEvent(this->ev)) {
switch (this->ev.type) {
case sf::Event::Closed:
this->window->close();
break;
case sf::Event::Resized:
this->handleResize(this->ev.size.width, this->ev.size.height);
break;
case sf::Event::KeyPressed:
if (this->ev.key.code == sf::Keyboard::Escape)
this->window->close();
break;
}
}
}
void Game::update() {
// Obecnie wrapper dla metody
// pollEvents(), nasłuchiwanie wydarzeń.
this->pollEvents();
}
void Game::render() {
// Mechanizm renderowania
// Czyszczenie ekranu
this->window->clear(sf::Color::White);
// Rysowanie tła
this->window->draw(this->boardSprite);
// Mechanizm wyświetlania
// W celu wyświetlenia na ekranie wyniku renderowania
this->window->display();
}
void Game::run() { void Game::run() {
Engine engine; Engine engine;
@@ -401,7 +279,12 @@ void Game::run() {
this->printLeaderboards(); this->printLeaderboards();
std::cout << "\n"; std::cout << "\n";
sf::sleep(sf::seconds(10.0f)); // Zapisz do pliku CSV
this->dumpLeaderboards(this->leaderboardLocation);
this->dumpLeaderboards("." + this->leaderboardLocation + ".old", false);
this->board.smartSleep(10000);
board.closeWindow(); board.closeWindow();
} }

View File

@@ -11,13 +11,6 @@ class Game {
private: private:
// Wskaźnik na okno.
sf::RenderWindow* window;
sf::VideoMode videoMode;
sf::Texture boardTexture;
sf::Sprite boardSprite;
std::vector<std::string> leaderboardName; std::vector<std::string> leaderboardName;
std::vector<unsigned int> leaderboardWins; std::vector<unsigned int> leaderboardWins;
sf::Music soundBuffer1; sf::Music soundBuffer1;
@@ -25,18 +18,12 @@ class Game {
// Metody // Metody
void initVariables(); void initVariables();
void initWindow();
void initBoard();
void initView();
void playStartTune(); void playStartTune();
void handleResize(unsigned int newWidth, unsigned int newHeight);
public: public:
// Aby przekazać je do Engine // Aby przekazać je do Engine
sf::Event ev;
std::string leaderboardLocation; std::string leaderboardLocation;
sf::View view;
// Konstruktor, destruktor // Konstruktor, destruktor
Game(); Game();
@@ -46,9 +33,6 @@ class Game {
const bool running() const; const bool running() const;
// Metody klasy // Metody klasy
void pollEvents();
void update();
void render();
void printGameWelcomeText() const; void printGameWelcomeText() const;
void readLeaderboards(std::string leaderboardLocation); void readLeaderboards(std::string leaderboardLocation);
void dumpLeaderboards(std::string leaderboardLocation = LEADERBOARD_FILE, bool verbose = true) const; void dumpLeaderboards(std::string leaderboardLocation = LEADERBOARD_FILE, bool verbose = true) const;