Skip to content

Commit 7b75a81

Browse files
committed
finishrefactor
1 parent 9c40cff commit 7b75a81

File tree

5 files changed

+71
-53
lines changed

5 files changed

+71
-53
lines changed

include/Game.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#pragma once
22

33
#include "check_gl.hpp"
4-
#include <glm/glm.hpp>
5-
#include <glm/ext.hpp>
64
#include <memory>
75

86
struct Game {
97
struct Private;
10-
std::unique_ptr<Private> m_private;
11-
GLFWwindow *m_window;
8+
std::unique_ptr<Private> const m_private;
9+
GLFWwindow *const m_window;
1210

1311
Game(GLFWwindow *window);
1412
~Game();
@@ -17,5 +15,8 @@ struct Game {
1715

1816
void initialize();
1917
void render();
18+
void cursor_pos_callback(double xpos, double ypos);
2019
void mouse_button_callback(int button, int action, int mods);
20+
void scroll_callback(double xoffset, double yoffset);
21+
void key_callback(int key, int scancode, int action, int mods);
2122
};

include/check_gl.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
#include <glad/glad.h>
44
#include <GLFW/glfw3.h>
55

6+
namespace check_gl {
67
const char *opengl_errno_name(GLenum err);
7-
void check_gl_error(const char *filename, int lineno, const char *expr);
8-
8+
void opengl_check_error(const char *filename, int lineno, const char *expr);
9+
bool opengl_has_extension(const char *extension);
10+
void opengl_try_enable_debug_message();
11+
void opengl_show_glfw_error_diagnose();
12+
}
913

1014
#define CHECK_GL(x) do { \
1115
(x); \
12-
check_gl_error(__FILE__, __LINE__, #x); \
16+
::check_gl::opengl_check_error(__FILE__, __LINE__, #x); \
1317
} while (0)
14-
15-
bool opengl_has_extension(const char *extension);
16-
17-
void opengl_try_enable_debug_message();
18-
19-
void opengl_show_glfw_error_diagnose();

src/Game.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "Game.hpp"
2-
#include "OBJ.hpp"
32
#include "check_gl.hpp"
3+
#include <glm/glm.hpp>
4+
#include <glm/ext.hpp>
5+
#include "OBJ.hpp"
46

57
struct Game::Private {
68
OBJ monkey;
@@ -12,6 +14,20 @@ Game::Game(GLFWwindow *window) : m_private(new Private), m_window(window) {
1214

1315
Game::~Game() = default;
1416

17+
void Game::initialize() {
18+
m_private->monkey.load_obj("/home/bate/Codes/opengltutor/assets/monkey.obj");
19+
CHECK_GL(glEnable(GL_DEPTH_TEST));
20+
CHECK_GL(glEnable(GL_MULTISAMPLE));
21+
CHECK_GL(glEnable(GL_BLEND));
22+
CHECK_GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
23+
CHECK_GL(glEnable(GL_LIGHTING));
24+
CHECK_GL(glEnable(GL_LIGHT0));
25+
CHECK_GL(glEnable(GL_COLOR_MATERIAL));
26+
CHECK_GL(glEnable(GL_CULL_FACE));
27+
CHECK_GL(glCullFace(GL_BACK));
28+
CHECK_GL(glFrontFace(GL_CCW));
29+
}
30+
1531
void Game::render() {
1632
int width, height;
1733
glfwGetWindowSize(m_window, &width, &height);
@@ -39,34 +55,39 @@ void Game::render() {
3955
CHECK_GL(glMatrixMode(GL_MODELVIEW));
4056
CHECK_GL(glLoadMatrixf(glm::value_ptr(view * model)));
4157

42-
m_monkey.draw_obj();
58+
m_private->monkey.draw_obj();
59+
}
60+
61+
void Game::cursor_pos_callback(double xpos, double ypos) {
62+
int width, height;
63+
glfwGetWindowSize(m_window, &width, &height);
64+
65+
float x = (float)(2 * xpos / width - 1);
66+
float y = (float)(2 * (height - ypos) / height - 1);
67+
68+
if (glfwGetMouseButton(m_window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
69+
// TODO: when left mouse button is dragged
70+
}
4371
}
4472

4573
void Game::mouse_button_callback(int button, int action, int mods) {
4674
double xpos, ypos;
47-
int width, height;
4875
glfwGetCursorPos(m_window, &xpos, &ypos);
76+
int width, height;
4977
glfwGetWindowSize(m_window, &width, &height);
5078

5179
float x = (float)(2 * xpos / width - 1);
5280
float y = (float)(2 * (height - ypos) / height - 1);
5381

5482
if ( button == GLFW_MOUSE_BUTTON_LEFT
5583
&& action == GLFW_PRESS
56-
) { // when left mouse button is pressed:
84+
) {
85+
// TODO: when left mouse button is pressed
5786
}
5887
}
5988

60-
void Game::initialize() {
61-
m_private->monkey.load_obj("/home/bate/Codes/opengltutor/assets/monkey.obj");
62-
CHECK_GL(glEnable(GL_DEPTH_TEST));
63-
CHECK_GL(glEnable(GL_MULTISAMPLE));
64-
CHECK_GL(glEnable(GL_BLEND));
65-
CHECK_GL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
66-
CHECK_GL(glEnable(GL_LIGHTING));
67-
CHECK_GL(glEnable(GL_LIGHT0));
68-
CHECK_GL(glEnable(GL_COLOR_MATERIAL));
69-
CHECK_GL(glEnable(GL_CULL_FACE));
70-
CHECK_GL(glCullFace(GL_BACK));
71-
CHECK_GL(glFrontFace(GL_CCW));
89+
void Game::scroll_callback(double xoffset, double yoffset) {
90+
}
91+
92+
void Game::key_callback(int key, int scancode, int action, int mods) {
7293
}

src/check_gl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <string>
77
#include <set>
88

9-
const char *opengl_errno_name(GLenum err) {
9+
const char *check_gl::opengl_errno_name(GLenum err) {
1010
switch (err) {
1111
#define PER_GL_ERROR(x) case GL_##x: return #x;
1212
PER_GL_ERROR(NO_ERROR)
@@ -21,15 +21,15 @@ const char *opengl_errno_name(GLenum err) {
2121
return "unknown error";
2222
}
2323

24-
void check_gl_error(const char *filename, int lineno, const char *expr) {
24+
void check_gl::opengl_check_error(const char *filename, int lineno, const char *expr) {
2525
GLenum err = glad_glGetError();
2626
if (err != GL_NO_ERROR) {
2727
std::cerr << filename << ":" << lineno << ": " << expr << " failed: " << opengl_errno_name(err) << '\n';
2828
std::terminate();
2929
}
3030
}
3131

32-
bool opengl_has_extension(const char *extension) {
32+
bool check_gl::opengl_has_extension(const char *extension) {
3333
static auto exts = [] {
3434
GLint n = 0;
3535
glGetIntegerv(GL_NUM_EXTENSIONS, &n);
@@ -43,7 +43,7 @@ bool opengl_has_extension(const char *extension) {
4343
return exts.find(extension) != exts.end();
4444
}
4545

46-
void APIENTRY opengl_debug_message_callback(
46+
static void APIENTRY opengl_debug_message_callback(
4747
GLenum source, GLenum type, GLuint id,
4848
GLenum severity, GLsizei length,
4949
const GLchar *msg, const void *data)
@@ -151,7 +151,7 @@ void APIENTRY opengl_debug_message_callback(
151151
}
152152
}
153153

154-
void opengl_try_enable_debug_message() {
154+
void check_gl::opengl_try_enable_debug_message() {
155155
if (opengl_has_extension("GL_ARB_debug_output")) {
156156
auto my_glDebugMessageCallbackARB = (PFNGLDEBUGMESSAGECALLBACKPROC)glfwGetProcAddress("glDebugMessageCallbackARB");
157157
if (my_glDebugMessageCallbackARB) {
@@ -161,7 +161,7 @@ void opengl_try_enable_debug_message() {
161161
}
162162
}
163163

164-
void opengl_show_glfw_error_diagnose() {
164+
void check_gl::opengl_show_glfw_error_diagnose() {
165165
const char *errmsg;
166166
glfwGetError(&errmsg);
167167
if (!errmsg) errmsg = "(no error)";

src/main.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@
77
#endif
88
#include "Game.hpp"
99

10-
namespace {
11-
static struct {
12-
template <class Ret, class ...Ts>
13-
using function_type = Ret(Ts...);
14-
15-
template <class ...Ts>
16-
operator function_type<void, GLFWwindow *, Ts...> *() {
17-
return [] (GLFWwindow *window, Ts ...args) {
18-
auto game = (Game *)glfwGetWindowUserPointer(window);
19-
if (game) [[likely]] {
20-
game->mouse_button_callback(args...);
21-
}
22-
};
23-
}
24-
} glfw_callback_helper;
10+
template <class ...Ts>
11+
static void (*glfw_game_callback(void (Game::*pFn)(Ts...)))(GLFWwindow *, Ts...) {
12+
static void (Game::*gpFn)(Ts...);
13+
gpFn = pFn;
14+
return [] (GLFWwindow *window, Ts ...args) -> void {
15+
auto game = (Game *)glfwGetWindowUserPointer(window);
16+
if (game) [[likely]] {
17+
(game->*gpFn)(args...);
18+
}
19+
};
2520
}
2621

2722
int main() {
@@ -65,7 +60,7 @@ int main() {
6560

6661
// Test if window creation succeed
6762
if (!window) {
68-
opengl_show_glfw_error_diagnose();
63+
check_gl::opengl_show_glfw_error_diagnose();
6964
glfwTerminate();
7065
return -1;
7166
}
@@ -117,7 +112,7 @@ int main() {
117112
std::cerr << "GLAD failed to load GL functions\n";
118113
return -1;
119114
}
120-
opengl_try_enable_debug_message();
115+
check_gl::opengl_try_enable_debug_message();
121116

122117
// Print diagnostic information
123118
std::cerr << "OpenGL version: " << (const char *)glGetString(GL_VERSION) << '\n';
@@ -126,7 +121,10 @@ int main() {
126121
Game game(window);
127122

128123
// Register window callbacks
129-
glfwSetMouseButtonCallback(window, glfw_callback_helper);
124+
glfwSetCursorPosCallback(window, glfw_game_callback(&Game::cursor_pos_callback));
125+
glfwSetMouseButtonCallback(window, glfw_game_callback(&Game::mouse_button_callback));
126+
glfwSetScrollCallback(window, glfw_game_callback(&Game::scroll_callback));
127+
glfwSetKeyCallback(window, glfw_game_callback(&Game::key_callback));
130128

131129
// Initialize data structures
132130
game.initialize();

0 commit comments

Comments
 (0)