Skip to content

Commit

Permalink
add gfx.text3d.spawn(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 14, 2024
1 parent c4170c0 commit eb5715e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 15 deletions.
6 changes: 6 additions & 0 deletions res/content/base/scripts/hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ function on_hud_open()
local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))
drop.rigidbody:set_vel(velocity)
end)

gfx.text3d.spawn({0.5, 99.5, 0.0015}, "Segmentation fault", {
scale = 0.005,
color = {0, 0, 0, 1},
displayMode = "static_billboard"
})
end
21 changes: 8 additions & 13 deletions src/graphics/render/TextsRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,20 @@ void TextsRenderer::renderTexts(
bool hudVisible,
bool frontLayer
) {
std::vector<TextNote> notes;
NotePreset preset;
preset.displayMode = NoteDisplayMode::STATIC_BILLBOARD;
preset.color = glm::vec4(0, 0, 0, 1);
preset.scale = 0.005f;

notes.emplace_back(
L"Segmentation fault",
std::move(preset),
glm::vec3(0.5f, 99.5f, 0.0015f)
);
auto& shader = assets.require<Shader>("ui3d");

shader.use();
shader.uniformMatrix("u_projview", camera.getProjView());
shader.uniformMatrix("u_apply", glm::mat4(1.0f));
batch.begin();
for (const auto& note : notes) {
renderText(note, context, camera, settings, hudVisible);
for (const auto& [id, note] : notes) {
renderText(*note, context, camera, settings, hudVisible);
}
batch.flush();
}

u64id_t TextsRenderer::add(std::unique_ptr<TextNote> note) {
u64id_t uid = nextNote++;
notes[uid] = std::move(note);
return uid;
}
10 changes: 10 additions & 0 deletions src/graphics/render/TextsRenderer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

#include <unordered_map>
#include <memory>

#include "typedefs.hpp"

class DrawContext;
class Camera;
class Assets;
Expand All @@ -13,6 +18,9 @@ class TextsRenderer {
const Assets& assets;
const Frustum& frustum;

std::unordered_map<u64id_t, std::unique_ptr<TextNote>> notes;
u64id_t nextNote = 1;

void renderText(
const TextNote& note,
const DrawContext& context,
Expand All @@ -30,4 +38,6 @@ class TextsRenderer {
bool hudVisible,
bool frontLayer
);

u64id_t add(std::unique_ptr<TextNote> note);
};
2 changes: 1 addition & 1 deletion src/graphics/render/WorldRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class WorldRenderer {
std::unique_ptr<LineBatch> lineBatch;
std::unique_ptr<Batch3D> batch3d;
std::unique_ptr<ChunksRenderer> chunks;
std::unique_ptr<TextsRenderer> texts;
std::unique_ptr<GuidesRenderer> guides;
std::unique_ptr<Skybox> skybox;
std::unique_ptr<ModelBatch> modelBatch;
Expand Down Expand Up @@ -67,6 +66,7 @@ class WorldRenderer {
float fogFactor
);
public:
std::unique_ptr<TextsRenderer> texts;
std::unique_ptr<ParticlesRenderer> particles;

static bool showChunkBorders;
Expand Down
3 changes: 2 additions & 1 deletion src/logic/scripting/lua/libs/api_lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ extern const luaL_Reg mat4lib[];
extern const luaL_Reg packlib[];
extern const luaL_Reg particleslib[];
extern const luaL_Reg playerlib[];
extern const luaL_Reg quatlib[]; // quat.cpp
extern const luaL_Reg quatlib[];
extern const luaL_Reg text3dlib[];
extern const luaL_Reg timelib[];
extern const luaL_Reg tomllib[];
extern const luaL_Reg utf8lib[];
Expand Down
29 changes: 29 additions & 0 deletions src/logic/scripting/lua/libs/libtext3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "api_lua.hpp"

#include "logic/scripting/scripting_hud.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/TextsRenderer.hpp"
#include "graphics/render/TextNote.hpp"
#include "engine.hpp"

using namespace scripting;

static int l_spawn(lua::State* L) {
auto position = lua::tovec3(L, 1);
auto text = lua::require_wstring(L, 2);
auto preset = lua::tovalue(L, 3);
auto extension = lua::tovalue(L, 4);

NotePreset notePreset {};
notePreset.deserialize(preset);
if (extension != nullptr) {
notePreset.deserialize(extension);
}
auto note = std::make_unique<TextNote>(text, notePreset, position);
return lua::pushinteger(L, renderer->texts->add(std::move(note)));
}

const luaL_Reg text3dlib[] = {
{"spawn", lua::wrap<l_spawn>},
{NULL, NULL}
};
1 change: 1 addition & 0 deletions src/logic/scripting/scripting_hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) {

lua::openlib(L, "hud", hudlib);
lua::openlib(L, "gfx", "particles", particleslib);
lua::openlib(L, "gfx", "text3d", text3dlib);

if (lua::getglobal(L, "__vc_create_hud_rules")) {
lua::call_nothrow(L, 0, 0);
Expand Down

0 comments on commit eb5715e

Please sign in to comment.