<commit_before>#ifndef GAMECOMPONENT_H
#define GAMECOMPONENT_H

#include <gui/component.h>

#include "tetrisgame.h"

class GameComponent : public gui::Component {
public:
	GameComponent() {
		setPreferredSize((float) tetrisGame_.getWidth(), (float) tetrisGame_.getHeight());
	}

	void draw(float deltaTime) override {
		glPushMatrix();
		gui::Dimension dim = getSize();

		// Centers the game and holds the correct proportions. The sides is transparent.
		if (tetrisGame_.getWidth() / dim.width_ > tetrisGame_.getHeight() / dim.height_) {
			// Black sides, up and down.
			double scale = (double) dim.width_ / tetrisGame_.getWidth();
			glTranslated(0, (dim.height_ - scale * tetrisGame_.getHeight()) * 0.5, 0);
			glScaled(scale, scale, 1);
		} else {
			// Black sides, left and right.
			double scale = (double) dim.height_ / tetrisGame_.getHeight();
			glTranslated((dim.width_ - scale*tetrisGame_.getWidth()) * 0.5, 0, 0);
			glScaled(scale,scale,1);
		}

		tetrisGame_.draw();
		glPopMatrix();

		tetrisGame_.update(deltaTime * 1000);
	}

	void addCallback(mw::Signal<NetworkEventPtr>::Callback callback) {
		tetrisGame_.addCallback(callback);
	}

	void closeGame() {
		tetrisGame_.closeGame();
	}

	void createLocalGame(const std::vector<DevicePtr>& devices, int nbrOfComputers) {
		tetrisGame_.createLocalGame(devices, nbrOfComputers);
	}

	void createLocalGame(const std::vector<DevicePtr>& devices, int nbrOfComputers, int width, int height, int maxLevel) {
		tetrisGame_.createLocalGame(devices, 0, width, height, maxLevel);
	}

	void createServerGame(const std::vector<DevicePtr>& devices, int nbrOfComputers, int port, int width, int height, int maxLevel) {
		tetrisGame_.createServerGame(devices, nbrOfComputers, port, width, height, maxLevel);
	}

	void createClientGame(const std::vector<DevicePtr>& devices, int nbrOfComputers, int port, std::string ip, int maxLevel) {
		tetrisGame_.createClientGame(devices, nbrOfComputers, port, ip, maxLevel);
	}

	void startGame() {
		tetrisGame_.startGame();
	}

	void restartGame() {
		tetrisGame_.restartGame();
	}

	void pause() {
		tetrisGame_.pause();
	}

	void setAis(const Ai& ai1, const Ai& ai2, const Ai& ai3, const Ai& ai4) {
		tetrisGame_.setAis(ai1, ai2, ai3, ai4);
	}

private:
	TetrisGame tetrisGame_;
};

#endif // GAMECOMPONENT_H<commit_msg>The game is ran in a new thread.<commit_after>#ifndef GAMECOMPONENT_H
#define GAMECOMPONENT_H

#include "tetrisgame.h"
#include "gamesound.h"

#include <gui/component.h>
#include <mw/sound.h>

#include <thread>
#include <mutex>

class GameComponent : public gui::Component {
public:
	GameComponent() : quit_(false) {
		setPreferredSize((float) tetrisGame_.getWidth(), (float) tetrisGame_.getHeight());
		thread_ = std::thread(std::bind(&GameComponent::run,this));
		networkEvents_.connect(std::bind(&GameComponent::callNetworkEvents, this, std::placeholders::_1));
	}

	~GameComponent() {
		mutex_.lock();
		quit_ = true;
		mutex_.unlock();
		thread_.join();
	}

	void draw(float deltaTime) override {
		glPushMatrix();
		gui::Dimension dim = getSize();
		mutex_.lock();
		// Centers the game and holds the correct proportions. The sides is transparent.
		if (tetrisGame_.getWidth() / dim.width_ > tetrisGame_.getHeight() / dim.height_) {
			// Black sides, up and down.
			double scale = (double) dim.width_ / tetrisGame_.getWidth();
			glTranslated(0, (dim.height_ - scale * tetrisGame_.getHeight()) * 0.5, 0);
			glScaled(scale, scale, 1);
		} else {
			// Black sides, left and right.
			double scale = (double) dim.height_ / tetrisGame_.getHeight();
			glTranslated((dim.width_ - scale*tetrisGame_.getWidth()) * 0.5, 0, 0);
			glScaled(scale,scale,1);
		}

		tetrisGame_.draw();
		glPopMatrix();
		mutex_.unlock();
	}

	void addCallback(mw::Signal<NetworkEventPtr>::Callback callback) {
		mutex_.lock();
		networkEvents_.connect(callback);
		mutex_.unlock();
	}

	void closeGame() {
		mutex_.lock();
		tetrisGame_.closeGame();
		mutex_.unlock();
	}

	void createLocalGame(const std::vector<DevicePtr>& devices, int nbrOfComputers) {
		mutex_.lock();
		tetrisGame_.createLocalGame(devices, nbrOfComputers);
		mutex_.unlock();
	}

	void createLocalGame(const std::vector<DevicePtr>& devices, int nbrOfComputers, int width, int height, int maxLevel) {
		mutex_.lock();
		tetrisGame_.createLocalGame(devices, nbrOfComputers, width, height, maxLevel);
		mutex_.unlock();
	}

	void createServerGame(const std::vector<DevicePtr>& devices, int nbrOfComputers, int port, int width, int height, int maxLevel) {
		mutex_.lock();
		tetrisGame_.createServerGame(devices, nbrOfComputers, port, width, height, maxLevel);
		mutex_.unlock();
	}

	void createClientGame(const std::vector<DevicePtr>& devices, int nbrOfComputers, int port, std::string ip, int maxLevel) {
		mutex_.lock();
		tetrisGame_.createClientGame(devices, nbrOfComputers, port, ip, maxLevel);
		mutex_.unlock();
	}

	void startGame() {
		mutex_.lock();
		tetrisGame_.startGame();
		mutex_.unlock();
	}

	void restartGame() {
		mutex_.lock();
		tetrisGame_.restartGame();
		mutex_.unlock();
	}

	void pause() {
		mutex_.lock();
		tetrisGame_.pause();
		mutex_.unlock();
	}

	void setAis(const Ai& ai1, const Ai& ai2, const Ai& ai3, const Ai& ai4) {
		mutex_.lock();
		tetrisGame_.setAis(ai1, ai2, ai3, ai4);
		mutex_.unlock();
	}

private:
	// Is called by the thread.
	void run() {
		mutex_.lock();
		bool quit = quit_;
		mutex_.unlock();
		while (!quit) {
			tetrisGame_.update(1);
			mutex_.lock();
			quit = quit_;
			mutex_.unlock();
			std::this_thread::sleep_for(std::chrono::milliseconds(1));
		}
	}

	// Makes callbacks thread safe.
	void callNetworkEvents(NetworkEventPtr networkEvent) {
		mutex_.lock();
		networkEvents_(networkEvent);
		mutex_.unlock();
	}

	void soundEffects(GameEvent gameEvent) {
		mw::Sound sound;
		switch (gameEvent) {
			case GameEvent::GRAVITY_MOVES_BLOCK:
				break;
			case GameEvent::BLOCK_COLLISION:
				sound = soundBlockCollision;
				break;
			case GameEvent::PLAYER_ROTATES_BLOCK:
				break;
			case GameEvent::ONE_ROW_REMOVED:
				// Fall through
			case GameEvent::TWO_ROW_REMOVED:
				// Fall through
			case GameEvent::THREE_ROW_REMOVED:
				sound = soundRowRemoved;
				break;
			case GameEvent::FOUR_ROW_REMOVED:
				sound = soundTetris;
				break;
			case GameEvent::GAME_OVER:
				break;
			default:
				break;
		}
		sound.play();
	}

	mw::Signal<NetworkEventPtr> networkEvents_;
	TetrisGame tetrisGame_;
	std::mutex mutex_;
	std::thread thread_;
	bool quit_;

	// Fix timestep.
	Uint32 timeStep_;
	Uint32 accumulator_;
};

#endif // GAMECOMPONENT_H
{'fraction_non_alphanumeric': 0.13644660784910254, 'fraction_numerical': 0.007301490721022209, 'mean_word_length': 4.605285592497869, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '1314216', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2249, 'n_words': 498}