JustPaste.it

#include <iostream>
#include <GLFW/glfw3.h>

static void error_callback(int error, const char* description)
{
fputs(description, stderr);
std::cout << "Error: " << description << std::endl;
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
// This is never called!
std::cout << "Key.." << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
{

}
}

void character_callback(GLFWwindow* window, unsigned int codepoint)
{
std::cout << "key char" << std::endl;
}

static void window_close_callback(GLFWwindow* window)
{
std::cout << "Closing window.." << std::endl;
}

int main()
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);

// glfwWindowHint(GLFW_RESIZABLE, 0);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_FOCUSED, 1);
bool fullscreen = false;
GLFWmonitor* monitor = fullscreen ? glfwGetPrimaryMonitor() : nullptr;
// const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// glfwWindowHint(GLFW_RED_BITS, mode->redBits);
// glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
// glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
// glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
window = glfwCreateWindow(1024, 768, "Simple GLFW Test", monitor, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
glfwSetCharCallback(window, character_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}