From ae49960048b4f1b369b06660cd8094c700d04504 Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Tue, 19 Mar 2024 00:32:31 -0500 Subject: [PATCH] Pre-testing event system / multithreading semiworking --- CMakeLists.txt | 2 +- includes/Events.h | 49 ++++++++++++++++++++++++++++ includes/Window.h | 4 +-- includes/funcs.h | 7 ++++ source/Events.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++ source/Window.cpp | 1 - source/funcs.cpp | 48 +++++++++++++++++++++++++++- source/main.cpp | 25 +++++---------- 8 files changed, 195 insertions(+), 22 deletions(-) create mode 100644 includes/Events.h create mode 100644 source/Events.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f0b04f..8d28dda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,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/Window.cpp source/funcs.cpp source/shader.cpp ../libs/glad/src/glad.c) +add_executable(main source/main.cpp source/Window.cpp source/Events.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/Events.h b/includes/Events.h new file mode 100644 index 0000000..e941254 --- /dev/null +++ b/includes/Events.h @@ -0,0 +1,49 @@ +#pragma once +#ifndef _NB_EVENTS +#define _NB_EVENTS + +#include +#include +#include +#include +namespace NB { + +class NBEvent { +friend class NBEventListener; +public: + NBEvent(void (*initFunc)(), uint64_t initMask, const char* initName=""); + + const std::string getName() const; + const uint64_t getMask() const; + void setMask(const uint64_t); + void setName(const char*); + void setFunc(void (*newFunc)()); + const uint64_t check(const uint64_t); + +private: + uint64_t mask; + void (*func)(); + std::string name; + +}; + +class NBEventListener { +public: + NBEventListener(NBEvent*, uint16_t, std::shared_ptr> initStatePtr=nullptr, uint64_t initState=0); + NBEvent& operator[](int); + + std::shared_ptr> getStatePtr(); + const uint64_t getState(); + const uint64_t raiseState(const uint64_t); + void setState(const uint64_t); + void check(); + +private: + NBEvent* eventList; + uint16_t numEvents; + std::shared_ptr> state; + +}; + +}; +#endif \ No newline at end of file diff --git a/includes/Window.h b/includes/Window.h index e6bd030..60b3b76 100644 --- a/includes/Window.h +++ b/includes/Window.h @@ -11,8 +11,8 @@ 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); + NBWindow(const std::array, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL); + NBWindow(const uint16_t, const uint16_t, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL); int init(); GLFWwindow* getWindow() const; diff --git a/includes/funcs.h b/includes/funcs.h index 802eb03..ede8db3 100644 --- a/includes/funcs.h +++ b/includes/funcs.h @@ -4,9 +4,16 @@ #include #include +#include +#include "Window.h" +#include "Events.h" void framebuffer_callback(GLFWwindow* window, int width, int height); void processInputs(GLFWwindow* window); +int renderingProcess(std::atomic&); + +int consoleProcess(std::atomic&); + #endif \ No newline at end of file diff --git a/source/Events.cpp b/source/Events.cpp new file mode 100644 index 0000000..4a56ba5 --- /dev/null +++ b/source/Events.cpp @@ -0,0 +1,81 @@ +#include "Events.h" +namespace NB { + +NBEvent::NBEvent(void (*initFunc)(), const uint64_t initMask, const char* initName) { + mask = initMask; + func = initFunc; + name = initName; +} + +const uint64_t NBEvent::getMask() const { + return mask; +} + +const std::string NBEvent::getName() const { + return name; +} + +void NBEvent::setMask(const uint64_t newMask) { + mask = newMask; +} +void NBEvent::setName(const char* newName) { + name = newName; +} + +void NBEvent::setFunc(void (*newFunc)()) { + func = newFunc; +} + +const uint64_t NBEvent::check(const uint64_t state) { + if (state & mask == mask) { + func(); + } + return state&(~mask); +} + +NBEventListener::NBEventListener(NBEvent* initEventList, uint16_t initNum, std::shared_ptr> initStatePtr, uint64_t initState) { + if (initStatePtr == nullptr) { + state = std::shared_ptr>(new std::atomic); + } else { + state = initStatePtr; + } + + *state = initState; + numEvents = initNum; + for (uint16_t i = 0; i < initNum; ++i) { + eventList[i] = initEventList[i]; + } +} + +NBEvent& NBEventListener::operator[](int ind) { + return eventList[ind % numEvents]; +} + +std::shared_ptr> NBEventListener::getStatePtr() { + return std::shared_ptr>(state); +} + +const uint64_t NBEventListener::getState() { + return *state; +} + +const uint64_t NBEventListener::raiseState(const uint64_t newState) { + uint64_t oldState = *state; + uint64_t tempState = oldState | newState; + *state = tempState; + return tempState; +} + +void NBEventListener::setState(const uint64_t newState) { + *state = newState; +} + +void NBEventListener::check() { + uint64_t oldState = *state; + for (uint16_t i = 0; i < numEvents; ++i) { + oldState = eventList[i].check(oldState); + } + *state = oldState; +} + +}; \ No newline at end of file diff --git a/source/Window.cpp b/source/Window.cpp index e19b889..28ee24f 100644 --- a/source/Window.cpp +++ b/source/Window.cpp @@ -38,7 +38,6 @@ int NBWindow::init() { return -1; } - std::cout << windowSize[0] << " by " << windowSize[1] << "\n"; glViewport(0, 0, windowSize[0], windowSize[1]); running=true; return 0; diff --git a/source/funcs.cpp b/source/funcs.cpp index d1f3a8f..83d5e94 100644 --- a/source/funcs.cpp +++ b/source/funcs.cpp @@ -8,4 +8,50 @@ void processInputs(GLFWwindow* window) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } -} \ No newline at end of file +} + +int renderingProcess(std::atomic& shouldStop) { + std::cout << "Howdy from rendering!\n"; + NB::NBWindow mywindow(800, 600, "Multithreading?"); + mywindow.init(); + GLFWwindow* window = mywindow.getWindow(); + + glfwSetFramebufferSizeCallback(window, framebuffer_callback); + + while(!glfwWindowShouldClose(window) && !shouldStop) { + processInputs(window); + + // glClearColor(r/255.0f, g/255.0f, b/255.0f, 1.0f); + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glfwPollEvents(); + glfwSwapBuffers(window); + } + shouldStop = true; + glfwSetWindowShouldClose(window, true); + + glfwTerminate(); + return 0; +} + +/* +int consoleProcess(std::atomic& shouldStop) { + char controlInput; + while(!shouldStop) { + std::cin >> controlInput; + switch (controlInput) { + case 'r': + r = (r==51)?0:51; + break; + case 'g': + g = (g==77)?0:77; + break; + case 'b': + b = (b==77)?0:77; + break; + default: + break; + } + } +}*/ \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 9c314c7..2ceca5e 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,32 +1,23 @@ #include #include - +#include #include "Window.h" +#include "Events.h" #include "funcs.h" #include "shader.h" int main() { std::cout << "Hello World!\n"; - - NB::NBWindow mywindow(800, 600, "Classy!"); - mywindow.resize(50, 50); - mywindow.init(); - GLFWwindow* window = mywindow.getWindow(); - glfwSetFramebufferSizeCallback(window, framebuffer_callback); + // std::shared_ptr> shouldClose(new std::atomic); + // *shouldClose = false; + //std::atomic r=51, g=77, b=77; + std::atomic shouldClose= false; - while(!glfwWindowShouldClose(window)) { - processInputs(window); + std::thread renderingThread(renderingProcess, std::ref(shouldClose)); - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - glfwPollEvents(); - glfwSwapBuffers(window); - } - - glfwTerminate(); + renderingThread.join(); return 0; } \ No newline at end of file