Skip to content

Instantly share code, notes, and snippets.

@0x61726b
Created September 28, 2017 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x61726b/e52fdb3efda58562181179249c4c4d3f to your computer and use it in GitHub Desktop.
Save 0x61726b/e52fdb3efda58562181179249c4c4d3f to your computer and use it in GitHub Desktop.
#include <stdio.h> // fprintf, sprintf, stderr
#include <stdlib.h> // exit
#include <math.h> // sqrt
#include <GLFW/glfw3.h>
#include <Windows.h>
// global variables
// we'll use global variables, since the GLFW callback functions we will add
// later will require access to these variables.
GLFWwindow* window;
double resx = 640, resy = 480;
// this function will be called internally by GLFW whenever an error occur.
void error_callback(int error, const char* description);
// we move all the initialization step into a function for brevity
// we'll put all future additional initialization of GLFW and similar functionality in here
int init();
int main()
{
if (!init()) {
printf("Could not init. Exiting\n");
return -1;
}
// set the background color to red
glClearColor(1.0, 0.0, 0.0, 1.0);
// main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT); // actually clear the window
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
int init()
{
// initialize all the internal state of GLFW
if (!glfwInit()) {
fprintf(stderr, "Error initializing GLFW\n");
return 0;
}
// tell GLFW to call error_callback if an internal error ever occur at some point inside GLFW functions.
glfwSetErrorCallback(error_callback);
// create the window
window = glfwCreateWindow(resx, resy, "Checkpoint 2: Using OpenGL functions.", NULL, NULL);
// check if the opening of the window failed whatever reason and clean up
if (!window) {
glfwTerminate();
return 0;
}
// in principle we can have multiple windows,
// so we set the newly created on as "current"
glfwMakeContextCurrent(window);
// Enable v-sync for now, if possible
glfwSwapInterval(1);
return 1; // success
}
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s (%d)\n", description, error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment