<commit_before>////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// This file is part of Swift2D.                                              //
//                                                                            //
// Copyright: (c) 2011-2014 <NAME> & <NAME>                    //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

// includes  -------------------------------------------------------------------
#include <swift2d/graphics/Window.hpp>
#include <swift2d/graphics/WindowManager.hpp>
#include <swift2d/utils/Logger.hpp>
#include <swift2d/settings.hpp>

#include <GLFW/glfw3.h>

namespace swift {

////////////////////////////////////////////////////////////////////////////////

Window::Window(bool debug)
  : Open(false)
  , HideCursor(false)
  , Title("Swift2D")
  , Size(math::vec2i(1024, 768))
  , window_(nullptr)
  , joystick_axis_cache_(static_cast<int>(JoystickId::JOYSTICK_NUM),
                         std::vector<float>(
                         static_cast<int>(JoystickAxisId::JOYSTICK_AXIS_NUM)))
  , joystick_button_cache_(static_cast<int>(JoystickId::JOYSTICK_NUM),
                           std::vector<int>(
                           static_cast<int>(
                           JoystickButtonId::JOYSTICK_BUTTON_NUM)))
  , vsync_dirty_(true)
  , fullscreen_dirty_(false)
  , init_glew_(true)
  //, debugger_(nullptr)
  //, log_sink_(nullptr)
  , debug_(debug) {

  Open.on_change().connect([this](bool val) {
    if (val) open();
    else     close();
  });

  SettingsWrapper::get().Settings->VSync.on_change().connect([this](bool) {
    vsync_dirty_ = true;
  });

  SettingsWrapper::get().Settings->Fullscreen.on_change().connect([this](bool) {
    fullscreen_dirty_ = true;
  });
}

////////////////////////////////////////////////////////////////////////////////

Window::~Window() {
  close();
  glfwDestroyWindow(window_);

  //delete debugger_;
  //delete log_sink_;
}


////////////////////////////////////////////////////////////////////////////////

void Window::process_input() {
  if (window_) {
    glfwPollEvents();
    update_joysticks();
  }
}

////////////////////////////////////////////////////////////////////////////////

void Window::set_active(bool active) {
  if (window_) {
    glfwMakeContextCurrent(window_);
  }
}

////////////////////////////////////////////////////////////////////////////////

void Window::display() {
  if (vsync_dirty_) {
    vsync_dirty_ = false;
    glfwSwapInterval(SettingsWrapper::get().Settings->VSync() ? 1 : 0);
  }

  if (window_) {
    glfwSwapBuffers(window_);
  }

  // if (fullscreen_dirty_) {
  //   fullscreen_dirty_ = false;

  //   close();
  //   open();
  // }
}

////////////////////////////////////////////////////////////////////////////////

bool Window::key_pressed(Key key) const {
  if (!window_) {
    return false;
  }

  return glfwGetKey(window_, (int)key) == GLFW_PRESS;
}

////////////////////////////////////////////////////////////////////////////////

math::vec2 Window::get_cursor_pos() const {
  if (!window_) {
    return math::vec2(0.f, 0.f);
  }
  double x, y;
  glfwGetCursorPos(window_, &x, &y);
  int height(render_context_.window_size.y());
  return math::vec2(x, height-y);
}

////////////////////////////////////////////////////////////////////////////////

void Window::init_context() {
  if (init_glew_) {
    init_glew_ = false;

    // init glew... seems a bit hacky, but works this way
    glewExperimental = GL_TRUE;
    auto e = glewInit();
    if (e != GLEW_OK) {
      LOG_ERROR << "Failed to initialize glew: " << glewGetErrorString(e) << std::endl;
    } else {
      LOG_MESSAGE << "Initialized OpenGL successfully." << std::endl;
    }

    glGetError();

    if (debug_) {
      /*debugger_ = new ogl::Debug();<commit_msg>added gl context output<commit_after>////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// This file is part of Swift2D.                                              //
//                                                                            //
// Copyright: (c) 2011-2014 <NAME> & <NAME>                    //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

// includes  -------------------------------------------------------------------
#include <swift2d/graphics/Window.hpp>
#include <swift2d/graphics/WindowManager.hpp>
#include <swift2d/utils/Logger.hpp>
#include <swift2d/settings.hpp>

#include <GLFW/glfw3.h>

namespace swift {

////////////////////////////////////////////////////////////////////////////////

Window::Window(bool debug)
  : Open(false)
  , HideCursor(false)
  , Title("Swift2D")
  , Size(math::vec2i(1024, 768))
  , window_(nullptr)
  , joystick_axis_cache_(static_cast<int>(JoystickId::JOYSTICK_NUM),
                         std::vector<float>(
                         static_cast<int>(JoystickAxisId::JOYSTICK_AXIS_NUM)))
  , joystick_button_cache_(static_cast<int>(JoystickId::JOYSTICK_NUM),
                           std::vector<int>(
                           static_cast<int>(
                           JoystickButtonId::JOYSTICK_BUTTON_NUM)))
  , vsync_dirty_(true)
  , fullscreen_dirty_(false)
  , init_glew_(true)
  //, debugger_(nullptr)
  //, log_sink_(nullptr)
  , debug_(debug) {

  Open.on_change().connect([this](bool val) {
    if (val) open();
    else     close();
  });

  SettingsWrapper::get().Settings->VSync.on_change().connect([this](bool) {
    vsync_dirty_ = true;
  });

  SettingsWrapper::get().Settings->Fullscreen.on_change().connect([this](bool) {
    fullscreen_dirty_ = true;
  });
}

////////////////////////////////////////////////////////////////////////////////

Window::~Window() {
  close();
  glfwDestroyWindow(window_);

  //delete debugger_;
  //delete log_sink_;
}


////////////////////////////////////////////////////////////////////////////////

void Window::process_input() {
  if (window_) {
    glfwPollEvents();
    update_joysticks();
  }
}

////////////////////////////////////////////////////////////////////////////////

void Window::set_active(bool active) {
  if (window_) {
    glfwMakeContextCurrent(window_);
  }
}

////////////////////////////////////////////////////////////////////////////////

void Window::display() {
  if (vsync_dirty_) {
    vsync_dirty_ = false;
    glfwSwapInterval(SettingsWrapper::get().Settings->VSync() ? 1 : 0);
  }

  if (window_) {
    glfwSwapBuffers(window_);
  }

  // if (fullscreen_dirty_) {
  //   fullscreen_dirty_ = false;

  //   close();
  //   open();
  // }
}

////////////////////////////////////////////////////////////////////////////////

bool Window::key_pressed(Key key) const {
  if (!window_) {
    return false;
  }

  return glfwGetKey(window_, (int)key) == GLFW_PRESS;
}

////////////////////////////////////////////////////////////////////////////////

math::vec2 Window::get_cursor_pos() const {
  if (!window_) {
    return math::vec2(0.f, 0.f);
  }
  double x, y;
  glfwGetCursorPos(window_, &x, &y);
  int height(render_context_.window_size.y());
  return math::vec2(x, height-y);
}

////////////////////////////////////////////////////////////////////////////////

void Window::init_context() {
  if (init_glew_) {
    init_glew_ = false;

    // init glew... seems a bit hacky, but works this way
    glewExperimental = GL_TRUE;
    auto e = glewInit();
    if (e != GLEW_OK) {
      LOG_ERROR << "Failed to initialize glew: " << glewGetErrorString(e) << std::endl;
    } else {
      int major = glfwGetWindowAttrib(window_, GLFW_CONTEXT_VERSION_MAJOR);
      int minor = glfwGetWindowAttrib(window_, GLFW_CONTEXT_VERSION_MINOR);
      int rev   = glfwGetWindowAttrib(window_, GLFW_CONTEXT_REVISION);
      LOG_MESSAGE << "Initialized OpenGL context " << major << "." << minor 
                  << "." << rev << " successfully." << std::endl;
    }

    glGetError();

    if (debug_) {
      /*debugger_ = new ogl::Debug();
{'fraction_non_alphanumeric': 0.32287157287157287, 'fraction_numerical': 0.007215007215007215, 'mean_word_length': 1.178365636458879, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4380077', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2161, 'n_words': 358}