Last active
September 17, 2020 05:49
-
-
Save roxlu/6698180 to your computer and use it in GitHub Desktop.
Super basic glfw setup - this uses GLFX to setup function pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
._DS* | |
*DS_Store* | |
build | |
install | |
extern | |
node-* | |
references |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8) | |
set(app ocean) | |
set(sd ${CMAKE_CURRENT_LIST_DIR}/../src/) | |
include_directories( | |
${CMAKE_CURRENT_LIST_DIR}/../src | |
${CMAKE_CURRENT_LIST_DIR}/../extern/include | |
) | |
add_executable(${app} | |
${sd}/main.cpp | |
) | |
if(APPLE) | |
find_library(fr_corefoundation CoreFoundation) | |
find_library(fr_cocoa Cocoa) | |
find_library(fr_opengl OpenGL) | |
find_library(fr_iokit IOKit) | |
find_library(fr_corevideo CoreVideo) | |
target_link_libraries( | |
${app} | |
${CMAKE_CURRENT_LIST_DIR}/../extern/lib/libglfw3.a | |
${fr_corefoundation} | |
${fr_cocoa} | |
${fr_opengl} | |
${fr_iokit} | |
${fr_corevideo} | |
-lz | |
) | |
elseif(WIN32) | |
get_filename_component(ed ${EXTERN_DIR} ABSOLUTE) | |
target_link_libraries( | |
${app} | |
${ed}/lib/glfw3.lib | |
Opengl32.lib | |
ws2_32.lib # opengl / glfw | |
psapi.lib # opengl / glfw | |
iphlpapi.lib # opengl / glfw | |
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
GLFW TEMPLATE | |
============= | |
Latest version: | |
------------------------------------------------------------ | |
https://gist.github.com/roxlu/6698180 | |
------------------------------------------------------------ | |
- 2020-05-06: Cleaned up a bit. | |
- 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 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 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); | |
} | |
/* -------------------------------------------------------------------- */ | |
void char_callback(GLFWwindow* win, unsigned int key) { } | |
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) { } | |
/* -------------------------------------------------------------------- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
if [ ! -d build ] ; then | |
mkdir build | |
fi | |
cd build | |
cmake ../ | |
cmake --build . | |
./ocean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment