Able to handle multiple windows

This commit is contained in:
Robert 2020-08-31 18:27:16 +02:00
parent fcadf735c9
commit 70835b8593
4 changed files with 110 additions and 2 deletions

View file

@ -1,7 +1,7 @@
add_executable(ComplexPlotting
"main.cpp"
"PlotWindow.hpp")
"PlotWindow.hpp" "PlotWindow.cpp")
file(GLOB vendor_SRC
${CMAKE_SOURCE_DIR}/vendor/src/*.c
@ -13,7 +13,6 @@ target_sources(ComplexPlotting PRIVATE
target_include_directories(ComplexPlotting PRIVATE
glfw
./primitives
${CMAKE_SOURCE_DIR}/vendor/include
)

53
src/PlotWindow.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "PlotWindow.hpp"
#include <iostream>
#include <string>
PlotWindow::PlotWindow(int w, int h, int id) :
window(nullptr), id(id)
{
window = glfwCreateWindow(w, h, (std::string("Plot ") + std::to_string(id)).c_str(), NULL, NULL);
if (window == nullptr)
{
const char* buffer = new const char[512];
glfwGetError(&buffer);
std::cerr << "Failed to initialize GLFWwindow: " << std::endl << buffer << std::endl;
delete[] buffer;
buffer = nullptr;
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cerr << "Failed to initialize GLAD" << std::endl;
return;
}
glViewport(0, 0, w, h);
}
void PlotWindow::Destroy()
{
glfwDestroyWindow(window);
}
void PlotWindow::GiveContext()
{
glfwMakeContextCurrent(window);
}
void PlotWindow::Display()
{
glClearColor(0.6f, 0.1f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
void PlotWindow::FramebufferSizeCallback(GLFWwindow* window, int w, int h)
{
glViewport(0, 0, w, h);
}

View file

@ -0,0 +1,22 @@
#pragma once
#include <glad/glad.h>
#include <glfw/glfw3.h>
class PlotWindow
{
public:
PlotWindow(int w, int h, int id);
bool ShouldClose() { return glfwWindowShouldClose(window); }
void Destroy();
void GiveContext();
void Display();
private:
int id;
GLFWwindow* window;
static void FramebufferSizeCallback(GLFWwindow* window, int w, int h);
};

View file

@ -1,6 +1,12 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <glad/glad.h>
#include <glfw/glfw3.h>
#include "PlotWindow.hpp"
int main(int argc, char** argv)
{
int result = glfwInit();
@ -10,4 +16,32 @@ int main(int argc, char** argv)
glfwGetError(&buffer);
std::cout << "Failed to initialize GLFW: " << std::endl << buffer << std::endl;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
std::vector<PlotWindow> windows;
windows.emplace_back(500, 500, 1);
windows.emplace_back(500, 500, 2);
windows.emplace_back(500, 500, 3);
while (!windows.empty())
{
glfwPollEvents();
for (std::vector<PlotWindow>::iterator it = windows.begin(); it != windows.end();)
{
if (it->ShouldClose())
{
it->Destroy();
it = windows.erase(it);
continue;
}
it->GiveContext();
it->Display();
it++;
}
}
}