Update some things.

This commit is contained in:
Signal 2026-04-06 18:30:52 -04:00
parent f215bc3742
commit f9d6e7a70a
35 changed files with 575 additions and 905 deletions

View file

@ -1,6 +1,7 @@
#include <chrono>
#include "Client.h"
#include "Network.h"
#include "Graphics/UIRenderer.h"
#include "Graphics/Graphics.h"
#include <Network/Network.h>
@ -8,10 +9,10 @@
namespace Artifact {
void Client::init() {
window = subsystem<Window>();
window = subsystem<WindowImpl>();
for (auto & system : subsystems) {
system->engine = this;
system->client = this;
system->init();
system->reload();
}
@ -45,7 +46,7 @@ void Client::tick() {
void Client::addDefaultSubsystems() {
addSubsystem<Window>(1080, 640, "Artifact Engine");
addSubsystem<NetworkClient>();
addSubsystem<ClientNetwork>();
auto graphics = addSubsystem<Graphics>();
{
graphics->addSubsystem<UIRenderer>();

View file

@ -3,14 +3,20 @@
#include <memory>
#include "Shared.h"
#include "Platform/Window.h"
#include <Settings.h>
namespace Artifact {
class ClientSubsystem;
class WindowImpl;
/// The client class.
class Client: public Engine<BaseSubsystem> {
class Client: public Engine<ClientSubsystem> {
public:
Window * window = nullptr;
WindowImpl * window = nullptr;
Settings settings;
Client() : settings(getClientConfigPath()) {}
void addDefaultSubsystems();
void initDefault();
@ -24,4 +30,11 @@ public:
void run();
};
class ClientSubsystem: public BaseSubsystem {
public:
Client * client = nullptr;
virtual void render() {}
virtual void tick() {}
};
}

View file

@ -0,0 +1,7 @@
#include "Camera.h"
namespace Artifact {
}

24
Client/Graphics/Camera.h Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <vector>
#include <typeindex>
#include <glm/glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
namespace Artifact {
class Camera {
public:
glm::vec3 pos;
glm::quat rot;
double fov;
float near;
float far;
std::vector<std::type_index> renderPasses;
Camera(glm::vec3 pos, double fov, float near, float far) : pos(pos), fov(fov), near(near), far(far) {}
};
}

View file

@ -8,7 +8,7 @@ namespace Artifact {
void Graphics::init() {
instance = wgpuCreateInstance(nullptr);
window = engine->subsystem<Window>();
window = client->subsystem<WindowImpl>();
surface = window->createWGPUSurface(instance);

View file

@ -3,6 +3,7 @@
#include <webgpu/webgpu.h>
#include <Shared.h>
#include "../Client.h"
#include "../Platform/Window.h"
namespace Artifact {
@ -18,7 +19,7 @@ public:
virtual void render(WGPUTextureView nextTexture, WGPUCommandEncoder encoder) {}
};
class Graphics: public BaseSubsystem, public Engine<GraphicsSubsystem> {
class Graphics: public ClientSubsystem, public Engine<GraphicsSubsystem> {
public:
WGPUInstance instance = nullptr;
WGPUSurface surface = nullptr;
@ -29,14 +30,11 @@ public:
WGPUTextureView depthTextureView = nullptr;
WGPUQueue queue = nullptr;
Window * window;
WindowImpl * window;
void init() override;
void deinit() override;
void render() override;
void onSubsystemAdd(GraphicsSubsystem * system) {
system->graphics = this;
}
WGPUTexture createTextureFromData(const void* data, uint32_t width, uint32_t height);
WGPUTexture createTextureFromFile(const char* file);

View file

@ -14,14 +14,14 @@ struct DrawUI {};
}
static std::unordered_map<Key, nk_keys> ArtifactToNuklear {};
static std::array<nk_keys, static_cast<size_t>(Key::Last) + 1> ArtifactToNuklear {};
namespace {
static struct _nkInit {
_nkInit() {
// Populate the Artifact-to-Nuklear key map.
#define X(nuklear, artifact) ArtifactToNuklear[Key::artifact] = nuklear
#define X(nuklear, artifact) ArtifactToNuklear[static_cast<int>(Key::artifact)] = nuklear
X(NK_KEY_CTRL, ControlLeft);
X(NK_KEY_CTRL, ControlRight);
@ -47,37 +47,36 @@ static struct _nkInit {
void UIRenderer::init() {
printf("UI: %p", graphics);
window = graphics->window;
window->listen<Events::InputBegin>([this](auto ev) {
graphics->window->listen<Events::InputBegin>([this](auto ev) {
nk_input_begin(&ctx);
});
window->listen<Events::InputEnd>([this](auto ev) {
graphics->window->listen<Events::InputEnd>([this](auto ev) {
nk_input_end(&ctx);
});
window->listen<Events::CursorPosEvent>([this](auto ev) {
graphics->window->listen<Events::CursorPosEvent>([this](auto ev) {
nk_input_motion(&ctx, ev.x, ev.y);
});
window->listen<Events::ScrollEvent>([this](auto ev) {
graphics->window->listen<Events::ScrollEvent>([this](auto ev) {
nk_input_scroll(&ctx, nk_vec2(ev.dx, ev.dy));
});
window->listen<Events::MouseEvent>([this](auto ev) {
graphics->window->listen<Events::MouseEvent>([this](auto ev) {
nk_input_button(&ctx, ev.button == Events::MOUSE_BUTTON_RIGHT ? NK_BUTTON_RIGHT : NK_BUTTON_LEFT, (int)ev.x, (int)ev.y, ev.state);
});
window->listen<Events::KeyDownEvent>([this](auto ev) {
nk_input_key(&ctx, ArtifactToNuklear[ev.key], true);
graphics->window->listen<Events::KeyDownEvent>([this](auto ev) {
nk_input_key(&ctx, ArtifactToNuklear[static_cast<int>(ev.key)], true);
});
window->listen<Events::KeyUpEvent>([this](auto ev) {
nk_input_key(&ctx, ArtifactToNuklear[ev.key], false);
graphics->window->listen<Events::KeyUpEvent>([this](auto ev) {
nk_input_key(&ctx, ArtifactToNuklear[static_cast<int>(ev.key)], false);
});
window->listen<Events::CharInputEvent>([this](auto ev) {
graphics->window->listen<Events::CharInputEvent>([this](auto ev) {
nk_input_char(&ctx, ev.codepoint);
});
@ -102,7 +101,6 @@ void UIRenderer::init() {
fclose(fontFile);
font = nk_font_atlas_add_from_memory(&atlas, fontData, fontSize, 13.0f, nullptr);
} else {
fprintf(stderr, (std::string("Failed to load font: ") + assetPath + "/fonts/Arial.ttf\n").c_str());
font = nk_font_atlas_add_default(&atlas, 13.0f, nullptr);
}

View file

@ -21,7 +21,6 @@ class UIRenderer: public GraphicsSubsystem, public EventTarget {
nk_font_atlas atlas {};
WGPURenderPipeline pipeline = nullptr;
Window * window = nullptr;
WGPUBuffer vertexBuffer = nullptr;
WGPUBuffer indexBuffer = nullptr;

View file

@ -1,3 +1,11 @@
#pragma once
#include <Shared.h>
namespace Artifact {
class WorldRenderer {
};
}

17
Client/LocalPlayer.cpp Normal file
View file

@ -0,0 +1,17 @@
#include "LocalPlayer.h"
#include "Platform/Window.h"
namespace Artifact {
void LocalPlayer::init() {
window = client->subsystem<WindowImpl>();
}
void LocalPlayer::tick() {
if (window->isKeyDown(Key::W)) {
}
}
}

26
Client/LocalPlayer.h Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#include <Shared.h>
#include "Client.h"
#include "Graphics/Camera.h"
namespace Artifact {
class LocalPlayerImpl: public ClientSubsystem {
public:
std::unique_ptr<Camera> camera = nullptr;
};
class LocalPlayer: public LocalPlayerImpl {
WindowImpl * window = nullptr;
public:
LocalPlayer() {
//camera = std::make_unique<Camera>(glm::vec3(0, 0, 0), 75);
}
void init() override;
void tick() override;
};
}

11
Client/Network.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include "Client.h"
#include <Network/Network.h>
namespace Artifact {
class ClientNetwork: public NetworkClient, public ClientSubsystem {};
}

View file

@ -6,6 +6,7 @@
namespace Artifact {
static std::array<Key, GLFW_KEY_LAST + 1> GLFWToArtifact {};
static std::array<int, static_cast<size_t>(Key::Last) + 1> ArtifactToGLFW {};
namespace {
// Ensure that GLFW is transparently initialized prior to window creation.
@ -15,7 +16,8 @@ static struct _glfwInit {
// Populate the global GLFW-to-Artifact keycode mapping.
GLFWToArtifact.fill(Key::Unknown);
#define X(glfw, artifact) GLFWToArtifact[glfw] = Key::artifact
#define X(glfw, artifact) GLFWToArtifact[glfw] = Key::artifact;\
ArtifactToGLFW[static_cast<int>(Key::artifact)] = glfw
X(GLFW_KEY_A, A);
X(GLFW_KEY_B, B);
X(GLFW_KEY_C, C);
@ -160,4 +162,16 @@ bool Window::shouldClose() {
return glfwWindowShouldClose(window);
}
void Window::setPointerLock() {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
void Window::releasePointerLock() {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
bool Window::isKeyDown(Key key) {
return glfwGetKey(window, ArtifactToGLFW[static_cast<int>(key)]) == GLFW_PRESS;
}
}

View file

@ -4,8 +4,9 @@
#include <GLFW/glfw3.h>
#include <webgpu/webgpu.h>
#include "Shared.h"
#include <Shared.h>
#include "Events.h"
#include "Client.h"
namespace Artifact {
@ -23,6 +24,8 @@ enum class Key {
DeleteForward, DeleteBackward,
ArrowLeft, ArrowRight, ArrowUp, ArrowDown,
Tab,
Last
};
namespace Events {
@ -72,7 +75,16 @@ struct MouseEvent {
}
class Window: public EventTarget, public BaseSubsystem {
class WindowImpl: public EventTarget, public ClientSubsystem {
public:
virtual bool shouldClose() = 0;
virtual WGPUSurface createWGPUSurface(WGPUInstance instance) = 0;
virtual void setPointerLock() = 0;
virtual void releasePointerLock() = 0;
virtual bool isKeyDown(Key key) = 0;
};
class Window: public WindowImpl {
private:
GLFWwindow * window = nullptr;
public:
@ -80,9 +92,12 @@ public:
void render() override;
WGPUSurface createWGPUSurface(WGPUInstance instance);
WGPUSurface createWGPUSurface(WGPUInstance instance) override;
void setTitle(std::string title);
bool shouldClose();
bool shouldClose() override;
void setPointerLock() override;
void releasePointerLock() override;
bool isKeyDown(Key key) override;
};
}

View file

@ -1,3 +0,0 @@
#include "Player.h"

View file

@ -1,11 +0,0 @@
#pragma once
#include <Shared.h>
namespace Artifact {
class Player: public BaseSubsystem {
};
}