-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathplatform.cpp
More file actions
113 lines (90 loc) · 3.2 KB
/
platform.cpp
File metadata and controls
113 lines (90 loc) · 3.2 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
#include "platform.hpp"
#include "../ImGui.hpp"
#include <Geode/loader/Log.hpp>
#include <Geode/cocos/platform/CCGL.h>
using namespace geode::prelude;
static bool g_shouldPassEventsToGDButTransformed = false;
static bool g_updateBuffer = false;
static ImRect g_GDWindowRect;
ImRect& getGDWindowRect() {
return g_GDWindowRect;
}
bool& shouldPassEventsToGDButTransformed() {
return g_shouldPassEventsToGDButTransformed;
}
bool& shouldUpdateGDRenderBuffer() {
return g_updateBuffer;
}
GLRenderCtx::~GLRenderCtx() {
this->cleanup();
}
void GLRenderCtx::cleanup() {
if (m_depthStencil) {
glDeleteRenderbuffers(1, &m_depthStencil);
m_depthStencil = 0;
}
if (m_texture) {
glDeleteTextures(1, &m_texture);
m_texture = 0;
}
if (m_buffer) {
glDeleteFramebuffers(1, &m_buffer);
m_buffer = 0;
}
}
GLRenderCtx::GLRenderCtx(ImVec2 const& size) : m_size(size) {}
ImTextureID GLRenderCtx::texture() const {
return static_cast<ImTextureID>(m_texture);
}
ImVec2 GLRenderCtx::size() const {
return m_size;
}
bool GLRenderCtx::begin() {
// save currently bound fbo and rbo
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_prevDrawBuffer);
glGetIntegerv(GL_RENDERBUFFER_BINDING, &m_prevReadBuffer);
if (!m_buffer) {
glGenFramebuffers(1, &m_buffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_buffer);
}
if (!m_texture) {
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB,
static_cast<GLsizei>(m_size.x),
static_cast<GLsizei>(m_size.y),
0,GL_RGB, GL_UNSIGNED_BYTE, 0
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
if (!m_depthStencil) {
glGenRenderbuffers(1, &m_depthStencil);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencil);
glRenderbufferStorage(
GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
static_cast<GLsizei>(m_size.x),
static_cast<GLsizei>(m_size.y)
);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencil);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0);
}
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
log::error("Unable to Render to Framebuffer");
this->cleanup();
return false;
}
// bind our framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, m_buffer);
return true;
}
void GLRenderCtx::end() {
// bind the renderbuffer and framebuffer that was bound before us
glBindRenderbuffer(GL_RENDERBUFFER, m_prevReadBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_prevDrawBuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
//glFlush();
}