-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
160 lines (144 loc) · 4.4 KB
/
main.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
152
153
154
155
156
157
158
159
160
#include <tiny-piyo/IEngine.h>
#include <tiny-piyo/IScreen.h>
#include <tiny-piyo/Camera.h>
#include <tiny-piyo/Input.h>
#include <tiny-piyo/Shaders.h>
#include <tiny-piyo/Mesh.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
class MainScreen : public IScreen
{
protected:
virtual void OnInit() override;
virtual void OnDestroy() override;
virtual void OnEntry() override;
virtual void OnExit() override;
virtual void OnUpdate() override;
virtual void OnDraw() override;
private:
BasicGeoShader _geoShader;
Camera _camera;
float2 _prevCursorPos;
Mesh _cylinder;
};
void MainScreen::OnInit()
{
std::printf("Screen OnInit()\n");
this->_geoShader.Init();
this->_camera.Init(640, 480, 90.0f, 0.01f, 1000.0f);
this->GetInputManager()->LockMouse();
this->_cylinder = ParseOBJ("res/obj/cylinder.obj");
}
void MainScreen::OnDestroy()
{
std::printf("Screen OnDestroy()\n");
this->_geoShader.Destroy();
}
void MainScreen::OnEntry() { std::printf("Screen OnEntry()\n"); }
void MainScreen::OnExit() { std::printf("Screen OnExit()\n"); }
void MainScreen::OnUpdate()
{
float2 cursorPos = this->GetInputManager()->GetCursorPos();
this->_camera.Rotate(((cursorPos - this->_prevCursorPos) / 200.0f).x, -((cursorPos - this->_prevCursorPos) / 200.0f).y, 0.0f);
this->_prevCursorPos = cursorPos;
if (this->GetInputManager()->IsKeyPressed(GLFW_KEY_R))
this->_camera.SetRotation(0.0f, 0.0f, 0.0f);
this->_camera.SetRotation(
this->_camera.GetRotation().x,
std::fmin(std::fmax(this->_camera.GetRotation().y, -PI / 2.0f), PI / 2.0f),
this->_camera.GetRotation().z
);
float delta = this->engine->GetDelta() / 7;
if (this->GetInputManager()->IsKeyDown(GLFW_KEY_W))
this->_camera.Translate(0.0f, 0.0f, delta);
if (this->GetInputManager()->IsKeyDown(GLFW_KEY_A))
this->_camera.Translate(-delta, 0.0f, 0.0f);
if (this->GetInputManager()->IsKeyDown(GLFW_KEY_S))
this->_camera.Translate(0.0f, 0.0f, -delta);
if (this->GetInputManager()->IsKeyDown(GLFW_KEY_D))
this->_camera.Translate(delta, 0.0f, 0.0f);
if (this->GetInputManager()->IsKeyDown(GLFW_KEY_Q))
this->_camera.Translate(0.0f, delta, 0.0f);
if (this->GetInputManager()->IsKeyDown(GLFW_KEY_E))
this->_camera.Translate(0.0f, -delta, 0.0f);
}
void MainScreen::OnDraw()
{
this->_camera.Update(this->_geoShader);
//this->_geoShader.DrawMesh(this->_cylinder);
this->_geoShader.DrawTri(
Vertex3D(
float3(-0.5f, -0.5f, 0.0f),
ColorRGBA8(0, 255, 0, 255)
),
Vertex3D(
float3( 0.5f, 0.5f, 0.0f),
ColorRGBA8(0, 0, 255, 255)
),
Vertex3D(
float3( 0.5f, -0.5f, 0.0f),
ColorRGBA8(255, 0, 0, 255)
)
);
this->_geoShader.DrawTri(
Vertex3D(
float3(-0.5f, -0.5f, 0.0f),
ColorRGBA8(0, 255, 0, 120)
),
Vertex3D(
float3(-0.5f, 0.5f, 0.0f),
ColorRGBA8(255, 0, 0, 120)
),
Vertex3D(
float3( 0.5f, 0.5f, 0.0f),
ColorRGBA8(0, 0, 255, 120)
)
);
/*this->_geoShader.DrawQuad(
Vertex3D(
float3(-1.0f, 1.0f, 5.0f),
ColorRGBA8(0, 255, 0, 255)
),
Vertex3D(
float3( 1.0f, 1.0f, 5.0f),
ColorRGBA8(0, 255, 0, 255)
),
Vertex3D(
float3( 1.0f, -1.0f, 5.0f),
ColorRGBA8(0, 255, 0, 255)
),
Vertex3D(
float3(-1.0f, -1.0f, 5.0f),
ColorRGBA8(0, 255, 0, 255)
)
);*/
this->_geoShader.End();
this->_geoShader.Render();
}
class Engine : public IEngine
{
virtual void OnPreInit() override;
virtual void OnPostInit() override;
virtual void OnPreDestroy() override;
virtual void OnPostDestroy() override;
};
void Engine::OnPreInit()
{
std::printf("Engine OnPreInit()\n");
this->name = "i3-floating";
}
void Engine::OnPostInit() {
std::printf("Engine OnPostInit()\n");
this->AddScreen("main", new MainScreen);
this->ChangeScreen("main");
}
void Engine::OnPreDestroy() { std::printf("Engine OnPreDestroy()\n"); }
void Engine::OnPostDestroy() { std::printf("Engine OnPostDestroy()\n"); }
int main()
{
Engine engine;
engine.Run();
system("PAUSE");
return 0;
}