-
Notifications
You must be signed in to change notification settings - Fork 28
/
gl_app.cc
101 lines (75 loc) · 2.46 KB
/
gl_app.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "gl/gl_app.hpp"
#include <chrono>
#include "example_config.hpp"
namespace example {
GLFWwindow* init_glfw_window(uint32_t width, uint32_t height,
const char* title) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// multisample
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (window == nullptr) {
exit(-2);
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
exit(-3);
}
return window;
}
GLApp::GLApp(int32_t width, int32_t height, std::string name,
const glm::vec4& clear_color)
: width_(width), height_(height), name_(name), clear_color_(clear_color) {}
GLApp::~GLApp() = default;
void GLApp::Run() {
Init();
RunLoop();
Destroy();
}
void GLApp::Init() {
window_ = init_glfw_window(width_, height_, name_.c_str());
glClearColor(clear_color_.x, clear_color_.y, clear_color_.z, clear_color_.w);
glClearStencil(0x0);
glStencilMask(0xFF);
// blend is need for anti-alias
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_STENCIL_TEST);
int32_t pp_width, pp_height;
glfwGetFramebufferSize(window_, &pp_width, &pp_height);
float density = (float)(pp_width * pp_width + pp_height * pp_height) /
(float)(width_ * width_ + height_ * height_);
skity::GPUContext ctx{skity::GPUBackendType::kOpenGL,
(void*)glfwGetProcAddress};
canvas_ = skity::Canvas::MakeHardwareAccelationCanvas(width_, height_,
density, &ctx);
canvas_->setDefaultTypeface(
skity::Typeface::MakeFromFile(EXAMPLE_DEFAULT_FONT));
OnStart();
}
void GLApp::RunLoop() {
float frame_count = 0;
uint64_t frame_total_caust = 0;
while (!glfwWindowShouldClose(window_)) {
auto frame_start = std::chrono::system_clock::now();
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
this->OnUpdate(0.f);
glfwSwapBuffers(window_);
glfwPollEvents();
}
}
void GLApp::Destroy() {
OnDestroy();
canvas_.reset();
}
void GLApp::GetCursorPos(double& x, double& y) {
double mx, my;
glfwGetCursorPos(window_, &mx, &my);
x = mx;
y = my;
}
} // namespace example