Skip to content

Instantly share code, notes, and snippets.

@minetoblend
Created December 4, 2018 15:43
Show Gist options
  • Save minetoblend/3bfba225167c42b60b627141e7276995 to your computer and use it in GitHub Desktop.
Save minetoblend/3bfba225167c42b60b627141e7276995 to your computer and use it in GitHub Desktop.
#pragma once
#include "Window.h"
#include "GLFW/glfw3.h"
#include <mutex>
#include "WindowManager.h"
#define synchronized(m) \
for(std::unique_lock<std::recursive_mutex> lk(m); lk; lk.unlock())
void checkWindow(GLFWwindow* window) {
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
}
Window::Window(const WindowConfig &config)
{
this->createWindow(config);
}
void Window::createWindow(const WindowConfig &config)
{
glfwDefaultWindowHints();
glfwWindowHint(GLFW_SAMPLES, config.samples);
glfwWindowHint(GLFW_MAXIMIZED, config.maximized);
this->window = glfwCreateWindow(config.width, config.height, "", NULL, NULL);
checkWindow(this->window);
glfwSwapInterval(config.samples);
glfwFocusWindow(this->window);
}
void Window::swapBuffers() const {
glfwSwapBuffers(this->window);
}
bool Window::isCloseRequested() const {
return glfwWindowShouldClose(this->window);
}
void Window::destroy() const {
glfwDestroyWindow(this->window);
}
GLFWwindow * Window::getWindow() const {
return this->window;
}
Window::~Window()
{
}
#pragma once
#include "GLFW/glfw3.h"
#include "Gui.h"
struct WindowConfig {
unsigned short width = 800;
unsigned short height = 600;
const char* title;
unsigned char samples = 2;
unsigned char swapInterval = 1;
bool maximized = false;
Gui *gui = new Gui();
};
class Window
{
private:
GLFWwindow *window;
void createWindow(const WindowConfig &config);
public:
Window(const WindowConfig &config);
~Window();
void swapBuffers() const;
bool isCloseRequested() const;
void destroy() const;
GLFWwindow *getWindow() const;
};
#pragma once
#include <vector>
#include "Window.h"
#include <mutex>
#include <map>
class WindowManager
{
private:
//std::vector<Window> windows;
std::map<GLFWwindow*, Window> windows;
void attachWindow(const Window &window);
public:
WindowManager();
~WindowManager();
Window createWindow(const WindowConfig &config);
void run();
};
void WM_init();
extern WindowManager wm;
#pragma once
#include "WindowManager.h"
#include "GLFW/glfw3.h"
#include <stdlib.h>
#include <algorithm>
static WindowManager wm;
WindowManager::WindowManager()
{
if (!glfwInit())
exit(EXIT_FAILURE);
}
WindowManager::~WindowManager()
{
glfwTerminate();
}
void WindowManager::attachWindow(const Window & window)
{
this->windows[window.getWindow()] = window;
}
Window WindowManager::createWindow(const WindowConfig & config)
{
Window window = Window(config);
attachWindow(window);
return window;
}
std::vector<GLFWwindow*> forRemoval;
void WindowManager::run() {
while (this->windows.size() > 0) {
glfwPollEvents();
std::for_each(this->windows.begin(), this->windows.end(), [](const std::pair<GLFWwindow*, Window> &e)->void {
if (e.second.isCloseRequested()) {
e.second.destroy();
forRemoval.push_back(e.first);
}
else {
e.second.swapBuffers();
}
});
for (int i = 0; i < forRemoval.size(); i++) {
this->windows.erase(forRemoval[i]);
}
forRemoval.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment