Main.cpp
#include "Window.h"
using namespace std;
int main()
{
using namespace sparky;
using namespace graphics;
Window window("Sparky", 800, 600);
glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
while (!window.closed())
{
window.clear();
if (window.isKeyPressed(GLFW_KEY_A))
{
std::cout << "pressed" << std::endl;
}
#if 1
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, 0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
#else
glDrawArrays(GL_ARRAY_BUFFER, 0, 6);
#endif
window.update();
}
return 0;
}
Window.cpp
#include "Window.h"
namespace sparky {
namespace graphics {
bool Window::m_Keys[MAX_KEYS];
bool Window::m_MouseButtons[MAX_BUTTONS];
void Window_Resize(GLFWwindow* window, int width, int height);
double Window::mx;
double Window::my;
Window::Window(const char* title, int width, int height)
{
m_Title = title;
m_Width = width;
m_Height = height;
if (!init())
glfwTerminate();
for (int i = 0; i < MAX_KEYS; i++)
{
m_Keys[i] = false;
}
for (int i = 0; i < MAX_BUTTONS; i++)
{
m_MouseButtons[i] = false;
}
}
Window::~Window()
{
glfwTerminate();
}
bool Window::init()
{
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW!" << std::endl;
return false;
}
m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!m_Window)
{
std::cout << "failed to create window" << std::endl;
return false;
}
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glfwMakeContextCurrent(m_Window);
glfwSetWindowUserPointer(m_Window, this);
void* glfwGetWindowUserPointer(GLFWwindow * m_Window);
glfwSetWindowSizeCallback(m_Window, Window_Resize);
glfwSetKeyCallback(m_Window, key_callback);
return true;
}
bool Window::isKeyPressed(unsigned int keycode)
{
if (keycode >= MAX_KEYS)
return false;
return m_Keys[keycode];
}
void Window::clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::update() {
glfwPollEvents();
glfwSwapBuffers(m_Window);
}
bool Window::closed() const
{
return glfwWindowShouldClose(m_Window) == 1;
}
void Window_Resize(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
Window* win = (Window*) glfwGetWindowUserPointer(window);
win->m_Keys[key] = action != GLFW_RELEASE;
}
}
}
Window.h
#pragma once
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
namespace sparky {
namespace graphics {
#define MAX_KEYS 1024
#define MAX_BUTTONS 32
class Window
{
private:
const char* m_Title;
int m_Width, m_Height;
GLFWwindow* m_Window;
bool m_Closed;
static bool m_Keys[MAX_KEYS];
static bool m_MouseButtons[MAX_BUTTONS];
static double mx, my;
public:
Window(const char* name, int width, int height);
~Window();
void clear() const;
void update();
bool closed() const;
inline int getWidth() const { return m_Width; }
inline int getHeight() const { return m_Height; }
static bool isKeyPressed(unsigned int keycode);
private:
bool init();
friend static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
};
}
}
this ether needs troubleshooting or a fix in the next release this is horrible