Skip to content

Commit e59b19b

Browse files
committed
add output function callback, create context class
1 parent a6f5272 commit e59b19b

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

include/gl_layer/context.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
extern "C" {
66
#endif
77

8-
int gl_layer_init(int gl_version_major, int gl_version_minor);
8+
int gl_layer_init(unsigned int gl_version_major, unsigned int gl_version_minor);
99
void gl_layer_terminate();
1010

1111
void gl_layer_callback(const char* name, void* func_ptr, int num_args, ...);
1212

13+
typedef void (*GLLayerOutputFun)(const char* text);
14+
void gl_layer_set_output_callback(GLLayerOutputFun callback);
15+
1316
#ifdef __cplusplus
1417
};
1518
#endif

src/context.cpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
1-
#include <gl_layer/context.h>
1+
#include <gl_layer/context.h>
2+
3+
#include <cstdio>
4+
5+
namespace gl_layer {
6+
7+
void default_output_func(const char* text) {
8+
printf("%s", text);
9+
}
10+
11+
struct Version {
12+
unsigned int major = 0;
13+
unsigned int minor = 0;
14+
};
15+
16+
class Context {
17+
public:
18+
explicit Context(Version gl_version) : gl_version(gl_version) {
19+
20+
}
21+
22+
void set_output_callback(GLLayerOutputFun callback) {
23+
output_fun = callback;
24+
}
25+
26+
private:
27+
Version gl_version;
28+
GLLayerOutputFun output_fun = &default_output_func;
29+
};
30+
31+
32+
namespace {
33+
Context* g_context = nullptr;
34+
}
35+
36+
} // namespace gl_layer
37+
38+
int gl_layer_init(unsigned int gl_version_major, unsigned int gl_version_minor) {
39+
gl_layer::g_context = new gl_layer::Context(gl_layer::Version{ gl_version_major, gl_version_minor });
40+
if (gl_layer::g_context) return 0;
41+
42+
return -1;
43+
}
44+
45+
void gl_layer_terminate() {
46+
delete gl_layer::g_context;
47+
}
48+
49+
void gl_layer_callback(const char* name, void* func_ptr, int num_args, ...) {
50+
if (!gl_layer::g_context) {
51+
// Report error: context not initialized.
52+
return;
53+
}
54+
}
55+
56+
void gl_layer_set_output_callback(GLLayerOutputFun callback) {
57+
if (!gl_layer::g_context) {
58+
// Report error: context not initialized.
59+
return;
60+
}
61+
gl_layer::g_context->set_output_callback(callback);
62+
}

0 commit comments

Comments
 (0)