Pre-testing event system / multithreading semiworking
This commit is contained in:
parent
8eeb05d895
commit
ae49960048
@ -11,7 +11,7 @@ include(CPACK)
|
|||||||
include_directories(./dep ./includes)
|
include_directories(./dep ./includes)
|
||||||
link_directories(../libs/glfw-3.3.9/build/src)
|
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)
|
target_link_libraries(main glfw3)
|
||||||
|
|
||||||
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
||||||
|
|||||||
49
includes/Events.h
Normal file
49
includes/Events.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_EVENTS
|
||||||
|
#define _NB_EVENTS
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
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<std::atomic<uint64_t>> initStatePtr=nullptr, uint64_t initState=0);
|
||||||
|
NBEvent& operator[](int);
|
||||||
|
|
||||||
|
std::shared_ptr<std::atomic<uint64_t>> 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<std::atomic<uint64_t>> state;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
#endif
|
||||||
@ -11,8 +11,8 @@ namespace NB {
|
|||||||
|
|
||||||
class NBWindow {
|
class NBWindow {
|
||||||
public:
|
public:
|
||||||
NBWindow(const std::array<uint16_t, 2>, const char* initName, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
NBWindow(const std::array<uint16_t, 2>, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
||||||
NBWindow(const uint16_t, const uint16_t, const char* initName, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
NBWindow(const uint16_t, const uint16_t, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
||||||
|
|
||||||
int init();
|
int init();
|
||||||
GLFWwindow* getWindow() const;
|
GLFWwindow* getWindow() const;
|
||||||
|
|||||||
@ -4,9 +4,16 @@
|
|||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include "Window.h"
|
||||||
|
#include "Events.h"
|
||||||
|
|
||||||
void framebuffer_callback(GLFWwindow* window, int width, int height);
|
void framebuffer_callback(GLFWwindow* window, int width, int height);
|
||||||
|
|
||||||
void processInputs(GLFWwindow* window);
|
void processInputs(GLFWwindow* window);
|
||||||
|
|
||||||
|
int renderingProcess(std::atomic<bool>&);
|
||||||
|
|
||||||
|
int consoleProcess(std::atomic<bool>&);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
81
source/Events.cpp
Normal file
81
source/Events.cpp
Normal file
@ -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<std::atomic<uint64_t>> initStatePtr, uint64_t initState) {
|
||||||
|
if (initStatePtr == nullptr) {
|
||||||
|
state = std::shared_ptr<std::atomic<uint64_t>>(new std::atomic<uint64_t>);
|
||||||
|
} 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<std::atomic<uint64_t>> NBEventListener::getStatePtr() {
|
||||||
|
return std::shared_ptr<std::atomic<uint64_t>>(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
@ -38,7 +38,6 @@ int NBWindow::init() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << windowSize[0] << " by " << windowSize[1] << "\n";
|
|
||||||
glViewport(0, 0, windowSize[0], windowSize[1]);
|
glViewport(0, 0, windowSize[0], windowSize[1]);
|
||||||
running=true;
|
running=true;
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -9,3 +9,49 @@ void processInputs(GLFWwindow* window) {
|
|||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int renderingProcess(std::atomic<bool>& 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<bool>& 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
@ -1,8 +1,9 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
|
#include "Events.h"
|
||||||
#include "funcs.h"
|
#include "funcs.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
|
||||||
@ -10,23 +11,13 @@
|
|||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello World!\n";
|
std::cout << "Hello World!\n";
|
||||||
|
|
||||||
NB::NBWindow mywindow(800, 600, "Classy!");
|
// std::shared_ptr<std::atomic<bool>> shouldClose(new std::atomic<bool>);
|
||||||
mywindow.resize(50, 50);
|
// *shouldClose = false;
|
||||||
mywindow.init();
|
//std::atomic<uint16_t> r=51, g=77, b=77;
|
||||||
GLFWwindow* window = mywindow.getWindow();
|
std::atomic<bool> shouldClose= false;
|
||||||
|
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_callback);
|
std::thread renderingThread(renderingProcess, std::ref(shouldClose));
|
||||||
|
|
||||||
while(!glfwWindowShouldClose(window)) {
|
renderingThread.join();
|
||||||
processInputs(window);
|
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
glfwPollEvents();
|
|
||||||
glfwSwapBuffers(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwTerminate();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user