Vivid
Loading...
Searching...
No Matches
Window.cpp
1#include "Window.h"
2#include "editor/camera/movable/EditorCamera.h"
3#include "editor/camera/movable/OrthoCamera.h"
4#include <iostream>
5#include "confs/Config.h"
6#include "inputs/InputHandler.h"
7
8#include "imgui/imgui/backends/imgui_impl_glfw.h"
9#include "imgui/imgui/backends/imgui_impl_opengl3.h"
10#include "imgui/imgui/imgui.h"
11#include "core/renderer/Renderer.h"
12#include "editor/gui/DockUI.h"
13#include "editor/Application.h"
14#include "core/ecs/ECS.h"
15#include "gui/SceneUI.h"
16
17#include "imguizmo/ImGuizmo.h"
18#include "common/maths/Vec.h"
19
20Window::Window(int width, int height, const char* title)
21{
22 m_Width = width;
23 m_Height = height;
24 m_Title = title;
25 m_PrevMousePosition = new Vivid::Maths::Vec2(0.0f, 0.0f);
26
27 if (!glfwInit())
28 {
29 std::cout << "GLFW failed to initialize!" << std::endl;
30 }
31
32 // FOR MACOS
33 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
34 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
35 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
36 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
37#ifdef __APPLE__
38 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
39#endif
40 m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
41 glfwSetInputMode(m_Window, GLFW_STICKY_KEYS, 1);
42 glfwSwapInterval(0);
43 if (!m_Window)
44 {
45 glfwTerminate();
46 return;
47 }
48
49 glfwMakeContextCurrent(m_Window);
50
51 IMGUI_CONFS
52
53 IMGUI_CHECKVERSION();
54 std::cout << glsl_version << "\n";
55 ImGui::CreateContext();
56 ImGui_ImplGlfw_InitForOpenGL(m_Window, true);
57 ImGui_ImplOpenGL3_Init(glsl_version);
58 ImGui::StyleColorsDark();
59 ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
60
61 VividGUI::ImGuiThemeSetup();
62}
63
64Window* Window::Init(int width, int height, const char* title)
65{
66 if (s_Instance == NULL)
67 s_Instance = new Window(width, height, title);
68
69 return s_Instance;
70}
71
72void Window::Clear() const
73{
74 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75}
76
78{
79 m_RenderingInterface = renderingInterface;
80 m_RenderingInterface->Setup();
81
82 glfwGetWindowSize(m_Window, &m_Width, &m_Height);
83 m_FrameBuffer = new FrameBuffer(m_Width, m_Height);
84}
85
86void Window::Update()
87{
88 glfwGetWindowSize(m_Window, &m_Width, &m_Height);
89 m_FrameBuffer->RescaleFrameBuffer(m_Width, m_Height);
90
91 // Handle keyboard input
92 glfwPollEvents();
93
94 glfwMakeContextCurrent(m_Window);
95 glfwSwapBuffers(m_Window);
96
97 glClear(GL_DEPTH_BUFFER_BIT);
98 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
99
100 Vivid::Renderer::Clear();
101
102 ImGui_ImplGlfw_NewFrame();
103 ImGui_ImplOpenGL3_NewFrame();
104 ImGui::NewFrame();
105
106 VividGUI::InitUI();
107
108 ImGuizmo::BeginFrame();
109
110 if (ImGui::Begin("Viewport"))
111 {
112 // Handle Custom Inputs
113 Camera* camera = Application::GetInstance()->GetCamera();
114 if (ImGui::IsWindowFocused())
115 {
116
117 if (m_RenderingInterface != nullptr)
118 {
119 // If editor camera allow to move.
120 if (typeid(*camera) == typeid(EditorCamera) || typeid(*camera) == typeid(OrthoCamera))
121 {
122 // TODO: Put this in a function
123 MovableCamera* movableCamera = static_cast<MovableCamera*>(camera);
124 if (InputHandler::IsKeyPressed(GLFW_KEY_W))
125 {
126 movableCamera->MoveForward();
127 }
128 if (InputHandler::IsKeyPressed(GLFW_KEY_S))
129 {
130 movableCamera->MoveBackward();
131 }
132 if (InputHandler::IsKeyPressed(GLFW_KEY_A))
133 {
134 movableCamera->MoveLeft();
135 }
136 if (InputHandler::IsKeyPressed(GLFW_KEY_D))
137 {
138 movableCamera->MoveRight();
139 }
140
142 if (InputHandler::IsMouseButtonPressed(GLFW_MOUSE_BUTTON_RIGHT))
143 {
144 movableCamera->ProcessMouseMovement(mousePosition.x - m_PrevMousePosition->x,
145 mousePosition.y - m_PrevMousePosition->y);
146 m_PrevMousePosition->x = mousePosition.x;
147 m_PrevMousePosition->y = mousePosition.y;
148 }
149 else
150 {
151 m_PrevMousePosition->x = mousePosition.x;
152 m_PrevMousePosition->y = mousePosition.y;
153 }
154 }
155 }
156 }
157
158 // Draw to a temporary framebuffer
159 // Some components might be drawing within imgui context
160 m_FrameBuffer->Bind();
161 if (m_RenderingInterface != nullptr)
162 {
163 m_RenderingInterface->Draw();
164 }
165
166 Vivid::ECS::Draw(camera);
167 m_FrameBuffer->Unbind();
168
169 m_ViewportWidth = ImGui::GetContentRegionAvail().x;
170 m_ViewportHeight = ImGui::GetContentRegionAvail().y;
171 // Get the starting position of the viewport
172 m_ViewportPosition = Vivid::Maths::Vec2(ImGui::GetCursorScreenPos().x, ImGui::GetCursorScreenPos().y);
173
174 Application::GetInstance()->GetCamera()->SetViewportSize(m_ViewportWidth, m_ViewportHeight);
175
176 ImGui::Image(
177 (ImTextureID)m_FrameBuffer->getFrameTexture(),
178 ImVec2(m_ViewportWidth, m_ViewportHeight),
179 { 0, 1 }, { 1, 0 });
180
181 if (typeid(*camera) == typeid(EditorCamera))
182 {
183 VividGUI::SceneUI::DrawGizmo(camera);
184 }
185 ImGui::End();
186 }
187
188 // Custom ImGui Rendering
189 if (m_RenderingInterface != nullptr)
190 {
191 m_RenderingInterface->ImGuiRender();
192 }
193
194 // Scene "Tree"
195 VividGUI::SceneUI::DrawSceneUI();
196
197 // End Docking
198 VividGUI::EndUI();
199
200 ImGui::Render();
201 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
202
203 m_FrameBuffer->Bind();
204 Vivid::Renderer::Clear();
205
206 GLCall(glClear(GL_DEPTH_BUFFER_BIT));
207 GLCall(glClearColor(1.0f, 1.0f, 1.0f, 1.0f));
208 m_FrameBuffer->Unbind();
209}
A class that represents the camera.
Definition: Camera.h:25
A class for the EditorCamera.
Definition: EditorCamera.h:14
Handles the creation and management of a frame buffer.
Definition: FrameBuffer.h:11
A class for the MovableCamera's.
Definition: MovableCamera.h:15
A class for the OrthoCamera's.
Definition: OrthoCamera.h:12
RenderingInterface class.
virtual void Draw()=0
Draw function.
virtual void ImGuiRender()=0
ImGuiRender function.
virtual void Setup()=0
Setup function.
A class that represents a window and is wrapper of GLFWwindow.
Definition: Window.h:15
void SetRenderingInterface(RenderingInterface *renderingInterface)
Sets the rendering interface.
Definition: Window.cpp:77
static Window * Init(int width, int height, const char *title)
Initializes the window.
Definition: Window.cpp:64
bool IsMouseButtonPressed(int button)
Checks if a mouse button is pressed or not.
bool IsKeyPressed(int key)
Checks if a key is pressed or not.
Definition: InputHandler.cpp:5
Vivid::Maths::Vec2 GetMousePosition()
Gets the mouse position.
Contains a 2D vector.
Definition: Vec.h:108