ArtifactEngine/src/core/Engine.h

173 lines
4.3 KiB
C++

#pragma once
#include <iostream>
#include <glfw3webgpu.h>
#include <GLFW/glfw3.h>
#include <webgpu/webgpu.h>
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#include <nuklear.h>
#include <cista.h>
#include <Window.h>
#include <Events.h>
#include <ClientWorld.h>
#include <ChunkRenderer.h>
#include <Texture.h>
namespace Artifact {
namespace Events {
struct PipelineReload {};
struct DrawUI {
nk_context* ctx;
};
}
/*
class Engine: public EventTarget {
public:
// Windowing
int viewportWidth = 1200;
int viewportHeight = 800;
GLFWwindow* window = []{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
auto w = glfwCreateWindow(1200, 800, "Artifact Engine", nullptr, nullptr);
if (!w) throw std::runtime_error("Failed to create GLFW window");
glfwShowWindow(w);
glfwPollEvents();
return w;
}();
WGPUInstance instance = nullptr;
WGPUAdapter adapter = nullptr;
WGPUDevice device = nullptr;
WGPUSurface surface = nullptr;
WGPUTextureFormat surfaceFormat = WGPUTextureFormat_BGRA8Unorm;
WGPUQueue queue = nullptr;
WGPURenderPipeline worldPipeline = nullptr;
WGPURenderPipeline objectPipeline = nullptr;
WGPURenderPipeline uiPipeline = nullptr;
std::unique_ptr<Util::Texture> depthTexture;
std::unique_ptr<World::World> world;
std::string assetPath = std::string(getenv("HOME")) + "/eclipse-workspace/ArtifactEngine/assets";
Engine();
~Engine();
void init();
void run();
};
/*/
// Forward declaration.
class Server;
class Engine: public EventTarget {
public:
// Windowing
int viewportWidth = 1200;
int viewportHeight = 800;
Window window;
// WebGPU
WGPUInstance instance = nullptr;
WGPUAdapter adapter = nullptr;
WGPUDevice device = nullptr;
WGPUSurface surface = nullptr;
WGPUTextureFormat surfaceFormat = WGPUTextureFormat_BGRA8Unorm;
WGPUQueue queue = nullptr;
WGPURenderPipeline worldPipeline = nullptr;
WGPURenderPipeline objectPipeline = nullptr;
WGPURenderPipeline uiPipeline = nullptr;
std::unique_ptr<Util::Texture> depthTexture;
// Nuklear
std::unique_ptr<nk_context> ui;
std::unique_ptr<Util::Texture> uiFontTexture;
std::unique_ptr<Util::Texture> uiDummyTexture;
nk_buffer uiVertexBufferNK;
nk_buffer uiIndexBufferNK;
nk_buffer uiCommandBufferNK;
WGPUBuffer uiVertexBuffer = nullptr;
WGPUBuffer uiIndexBuffer = nullptr;
WGPUBuffer uiUniformBuffer = nullptr;
WGPUSampler uiSampler = nullptr;
WGPUBindGroupLayout uiBgl = nullptr;
// World
std::unique_ptr<World::ClientWorld> world;
WGPUBuffer worldUniformBuffer = nullptr;
WGPUSampler worldSampler = nullptr;
WGPUBindGroupLayout worldBgl = nullptr;
WGPUBindGroup worldBindGroup = nullptr;
std::unique_ptr<Util::Texture> nodeTextureAtlas;
std::unique_ptr<ChunkRenderer> chunkRenderer = std::make_unique<ChunkRenderer>(this);
// Misc
std::string assetPath = std::string(getenv("HOME")) + "/eclipse-workspace/ArtifactEngine/assets";
glm::mat4 ortho;
Server* server = nullptr;
Engine();
~Engine();
// Initialize the engine: open the window, set up the wgpu device, etc.
void init();
// Reload shaders, processing overrides if present.
void reloadShaders();
// Create (and refresh) the pipelines used to render the world.
void makeWorldPipelines();
// Create (and refresh) the pipeline used to render entities.
void makeObjectPipeline();
// Create (and refresh) the pipeline used to render the UI.
void makeUIPipeline();
// Manages the main loop.
void run();
// Performs rendering per-frame.
void render();
// Updates game logic per-step.
void tick();
// Send a message to the server.
template<typename T>
void sendMessage(T msg);
// Receive a message from the server.
template<typename T>
void receiveMessage(T msg) {
if constexpr (std::is_same_v<T, Events::Initialized>) {
std::cout << "Message received" << std::endl;
} else if constexpr (std::is_same_v<T, Events::ChunkChanged>) {
auto ev = (Events::ChunkChanged) msg;
world->chunks.emplace(ev.pos, std::make_unique<World::Chunk>(ev.data));
chunkRenderer->addChunk(ev.pos, world->chunks.at(ev.pos).get());
}
}
};
//*/
}
#include <Server.h>
template<typename T>
void Artifact::Engine::sendMessage(T msg){
if(server) {
server->receiveMessage(msg);
} else {
//auto buffer = cista::serialize(msg);
}
}