Skip to content

Instantly share code, notes, and snippets.

@romanlarionov
Last active August 11, 2019 17:15
Show Gist options
  • Save romanlarionov/3c353cc29254133d4c44cd10e35bc86a to your computer and use it in GitHub Desktop.
Save romanlarionov/3c353cc29254133d4c44cd10e35bc86a to your computer and use it in GitHub Desktop.
OpenGL Context Creation Template
cmake_minimum_required(VERSION 2.6)
project(OpenGL_APP)
option(GLFW_BUILD_DOCS OFF)
option(GLFW_BUILD_EXAMPLES OFF)
option(GLFW_BUILD_TESTS OFF)
add_subdirectory(deps/glfw)
include_directories("deps/glfw/include"
"deps/glad/include/")
file(GLOB DEPS_SOURCES deps/glad/src/glad.c)
source_group("deps" FILES ${DEPS_SOURCES})
add_executable(${PROJECT_NAME} main.cpp ${DEPS_SOURCES})
target_link_libraries(${PROJECT_NAME} glfw ${GLFW_LIBRARIES})
// Getting up and running with your rendering concept without
// having to retype the same old stuff over and over again..
#include <iostream>
#include <string>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
const int texture_width = 720;
const int texture_height = 480;
const int msaa_rate = 1;
const std::string app_name = "Temp";
static void keyCallbackFunc(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
static void cursorPosCallbackFunc(GLFWwindow* window, double xpos, double ypos)
{
}
void errorCallbackFunc(int error, const char* description)
{
std::cerr << "Error: " << description << std::endl;
}
GLFWwindow* initGL()
{
if (!glfwInit())
{
std::cerr << "Error: GLFW failed to initialize.\n";
glfwTerminate();
system("pause");
exit(1);
}
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_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_SAMPLES, msaa_rate);
GLFWwindow *window = glfwCreateWindow(texture_width, texture_height, app_name, nullptr, nullptr);
if (!window)
{
std::cerr << "Error: Window context failed to initialize.\n";
glfwTerminate();
system("pause");
exit(1);
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cerr << "Error: GLAD failed to load OpenGL.\n";
glfwTerminate();
system("pause");
exit(1);
}
glViewport(0, 0, texture_width, texture_height);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glfwSetKeyCallback(window, keyCallbackFunc);
glfwSetCursorPosCallback(window, cursorPosCallbackFunc);
glfwSetErrorCallback(errorCallbackFunc);
return window;
}
void mainLoop(GLFWwindow *window)
{
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear();
// do stuff
glfwSwapBuffers(window);
}
glfwTerminate();
}
int main()
{
GLFWwindow *window = initGL();
mainLoop(window);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment