Skip to content

Commit 55e36ba

Browse files
authored
Merge pull request #1 from Digit16/main_structure_rework
Move code from main to thair respective files.
2 parents 069aadb + c7329ce commit 55e36ba

File tree

10 files changed

+343
-276
lines changed

10 files changed

+343
-276
lines changed

Callbacks.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Callbacks.h"
2+
#include <cstdio>
3+
4+
#include "Game.h"
5+
6+
7+
void errorCallback(int error, const char* description) {
8+
fputs(description, stderr);
9+
}
10+
11+
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
12+
Game& game = Game::getInstance();
13+
game.m_textArea.keyInput(key, scancode, action, mods);
14+
}
15+
16+
void charCallback(GLFWwindow* window, unsigned int codepoint) {
17+
Game& game = Game::getInstance();
18+
game.m_textArea.charInput(codepoint);
19+
}
20+
21+
void windowResizeCallback(GLFWwindow* window, int width, int height) {
22+
if (height == 0) return;
23+
glViewport(0, 0, width, height);
24+
}
25+

Callbacks.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <GL/glew.h>
4+
#include <GLFW/glfw3.h>
5+
6+
7+
void errorCallback(int error, const char* description);
8+
9+
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
10+
11+
void charCallback(GLFWwindow* window, unsigned int codepoint);
12+
13+
void windowResizeCallback(GLFWwindow* window, int width, int height);

Game.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include "Game.h"
2+
#include "Callbacks.h"
3+
#include "Utils.h"
4+
5+
void Game::initOpenGL() {
6+
7+
glfwSetErrorCallback(errorCallback);
8+
9+
if (!glfwInit()) {
10+
fprintf(stderr, "Failed to initialize GLFW.\n");
11+
exit(EXIT_FAILURE);
12+
}
13+
14+
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
15+
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
16+
17+
int width = 800;
18+
int height = 800;
19+
20+
m_window = glfwCreateWindow(width, height, "OpenGL", NULL, NULL);
21+
22+
if (!m_window)
23+
{
24+
fprintf(stderr, "Failed to create window.\n");
25+
glfwTerminate();
26+
exit(EXIT_FAILURE);
27+
}
28+
29+
glfwMakeContextCurrent(m_window);
30+
glfwSwapInterval(1);
31+
32+
if (glewInit() != GLEW_OK) {
33+
fprintf(stderr, "Failed to initialize GLEW.\n");
34+
glfwTerminate();
35+
exit(EXIT_FAILURE);
36+
}
37+
}
38+
39+
void Game::init() {
40+
41+
glClearColor(0.8f, 0.8f, 0.8f, 1);
42+
glEnable(GL_DEPTH_TEST);
43+
44+
glEnable(GL_BLEND);
45+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
46+
47+
48+
glfwSetWindowSizeCallback(m_window, windowResizeCallback);
49+
glfwSetKeyCallback(m_window, keyCallback);
50+
glfwSetCharCallback(m_window, charCallback);
51+
52+
m_sp2d = new ShaderProgram("shaders/v_2d.glsl", "shaders/g_2d.glsl", "shaders/f_2d.glsl");
53+
m_sp2dPost = new ShaderProgram("shaders/v_post.glsl", nullptr, "shaders/f_post.glsl");
54+
55+
m_asciiTexture = loadTexture("assets/ascii.png");
56+
57+
m_frameColorBuffer = createColorBuffer(m_window);
58+
m_frameDepthBuffer = createDepthBuffer(m_window);
59+
m_frameBuffer = createFrameBuffer(m_frameColorBuffer, m_frameDepthBuffer);
60+
61+
}
62+
63+
void Game::update(double delta) {
64+
m_textArea.update(delta);
65+
66+
}
67+
68+
void Game::draw() {
69+
glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);
70+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71+
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
72+
73+
m_sp2d->use();
74+
glEnableVertexAttribArray(m_sp2d->a("pos"));
75+
glEnableVertexAttribArray(m_sp2d->a("dim"));
76+
glEnableVertexAttribArray(m_sp2d->a("tex"));
77+
glEnableVertexAttribArray(m_sp2d->a("inv"));
78+
79+
bindTilemap(m_sp2d, m_asciiTexture, 2, glm::ivec2(16, 16));
80+
81+
int width, height;
82+
glfwGetWindowSize(m_window, &width, &height);
83+
glUniform2f(m_sp2d->u("window"), width, height);
84+
glUniform1f(m_sp2d->u("layer"), 0.0f);
85+
86+
m_textArea.draw(m_sp2d);
87+
88+
// The value zero is reserved to represent the default framebuffer provided by the windowing system
89+
glBindFramebuffer(GL_FRAMEBUFFER, 0);
90+
glDisable(GL_DEPTH_TEST);
91+
glClear(GL_COLOR_BUFFER_BIT);
92+
93+
m_sp2dPost->use();
94+
glActiveTexture(GL_TEXTURE0 + 3);
95+
glBindTexture(GL_TEXTURE_2D, m_frameColorBuffer);
96+
glDrawArrays(GL_TRIANGLES, 0, 6);
97+
glUniform1i(m_sp2dPost->u("colorBuffer"), 3);
98+
glfwSwapBuffers(m_window);
99+
}
100+
101+
void Game::clean() {
102+
delete m_sp2d;
103+
delete m_sp2dPost;
104+
105+
glDeleteFramebuffers(1, &m_frameBuffer);
106+
glDeleteTextures(1, &m_frameColorBuffer);
107+
glDeleteRenderbuffers(1, &m_frameDepthBuffer);
108+
}
109+
110+
void Game::cleanOpenGL() {
111+
glfwDestroyWindow(m_window);
112+
glfwTerminate();
113+
}
114+
115+

Game.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
3+
#include "Callbacks.h"
4+
#include "ShaderProgram.h"
5+
#include "GraphicalTextArea.h"
6+
7+
8+
class Game{
9+
10+
private:
11+
void initOpenGL();
12+
void init();
13+
void update(double delta);
14+
void draw();
15+
void clean();
16+
void cleanOpenGL();
17+
18+
public:
19+
static Game& getInstance() {
20+
static Game instance;
21+
return instance;
22+
}
23+
24+
void run() {
25+
initOpenGL();
26+
init();
27+
28+
double lastFrameTime = glfwGetTime();
29+
30+
while (!glfwWindowShouldClose(m_window))
31+
{
32+
double newFrameTime = glfwGetTime();
33+
double delta = newFrameTime - lastFrameTime;
34+
35+
update(delta);
36+
draw();
37+
38+
lastFrameTime = newFrameTime;
39+
glfwPollEvents();
40+
}
41+
42+
clean();
43+
cleanOpenGL();
44+
}
45+
46+
private:
47+
GLFWwindow* m_window;
48+
GraphicalTextArea m_textArea{ };
49+
50+
ShaderProgram* m_sp2d{ nullptr };
51+
ShaderProgram* m_sp2dPost{ nullptr };
52+
53+
GLuint m_frameColorBuffer;
54+
GLuint m_frameDepthBuffer;
55+
GLuint m_frameBuffer;
56+
57+
GLuint m_asciiTexture;
58+
59+
friend void errorCallback(int error, const char* description);
60+
friend void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
61+
friend void charCallback(GLFWwindow* window, unsigned int codepoint);
62+
friend void windowResizeCallback(GLFWwindow* window, int width, int height);
63+
};
64+

GraphicalTextArea.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@ bool GraphicalTextArea::charInput(unsigned int codepoint) {
8787
return true;
8888
}
8989

90-
void GraphicalTextArea::draw(ShaderProgram* shader, double delta) {
91-
90+
void GraphicalTextArea::update(double delta) {
9291
// update cursor blink
9392
cursorBlinkTimer.update(delta);
9493
if (cursorBlinkTimer.done()) {
9594
cursorBlinkState = !cursorBlinkState;
9695
cursorBlinkTimer.restart();
9796
}
97+
}
98+
99+
void GraphicalTextArea::draw(ShaderProgram* shader) {
98100

99101
data.clear();
100102

GraphicalTextArea.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class GraphicalTextArea : public TextArea {
2020
bool keyInput(int key, int scancode, int action, int mods);
2121
bool charInput(unsigned int codepoint);
2222

23-
void draw(ShaderProgram* shader, double delta);
23+
void update(double delta);
24+
void draw(ShaderProgram* shader);
2425

2526
private:
2627
std::vector<DrawData> data;

0 commit comments

Comments
 (0)