basic glfw app setup

This commit is contained in:
Lauchmelder 2021-12-20 04:31:44 +01:00
commit eed4eec20c
8 changed files with 137 additions and 0 deletions

47
src/Application.cpp Normal file
View file

@ -0,0 +1,47 @@
#include "Application.hpp"
#include <stdexcept>
#include <sstream>
#include <GLFW/glfw3.h>
Application::~Application()
{
if (window != nullptr)
{
glfwDestroyWindow(window);
window = nullptr;
}
glfwTerminate();
}
void Application::Init(int width, int height, const std::string& title)
{
if(window == nullptr)
glfwInit();
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if (window == nullptr)
{
const char* errorbuf;
int errorcode = glfwGetError(&errorbuf);
glfwTerminate();
std::stringstream errorstream;
errorstream << "Failed to create GLFWwindow (" << errorcode << "): \n" << errorbuf << std::endl;
throw std::runtime_error(errorstream.str());
}
glfwMakeContextCurrent(window);
}
void Application::Launch()
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glfwSwapBuffers(window);
}
}

35
src/Application.hpp Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include <string>
struct GLFWwindow;
class Application
{
/////////////////////////////////////////////////////////
////// SINGLETON BOILERPLATE ////////////////////////////
/////////////////////////////////////////////////////////
public:
static Application& Instance()
{
static Application app;
return app;
}
private:
Application() = default;
~Application();
Application(const Application& other) = delete;
/////////////////////////////////////////////////////////
////// APPLICATION IMPLEMENTATION ///////////////////////
/////////////////////////////////////////////////////////
public:
void Init(int width, int height, const std::string& title);
void Launch();
private:
GLFWwindow* window = nullptr;
};

7
src/CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
add_executable(visualizer
"main.cpp" "Application.cpp"
)
target_include_directories(visualizer PUBLIC ${GLFW3_INCLUDE_DIRS})
target_link_libraries(visualizer PUBLIC ${GLFW3_INCLUDE_DIRS})

21
src/main.cpp Normal file
View file

@ -0,0 +1,21 @@
#include <iostream>
#include "Application.hpp"
int main(int argc, char** argv)
{
Application& app = Application::Instance();
try
{
app.Init(800, 800, "Visualizer");
}
catch (const std::runtime_error& err)
{
std::cerr << "Application initialization failed\n\n" << err.what();
return 1;
}
app.Launch();
return 0;
}