Skip to content

Commit

Permalink
modify ChessSDL_GameLoopIteration
Browse files Browse the repository at this point in the history
compatibility issue with emscripten would result in delayed rendering
  • Loading branch information
spiroskou committed Sep 9, 2024
1 parent 5276a49 commit 6a9f23a
Show file tree
Hide file tree
Showing 5 changed files with 10,538 additions and 20 deletions.
40 changes: 20 additions & 20 deletions ChessSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ void ChessSDL_GameLoopIteration()
static bool isPieceSelected = false;
static Move move{ 0 };

if (getTurnCounter() % 2 == 0 && !QUIT) {
auto start = std::chrono::high_resolution_clock::now();
Move aiMove = findBestMove(depth);
auto end = std::chrono::high_resolution_clock::now();

std::chrono::duration<double> elapsed = end - start;
double calculationTime = elapsed.count();

if (calculationTime < 0.5) {
SDL_Delay(500);
}

QUIT = ChessSDL_MakeTheMove(aiMove);
if (QUIT) {
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#endif
}
}

while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
QUIT = true;
Expand All @@ -389,7 +409,6 @@ void ChessSDL_GameLoopIteration()
if (handleFirstClick(move, row, col, isPieceSelected) == 0) {
if (handleSecondClick(move, row, col, isPieceSelected)) {
QUIT = ChessSDL_MakeTheMove(move);

if (QUIT) {
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
Expand All @@ -405,23 +424,4 @@ void ChessSDL_GameLoopIteration()
}
}

if (getTurnCounter() % 2 == 0 && !QUIT) {
auto start = std::chrono::high_resolution_clock::now();
Move aiMove = findBestMove(depth);
auto end = std::chrono::high_resolution_clock::now();

std::chrono::duration<double> elapsed = end - start;
double calculationTime = elapsed.count();

if (calculationTime < 1.0) {
SDL_Delay(1000);
}

QUIT = ChessSDL_MakeTheMove(aiMove);
if (QUIT) {
#ifdef __EMSCRIPTEN__
emscripten_cancel_main_loop();
#endif
}
}
}
Binary file added docs/index.data
Binary file not shown.
Loading

0 comments on commit 6a9f23a

Please sign in to comment.