<commit_before>#include <unordered_map>

#include "external/glad/glad.h"
#include <GLFW/glfw3.h>
#include <imgui.h>

#include "InputHandler.h"
#include "Log.h"
#include "opengl.hpp"
#include "Window.h"

#include "external/imgui_impl_glfw_gl3.h"

static std::unordered_map<std::string, Window *> *windowMap = nullptr;

void Window::ErrorCallback(int error, char const* description)
{
  if (error == 65545 || error == 65543)
    LogInfo("Couldn't setup OpenGL, trying a lower version\n");
  else
    LogError("GLFW error %d was thrown:\n\t%s\n", error, description);
}

void Window::KeyCallback( GLFWwindow* window, int key, int scancode
                            , int action, int mods)
{
  Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
  instance->mInputHandler->FeedKeyboard(key, scancode, action, mods);

#ifdef _WIN32
  bool should_close = (key == GLFW_KEY_F4) && (mods == GLFW_MOD_ALT);
#elif defined __APPLE__
  bool should_close = (key == GLFW_KEY_Q) && (mods == GLFW_MOD_SUPER);
#elif defined __linux__
  bool should_close = (key == GLFW_KEY_Q) && (mods == GLFW_MOD_CONTROL);
#else
  bool should_close = false;
#endif
  should_close |= (key == GLFW_KEY_ESCAPE);
  if (should_close)
    glfwSetWindowShouldClose(window, true);

  ImGui_ImplGlfwGL3_KeyCallback(window, key, scancode, action, mods);
}

void Window::MouseCallback( GLFWwindow* window, int button, int action
                              , int mods
                              )
{
  Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
  instance->mInputHandler->FeedMouseButtons(button, action, mods);
}

void Window::CursorCallback(GLFWwindow* window, double x, double y)
{
  Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
  instance->mInputHandler->FeedMouseMotion(glm::vec2(x, y));
}

void Window::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
	Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
	instance->mWidth = static_cast<unsigned int>(width);
	instance->mHeight = static_cast<unsigned int>(height);
	if (instance->mCamera != nullptr)
		instance->mCamera->SetAspect(static_cast<float>(width) / static_cast<float>(height));
}


Window *Window::Create(std::string mTitle, unsigned int w, unsigned int h, unsigned int msaa, bool fullscreen, SwapStrategy swap)
{
	bool ok = Param(w > 0 && h > 0) ||
		Param(!mTitle.empty());
	if (!ok) {
		LogWarning("No window created! (%s)", mTitle.c_str());
		return nullptr;
	}
	auto elem = windowMap->find(mTitle);
	if (elem != windowMap->end()) {
		LogWarning("A window named %s already exists", mTitle.c_str());
		return nullptr;
	}
	Window *window = new Window(mTitle, w, h, msaa, fullscreen, swap);
	(*windowMap)[mTitle] = window;
	return window;
}

bool Window::Destroy(Window *window)
{
	auto elem = windowMap->find(window->GetTitle());
	if (elem == windowMap->end()) {
		LogWarning("This window isn't mine to destroy");
		return false;
	}
	windowMap->erase(window->GetTitle());
	delete window;
	return true;
}

std::string Window::GetTitle() const
{
	return mTitle;
}

Window::Window(std::string mTitle_, unsigned w_, unsigned h_, unsigned int msaa_, bool fullscreen_, SwapStrategy swap_) :
	mTitle(mTitle_), mWidth(w_), mHeight(h_), mMSAA(msaa_), mFullscreen(fullscreen_), mSwap(swap_), mWindowGLFW(nullptr), mInputHandler(nullptr), mCamera(nullptr)
{
	Show();
}

Window::~Window()
{
}

bool Window::Show()
{
	if (mWindowGLFW == nullptr) {
#ifdef __APPLE__
		glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

		glfwWindowHint(GLFW_SAMPLES, static_cast<int>(mMSAA));

		GLFWmonitor* const monitor = mFullscreen ? glfwGetPrimaryMonitor()
                                                 : nullptr;
		mWindowGLFW = glfwCreateWindow(static_cast<int>(mWidth), static_cast<int>(mHeight)<commit_msg>core/Window: Use static variables for default OpenGL version<commit_after>#include <unordered_map>

#include "external/glad/glad.h"
#include <GLFW/glfw3.h>
#include <imgui.h>

#include "InputHandler.h"
#include "Log.h"
#include "opengl.hpp"
#include "Window.h"

#include "external/imgui_impl_glfw_gl3.h"

static std::unordered_map<std::string, Window *> *windowMap = nullptr;
static int default_opengl_major_version = 4;
static int default_opengl_minor_version = 1;

void Window::ErrorCallback(int error, char const* description)
{
  if (error == 65545 || error == 65543)
    LogInfo("Couldn't setup OpenGL, trying a lower version\n");
  else
    LogError("GLFW error %d was thrown:\n\t%s\n", error, description);
}

void Window::KeyCallback( GLFWwindow* window, int key, int scancode
                            , int action, int mods)
{
  Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
  instance->mInputHandler->FeedKeyboard(key, scancode, action, mods);

#ifdef _WIN32
  bool should_close = (key == GLFW_KEY_F4) && (mods == GLFW_MOD_ALT);
#elif defined __APPLE__
  bool should_close = (key == GLFW_KEY_Q) && (mods == GLFW_MOD_SUPER);
#elif defined __linux__
  bool should_close = (key == GLFW_KEY_Q) && (mods == GLFW_MOD_CONTROL);
#else
  bool should_close = false;
#endif
  should_close |= (key == GLFW_KEY_ESCAPE);
  if (should_close)
    glfwSetWindowShouldClose(window, true);

  ImGui_ImplGlfwGL3_KeyCallback(window, key, scancode, action, mods);
}

void Window::MouseCallback( GLFWwindow* window, int button, int action
                              , int mods
                              )
{
  Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
  instance->mInputHandler->FeedMouseButtons(button, action, mods);
}

void Window::CursorCallback(GLFWwindow* window, double x, double y)
{
  Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
  instance->mInputHandler->FeedMouseMotion(glm::vec2(x, y));
}

void Window::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
	Window* instance = static_cast<Window*>(glfwGetWindowUserPointer(window));
	instance->mWidth = static_cast<unsigned int>(width);
	instance->mHeight = static_cast<unsigned int>(height);
	if (instance->mCamera != nullptr)
		instance->mCamera->SetAspect(static_cast<float>(width) / static_cast<float>(height));
}


Window *Window::Create(std::string mTitle, unsigned int w, unsigned int h, unsigned int msaa, bool fullscreen, SwapStrategy swap)
{
	bool ok = Param(w > 0 && h > 0) ||
		Param(!mTitle.empty());
	if (!ok) {
		LogWarning("No window created! (%s)", mTitle.c_str());
		return nullptr;
	}
	auto elem = windowMap->find(mTitle);
	if (elem != windowMap->end()) {
		LogWarning("A window named %s already exists", mTitle.c_str());
		return nullptr;
	}
	Window *window = new Window(mTitle, w, h, msaa, fullscreen, swap);
	(*windowMap)[mTitle] = window;
	return window;
}

bool Window::Destroy(Window *window)
{
	auto elem = windowMap->find(window->GetTitle());
	if (elem == windowMap->end()) {
		LogWarning("This window isn't mine to destroy");
		return false;
	}
	windowMap->erase(window->GetTitle());
	delete window;
	return true;
}

std::string Window::GetTitle() const
{
	return mTitle;
}

Window::Window(std::string mTitle_, unsigned w_, unsigned h_, unsigned int msaa_, bool fullscreen_, SwapStrategy swap_) :
	mTitle(mTitle_), mWidth(w_), mHeight(h_), mMSAA(msaa_), mFullscreen(fullscreen_), mSwap(swap_), mWindowGLFW(nullptr), mInputHandler(nullptr), mCamera(nullptr)
{
	Show();
}

Window::~Window()
{
}

bool Window::Show()
{
	if (mWindowGLFW == nullptr) {
#ifdef __APPLE__
		glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, default_opengl_major_version);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, default_opengl_minor_version);

		glfwWindowHint(GLFW_SAMPLES, static_cast<int>(mMSAA));

		GLFWmonitor* const monitor = mFullscreen ? glfwGetPrimaryMonitor()
                                                 : nullptr;
		mWindowGLFW = glfwCreateWindow(static_cast<int>(mWidth), static_cast<int>(mHeight)
{'fraction_non_alphanumeric': 0.13516470159231797, 'fraction_numerical': 0.005105141606904096, 'mean_word_length': 5.117472118959108, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3667347', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2780, 'n_words': 610}