Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Super basic glfw setup - this uses GLFX to setup function pointers
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()
/*
BASIC GLFW + GLXW WINDOW AND OPENGL SETUP
------------------------------------------
See https://gist.github.com/roxlu/6698180 for the latest version of the example.
We make use of the GLAD library for GL loading, see: https://github.com/Dav1dde/glad/
*/
#include <stdio.h>
#include <stdlib.h>
#include <poly/Glad.h>
#include <GLFW/glfw3.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);
int w = 1280;
int h = 720;
int main() {
glfwSetErrorCallback(error_callback);
if(!glfwInit()) {
printf("Error: cannot setup glfw.\n");
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GL_FALSE);
GLFWwindow* win = NULL;
win = glfwCreateWindow(w, h, "GLFW", 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);
glfwMakeContextCurrent(win);
glfwSwapInterval(1);
if (!gladLoadGL()) {
printf("Cannot load GL.\n");
exit(1);
}
// ----------------------------------------------------------------
// THIS IS WHERE YOU START CALLING OPENGL FUNCTIONS, NOT EARLIER!!
// ----------------------------------------------------------------
while(!glfwWindowShouldClose(win)) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, w, h);
glClearColor(0.12f, 0.12f, 0.12f, 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(action != GLFW_PRESS) {
return;
}
switch(key) {
case GLFW_KEY_SPACE: {
break;
}
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 button_callback(GLFWwindow* win, int bt, int action, int mods) {
double mouse_x = 0;
double mouse_y = 0;
glfwGetCursorPos(win, &mouse_x, &mouse_y);
if (GLFW_PRESS == action) {
}
else if (GLFW_RELEASE == action) {
}
else {
printf("Unhandled mouse action.\n");
}
}
void error_callback(int err, const char* desc) {
printf("GLFW error: %s (%d)\n", desc, err);
}
#!/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