Created
September 9, 2021 19:23
-
-
Save newvertex/931cd7ab91495e20b364f7c3abdd59c9 to your computer and use it in GitHub Desktop.
basic GLFW window with opengl context
This file contains hidden or 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
#define GLFW_INCLUDE_NONE | |
#include <GLFW/glfw3.h> | |
#include <glad/glad.h> | |
#include <iostream> | |
constexpr float R = 0.2f; | |
constexpr float G = 0.4f; | |
constexpr float B = 0.9f; | |
constexpr float A = 1.0f; | |
void errorCallback(int errorCode, const char *description) | |
{ | |
std::cerr << "Error(glfw): [" << errorCode << "] " << description << '\n'; | |
} | |
void keyCallback(GLFWwindow *window, int key, int scancode, | |
int action, int mods) | |
{ | |
if (action == GLFW_PRESS) { | |
if (key == GLFW_KEY_ESCAPE) | |
glfwSetWindowShouldClose(window, GL_TRUE); | |
} | |
} | |
void cursorPosCallback(GLFWwindow *window, double xpos, double ypos) | |
{ | |
} | |
void buttonCallback(GLFWwindow *window, int button, int action, int mods) | |
{ | |
} | |
void scrollCallback(GLFWwindow *window, double xoffset, double yoffset) | |
{ | |
} | |
void glfwContext(int width, int height, const char *title) | |
{ | |
std::cout << "GLFW Window Creation\n"; | |
glfwSetErrorCallback(errorCallback); | |
if (!glfwInit()) | |
{ | |
std::cerr << "glfw: unable to initialize.\n"; | |
return; | |
} | |
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); | |
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); | |
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); | |
GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr); | |
if (!window) | |
{ | |
std::cerr << "glfw: failed to create window.\n"; | |
return; | |
} | |
std::cout << "glfw: version " << GLFW_VERSION_MAJOR << '.' | |
<< GLFW_VERSION_MINOR << '.' << GLFW_VERSION_REVISION << '\n'; | |
glfwSetCursorPosCallback(window, cursorPosCallback); | |
glfwSetMouseButtonCallback(window, buttonCallback); | |
glfwSetScrollCallback(window, scrollCallback); | |
glfwSetKeyCallback(window, keyCallback); | |
glfwMakeContextCurrent(window); | |
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) | |
{ | |
std::cerr << "glad: failed to initialize OpenGL.\n"; | |
return; | |
} | |
std::cout << "OpenGL: version " << GLVersion.major << '.' | |
<< GLVersion.minor << '\n'; | |
glfwSwapInterval(1); | |
glfwShowWindow(window); | |
int bufferWidth; | |
int bufferHeight; | |
while (!glfwWindowShouldClose(window)) | |
{ | |
glfwGetFramebufferSize(window, &bufferWidth, &bufferHeight); | |
glViewport(0, 0, bufferWidth, bufferHeight); | |
glClearColor(R, G, B, A); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
} | |
glfwDestroyWindow(window); | |
glfwTerminate(); | |
glfwSetErrorCallback(nullptr); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int width = 640; | |
int height = 480; | |
const char *title = "Hello World!"; | |
glfwContext(width, height, title); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment