<commit_before>#include "Engine.h"

#include "Globals.h"
#include "../Field.h" // for FlagCode

#include <SDL.h>
#include <SDL_image.h>
#include <cstdio>

namespace AGE
{

Engine::Engine() :
  _isInitialized(false),
  _isClosed(false),
  _board(startingBoardHeight, startingBoardWidth)
{
  _board.newGame(startingTotalMines);
}


Engine::~Engine()
{
  close();
}


bool Engine::init()
{
  bool successFlag = true;
  if (!_isInitialized)
  {
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
      printf("SDL failed to initialize.\nSDL error: %s\n", SDL_GetError());
      successFlag = false;
    }
    else
    {
      // Sets linear texture filtering:
      if (!SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1"))
      {
        printf("Warning: Could not enable linear texture filtering.\n");
      }

      window = SDL_CreateWindow(
          windowTitle,
          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
          screenWidth, screenHeight, SDL_WINDOW_SHOWN
          );
      if (nullptr == window)
      {
        printf("SDL window could not be created!\nSDL error: %s\n",
            SDL_GetError());
        successFlag = false;
      }
      else
      {
        renderer = SDL_CreateRenderer(window, -1,
            SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        if (nullptr == renderer)
        {
          printf("Could not create a vsynced renderer.\n");
          printf("SDL error: %s\n", SDL_GetError());
          successFlag = false;
        }
        else
        {
          int imgFlags = IMG_INIT_PNG;
          if (!(IMG_Init(imgFlags) & imgFlags))
          {
            printf("SDL_Image module could not initialize.\n");
            printf("SDL_image error: %s\n", IMG_GetError());
            successFlag = false;
          }
          else
          {
            screenSurface = SDL_GetWindowSurface(window);
          }
        }
      }
    }

    _isInitialized = true;
  }
  return successFlag;
}


bool Engine::loadMedia()
{
  bool successFlag = true;

  boardSprite.loadFromFile("./Graphics/Tiles.png", renderer);

  return successFlag;
}


void Engine::startLoop()
{
  SDL_Event sdlEvent;
  bool quitFlag = false;
  while (!quitFlag)
  {
    while (0 != SDL_PollEvent(&sdlEvent))
    {
      // --- Processes user's input: ---

      // Case: user quits the application:
      if (SDL_QUIT == sdlEvent.type)
      {
        quitFlag = true;
      }

      // Case: user clicks with a mouse:
      else if (SDL_MOUSEBUTTONDOWN == sdlEvent.type)
      {
        int x, y;
        Uint32 buttonState = SDL_GetMouseState(&x, &y);

        int tileX = x / TILE_SIZE;
        int tileY = y / TILE_SIZE;
        printf("Tile: (y=%d, x=%d)\n", tileY, tileX);

        // Left mouse button:
        if (buttonState & SDL_BUTTON(SDL_BUTTON_LEFT))
        {
          printf("[Mouse] Left button pressed.\n");
          _board.discover(tileY, tileX);
        }

        // Right mouse button:
        else if (buttonState & SDL_BUTTON(SDL_BUTTON_RIGHT))
        {
          printf("[Mouse] Right button pressed.\n");
          _board.cycleFlag(tileY, tileX);
        }

        // Middle mouse button:
        else if (buttonState & SDL_BUTTON(SDL_BUTTON_MIDDLE))
        {
          printf("[Mouse] Middle button pressed.\n");
          _board.newGame(_board.totalMines());
        }
      }

      // --- Redraws the board: ---
      draw();
    }
  }
}


void Engine::close()
{
  if (!_isClosed)
  {
    boardSprite.free();

    SDL_DestroyRenderer(renderer);
    renderer = nullptr;
    SDL_DestroyWindow(window);
    window = nullptr;

    IMG_Quit();
    SDL_Quit();

    _isClosed = true;
  }
}

// private methods:

void Engine::draw()
{
  SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
  SDL_RenderClear(renderer);

  // Draws only known fields (while the game is still in progress)
  if ((_board.state() == GameState::FRESH) ||
      (_board.state() == GameState::IN_PROGRESS))
  {<commit_msg>Use Logger utility in the AGE::Engine class<commit_after>#include "Engine.h"

#include "Globals.h"
#include "../Field.h" // for FlagCode
#include "../Log/Logger.h"

#include <SDL.h>
#include <SDL_image.h>

namespace AGE
{

Engine::Engine() :
  _isInitialized(false),
  _isClosed(false),
  _board(startingBoardHeight, startingBoardWidth)
{
  Log::VERBOSE() << "Entering the Engine class constructor...";
  _board.newGame(startingTotalMines);
}


Engine::~Engine()
{
  Log::VERBOSE() << "Entering the Engine class destructor...";
  close();
}


bool Engine::init()
{
  Log::VERBOSE() << "Entering the Engine::init() method...";
  bool successFlag = true;
  if (!_isInitialized)
  {
    Log::DEBUG() << "Initializing the SDL";
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
      Log::ERROR() << "SDL failed to initialize!";
      Log::ERROR() << "SDL error: " << SDL_GetError();
      successFlag = false;
    }
    else
    {
      // Sets linear texture filtering:
      Log::DEBUG() << "Setting linear texture filtering";
      if (!SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1"))
      {
        Log::WARNING() << "Could not enable linear texture filtering.";
      }

      Log::VERBOSE() << "Creating the game window";
      window = SDL_CreateWindow(
          windowTitle,
          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
          screenWidth, screenHeight, SDL_WINDOW_SHOWN
          );
      if (nullptr == window)
      {
        Log::ERROR() << "SDL window could not be created!";
        Log::ERROR() << "SDL error: " << SDL_GetError();
        successFlag = false;
      }
      else
      {
        Log::VERBOSE() << "Creating a vertical-synced renderer";
        renderer = SDL_CreateRenderer(window, -1,
            SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        if (nullptr == renderer)
        {
          Log::ERROR() << "Could not create a vsynced renderer!";
          Log::ERROR() << "SDL error: " << SDL_GetError();
          successFlag = false;
        }
        else
        {
          Log::VERBOSE() << "Initializing the IMG module";
          int imgFlags = IMG_INIT_PNG;
          if (!(IMG_Init(imgFlags) & imgFlags))
          {
            Log::ERROR() << "SDL_Image module could not initialize.";
            Log::ERROR() << "SDL_image error: ", IMG_GetError();
            successFlag = false;
          }
          else
          {
            screenSurface = SDL_GetWindowSurface(window);
          }
        }
      }
    }

    _isInitialized = true;
  }
  return successFlag;
}


bool Engine::loadMedia()
{
  Log::VERBOSE() << "Entering the Engine::loadMedia() method...";
  bool successFlag = true;

  Log::VERBOSE() << "Loading the Board sprite";
  boardSprite.loadFromFile("./Graphics/Tiles.png", renderer);

  return successFlag;
}


void Engine::startLoop()
{
  Log::VERBOSE() << "Entering the Engine::startLoop() method...";
  SDL_Event sdlEvent;
  bool quitFlag = false;
  while (!quitFlag)
  {
    while (0 != SDL_PollEvent(&sdlEvent))
    {
      // --- Processes user's input: ---

      // Case: user quits the application:
      if (SDL_QUIT == sdlEvent.type)
      {
        quitFlag = true;
      }

      // Case: user clicks with a mouse:
      else if (SDL_MOUSEBUTTONDOWN == sdlEvent.type)
      {
        int x, y;
        Uint32 buttonState = SDL_GetMouseState(&x, &y);

        int tileX = x / TILE_SIZE;
        int tileY = y / TILE_SIZE;

        // Left mouse button:
        if (buttonState & SDL_BUTTON(SDL_BUTTON_LEFT))
        {
          Log::DEBUG() << "[Mouse] LMB (" << x << ", " << y << ") "
              << "[tile: (" << tileX << ", " << tileY << "])";
          _board.discover(tileY, tileX);
        }

        // Right mouse button:
        else if (buttonState & SDL_BUTTON(SDL_BUTTON_RIGHT))
        {
          Log::DEBUG() << "[Mouse] RMB (" << x << ", " << y << ") "
              << "[tile: (" << tileX << ", " << tileY << "])";
          _board.cycleFlag(tileY, tileX);
        }

        // Middle mouse button:
        else if (buttonState & SDL_BUTTON(SDL_BUTTON_MIDDLE))
        {
          Log::DEBUG() << "[Mouse] MMB (" << x << ", " << y << ") "
              << "[tile: (" << tileX << ", " << tileY << "])";
          _board.newGame(_board.totalMines());
        }
      }

      // --- Redraws the board: ---
      draw();
    }
  }
}


void Engine::close()
{
  Log::VERBOSE() << "Entering the Engine::close() method...";
  if (!_isClosed)
  {
    boardSprite.free();

    Log::DEBUG() << "[SDL] Destroying the renderer";
    SDL_DestroyRenderer(renderer);
    renderer = nullptr;
    Log::DEBUG() << "[SDL] Destroying the game window";
    SDL_DestroyWindow(window);
    window = nullptr;

    Log::DEBUG() << "[SDL] Quitting the SDL";
    IMG_Quit();
    SDL_Quit();

    _isClosed = true;
  }
}

// private methods:

void Engine::draw()
{
  Log::VERBOSE() << "Entering the Engine::Draw() method...";
  SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
  SDL_RenderClear(renderer);

  // Draws only known fields (while the game is still in progress)
  if ((_board.state() == GameState::FRESH) ||
      (_board.state() == GameState::IN_PROGRESS))
  {
{'fraction_non_alphanumeric': 0.14621609798775154, 'fraction_numerical': 0.003499562554680665, 'mean_word_length': 2.2544483985765122, 'pattern_counts': {'":': 0, '<': 126, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3466856', 'n_tokens_mistral': 3099, 'n_tokens_neox': 2957, 'n_words': 647}