123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #include <iostream>
- #include "rendering/Window.h"
- Window::Window(TextInput*& textInput, const WindowOptions& options)
- : textInput(textInput), window(nullptr) {
- glfwDefaultWindowHints();
- glfwWindowHint(GLFW_VISIBLE, 0);
- glfwWindowHint(GLFW_RESIZABLE, 1);
- glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
- glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
- if(options.es) {
- glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
- } else {
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, options.majorVersion);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, options.minorVersion);
- GLFWmonitor* monitor =
- options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
- window = glfwCreateWindow(options.size.width, options.size.height,
- options.name, monitor, nullptr);
- if(window == nullptr) {
- std::cout << "could not create window\n";
- return;
- }
- glfwSetWindowUserPointer(window, this);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetCharCallback(window, charCallback);
- glfwMakeContextCurrent(window);
- glfwSwapInterval(options.vsync);
- }
- Window::~Window() {
- if(window != nullptr) {
- glfwDestroyWindow(window);
- }
- }
- bool Window::hasError() const {
- return window == nullptr;
- }
- void Window::show() {
- glfwShowWindow(window);
- }
- bool Window::shouldClose() const {
- return glfwWindowShouldClose(window);
- }
- void Window::swapBuffers() {
- glfwSwapBuffers(window);
- }
- void Window::trapCursor(bool trap) {
- glfwSetInputMode(window, GLFW_CURSOR,
- trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
- }
- Size Window::getSize() const {
- Size size(0, 0);
- glfwGetWindowSize(window, &size.width, &size.height);
- return size;
- }
- Size Window::getFramebufferSize() const {
- Size size(0, 0);
- glfwGetFramebufferSize(window, &size.width, &size.height);
- return size;
- }
- bool Window::isKeyDown(int key) const {
- return glfwGetKey(window, key) == GLFW_PRESS;
- }
- bool Window::isMouseDown(int mouse) const {
- return glfwGetMouseButton(window, mouse) == GLFW_PRESS;
- }
- void Window::getMousePosition(double& x, double& y) const {
- glfwGetCursorPos(window, &x, &y);
- }
- void Window::keyCallback(GLFWwindow* w, int key, int scancode, int action,
- int mods) {
- void* p = glfwGetWindowUserPointer(w);
- if(p != nullptr) {
- TextInput* input = static_cast<Window*>(p)->textInput;
- if(input != nullptr) {
- input->onKeyEvent(key, scancode, action, mods);
- }
- }
- }
- void Window::charCallback(GLFWwindow* w, unsigned int codepoint) {
- void* p = glfwGetWindowUserPointer(w);
- if(p != nullptr) {
- TextInput* input = static_cast<Window*>(p)->textInput;
- if(input != nullptr) {
- input->onCharEvent(codepoint);
- }
- }
- }
|