From 37dbe741fa60ff7fca14720bab2d57136dec05f7 Mon Sep 17 00:00:00 2001 From: ktyl Date: Fri, 28 Jul 2023 01:39:37 +0200 Subject: [PATCH] chore: extract graphics init to function --- src/hello.cpp | 52 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/hello.cpp b/src/hello.cpp index cc09a66..4f624ef 100644 --- a/src/hello.cpp +++ b/src/hello.cpp @@ -82,6 +82,38 @@ float getTime() #include #include "astro/twoBodyMethods.hpp" +// Initialize GLFW, OpenGL and GLEW, open a window and make it the current context. +// Returns 0 for success, and -1 for any failure. +// Failures are printed to STDERR. +int initGraphics(GLFWwindow** window) +{ + // Set up GLFW, OpenGL and GLEW. + if (!glfwInit()) + { + std::cerr << "Failed to initialize GLFW" << std::endl; + return -1; + } + + *window = glfwCreateWindow(640, 480, "Hello Astro", NULL, NULL); + if (!window) + { + glfwTerminate(); + std::cerr << "Failed to open window with GLFW" << std::endl; + return -1; + } + + glfwMakeContextCurrent(*window); + + glewExperimental = GL_TRUE; + if (glewInit() != GLEW_OK) + { + std::cerr << "Failed to initialize GLEW" << std::endl; + return -1; + } + + return 0; +} + int main() { // Calculate period of ISS orbit around the Earth @@ -94,26 +126,10 @@ int main() glm::vec3 v(0.0, 1.0, 2.0); std::cout << "(" << v.x << ", " << v.y << ", " << v.z << ")" << std::endl; - // Set up GLFW, OpenGL and GLEW. - if (!glfwInit()) + GLFWwindow* window = nullptr; + if (initGraphics(&window) != 0) return -1; - GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Astro", NULL, NULL); - if (!window) - { - glfwTerminate(); - return -1; - } - - glfwMakeContextCurrent(window); - - glewExperimental = GL_TRUE; - if (glewInit() != GLEW_OK) - { - std::cerr << "Failed to initialize GLEW" << std::endl; - return -1; - } - // VAO, VBO float vertices[] = {