Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Callbacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "Callbacks.h"
#include <cstdio>

#include "Game.h"


void errorCallback(int error, const char* description) {
fputs(description, stderr);
}

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
Game& game = Game::getInstance();
game.m_textArea.keyInput(key, scancode, action, mods);
}

void charCallback(GLFWwindow* window, unsigned int codepoint) {
Game& game = Game::getInstance();
game.m_textArea.charInput(codepoint);
}

void windowResizeCallback(GLFWwindow* window, int width, int height) {
if (height == 0) return;
glViewport(0, 0, width, height);
}

13 changes: 13 additions & 0 deletions Callbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <GL/glew.h>
#include <GLFW/glfw3.h>


void errorCallback(int error, const char* description);

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);

void charCallback(GLFWwindow* window, unsigned int codepoint);

void windowResizeCallback(GLFWwindow* window, int width, int height);
115 changes: 115 additions & 0 deletions Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "Game.h"
#include "Callbacks.h"
#include "Utils.h"

void Game::initOpenGL() {

glfwSetErrorCallback(errorCallback);

if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW.\n");
exit(EXIT_FAILURE);
}

glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);

int width = 800;
int height = 800;

m_window = glfwCreateWindow(width, height, "OpenGL", NULL, NULL);

if (!m_window)
{
fprintf(stderr, "Failed to create window.\n");
glfwTerminate();
exit(EXIT_FAILURE);
}

glfwMakeContextCurrent(m_window);
glfwSwapInterval(1);

if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW.\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
}

void Game::init() {

glClearColor(0.8f, 0.8f, 0.8f, 1);
glEnable(GL_DEPTH_TEST);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


glfwSetWindowSizeCallback(m_window, windowResizeCallback);
glfwSetKeyCallback(m_window, keyCallback);
glfwSetCharCallback(m_window, charCallback);

m_sp2d = new ShaderProgram("shaders/v_2d.glsl", "shaders/g_2d.glsl", "shaders/f_2d.glsl");
m_sp2dPost = new ShaderProgram("shaders/v_post.glsl", nullptr, "shaders/f_post.glsl");

m_asciiTexture = loadTexture("assets/ascii.png");

m_frameColorBuffer = createColorBuffer(m_window);
m_frameDepthBuffer = createDepthBuffer(m_window);
m_frameBuffer = createFrameBuffer(m_frameColorBuffer, m_frameDepthBuffer);

}

void Game::update(double delta) {
m_textArea.update(delta);

}

void Game::draw() {
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

m_sp2d->use();
glEnableVertexAttribArray(m_sp2d->a("pos"));
glEnableVertexAttribArray(m_sp2d->a("dim"));
glEnableVertexAttribArray(m_sp2d->a("tex"));
glEnableVertexAttribArray(m_sp2d->a("inv"));

bindTilemap(m_sp2d, m_asciiTexture, 2, glm::ivec2(16, 16));

int width, height;
glfwGetWindowSize(m_window, &width, &height);
glUniform2f(m_sp2d->u("window"), width, height);
glUniform1f(m_sp2d->u("layer"), 0.0f);

m_textArea.draw(m_sp2d);

// The value zero is reserved to represent the default framebuffer provided by the windowing system
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);

m_sp2dPost->use();
glActiveTexture(GL_TEXTURE0 + 3);
glBindTexture(GL_TEXTURE_2D, m_frameColorBuffer);
glDrawArrays(GL_TRIANGLES, 0, 6);
glUniform1i(m_sp2dPost->u("colorBuffer"), 3);
glfwSwapBuffers(m_window);
}

void Game::clean() {
delete m_sp2d;
delete m_sp2dPost;

glDeleteFramebuffers(1, &m_frameBuffer);
glDeleteTextures(1, &m_frameColorBuffer);
glDeleteRenderbuffers(1, &m_frameDepthBuffer);
}

void Game::cleanOpenGL() {
glfwDestroyWindow(m_window);
glfwTerminate();
}


64 changes: 64 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "Callbacks.h"
#include "ShaderProgram.h"
#include "GraphicalTextArea.h"


class Game{

private:
void initOpenGL();
void init();
void update(double delta);
void draw();
void clean();
void cleanOpenGL();

public:
static Game& getInstance() {
static Game instance;
return instance;
}

void run() {
initOpenGL();
init();

double lastFrameTime = glfwGetTime();

while (!glfwWindowShouldClose(m_window))
{
double newFrameTime = glfwGetTime();
double delta = newFrameTime - lastFrameTime;

update(delta);
draw();

lastFrameTime = newFrameTime;
glfwPollEvents();
}

clean();
cleanOpenGL();
}

private:
GLFWwindow* m_window;
GraphicalTextArea m_textArea{ };

ShaderProgram* m_sp2d{ nullptr };
ShaderProgram* m_sp2dPost{ nullptr };

GLuint m_frameColorBuffer;
GLuint m_frameDepthBuffer;
GLuint m_frameBuffer;

GLuint m_asciiTexture;

friend void errorCallback(int error, const char* description);
friend void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
friend void charCallback(GLFWwindow* window, unsigned int codepoint);
friend void windowResizeCallback(GLFWwindow* window, int width, int height);
};

6 changes: 4 additions & 2 deletions GraphicalTextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ bool GraphicalTextArea::charInput(unsigned int codepoint) {
return true;
}

void GraphicalTextArea::draw(ShaderProgram* shader, double delta) {

void GraphicalTextArea::update(double delta) {
// update cursor blink
cursorBlinkTimer.update(delta);
if (cursorBlinkTimer.done()) {
cursorBlinkState = !cursorBlinkState;
cursorBlinkTimer.restart();
}
}

void GraphicalTextArea::draw(ShaderProgram* shader) {

data.clear();

Expand Down
3 changes: 2 additions & 1 deletion GraphicalTextArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GraphicalTextArea : public TextArea {
bool keyInput(int key, int scancode, int action, int mods);
bool charInput(unsigned int codepoint);

void draw(ShaderProgram* shader, double delta);
void update(double delta);
void draw(ShaderProgram* shader);

private:
std::vector<DrawData> data;
Expand Down
Loading