Files
Seele/src/Engine/Window/GameView.cpp
T

149 lines
4.3 KiB
C++
Raw Normal View History

2023-02-24 22:09:07 +01:00
#include "GameView.h"
#include "Graphics/Graphics.h"
#include "Window/Window.h"
#include "Component/KeyboardInput.h"
#include "Actor/CameraActor.h"
#include "Asset/AssetRegistry.h"
2023-11-08 23:27:21 +01:00
#include "System/LightGather.h"
#include "System/MeshUpdater.h"
2023-11-10 19:18:09 +01:00
#include "System/CameraUpdater.h"
2023-02-24 22:09:07 +01:00
using namespace Seele;
GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreateInfo &createInfo, std::string dllPath)
: View(graphics, window, createInfo, "Game")
2023-11-06 14:47:21 +01:00
, scene(new Scene(graphics))
2023-02-24 22:09:07 +01:00
, gameInterface(dllPath)
, renderGraph(RenderGraphBuilder::build(
2023-11-10 22:26:47 +01:00
DepthPrepass(graphics, scene),
LightCullingPass(graphics, scene),
BasePass(graphics, scene),
SkyboxRenderPass(graphics, scene)
2023-02-24 22:09:07 +01:00
))
{
2023-11-08 23:27:21 +01:00
camera = new CameraActor(scene);
2023-04-09 16:39:53 +02:00
reloadGame();
2023-02-24 22:09:07 +01:00
renderGraph.updateViewport(viewport);
}
GameView::~GameView()
{
}
void GameView::beginUpdate()
{
}
void GameView::update()
{
static auto startTime = std::chrono::high_resolution_clock::now();
2023-10-24 15:01:09 +02:00
for (VertexData* vd : VertexData::getList())
{
vd->resetMeshData();
}
2023-02-24 22:09:07 +01:00
systemGraph->run(threadPool, updateTime);
scene->update(updateTime);
2023-10-24 15:01:09 +02:00
for (VertexData* vd : VertexData::getList())
{
vd->createDescriptors();
}
2023-11-05 10:36:01 +01:00
scene->getLightEnvironment()->commit();
2023-02-24 22:09:07 +01:00
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> duration = (endTime - startTime);
updateTime = duration.count();
startTime = endTime;
}
void GameView::commitUpdate()
{
}
void GameView::prepareRender()
{
}
void GameView::render()
{
2023-10-24 15:01:09 +02:00
renderGraph.render(camera->accessComponent<Component::Camera>());
2023-02-24 22:09:07 +01:00
}
2023-04-09 16:39:53 +02:00
void GameView::reloadGame()
{
2023-08-26 15:19:12 +02:00
gameInterface.reload(AssetRegistry::getInstance());
2023-04-09 16:39:53 +02:00
systemGraph = new SystemGraph();
gameInterface.getGame()->setupScene(scene, systemGraph);
2023-11-08 23:27:21 +01:00
systemGraph->addSystem(new System::LightGather(scene));
systemGraph->addSystem(new System::MeshUpdater(scene));
2023-11-10 19:18:09 +01:00
systemGraph->addSystem(new System::CameraUpdater(scene));
2023-04-09 16:39:53 +02:00
}
2023-02-24 22:09:07 +01:00
void GameView::keyCallback(KeyCode code, InputAction action, KeyModifier)
{
2023-11-06 14:47:21 +01:00
scene->view<Component::KeyboardInput>([=, this](Component::KeyboardInput& input)
2023-02-24 22:09:07 +01:00
{
//if(code == KeyCode::KEY_R && action == InputAction::PRESS)
//{
// auto cubeMesh = AssetRegistry::findMesh("cube");
// PEntity cube2 = new Entity(scene);
// Component::Transform& cube2Transform = cube2->attachComponent<Component::Transform>();
// cube2Transform.setPosition(Vector(0, 20, 0));
// cube2Transform.setRotation(Quaternion(Vector(0.f, 90.f, 90.f)));
// cube2Transform.setScale(Vector(2.f, 2.f, 2.f));
// cubeMesh->physicsMesh.type = Component::ColliderType::DYNAMIC;
// cube2->attachComponent<Component::Collider>(cubeMesh->physicsMesh);
// cube2->attachComponent<Component::StaticMesh>(cubeMesh);
// Component::RigidBody& physics2 = cube2->attachComponent<Component::RigidBody>();
// physics2.linearMomentum = Vector(0, -10, 0);
// physics2.angularMomentum = Vector(0, 0, 0);
//}
//
//if(code == KeyCode::KEY_B && action == InputAction::PRESS)
//{
// showDebug = !showDebug;
// debugPassData.vertices.clear();
//}
2023-04-09 16:39:53 +02:00
if(code == KeyCode::KEY_R && action == InputAction::PRESS)
{
reloadGame();
}
2023-02-24 22:09:07 +01:00
input.keys[code] = action != InputAction::RELEASE;
});
}
void GameView::mouseMoveCallback(double xPos, double yPos)
{
scene->view<Component::KeyboardInput>([=](Component::KeyboardInput& input)
{
input.mouseX = static_cast<float>(xPos);
input.mouseY = static_cast<float>(yPos);
});
}
void GameView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier)
{
scene->view<Component::KeyboardInput>([=](Component::KeyboardInput& input)
{
if (button == MouseButton::MOUSE_BUTTON_1)
{
input.mouse1 = action != InputAction::RELEASE;
}
if (button == MouseButton::MOUSE_BUTTON_2)
{
input.mouse2 = action != InputAction::RELEASE;
}
});
}
void GameView::scrollCallback(double, double)
{
}
void GameView::fileCallback(int, const char**)
{
}