Uhhh changes idek anymore mayn
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
#include <exception>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||
|
||||
class Error : public std::exception {
|
||||
public:
|
||||
Error(const unsigned int code=0, std::exception* const trace=nullptr) noexcept;
|
||||
Error(std::string, std::exception* const=nullptr) noexcept;
|
||||
Error(const Error&) noexcept;
|
||||
|
||||
unsigned int code() const noexcept;
|
||||
virtual const char* type() const noexcept final;
|
||||
virtual const char* what() const noexcept override final;
|
||||
|
||||
template<typename NBError>
|
||||
friend const char* ErrorCodeLookup(unsigned int);
|
||||
protected:
|
||||
static const ErrorCodeMap ErrorCodes;
|
||||
Error(
|
||||
const unsigned int,
|
||||
std::string,
|
||||
std::exception* const,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept;
|
||||
|
||||
const char* _type;
|
||||
const unsigned int _code;
|
||||
std::string _msg;
|
||||
};
|
||||
|
||||
template<typename NBError>
|
||||
const char* ErrorCodeLookup(unsigned int code) {
|
||||
for (auto kv : NBError::ErrorCodes) {
|
||||
if (kv.first == code) {
|
||||
return kv.second;
|
||||
}
|
||||
}
|
||||
|
||||
throw Error(666);
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
#endif // _NB_ERROR
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#ifndef _NB_OPENGL_LOADER
|
||||
#define _NB_OPENGL_LOADER
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace NB {
|
||||
|
||||
static unsigned int __NB_GL_DEBUG_ERROR_CODE__;
|
||||
|
||||
static std::string formatDebugString(const std::string& msg, const std::string& file="", int line=-1) {
|
||||
std::string ret = "";
|
||||
if (file != "") {
|
||||
ret += "In file " + file;
|
||||
if (line >= 0) {
|
||||
ret += " at line " + std::to_string(line);
|
||||
}
|
||||
ret += ":\n\t";
|
||||
}
|
||||
return ret + msg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define NB_GL_DEBUG_THROW(cmd, when) while(NB::__NB_GL_DEBUG_ERROR_CODE__=glGetError()){\
|
||||
std::cout << "[GL ERROR]: " << NB::__NB_GL_DEBUG_ERROR_CODE__;\
|
||||
std::cout << " " << when << " " << cmd << "\n";\
|
||||
}
|
||||
#define NB_GL_DEBUG(cmd) NB_GL_DEBUG_THROW(#cmd, "before"); cmd; NB_GL_DEBUG_THROW(#cmd, "after");
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,123 @@
|
||||
#pragma once
|
||||
#ifndef _NB_UTIL_THREADSAFE_QUEUE
|
||||
#define _NB_UTIL_THREADSAFE_QUEUE
|
||||
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <type_traits>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T, bool isPoly>
|
||||
class ThreadsafeQueue_impl;
|
||||
|
||||
// Non-polymorphic
|
||||
template<typename T>
|
||||
class ThreadsafeQueue_impl<T, false> {
|
||||
public:
|
||||
bool empty() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.empty();
|
||||
}
|
||||
unsigned int size() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void push(U&& val) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_queue.push(std::forward<U>(val));
|
||||
_cond.notify_one();
|
||||
}
|
||||
bool pop(std::shared_ptr<T>& dest) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (_queue.empty()) { return false; }
|
||||
dest = std::make_shared<T>(std::move(_queue.front()));
|
||||
_queue.pop();
|
||||
return true;
|
||||
}
|
||||
std::shared_ptr<T> popBlock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
_cond.wait(lock, [&]{ return !_queue.empty(); });
|
||||
auto ret = std::make_shared<T>(std::move(_queue.front()));
|
||||
_queue.pop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected:
|
||||
ThreadsafeQueue_impl() {}
|
||||
|
||||
std::queue<T> _queue;
|
||||
std::condition_variable _cond;
|
||||
mutable std::mutex _mutex;
|
||||
|
||||
};
|
||||
|
||||
// Polymorphic
|
||||
template<typename T>
|
||||
class ThreadsafeQueue_impl<T, true> {
|
||||
public:
|
||||
bool empty() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.empty();
|
||||
}
|
||||
unsigned int size() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void push(U&& val) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::shared_ptr<T> newval = std::make_shared<T>(std::forward<U>(val));
|
||||
_queue.push(newval);
|
||||
_cond.notify_one();
|
||||
}
|
||||
bool pop(std::shared_ptr<T>& dest) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (_queue.empty()) { return false; }
|
||||
dest = std::move(_queue.front());
|
||||
_queue.pop();
|
||||
return true;
|
||||
}
|
||||
std::shared_ptr<T> popBlock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
_cond.wait(lock, [&]{ return !_queue.empty(); });
|
||||
auto ret = std::move(_queue.front());
|
||||
_queue.pop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected:
|
||||
ThreadsafeQueue_impl() {}
|
||||
|
||||
std::queue<std::shared_ptr<T>> _queue;
|
||||
std::condition_variable _cond;
|
||||
mutable std::mutex _mutex;
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class ThreadsafeQueue : public ThreadsafeQueue_impl<T, std::is_polymorphic<T>::value> {
|
||||
public:
|
||||
using Implementation = ThreadsafeQueue_impl<T, std::is_polymorphic<T>::value>;
|
||||
|
||||
ThreadsafeQueue() = default;
|
||||
~ThreadsafeQueue() = default;
|
||||
|
||||
ThreadsafeQueue(ThreadsafeQueue& rhs) = delete;
|
||||
ThreadsafeQueue& operator=(ThreadsafeQueue& rhs) = delete;
|
||||
ThreadsafeQueue(ThreadsafeQueue&& rhs) = delete;
|
||||
ThreadsafeQueue& operator=(ThreadsafeQueue&& rhs) = delete;
|
||||
|
||||
operator bool() const { return !Implementation::empty(); }
|
||||
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
#endif // _NB_UTIL_THREADSAFE_QUEUE
|
||||
+37
-7
@@ -2,37 +2,67 @@
|
||||
#ifndef _NB_WINDOW
|
||||
#define _NB_WINDOW
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "GLLoad.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace NB {
|
||||
|
||||
class GLError : public std::runtime_error {
|
||||
public:
|
||||
GLError(const std::string&);
|
||||
};
|
||||
|
||||
class WindowError : public std::runtime_error {
|
||||
public:
|
||||
WindowError(const std::string&);
|
||||
};
|
||||
|
||||
class Window {
|
||||
public:
|
||||
static bool StrictInitialization;
|
||||
|
||||
static int getGLFWHint(int);
|
||||
static int setGLFWHint(int, int);
|
||||
static Window* getCurrent();
|
||||
|
||||
Window(const std::array<uint16_t, 2>, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
||||
Window(const uint16_t, const uint16_t, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
|
||||
~Window();
|
||||
|
||||
|
||||
int init();
|
||||
int getWindowHint(int) const;
|
||||
GLFWwindow* getWindow() const;
|
||||
std::array<uint16_t, 2> getSize() const;
|
||||
float getAspectRatio() const;
|
||||
std::string getName() const;
|
||||
|
||||
int init();
|
||||
int setWindowHint(int, int);
|
||||
void setCurrent();
|
||||
void resize(const std::array<uint16_t, 2>);
|
||||
void resize(const uint16_t x, const uint16_t y);
|
||||
|
||||
protected:
|
||||
static std::atomic<uint8_t> windowCount;
|
||||
static std::map<GLFWwindow*, Window*> WindowContexts;
|
||||
static std::map<int, int> GLFWHints;
|
||||
static Window* _current;
|
||||
static bool _glfw_init;
|
||||
static bool checkKillGLFW();
|
||||
|
||||
std::map<int, int> windowHints;
|
||||
std::array<uint16_t, 2> windowSize;
|
||||
float _aspect_ratio;
|
||||
std::string windowName;
|
||||
bool ready=false, running=false;
|
||||
GLFWwindow* window;
|
||||
GLFWmonitor* monitor;
|
||||
GLFWwindow* shareWindow;
|
||||
bool _init = false;
|
||||
int gladResponse = -1;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user