Created custom Window class

This commit is contained in:
NaifBanana 2024-03-16 03:11:50 -05:00
parent b99b60c1e2
commit 8eeb05d895
6 changed files with 137 additions and 24 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
build/
build/
.vscode/

View File

@ -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)

15
includes/Util.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#ifndef _NB_UTIL
#define _NB_UTIL
#include <array>
namespace nb{
template<unsigned int N, typename T>
struct Size {
std::array<T, N> vals;
};
};
#endif

34
includes/Window.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#ifndef _NB_WINDOW
#define _NB_WINDOW
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <array>
#include <string>
#include <iostream>
namespace NB {
class NBWindow {
public:
NBWindow(const std::array<uint16_t, 2>, 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<uint16_t, 2> getSize() const;
std::string getName() const;
void resize(const std::array<uint16_t, 2>);
void resize(const uint16_t x, const uint16_t y);
private:
std::array<uint16_t, 2> windowSize;
std::string windowName;
bool ready=false, running=false;
GLFWwindow* window;
GLFWmonitor* monitor;
GLFWwindow* shareWindow;
};
};
#endif

75
source/Window.cpp Normal file
View File

@ -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<uint16_t, 2> 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<uint16_t, 2> NBWindow::getSize() const {
return windowSize;
}
std::string NBWindow::getName() const {
return windowName;
}
void NBWindow::resize(const std::array<uint16_t, 2> 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]);
}
}
};

View File

@ -1,33 +1,20 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#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);
}