From 8eeb05d8953eef4e889c443cf6a29b62b3a5d07e Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Sat, 16 Mar 2024 03:11:50 -0500 Subject: [PATCH] Created custom Window class --- .gitignore | 3 +- CMakeLists.txt | 7 +++-- includes/Util.h | 15 ++++++++++ includes/Window.h | 34 +++++++++++++++++++++ source/Window.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++ source/main.cpp | 27 ++++------------- 6 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 includes/Util.h create mode 100644 includes/Window.h create mode 100644 source/Window.cpp diff --git a/.gitignore b/.gitignore index d163863..01f9cb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ \ No newline at end of file +build/ +.vscode/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a3e67c7..0f0b04f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,8 @@ cmake_minimum_required(VERSION 3.10) -project(Multithreading) +project(Multithreading VERSION 0.1.0 LANGUAGES C CXX) + +include(CTest) +enable_testing() set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) @@ -8,7 +11,7 @@ include(CPACK) include_directories(./dep ./includes) link_directories(../libs/glfw-3.3.9/build/src) -add_executable(main source/main.cpp source/funcs.cpp source/shader.cpp ../libs/glad/src/glad.c) +add_executable(main source/main.cpp source/Window.cpp source/funcs.cpp source/shader.cpp ../libs/glad/src/glad.c) target_link_libraries(main glfw3) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) diff --git a/includes/Util.h b/includes/Util.h new file mode 100644 index 0000000..55baee0 --- /dev/null +++ b/includes/Util.h @@ -0,0 +1,15 @@ +#pragma once +#ifndef _NB_UTIL +#define _NB_UTIL + +#include + +namespace nb{ + +template +struct Size { + std::array vals; +}; + +}; +#endif \ No newline at end of file diff --git a/includes/Window.h b/includes/Window.h new file mode 100644 index 0000000..e6bd030 --- /dev/null +++ b/includes/Window.h @@ -0,0 +1,34 @@ +#pragma once +#ifndef _NB_WINDOW +#define _NB_WINDOW + +#include +#include +#include +#include +#include +namespace NB { + +class NBWindow { +public: + NBWindow(const std::array, const char* initName, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL); + NBWindow(const uint16_t, const uint16_t, const char* initName, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL); + + int init(); + GLFWwindow* getWindow() const; + std::array getSize() const; + std::string getName() const; + void resize(const std::array); + void resize(const uint16_t x, const uint16_t y); + +private: + std::array windowSize; + std::string windowName; + bool ready=false, running=false; + GLFWwindow* window; + GLFWmonitor* monitor; + GLFWwindow* shareWindow; +}; + +}; +#endif \ No newline at end of file diff --git a/source/Window.cpp b/source/Window.cpp new file mode 100644 index 0000000..e19b889 --- /dev/null +++ b/source/Window.cpp @@ -0,0 +1,75 @@ +#include "Window.h" +namespace NB { + +NBWindow::NBWindow(const uint16_t x, const uint16_t y, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) { + windowSize = {x, y}; + windowName = std::string(initName); + monitor = initMonitor; + shareWindow = initWindow; + + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + ready = true; +} + +NBWindow::NBWindow(const std::array initSize, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) : + NBWindow(initSize[0], initSize[1], initName, initMonitor, initWindow){} + +int NBWindow::init() { + if (!ready) { + std::cout << "NB::NBWINDOW::NOT READY\n"; + return -1; + } + + window = glfwCreateWindow(windowSize[0], windowSize[0], windowName.c_str(), monitor, shareWindow); + if (window == NULL) { + std::cout << "NB::NBWINDOW::COULD NOT CREATE WINDOW\n"; + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + + if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { + std::cout << "NB::NBWINDOW::COULD NOT INITIALIZE GLAD\n"; + glfwTerminate(); + return -1; + } + + std::cout << windowSize[0] << " by " << windowSize[1] << "\n"; + glViewport(0, 0, windowSize[0], windowSize[1]); + running=true; + return 0; +} + +GLFWwindow* NBWindow::getWindow() const { + return window; +} + +std::array NBWindow::getSize() const { + return windowSize; +} + +std::string NBWindow::getName() const { + return windowName; +} + +void NBWindow::resize(const std::array newSize) { + windowSize = newSize; + if (running) { + glViewport(0, 0, windowSize[0], windowSize[1]); + glfwSetWindowSize(window, windowSize[0], windowSize[1]); + } +} + +void NBWindow::resize(const uint16_t x, const uint16_t y) { + windowSize = {x, y}; + if (running) { + glViewport(0, 0, windowSize[0], windowSize[1]); + glfwSetWindowSize(window, windowSize[0], windowSize[1]); + } +} + +}; \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 36210ba..9c314c7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,33 +1,20 @@ #include #include -#include +#include "Window.h" #include "funcs.h" #include "shader.h" int main() { std::cout << "Hello World!\n"; - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - GLFWwindow* window = glfwCreateWindow(800, 600, "Howdy!", NULL, NULL); - if (window == NULL) { - std::cout << "Could not open window!\n"; - glfwTerminate(); - return -1; - } - glfwMakeContextCurrent(window); - - if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { - std::cout << "Could not initialize GLAD!\n"; - return -1; - } - glViewport(0, 0, 800, 600); + NB::NBWindow mywindow(800, 600, "Classy!"); + mywindow.resize(50, 50); + mywindow.init(); + GLFWwindow* window = mywindow.getWindow(); + glfwSetFramebufferSizeCallback(window, framebuffer_callback); while(!glfwWindowShouldClose(window)) { @@ -36,8 +23,6 @@ int main() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - - glfwPollEvents(); glfwSwapBuffers(window); }