Last active
August 30, 2020 14:49
-
-
Save Pikachuxxxx/67774ef324ef8f4724b49ca706d2261f to your computer and use it in GitHub Desktop.
The Initialisation Code for Basic OpenGL Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// GLEW | |
#define GLEW_STATIC | |
#include <GL/glew.h> | |
// GLFW | |
#include <GLFW/glfw3.h> | |
void glfw_initialisation_error(int error, const char* description){ | |
std::cerr << "ERROR::INITIALISATION::GLFW::" << error << "::DESCRIPTION::" << description << std::endl; | |
} | |
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){ | |
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ | |
std::cout << "Escape Key Pressed..." << '\n'; | |
glfwSetWindowShouldClose(window, GLFW_TRUE); | |
} | |
} | |
int main(int argc, char const *argv[]) { | |
const int Width = 800, Height = 600; | |
glfwSetErrorCallback(glfw_initialisation_error); | |
if(!glfwInit()) | |
return EXIT_FAILURE; | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
GLFWwindow* window; | |
window = glfwCreateWindow(Width, Height, "OpenGL Window", NULL, NULL); | |
if(!window){ | |
std::cerr << "Could Not initialise Window" << '\n'; | |
glfwTerminate(); | |
return EXIT_FAILURE; | |
} | |
glfwMakeContextCurrent(window); | |
glfwSwapInterval(1); // Enable V-Sync | |
glfwSetKeyCallback(window, key_callback); | |
glewExperimental = GL_TRUE; | |
if(glewInit() != GLEW_OK){ | |
std::cerr << "Could Not initialise GLEW" << '\n'; | |
return EXIT_FAILURE; | |
} | |
glViewport(0, 0, Width, Height); | |
while (!glfwWindowShouldClose(window)) { | |
glfwPollEvents(); | |
glClearColor(1, 1, 1, 1); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glfwSwapBuffers(window); | |
} | |
glfwTerminate(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment