Init
This commit is contained in:
+161
@@ -0,0 +1,161 @@
|
||||
#include "Events.h"
|
||||
|
||||
namespace NB {
|
||||
|
||||
void NULL_FUNC() {}
|
||||
|
||||
std::invalid_argument null_mask_error("NULL MASK NOT ALLOWED");
|
||||
|
||||
Events::Events(const uint64_t initMask, void (*initFunc)(), const char* initName):
|
||||
mask(initMask), func(initFunc), name(initName) {
|
||||
if (mask == 0x0) {
|
||||
throw null_mask_error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Events::Events(const uint64_t initMask, const char* initName):
|
||||
Events(initMask, NULL_FUNC, initName) {}
|
||||
|
||||
Events::Events(const Events& cpy)
|
||||
:Events(cpy.mask, cpy.func, cpy.name.c_str()) {}
|
||||
|
||||
Events& Events::operator=(const Events& cpy) {
|
||||
func = cpy.func;
|
||||
name = cpy.name;
|
||||
mask = cpy.mask;
|
||||
if (mask==0x0) {
|
||||
throw null_mask_error;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Events::~Events() { func = nullptr; }
|
||||
|
||||
const std::string Events::getName() const { return name; }
|
||||
|
||||
const uint64_t Events::getMask() const { return mask; }
|
||||
|
||||
void Events::setMask(const uint64_t newMask) {
|
||||
mask = newMask;
|
||||
}
|
||||
|
||||
void Events::setName(const char* newName) {
|
||||
name = newName;
|
||||
}
|
||||
|
||||
void Events::setFunc(void (*newFunc)()) {
|
||||
func = newFunc;
|
||||
}
|
||||
|
||||
NBEventBasic* NBEventBasic::clone() const { return new NBEventBasic(*this); }
|
||||
|
||||
NBEventState* NBEventState::clone() const { return new NBEventState(*this); }
|
||||
|
||||
const uint64_t NBEventBasic::check(const uint64_t refState) const {
|
||||
if ((mask!=0) && ((refState&mask)==mask)) {
|
||||
func();
|
||||
return refState&(~mask);
|
||||
}
|
||||
return refState;
|
||||
}
|
||||
|
||||
const uint64_t NBEventState::check(const uint64_t refState) const {
|
||||
if ((mask!=0) && ((refState&mask)==mask)) {
|
||||
func();
|
||||
}
|
||||
return refState;
|
||||
}
|
||||
|
||||
NBEventListener::NBEventListener(Events** initEventList, uint16_t initNum, const uint64_t initState, state_register* initStatePtr) {
|
||||
if (initStatePtr == nullptr) {
|
||||
state = new state_register;
|
||||
} else {
|
||||
state = initStatePtr;
|
||||
}
|
||||
|
||||
state->store(initState);
|
||||
numEvents = initNum;
|
||||
eventList = new Events*[numEvents];
|
||||
for (uint16_t i = 0; i < initNum; ++i) {
|
||||
eventList[i] = initEventList[i]->clone();
|
||||
}
|
||||
}
|
||||
|
||||
state_register* NBEventListener::getStatePtr() const {
|
||||
return state;
|
||||
}
|
||||
|
||||
const uint64_t NBEventListener::getState() const {
|
||||
return state->load();
|
||||
}
|
||||
|
||||
void NBEventListener::raiseFlags(const uint64_t newState) {
|
||||
bufferLock.lock();
|
||||
stateBuffer.push(NBStateChange{STATE_RAISE, newState});
|
||||
bufferLock.unlock();
|
||||
}
|
||||
|
||||
void NBEventListener::raiseFlags(const Events& newEvent) {
|
||||
raiseFlags(newEvent.mask);
|
||||
}
|
||||
|
||||
void NBEventListener::dropFlags(const uint64_t dropState) {
|
||||
bufferLock.lock();
|
||||
stateBuffer.push(NBStateChange{STATE_DROP, dropState});
|
||||
bufferLock.unlock();
|
||||
}
|
||||
|
||||
void NBEventListener::dropFlags(const Events& dropEvent) {
|
||||
dropFlags(dropEvent.mask);
|
||||
}
|
||||
|
||||
const bool NBEventListener::snoop(const uint64_t refState) const {
|
||||
return ((getState()&refState)==refState);
|
||||
}
|
||||
|
||||
const bool NBEventListener::snoop(const Events& refEvent) const {
|
||||
return snoop(refEvent.mask);
|
||||
}
|
||||
|
||||
void NBEventListener::_setState(const uint64_t newState) {
|
||||
bufferLock.lock();
|
||||
std::queue<NBStateChange>().swap(stateBuffer);
|
||||
bufferLock.unlock();
|
||||
state->store(newState);
|
||||
}
|
||||
|
||||
void NBEventListener::listen() {
|
||||
uint64_t oldState = getState();
|
||||
for (uint16_t i = 0; i < numEvents; ++i) {
|
||||
oldState = eventList[i]->check(oldState);
|
||||
}
|
||||
NBStateChange curr;
|
||||
bufferLock.lock();
|
||||
for (; !stateBuffer.empty(); stateBuffer.pop()) {
|
||||
curr = stateBuffer.front();
|
||||
switch (curr.type) {
|
||||
case STATE_RAISE:
|
||||
oldState |= curr.mask;
|
||||
break;
|
||||
case STATE_DROP:
|
||||
oldState &= ~curr.mask;
|
||||
break;
|
||||
case STATE_SET:
|
||||
oldState = curr.mask;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
bufferLock.unlock();
|
||||
_setState(oldState);
|
||||
}
|
||||
|
||||
void NBEventListener::listen(const Events& refEvent) {
|
||||
uint64_t oldState = getState();
|
||||
oldState = refEvent.check(oldState);
|
||||
_setState(oldState);
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,91 @@
|
||||
#include "Window.h"
|
||||
namespace NB {
|
||||
|
||||
std::atomic<uint8_t> Window::windowCount(0);
|
||||
|
||||
Window::Window(const uint16_t x, const uint16_t y, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) {
|
||||
if (windowCount == 0) {
|
||||
glfwInit();
|
||||
}
|
||||
|
||||
windowSize = {x, y};
|
||||
windowName = std::string(initName);
|
||||
monitor = initMonitor;
|
||||
shareWindow = initWindow;
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
window = glfwCreateWindow(windowSize[0], windowSize[0], windowName.c_str(), monitor, shareWindow);
|
||||
if (window == NULL) {
|
||||
std::cout << "NB::NBWINDOW::COULD NOT CREATE WINDOW(" << windowName << ")\n";
|
||||
if (windowCount == 0) { glfwTerminate(); }
|
||||
ready = false;
|
||||
} else {
|
||||
windowCount++;
|
||||
ready = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Window::Window(const std::array<uint16_t, 2> initSize, const char* initName, GLFWmonitor* initMonitor, GLFWwindow* initWindow) :
|
||||
Window(initSize[0], initSize[1], initName, initMonitor, initWindow){}
|
||||
|
||||
Window::~Window() {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
glfwDestroyWindow(window);
|
||||
windowCount--;
|
||||
if (windowCount == 0) {
|
||||
glfwTerminate();
|
||||
}
|
||||
}
|
||||
|
||||
int Window::init() {
|
||||
if (!ready) {
|
||||
std::cout << "NB::NBWINDOW::NOT READY\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
|
||||
std::cout << "NB::NBWINDOW::COULD NOT INITIALIZE GLAD\n";
|
||||
if (windowCount == 0) { glfwTerminate(); }
|
||||
return -1;
|
||||
}
|
||||
|
||||
glViewport(0, 0, windowSize[0], windowSize[1]);
|
||||
running=true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
GLFWwindow* Window::getWindow() const {
|
||||
return window;
|
||||
}
|
||||
|
||||
std::array<uint16_t, 2> Window::getSize() const {
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
std::string Window::getName() const {
|
||||
return windowName;
|
||||
}
|
||||
|
||||
void Window::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 Window::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]);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user