-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestMain.cpp
More file actions
136 lines (110 loc) · 3.92 KB
/
testMain.cpp
File metadata and controls
136 lines (110 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <vector>
#include <cmath>
#include <memory>
#include <string>
#include "Application/Window.h"
#include "Renderer/GraphicsBackend.h"
#include "Renderer/RenderSystem.h"
#include "Renderer/Camera.h"
#include "Renderer/Nut/NutContext.h"
#include "Resources/RuntimeAsset/RuntimeWGSLMaterial.h"
#include "dawn/dawn_proc.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkFont.h"
#include "include/core/SkTypeface.h"
#include "include/effects/SkGradientShader.h"
using namespace Nut;
TextureAPtr CreateFallbackTexture(std::shared_ptr<NutContext> context, uint8_t r, uint8_t g, uint8_t b)
{
uint8_t pixel[] = {r, g, b, 255};
return TextureBuilder()
.SetPixelData(pixel, 1, 1, 4)
.SetSize(1, 1)
.SetFormat(wgpu::TextureFormat::RGBA8Unorm)
.Build(context);
}
int main(int argc, char* argv[])
{
dawnProcSetProcs(&dawn::native::GetProcs());
const int WIDTH = 1280;
const int HEIGHT = 720;
auto window = PlatformWindow::Create("LumaEngine Hybrid Rendering (Skia + WGPU)", WIDTH, HEIGHT);
if (!window) return -1;
GraphicsBackendOptions options;
options.windowHandle = window->GetNativeWindowHandle();
options.width = WIDTH;
options.height = HEIGHT;
options.backendTypePriority = {BackendType::D3D12, BackendType::Vulkan, BackendType::Metal};
options.qualityLevel = ::QualityLevel::High;
options.enableVSync = true;
auto backend = GraphicsBackend::Create(options);
if (!backend)
{
std::cerr << "GraphicsBackend Init Failed" << std::endl;
return -1;
}
std::cout << "Backend Initialized." << std::endl;
auto renderSystem = RenderSystem::Create(*backend);
auto nutContext = backend->GetNutContext();
auto texture = nutContext->LoadTextureFromFile("./Test.png");
if (!texture)
{
std::cout << "Test.png not found, creating fallback texture." << std::endl;
texture = CreateFallbackTexture(nutContext, 255, 255, 255);
}
CameraProperties camProps;
camProps.position = {0.0f, 0.0f};
camProps.viewport = SkRect::MakeXYWH(0, 0, (float)WIDTH, (float)HEIGHT);
camProps.zoom = {1.0f,1.0f};
camProps.rotation = 0.0f;
camProps.clearColor = {0.15f, 0.15f, 0.15f, 1.0f};
CameraManager::GetInstance().GetActiveCamera().SetProperties(camProps);
std::vector<RenderableTransform> transforms;
const int COUNT = 1;
for (int i = 0; i < COUNT; ++i)
{
float x = (i - (COUNT - 1) / 2.0f) * 120.0f;
transforms.emplace_back(SkPoint{x, 0}, .1f, .1f, 0.0f);
}
std::cout << "Starting Render Loop..." << std::endl;
bool running = true;
window->OnCloseRequest += [&]() { running = false; };
float time = 0.0f;
while (running && !window->ShouldClose())
{
window->PollEvents();
time += 0.02f;
for (size_t i = 0; i < transforms.size(); ++i)
{
float angle = time + i * 0.5f;
transforms[i].sinR = sin(angle);
transforms[i].cosR = cos(angle);
}
if (backend->BeginFrame())
{
WGPUSpriteBatch batch;
batch.image = texture;
batch.transforms = transforms.data();
batch.count = transforms.size();
batch.color = {1.0f, 1.0f, 1.0f, 1.0f};
batch.filterQuality = 1;
renderSystem->Submit(batch);
RawDrawBatch skiaBatch;
skiaBatch.drawFunc += [WIDTH, HEIGHT](SkCanvas* canvas)
{
SkPaint paint;
paint.setColor(SK_ColorRED);
SkRect rect = SkRect::MakeXYWH(50, 50, 200, 200);
canvas->drawRect(rect, paint);
};
renderSystem->Submit(skiaBatch);
renderSystem->Submit(batch);
renderSystem->Flush();
backend->Submit();
backend->PresentFrame();
}
SDL_Delay(16);
}
return 0;
}