|
/* |
|
BASIC GLFW + GLXW WINDOW AND OPENGL SETUP |
|
------------------------------------------ |
|
See https://gist.github.com/roxlu/6698180 for the latest version of the example. |
|
|
|
- 2019-07-24: Cleaned up, starting to keep history. |
|
|
|
NOTES: |
|
- We make use of the GLAD library for GL loading, see: https://github.com/Dav1dde/glad/ |
|
- Add the following to your cmakelists: |
|
|
|
list(APPEND poly_sources |
|
${POLY_DIR}/src/poly/CommandLineParser.cpp |
|
${POLY_DIR}/src/poly/CommandLineOptions.cpp |
|
) |
|
|
|
*/ |
|
#include <stdlib.h> |
|
#include <stdio.h> |
|
#include <sstream> |
|
#include <poly/Glad.h> |
|
#include <GLFW/glfw3.h> |
|
#include <poly/Log.h> |
|
#include <poly/CommandLineOptions.h> |
|
|
|
using namespace poly; |
|
|
|
/* -------------------------------------------- */ |
|
|
|
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_h = 0; |
|
int win_w = 0; |
|
|
|
/* -------------------------------------------- */ |
|
|
|
int main(int argc, char* argv[]) { |
|
|
|
glfwSetErrorCallback(error_callback); |
|
|
|
CommandLineOptions cmd; |
|
cmd.addU32("width", false, 1920, "Window width"); |
|
cmd.addU32("height", false, 1080, "Window height"); |
|
cmd.addU32("x", false, 0, "Window X position"); |
|
cmd.addU32("y", false, 0, "Window Y position"); |
|
cmd.addFlag("decorated", "Draw window title, min/max buttons and border."); |
|
|
|
if (0 != cmd.init(argc, argv)) { |
|
SX_ERROR("Failed to initialize."); |
|
exit(EXIT_FAILURE); |
|
} |
|
|
|
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, (true == cmd.hasFlag("decorated")) ? GL_TRUE : GL_FALSE); |
|
|
|
GLFWwindow* win = NULL; |
|
int w = cmd.getU32("width"); |
|
int h = cmd.getU32("height"); |
|
int x = cmd.getU32("x"); |
|
int y = cmd.getU32("y"); |
|
win_w = w; |
|
win_h = h; |
|
|
|
win = glfwCreateWindow(w, h, "application title", 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 (cmd.getU32("x") && cmd.getU32("y")) { |
|
glfwSetWindowPos(win, x, y); |
|
} |
|
|
|
if (!gladLoadGL()) { |
|
printf("Cannot load GL.\n"); |
|
exit(1); |
|
} |
|
|
|
// ---------------------------------------------------------------- |
|
// THIS IS WHERE YOU START CALLING OPENGL FUNCTIONS, NOT EARLIER!! |
|
// ---------------------------------------------------------------- |
|
|
|
poly_log_init(1024, argc, argv); |
|
poly_log_add_sink_stdout(); |
|
|
|
glDisable(GL_DEPTH_TEST); |
|
glDisable(GL_DITHER); |
|
glEnable(GL_BLEND); |
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
/* -------------------------------------------------------------------- */ |
|
|
|
/* Our main render loop. */ |
|
while(!glfwWindowShouldClose(win)) { |
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|
glViewport(0, 0, w, 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 char_callback(GLFWwindow* win, unsigned int key) { |
|
} |
|
|
|
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 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) { |
|
|
|
double mx = 0.0; |
|
double my = 0.0; |
|
glfwGetCursorPos(win, &mx, &my); |
|
|
|
if (GLFW_PRESS == action) { |
|
} |
|
else if (GLFW_RELEASE == action) { |
|
} |
|
} |
|
|
|
void error_callback(int err, const char* desc) { |
|
printf("GLFW error: %s (%d)\n", desc, err); |
|
} |
|
|
|
/* -------------------------------------------------------------------- */ |