generated from 4D-Modding/4dmod-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateTest.cpp
151 lines (124 loc) · 4.24 KB
/
StateTest.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "StateTest.h"
StateTest StateTest::instanceObj{};
void StateTest::updateProjection(const glm::ivec2& size)
{
glm::mat4 projection2D = glm::ortho(0.f, (float)size.x, (float)size.y, 0.f, -1.0f, 1.0f);
glViewport(0, 0, size.x, size.y);
// update all 2D shaders
const Shader* textShader = ShaderManager::get("textShader");
textShader->use();
glUniformMatrix4fv(glGetUniformLocation(textShader->id(), "P"), 1, GL_FALSE, &projection2D[0][0]);
const Shader* tex2DShader = ShaderManager::get("tex2DShader");
tex2DShader->use();
glUniformMatrix4fv(glGetUniformLocation(tex2DShader->id(), "P"), 1, GL_FALSE, &projection2D[0][0]);
const Shader* quadShader = ShaderManager::get("quadShader");
quadShader->use();
glUniformMatrix4fv(glGetUniformLocation(quadShader->id(), "P"), 1, GL_FALSE, &projection2D[0][0]);
}
void StateTest::viewportCallback(void* user, const glm::ivec4& pos, const glm::ivec2& scroll)
{
GLFWwindow* window = (GLFWwindow*)user;
// update the viewport
int wWidth, wHeight;
glfwGetWindowSize(window, &wWidth, &wHeight);
glViewport(pos.x, wHeight - pos.y - pos.w, pos.z, pos.w);
// create a 2D projection matrix from the specified dimensions and scroll position
glm::mat4 projection2D = glm::ortho(0.0f, (float)pos.z, (float)pos.w, 0.0f, -1.0f, 1.0f);
projection2D = glm::translate(projection2D, { scroll.x, scroll.y, 0 });
// update all 2D shaders
const Shader* textShader = ShaderManager::get("textShader");
textShader->use();
glUniformMatrix4fv(glGetUniformLocation(textShader->id(), "P"), 1, GL_FALSE, &projection2D[0][0]);
const Shader* tex2DShader = ShaderManager::get("tex2DShader");
tex2DShader->use();
glUniformMatrix4fv(glGetUniformLocation(tex2DShader->id(), "P"), 1, GL_FALSE, &projection2D[0][0]);
const Shader* quadShader = ShaderManager::get("quadShader");
quadShader->use();
glUniformMatrix4fv(glGetUniformLocation(quadShader->id(), "P"), 1, GL_FALSE, &projection2D[0][0]);
}
void StateTest::btn1Callback(void* user)
{
StateTest::instanceObj.clicks++;
// update the text
StateTest::instanceObj.text.setText(std::to_string(StateTest::instanceObj.clicks));
}
void StateTest::init(StateManager& s)
{
int width, height;
glfwGetWindowSize(s.window, &width, &height);
qs = ShaderManager::get("quadShader");
font = { ResourceManager::get("pixelfont.png"), ShaderManager::get("textShader") };
qr.shader = qs;
qr.init();
ui = gui::Interface{ s.window };
ui.viewportCallback = viewportCallback;
ui.viewportUser = s.window;
ui.font = &font;
ui.qr = &qr;
ui.cursor = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
glfwSetCursor(s.window, ui.cursor);
btn1.text = "+1";
btn1.alignX(gui::ALIGN_CENTER_X);
btn1.alignY(gui::ALIGN_CENTER_Y);
btn1.width = 350;
btn1.height = 50;
btn1.callback = btn1Callback;
btn1.user = s.window;
text.setText(std::to_string(clicks));
text.size = 4;
text.shadow = true;
text.fancy = true;
text.alignX(gui::ALIGN_CENTER_X);
text.offsetY(150);
ui.addElement(&btn1);
ui.addElement(&text);
updateProjection({ width, height });
}
void StateTest::render(StateManager& s)
{
// render the state behind
glEnable(GL_DEPTH_TEST);
s.states[s.states.size() - 2]->render(s);
// get width and height of window
int w, h;
glfwGetWindowSize(s.window, &w, &h);
glDisable(GL_DEPTH_TEST); // disable depth (since you are rendering 2D)
// render a background using the QuadRenderer
qr.setColor(0, 0, 0, 0.6);
qr.setPos(0, 0, w, h);
qr.setQuadRendererMode(GL_TRIANGLES);
qr.render();
// render the ui
ui.render();
}
void StateTest::mouseInput(StateManager& s, double xPos, double yPos)
{
ui.mouseInput(xPos, yPos);
}
void StateTest::scrollInput(StateManager& s, double xOff, double yOff)
{
ui.scrollInput(xOff, yOff);
}
void StateTest::keyInput(StateManager& s, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) // close the state
s.popState();
else
ui.keyInput(key, scancode, action, mods);
}
void StateTest::charInput(StateManager& s, uint32_t codepoint)
{
ui.charInput(codepoint);
}
void StateTest::mouseButtonInput(StateManager& s, int btn, int action, int mods)
{
ui.mouseButtonInput(btn, action, mods);
}
void StateTest::close(StateManager& s)
{
ui.clear();
}
void StateTest::windowResize(StateManager& s, int w, int h)
{
updateProjection({ w, h });
}