Skip to content

Commit d826271

Browse files
Merge pull request #391 from FlyAndNotDown/master
Feat: Misc Update
2 parents 576e70c + d150d81 commit d826271

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1754
-213
lines changed

Editor/Include/Editor/EditorModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <Runtime/Engine.h>
88

99
namespace Editor {
10-
class EditorModule final : public Runtime::IEngineModule {
10+
class EditorModule final : public Runtime::EngineModule {
1111
public:
1212
void OnUnload() override;
1313
::Core::ModuleType Type() const override;

Editor/Qml/EWidgetSamples.qml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import QtQuick
22
import QtQuick.Controls
3+
import QtQuick.Controls.Basic
34
import QtQuick.Layouts
45

56
Rectangle {
@@ -146,7 +147,7 @@ Rectangle {
146147

147148
RowLayout {
148149
Layout.leftMargin: 5
149-
Layout.topMargin: 15
150+
Layout.topMargin: 35
150151

151152
EText {
152153
text: 'Texts'
@@ -195,7 +196,7 @@ Rectangle {
195196

196197
RowLayout {
197198
Layout.leftMargin: 5
198-
Layout.topMargin: 15
199+
Layout.topMargin: 35
199200

200201
EText {
201202
text: 'Icons'
@@ -275,7 +276,7 @@ Rectangle {
275276

276277
RowLayout {
277278
Layout.leftMargin: 5
278-
Layout.topMargin: 15
279+
Layout.topMargin: 35
279280
EText {
280281
text: 'Switches'
281282
style: EText.Style.Title1
@@ -316,7 +317,7 @@ Rectangle {
316317

317318
RowLayout {
318319
Layout.leftMargin: 5
319-
Layout.topMargin: 15
320+
Layout.topMargin: 35
320321
EText {
321322
text: 'TextInput'
322323
style: EText.Style.Title1

Editor/Src/Widget/GraphicsSampleWidget.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ namespace Editor {
143143
{
144144
static std::vector<RHI::PixelFormat> formatQualifiers = {
145145
RHI::PixelFormat::rgba8Unorm,
146-
RHI::PixelFormat::bgra8Unorm};
146+
RHI::PixelFormat::bgra8Unorm
147+
};
147148

148149
if (swapChain != nullptr) {
149150
WaitDeviceIdle();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by johnk on 2025/3/24.
3+
//
4+
5+
#pragma once
6+
7+
#include <Common/Memory.h>
8+
#include <Launch/GameViewport.h>
9+
#include <Runtime/Engine.h>
10+
#include <Runtime/GameModule.h>
11+
12+
namespace Launch {
13+
class GameApplication {
14+
public:
15+
GameApplication(int argc, char* argv[]);
16+
~GameApplication();
17+
18+
void Tick();
19+
bool ShouldClose() const;
20+
21+
private:
22+
double lastFrameTimeSeconds;
23+
double thisFrameTimeSeconds;
24+
float deltaTimeSeconds;
25+
Common::UniquePtr<GameViewport> viewport;
26+
Runtime::Engine* engine;
27+
Runtime::GameModule* gameModule;
28+
};
29+
}

Engine/Source/Launch/Include/Launch/GameClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Launch {
1111
class GameViewport;
1212

13-
class GameClient : public Runtime::Client {
13+
class GameClient final : public Runtime::Client {
1414
public:
1515
explicit GameClient(GameViewport& inViewport);
1616
~GameClient() override;

Engine/Source/Launch/Include/Launch/GameViewport.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,50 @@
44

55
#pragma once
66

7+
#include <GLFW/glfw3.h>
8+
#if PLATFORM_WINDOWS
9+
#define GLFW_EXPOSE_NATIVE_WIN32
10+
#elif PLATFORM_MACOS
11+
#define GLFW_EXPOSE_NATIVE_COCOA
12+
#endif
13+
#include <GLFW/glfw3native.h>
14+
15+
#include <RHI/RHI.h>
716
#include <Runtime/Viewport.h>
17+
#include <Launch/GameClient.h>
18+
#include <Render/RenderModule.h>
819

920
namespace Launch {
21+
struct GameViewportDesc {
22+
std::string title;
23+
uint32_t width;
24+
uint32_t height;
25+
};
26+
1027
class GameViewport final : public Runtime::Viewport {
1128
public:
12-
GameViewport();
29+
explicit GameViewport(const GameViewportDesc& inDesc);
1330
~GameViewport() override;
1431

1532
Runtime::Client& GetClient() override;
1633
Runtime::PresentInfo GetNextPresentInfo() override;
17-
size_t GetWidth() const override;
18-
size_t GetHeight() const override;
34+
uint32_t GetWidth() const override;
35+
uint32_t GetHeight() const override;
36+
void Resize(uint32_t inWidth, uint32_t inHeight) override;
37+
38+
bool ShouldClose() const;
39+
void PollEvents() const;
40+
void RecreateSwapChain(uint32_t inWidth, uint32_t inHeight);
41+
void WaitDeviceIdle() const;
1942

2043
private:
21-
// TODO glfw window
22-
// TODO rhi swap chain
23-
// TODO game client
44+
Render::RenderModule& renderModule;
45+
GameClient client;
46+
GLFWwindow* window;
47+
RHI::Device* device;
48+
Common::UniquePtr<RHI::Semaphore> imageReadySemaphore;
49+
Common::UniquePtr<RHI::Semaphore> renderFinishedSemaphore;
50+
Common::UniquePtr<RHI::Surface> surface;
51+
Common::UniquePtr<RHI::SwapChain> swapChain;
2452
};
2553
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Created by johnk on 2025/3/24.
3+
//
4+
5+
#include <Common/Time.h>
6+
#include <Launch/GameApplication.h>
7+
#include <Core/Cmdline.h>
8+
#include <RHI/Instance.h>
9+
#include <Runtime/Engine.h>
10+
#include <Runtime/Settings/Registry.h>
11+
#include <Runtime/Settings/Game.h>
12+
13+
namespace Launch {
14+
extern std::string gameModuleName;
15+
16+
static Core::CmdlineArgValue<std::string> caRhiType(
17+
"rhiType", "-rhi", RHI::GetPlatformDefaultRHIAbbrString(),
18+
"rhi abbr string, can be 'dx12' or 'vulkan'");
19+
20+
GameApplication::GameApplication(int argc, char* argv[])
21+
: lastFrameTimeSeconds(Common::TimePoint::Now().ToSeconds())
22+
, thisFrameTimeSeconds(Common::TimePoint::Now().ToSeconds())
23+
, deltaTimeSeconds(0)
24+
{
25+
Core::Cli::Get().Parse(argc, argv);
26+
27+
Runtime::EngineInitParams engineInitParams;
28+
engineInitParams.logToFile = true;
29+
engineInitParams.rhiType = caRhiType.GetValue();
30+
Runtime::EngineHolder::Load(gameModuleName, engineInitParams);
31+
32+
engine = &Runtime::EngineHolder::Get();
33+
gameModule = &Core::ModuleManager::Get().GetTyped<Runtime::GameModule>(gameModuleName);
34+
35+
GameViewportDesc viewportDesc;
36+
viewportDesc.title = gameModule->GetGameName();
37+
// TODO load from config
38+
viewportDesc.width = 1024;
39+
viewportDesc.height = 768;
40+
viewport = Common::MakeUnique<GameViewport>(viewportDesc);
41+
42+
auto& settingRegistry = Runtime::SettingsRegistry::Get();
43+
settingRegistry.LoadAllSettings();
44+
45+
const auto& gameSettings = Runtime::SettingsRegistry::Get().GetSettings<Runtime::GameSettings>();
46+
const auto startupLevel = Runtime::AssetManager::Get().SyncLoad<Runtime::Level>(gameSettings.gameStartupLevel, Runtime::Level::GetStaticClass());
47+
viewport->GetClient().GetWorld().LoadFrom(startupLevel);
48+
}
49+
50+
GameApplication::~GameApplication()
51+
{
52+
Runtime::EngineHolder::Unload();
53+
}
54+
55+
void GameApplication::Tick()
56+
{
57+
thisFrameTimeSeconds = Common::TimePoint::Now().ToSeconds();
58+
deltaTimeSeconds = thisFrameTimeSeconds - lastFrameTimeSeconds;
59+
lastFrameTimeSeconds = thisFrameTimeSeconds;
60+
61+
engine->Tick(deltaTimeSeconds);
62+
viewport->PollEvents();
63+
}
64+
65+
bool GameApplication::ShouldClose() const
66+
{
67+
return viewport->ShouldClose();
68+
}
69+
}

Engine/Source/Launch/Src/GameViewport.cpp

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,129 @@
33
//
44

55
#include <Launch/GameViewport.h>
6+
#include <Runtime/Engine.h>
7+
8+
namespace Launch::Internal {
9+
static void* GetGlfwPlatformWindow(GLFWwindow* inWindow)
10+
{
11+
#if PLATFORM_WINDOWS
12+
return glfwGetWin32Window(inWindow);
13+
#elif PLATFORM_MACOS
14+
return glfwGetCocoaView(inWindow);
15+
#else
16+
Unimplement();
17+
return nullptr;
18+
#endif
19+
}
20+
}
621

722
namespace Launch {
8-
GameViewport::GameViewport()
23+
GameViewport::GameViewport(const GameViewportDesc& inDesc)
24+
: renderModule(Runtime::EngineHolder::Get().GetRenderModule())
25+
, client(*this)
926
{
10-
// TODO
27+
glfwInit();
28+
window = glfwCreateWindow(static_cast<int>(inDesc.width), static_cast<int>(inDesc.height), inDesc.title.c_str(), nullptr, nullptr);
29+
30+
device = renderModule.GetDevice();
31+
imageReadySemaphore = device->CreateSemaphore();
32+
renderFinishedSemaphore = device->CreateSemaphore();
33+
34+
surface = device->CreateSurface(RHI::SurfaceCreateInfo(Internal::GetGlfwPlatformWindow(window)));
35+
RecreateSwapChain(inDesc.width, inDesc.height);
1136
}
1237

1338
GameViewport::~GameViewport()
1439
{
15-
// TODO
40+
WaitDeviceIdle();
41+
glfwDestroyWindow(window);
42+
glfwTerminate();
1643
}
1744

1845
Runtime::Client& GameViewport::GetClient()
1946
{
20-
// TODO
21-
return *static_cast<Runtime::Client*>(nullptr);
47+
return client;
2248
}
2349

2450
Runtime::PresentInfo GameViewport::GetNextPresentInfo()
2551
{
26-
// TODO
27-
return Runtime::PresentInfo();
52+
const auto backTextureIndex = swapChain->AcquireBackTexture(imageReadySemaphore.Get());
53+
54+
Runtime::PresentInfo result;
55+
result.backTexture = swapChain->GetTexture(backTextureIndex);
56+
result.imageReadySemaphore = imageReadySemaphore.Get();
57+
result.renderFinishedSemaphore = renderFinishedSemaphore.Get();
58+
return result;
59+
}
60+
61+
uint32_t GameViewport::GetWidth() const
62+
{
63+
int width;
64+
glfwGetWindowSize(window, &width, nullptr);
65+
return static_cast<uint32_t>(width);
66+
}
67+
68+
uint32_t GameViewport::GetHeight() const
69+
{
70+
int height;
71+
glfwGetWindowSize(window, nullptr, &height);
72+
return static_cast<uint32_t>(height);
2873
}
2974

30-
size_t GameViewport::GetWidth() const
75+
void GameViewport::Resize(uint32_t inWidth, uint32_t inHeight)
3176
{
32-
// TODO
33-
return 0;
77+
glfwSetWindowSize(window, static_cast<int>(inWidth), static_cast<int>(inHeight));
78+
RecreateSwapChain(inWidth, inHeight);
3479
}
3580

36-
size_t GameViewport::GetHeight() const
81+
bool GameViewport::ShouldClose() const
3782
{
38-
// TODO
39-
return 0;
83+
return static_cast<bool>(glfwWindowShouldClose(window));
84+
}
85+
86+
void GameViewport::PollEvents() const // NOLINT
87+
{
88+
glfwPollEvents();
89+
}
90+
91+
void GameViewport::RecreateSwapChain(uint32_t inWidth, uint32_t inHeight)
92+
{
93+
static std::vector<RHI::PixelFormat> formatQualifiers = {
94+
RHI::PixelFormat::rgba8Unorm,
95+
RHI::PixelFormat::bgra8Unorm
96+
};
97+
98+
WaitDeviceIdle();
99+
if (swapChain.Valid()) {
100+
swapChain.Reset();
101+
}
102+
103+
std::optional<RHI::PixelFormat> pixelFormat = {};
104+
for (const auto format : formatQualifiers) {
105+
if (device->CheckSwapChainFormatSupport(surface.Get(), format)) {
106+
pixelFormat = format;
107+
break;
108+
}
109+
}
110+
Assert(pixelFormat.has_value());
111+
112+
swapChain = device->CreateSwapChain(
113+
RHI::SwapChainCreateInfo()
114+
.SetPresentQueue(device->GetQueue(RHI::QueueType::graphics, 0))
115+
.SetSurface(surface.Get())
116+
.SetTextureNum(2)
117+
.SetFormat(pixelFormat.value())
118+
.SetWidth(inWidth)
119+
.SetHeight(inHeight)
120+
.SetPresentMode(RHI::PresentMode::immediately));
121+
}
122+
123+
void GameViewport::WaitDeviceIdle() const
124+
{
125+
renderModule.GetRenderThread().Flush();
126+
127+
const Common::UniquePtr<RHI::Fence> fence = device->CreateFence(false);
128+
device->GetQueue(RHI::QueueType::graphics, 0)->Flush(fence.Get());
129+
fence->Wait();
40130
}
41131
}

Engine/Source/Launch/Src/Main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Created by johnk on 2025/2/19.
33
//
44

5+
#include <Launch/GameApplication.h>
6+
57
int main(int argc, char* argv[])
68
{
7-
// TODO parse command line arguments
8-
// TODO load runtime module
9-
// TODO load engine
10-
// TODO create game viewport
11-
// TODO load startup level to world
12-
// TODO main loop tick
9+
Launch::GameApplication app(argc, argv);
10+
while (!app.ShouldClose()) {
11+
app.Tick();
12+
}
1313
return 0;
1414
}

0 commit comments

Comments
 (0)