Skip to content

Instantly share code, notes, and snippets.

@romualdo97
Created December 7, 2020 18:11
Show Gist options
  • Save romualdo97/6f07ba23da1c51c738d0a1507b29548d to your computer and use it in GitHub Desktop.
Save romualdo97/6f07ba23da1c51c738d0a1507b29548d to your computer and use it in GitHub Desktop.
Hello world app using GLFW
#include <glad\glad.h>
#include <GLFW\glfw3.h>
#include <iostream>
#define W 960
#define H 480
#define WINDOW_TITLE "Hello window"
void resize_framebuffer_cb(GLFWwindow *window, int w, int h);
void process_input(GLFWwindow *window);
int main(void)
{
// configure window and context
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// create window
GLFWwindow *window = glfwCreateWindow(W, H, WINDOW_TITLE, NULL, NULL);
if (window == NULL)
{
std::cout << "Window could not be created\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// init glad for call opengl functions
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
{
std::cout << "Could not load GLAD\n";
return -1;
}
// register glfw events
glfwSetFramebufferSizeCallback(window, resize_framebuffer_cb);
// ======================================================================
// start viewport
glViewport(0, 0, W, H);
while (!glfwWindowShouldClose(window))
{
glClearColor(0.2, 0.5, 0.2, 1.0); // set the clear color
glClear(GL_COLOR_BUFFER_BIT); // clear color buffer bitfield
process_input(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}
void resize_framebuffer_cb(GLFWwindow *window, int w, int h)
{
glViewport(0, 0, w, h);
}
void process_input(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE))
{
glfwSetWindowShouldClose(window, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment