Files
LEngine.Editor/Src/GameEditor.cpp
T
2025-05-13 02:41:57 +03:00

234 lines
13 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <Windows.h>
#include <LEngine/LEngine.h>
#include <LEngine/Mesh.h>
#include <LEngine/GameObject.h>
#include <LEngine/SceneRenderer.h>
#include <LEngine/AssetManager.h>
#include <LEngine/ObjMeshLoader.h>
#include "FreeCameraController.h"
#include <lframework/Input/Input.h>
#include <chrono>
#include "BoundsView.h"
#include <LEngine/AssetDatabase.h>
#include <LEngine/AssetMetadata.h>
#include <LEngine/OutputArchive.h>
#include <LEngine/InputArchive.h>
#include <LEngine/Asset.h>
#include <LEngine/AssetFile.h>
#include <sstream>
#include <LEngine/Graphics/VulkanApplication.h>
#include <LEngine/Graphics/VulkanDynamicLibrary.h>
#include <LEngine/Graphics/RenderWindow.h>
#include <LEngine/WindowRenderOutput.h>
#include <LEngine/Graphics/ShaderCompiler.h>
#include <LMath/lmath.h>
GameObjectPtr CreateCube() {
auto cubeObject = GameObject::create();
cubeObject->AddComponent<MeshRenderer>()->SetMesh(Mesh::Create([](MeshPtr mesh) {MeshGenerator::Cube(mesh, true); }));
return cubeObject;
}
struct TestObject : public Object {
uint8_t uint8_t_;
uint16_t uint16_t_;
uint32_t uint32_t_;
uint64_t uint64_t_;
std::string string_;
LString wstring_;
std::shared_ptr<TestObject> next;
virtual void serialize(Archive& archive) override {
archive << uint8_t_ << uint16_t_ << uint32_t_;
archive << uint64_t_ << string_ << wstring_;
auto ptr = std::static_pointer_cast<Object>(next);
archive << ptr;
}
};
void SaveScene(const LString& path) {
AssetDatabase::get().open_asset_file(path);
AssetFile asset(LFramework::Guid(1, 2, 3, 4), path);
//find all game objects
auto scene_objects = Object::FindObjectsOfType<GameObject>();
//add all game objects and components to scene asset
for (auto obj : scene_objects) {
asset.AddObject(obj);
auto components = obj->GetComponents<Component>();
for (auto component : components) {
asset.AddObject(component);
}
}
asset.Save();
}
void CreateTestScene() {
using namespace lm;
auto cameraGo = GameObject::create();
cameraGo->SetName(LTEXT("Main camera"));
cameraGo->AddComponent<Camera>()->fov = 45.0f;
cameraGo->transform->SetLocalPosition(float3(0.0f, 4.0f, -4.0f));
cameraGo->AddComponent<FreeCameraController>();
}
void LoadScene(const LString& path) {
auto archive = AssetDatabase::get().open_asset_file(path);
}
//int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
int main(int argc, char** argv) {
using namespace lm;
const char* currentFilePathRaw = __FILE__;
std::filesystem::path currentFilePath(currentFilePathRaw);
auto assetsDirPath = currentFilePath.parent_path().parent_path() / "Assets";
float angleY = 3.14159265358979f / 2;
auto q = Quaternion_f::angleAxis(angleY, float3(0.0f, 1.0f, 0.0f));
auto mm = lm::matrix4x4RotationQuaternion(q);
auto mm2 = lm::matrix4x4RotationY<float>(angleY);
float4 v0{ 1.0f, 0.0f, 0.0f, 1.0f};
auto v1 = lm::mul(mm2, v0);
auto right = mm.get(0);
auto up = mm.y();
auto forward = mm.z();
auto right2= mm.x();
auto up2 = mm.y();
auto forward2 = mm.z();
//matrix4x4RotationQuaternion(_rotation);
//auto positionMatrix = matrix4x4Translation(_position);
VulkanApplication app(RenderWindow::getRequiredExtensionNames(), "LEngine::GameEditor", 1);
auto window = std::make_shared<RenderWindow>(app.instance(), LTEXT("LEngine::GameEditor::😀RenderWindow"));
AssetDatabase::get().set_root_path(assetsDirPath.string<LChar>());
auto engine = LEngine::Instance();
engine->Init(assetsDirPath.string<LChar>(), app.instance(), window);
engine->LoadScene("");
auto renderer = engine->GetRenderer();
auto ints = Input::Instance();
auto tex = AssetManager::Instance()->LoadTexture(LTEXT("textures/1_store.dds"));
auto sceneptr = LEngine::Instance()->GetActiveScene();
//auto editorWindow = UiSwapChainWindow::Create(LTEXT("LEngineEditorWindow"));
ObjMeshLoader loader;
auto objects = loader.Load(AssetManager::Instance()->GetFullAssetPath(LTEXT("Boxes.obj")));
auto obj = objects[0];
auto mr = obj->GetComponent<MeshRenderer>();
auto m = mr->GetMesh();
std::vector<Vertex> v;
v.resize(3);
v[0].position = float4(1.0f, 0.0f, 0.0f, 1.0f);
v[0].normal = float3(1.0f, 0.0f, 0.0f);
v[1].position = float4(0.0f, 0.0f, 0.0f, 1.0f);
v[2].position = float4(0.0f, 0.0f, 1.0f, 1.0f);
v[2].normal = float3(0.0f, 0.0f, 1.0f);
m->SetVertices(v);
LString scene = LTEXT("TestScene.asset");
CreateTestScene();
//SaveScene(scene);
//LoadScene(scene);
//editorWindow->size = UiSize(640, 480);
// auto output = std::make_shared<SwapChainOutput>(engine->GetRenderer(), editorWindow->GetWindow()->GetHwnd(), 640, 480, false);
//engine->AddOutput(output);
auto renderWindowOutput = std::make_shared<WindowRenderOutput>(window);
auto sceneRenderer = std::make_shared<SceneRenderer>(renderer, renderWindowOutput);
//ob[0]->AddComponent<BoundsView>();
//auto mat = Material::Create(Shader::Create(LTEXT("shaders/shader.fx")), "");
//mat->GetParametersBlock().SetTexture("diffuse", tex);
//for (auto o : ob)
//{
// o->GetComponent<MeshRenderer>()->SetMaterial(mat, 0);
//}
/*AssetMetadata meta(L"D:\\lengine\\Assets\\Boxes.obj");
meta.Save();
auto& factory = ObjectsFactory::instance();
*/
auto cameras = Object::FindObjectsOfType<Camera>();
auto cameraGo = cameras[0]->gameObject;
cameraGo->transform->SetLocalPosition(float3(0.0f, 0.0f, -4.0f));
typedef std::chrono::high_resolution_clock clock;
auto last_time = clock::now();
float delta_time = 0.0f;
float totalTime = 0;
while (window->update()) {
auto now_time = clock::now();
delta_time = (float)((double)std::chrono::duration_cast<std::chrono::microseconds>(now_time - last_time).count() / 1000000.0);
last_time = now_time;
totalTime += delta_time;
if (totalTime >= 30.0f)
{
//sceneRenderer->createPSO = false;
}
//std::chrono::high_resolution_clock::now();
LEngine::Instance()->GetActiveScene()->tick(delta_time);
sceneRenderer->RenderScene(engine->GetActiveScene(), cameraGo->GetComponent<Camera>());
// sceneRenderer->RenderScene(engine->GetRenderer(),, output);
//editorWindow->Update(1);
Input::Instance()->strobe();
//output->Present();а
}
return 0;
}