chore: extract graphics init to function

This commit is contained in:
ktyl 2023-07-28 01:39:37 +02:00 committed by Cat Flynn
parent 5d80d4a033
commit 37dbe741fa
1 changed files with 34 additions and 18 deletions

View File

@ -82,6 +82,38 @@ float getTime()
#include <cmath>
#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[] =
{