Skip to content

Feat: Misc Update #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Editor/Include/Editor/EditorModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Runtime/Engine.h>

namespace Editor {
class EditorModule final : public Runtime::IEngineModule {
class EditorModule final : public Runtime::EngineModule {
public:
void OnUnload() override;
::Core::ModuleType Type() const override;
Expand Down
9 changes: 5 additions & 4 deletions Editor/Qml/EWidgetSamples.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQuick.Layouts

Rectangle {
Expand Down Expand Up @@ -146,7 +147,7 @@ Rectangle {

RowLayout {
Layout.leftMargin: 5
Layout.topMargin: 15
Layout.topMargin: 35

EText {
text: 'Texts'
Expand Down Expand Up @@ -195,7 +196,7 @@ Rectangle {

RowLayout {
Layout.leftMargin: 5
Layout.topMargin: 15
Layout.topMargin: 35

EText {
text: 'Icons'
Expand Down Expand Up @@ -275,7 +276,7 @@ Rectangle {

RowLayout {
Layout.leftMargin: 5
Layout.topMargin: 15
Layout.topMargin: 35
EText {
text: 'Switches'
style: EText.Style.Title1
Expand Down Expand Up @@ -316,7 +317,7 @@ Rectangle {

RowLayout {
Layout.leftMargin: 5
Layout.topMargin: 15
Layout.topMargin: 35
EText {
text: 'TextInput'
style: EText.Style.Title1
Expand Down
3 changes: 2 additions & 1 deletion Editor/Src/Widget/GraphicsSampleWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ namespace Editor {
{
static std::vector<RHI::PixelFormat> formatQualifiers = {
RHI::PixelFormat::rgba8Unorm,
RHI::PixelFormat::bgra8Unorm};
RHI::PixelFormat::bgra8Unorm
};

if (swapChain != nullptr) {
WaitDeviceIdle();
Expand Down
29 changes: 29 additions & 0 deletions Engine/Source/Launch/Include/Launch/GameApplication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Created by johnk on 2025/3/24.
//

#pragma once

#include <Common/Memory.h>
#include <Launch/GameViewport.h>
#include <Runtime/Engine.h>
#include <Runtime/GameModule.h>

namespace Launch {
class GameApplication {
public:
GameApplication(int argc, char* argv[]);
~GameApplication();

void Tick();
bool ShouldClose() const;

private:
double lastFrameTimeSeconds;
double thisFrameTimeSeconds;
float deltaTimeSeconds;
Common::UniquePtr<GameViewport> viewport;
Runtime::Engine* engine;
Runtime::GameModule* gameModule;
};
}
2 changes: 1 addition & 1 deletion Engine/Source/Launch/Include/Launch/GameClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Launch {
class GameViewport;

class GameClient : public Runtime::Client {
class GameClient final : public Runtime::Client {
public:
explicit GameClient(GameViewport& inViewport);
~GameClient() override;
Expand Down
40 changes: 34 additions & 6 deletions Engine/Source/Launch/Include/Launch/GameViewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,50 @@

#pragma once

#include <GLFW/glfw3.h>
#if PLATFORM_WINDOWS
#define GLFW_EXPOSE_NATIVE_WIN32
#elif PLATFORM_MACOS
#define GLFW_EXPOSE_NATIVE_COCOA
#endif
#include <GLFW/glfw3native.h>

#include <RHI/RHI.h>
#include <Runtime/Viewport.h>
#include <Launch/GameClient.h>
#include <Render/RenderModule.h>

namespace Launch {
struct GameViewportDesc {
std::string title;
uint32_t width;
uint32_t height;
};

class GameViewport final : public Runtime::Viewport {
public:
GameViewport();
explicit GameViewport(const GameViewportDesc& inDesc);
~GameViewport() override;

Runtime::Client& GetClient() override;
Runtime::PresentInfo GetNextPresentInfo() override;
size_t GetWidth() const override;
size_t GetHeight() const override;
uint32_t GetWidth() const override;
uint32_t GetHeight() const override;
void Resize(uint32_t inWidth, uint32_t inHeight) override;

bool ShouldClose() const;
void PollEvents() const;
void RecreateSwapChain(uint32_t inWidth, uint32_t inHeight);
void WaitDeviceIdle() const;

private:
// TODO glfw window
// TODO rhi swap chain
// TODO game client
Render::RenderModule& renderModule;
GameClient client;
GLFWwindow* window;
RHI::Device* device;
Common::UniquePtr<RHI::Semaphore> imageReadySemaphore;
Common::UniquePtr<RHI::Semaphore> renderFinishedSemaphore;
Common::UniquePtr<RHI::Surface> surface;
Common::UniquePtr<RHI::SwapChain> swapChain;
};
}
69 changes: 69 additions & 0 deletions Engine/Source/Launch/Src/GameApplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Created by johnk on 2025/3/24.
//

#include <Common/Time.h>
#include <Launch/GameApplication.h>
#include <Core/Cmdline.h>
#include <RHI/Instance.h>
#include <Runtime/Engine.h>
#include <Runtime/Settings/Registry.h>
#include <Runtime/Settings/Game.h>

namespace Launch {
extern std::string gameModuleName;

static Core::CmdlineArgValue<std::string> caRhiType(
"rhiType", "-rhi", RHI::GetPlatformDefaultRHIAbbrString(),
"rhi abbr string, can be 'dx12' or 'vulkan'");

GameApplication::GameApplication(int argc, char* argv[])
: lastFrameTimeSeconds(Common::TimePoint::Now().ToSeconds())
, thisFrameTimeSeconds(Common::TimePoint::Now().ToSeconds())
, deltaTimeSeconds(0)
{
Core::Cli::Get().Parse(argc, argv);

Runtime::EngineInitParams engineInitParams;
engineInitParams.logToFile = true;
engineInitParams.rhiType = caRhiType.GetValue();
Runtime::EngineHolder::Load(gameModuleName, engineInitParams);

engine = &Runtime::EngineHolder::Get();
gameModule = &Core::ModuleManager::Get().GetTyped<Runtime::GameModule>(gameModuleName);

GameViewportDesc viewportDesc;
viewportDesc.title = gameModule->GetGameName();
// TODO load from config
viewportDesc.width = 1024;
viewportDesc.height = 768;
viewport = Common::MakeUnique<GameViewport>(viewportDesc);

auto& settingRegistry = Runtime::SettingsRegistry::Get();
settingRegistry.LoadAllSettings();

const auto& gameSettings = Runtime::SettingsRegistry::Get().GetSettings<Runtime::GameSettings>();
const auto startupLevel = Runtime::AssetManager::Get().SyncLoad<Runtime::Level>(gameSettings.gameStartupLevel, Runtime::Level::GetStaticClass());
viewport->GetClient().GetWorld().LoadFrom(startupLevel);
}

GameApplication::~GameApplication()
{
Runtime::EngineHolder::Unload();
}

void GameApplication::Tick()
{
thisFrameTimeSeconds = Common::TimePoint::Now().ToSeconds();
deltaTimeSeconds = thisFrameTimeSeconds - lastFrameTimeSeconds;
lastFrameTimeSeconds = thisFrameTimeSeconds;

engine->Tick(deltaTimeSeconds);
viewport->PollEvents();
}

bool GameApplication::ShouldClose() const
{
return viewport->ShouldClose();
}
}
116 changes: 103 additions & 13 deletions Engine/Source/Launch/Src/GameViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,129 @@
//

#include <Launch/GameViewport.h>
#include <Runtime/Engine.h>

namespace Launch::Internal {
static void* GetGlfwPlatformWindow(GLFWwindow* inWindow)
{
#if PLATFORM_WINDOWS
return glfwGetWin32Window(inWindow);
#elif PLATFORM_MACOS
return glfwGetCocoaView(inWindow);
#else
Unimplement();
return nullptr;
#endif
}
}

namespace Launch {
GameViewport::GameViewport()
GameViewport::GameViewport(const GameViewportDesc& inDesc)
: renderModule(Runtime::EngineHolder::Get().GetRenderModule())
, client(*this)
{
// TODO
glfwInit();
window = glfwCreateWindow(static_cast<int>(inDesc.width), static_cast<int>(inDesc.height), inDesc.title.c_str(), nullptr, nullptr);

device = renderModule.GetDevice();
imageReadySemaphore = device->CreateSemaphore();
renderFinishedSemaphore = device->CreateSemaphore();

surface = device->CreateSurface(RHI::SurfaceCreateInfo(Internal::GetGlfwPlatformWindow(window)));
RecreateSwapChain(inDesc.width, inDesc.height);
}

GameViewport::~GameViewport()
{
// TODO
WaitDeviceIdle();
glfwDestroyWindow(window);
glfwTerminate();
}

Runtime::Client& GameViewport::GetClient()
{
// TODO
return *static_cast<Runtime::Client*>(nullptr);
return client;
}

Runtime::PresentInfo GameViewport::GetNextPresentInfo()
{
// TODO
return Runtime::PresentInfo();
const auto backTextureIndex = swapChain->AcquireBackTexture(imageReadySemaphore.Get());

Runtime::PresentInfo result;
result.backTexture = swapChain->GetTexture(backTextureIndex);
result.imageReadySemaphore = imageReadySemaphore.Get();
result.renderFinishedSemaphore = renderFinishedSemaphore.Get();
return result;
}

uint32_t GameViewport::GetWidth() const
{
int width;
glfwGetWindowSize(window, &width, nullptr);
return static_cast<uint32_t>(width);
}

uint32_t GameViewport::GetHeight() const
{
int height;
glfwGetWindowSize(window, nullptr, &height);
return static_cast<uint32_t>(height);
}

size_t GameViewport::GetWidth() const
void GameViewport::Resize(uint32_t inWidth, uint32_t inHeight)
{
// TODO
return 0;
glfwSetWindowSize(window, static_cast<int>(inWidth), static_cast<int>(inHeight));
RecreateSwapChain(inWidth, inHeight);
}

size_t GameViewport::GetHeight() const
bool GameViewport::ShouldClose() const
{
// TODO
return 0;
return static_cast<bool>(glfwWindowShouldClose(window));
}

void GameViewport::PollEvents() const // NOLINT
{
glfwPollEvents();
}

void GameViewport::RecreateSwapChain(uint32_t inWidth, uint32_t inHeight)
{
static std::vector<RHI::PixelFormat> formatQualifiers = {
RHI::PixelFormat::rgba8Unorm,
RHI::PixelFormat::bgra8Unorm
};

WaitDeviceIdle();
if (swapChain.Valid()) {
swapChain.Reset();
}

std::optional<RHI::PixelFormat> pixelFormat = {};
for (const auto format : formatQualifiers) {
if (device->CheckSwapChainFormatSupport(surface.Get(), format)) {
pixelFormat = format;
break;
}
}
Assert(pixelFormat.has_value());

swapChain = device->CreateSwapChain(
RHI::SwapChainCreateInfo()
.SetPresentQueue(device->GetQueue(RHI::QueueType::graphics, 0))
.SetSurface(surface.Get())
.SetTextureNum(2)
.SetFormat(pixelFormat.value())
.SetWidth(inWidth)
.SetHeight(inHeight)
.SetPresentMode(RHI::PresentMode::immediately));
}

void GameViewport::WaitDeviceIdle() const
{
renderModule.GetRenderThread().Flush();

const Common::UniquePtr<RHI::Fence> fence = device->CreateFence(false);
device->GetQueue(RHI::QueueType::graphics, 0)->Flush(fence.Get());
fence->Wait();
}
}
12 changes: 6 additions & 6 deletions Engine/Source/Launch/Src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Created by johnk on 2025/2/19.
//

#include <Launch/GameApplication.h>

int main(int argc, char* argv[])
{
// TODO parse command line arguments
// TODO load runtime module
// TODO load engine
// TODO create game viewport
// TODO load startup level to world
// TODO main loop tick
Launch::GameApplication app(argc, argv);
while (!app.ShouldClose()) {
app.Tick();
}
return 0;
}
Loading