Skip to content

Instantly share code, notes, and snippets.

@roxlu

roxlu/test-opengl.cpp Secret

Created May 28, 2020
Embed
What would you like to do?
Simplified GLFW template
#define GLFW_EXPOSE_NATIVE_X11
#define GLFW_EXPOSE_NATIVE_GLX
/* -------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
/* -------------------------------------------- */
void button_callback(GLFWwindow* win, int bt, int action, int mods);
void cursor_callback(GLFWwindow* win, double x, double y);
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods);
void char_callback(GLFWwindow* win, unsigned int key);
void error_callback(int err, const char* desc);
void resize_callback(GLFWwindow* window, int width, int height);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
/* -------------------------------------------- */
int win_w = 1280;
int win_h = 720;
/* -------------------------------------------- */
int main(int argc, char* argv[]) {
glfwSetErrorCallback(error_callback);
if(!glfwInit()) {
printf("Error: cannot setup glfw.\n");
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GL_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_DECORATED, GL_FALSE);
GLFWwindow* win = NULL;
win = glfwCreateWindow(win_w, win_h, "OpenGL", NULL, NULL);
if(!win) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetFramebufferSizeCallback(win, resize_callback);
glfwSetKeyCallback(win, key_callback);
glfwSetCharCallback(win, char_callback);
glfwSetCursorPosCallback(win, cursor_callback);
glfwSetMouseButtonCallback(win, button_callback);
glfwSetScrollCallback(win, scroll_callback);
glfwMakeContextCurrent(win);
glfwSwapInterval(1);
if (!gladLoadGL()) {
printf("Cannot load GL.\n");
exit(1);
}
/* -------------------------------------------- */
glDisable(GL_DEPTH_TEST);
glDisable(GL_DITHER);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while(!glfwWindowShouldClose(win)) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, win_w, win_h);
glClearColor(0.13f, 0.13f, 0.13f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(win);
glfwPollEvents();
}
/* -------------------------------------------- */
glfwTerminate();
return EXIT_SUCCESS;
}
/* -------------------------------------------- */
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) {
if (GLFW_RELEASE == action) {
return;
}
switch(key) {
case GLFW_KEY_ESCAPE: {
glfwSetWindowShouldClose(win, GL_TRUE);
break;
}
};
}
void error_callback(int err, const char* desc) {
printf("GLFW error: %s (%d)\n", desc, err);
}
/* -------------------------------------------- */
void resize_callback(GLFWwindow* window, int width, int height) { }
void cursor_callback(GLFWwindow* win, double x, double y) { }
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { }
void button_callback(GLFWwindow* win, int bt, int action, int mods) { }
void char_callback(GLFWwindow* win, unsigned int key) { }
/* -------------------------------------------- */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.