summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <lag1924@gmail.com>2021-06-18 14:14:19 +0200
committerLaG1924 <lag1924@gmail.com>2021-06-18 16:52:47 +0200
commit8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851 (patch)
treee0f585207f4dde33999e73d3fbde65f0c76f8b16
parentImplemented main menu in Rml and improved RmlUi support (diff)
downloadAltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.tar
AltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.tar.gz
AltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.tar.bz2
AltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.tar.lz
AltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.tar.xz
AltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.tar.zst
AltCraft-8c033fff3d82d5f0e4c5d2eb3c5d10efc60ee851.zip
-rw-r--r--cwd/assets/altcraft/scripts/init.lua7
-rw-r--r--cwd/assets/altcraft/ui/main-menu-styles.rcss (renamed from cwd/test.rcss)0
-rw-r--r--cwd/assets/altcraft/ui/main-menu.rml (renamed from cwd/test.rml)6
-rw-r--r--src/AssetManager.cpp3
-rw-r--r--src/AssetManager.hpp2
-rw-r--r--src/Plugin.cpp2
-rw-r--r--src/Render.cpp14
-rw-r--r--src/Render.hpp4
-rw-r--r--src/Rml.cpp46
-rw-r--r--src/Rml.hpp26
10 files changed, 94 insertions, 16 deletions
diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua
index 96b5ec3..b9b34a7 100644
--- a/cwd/assets/altcraft/scripts/init.lua
+++ b/cwd/assets/altcraft/scripts/init.lua
@@ -9,11 +9,14 @@ local plugin = {
}
function plugin.onLoad ()
- print("Loaded AltCraft plugin!")
+ rmlui:LoadFontFace("altcraft/fonts/OpenSans-Regular")
end
function plugin.onChangeState (newState)
- AC.LogWarning("New state: "..newState)
+ if newState == "MainMenu" then
+ local con = rmlui.contexts["default"]
+ con:LoadDocument("altcraft/ui/main-menu"):Show()
+ end
end
function plugin.onUnload ()
diff --git a/cwd/test.rcss b/cwd/assets/altcraft/ui/main-menu-styles.rcss
index ee86894..ee86894 100644
--- a/cwd/test.rcss
+++ b/cwd/assets/altcraft/ui/main-menu-styles.rcss
diff --git a/cwd/test.rml b/cwd/assets/altcraft/ui/main-menu.rml
index de48053..9fd2898 100644
--- a/cwd/test.rml
+++ b/cwd/assets/altcraft/ui/main-menu.rml
@@ -1,6 +1,6 @@
<rml>
<head>
- <link type="text/rcss" href="test.rcss" />
+ <link type="text/rcss" href="main-menu-styles" />
</head>
<body>
<strong class="mc-title" id="title">AltCraft</strong>
@@ -10,8 +10,8 @@
<input class="mc-text" id="username" value="HelloOne"/>
<button class="mc-button" id="connect" onclick="
AC.ConnectToServer(
- document: GetElementById('hostname'): GetAttribute('value'),
- document: GetElementById('username'): GetAttribute('value'));
+ document:GetElementById('hostname'):GetAttribute('value'),
+ document:GetElementById('username'):GetAttribute('value'));
document:Close()">Connect</button>
<button class="mc-button" id="options">Options...</button>
<button class="mc-button" id="exit" onclick="AC.Exit()">Quit game</button>
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 0db495a..8528a1b 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -64,6 +64,9 @@ void AssetManager::InitAssetManager()
ParseBlockModels();
PluginSystem::Init();
+}
+
+void AssetManager::InitPostRml() {
LoadScripts();
}
diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp
index bf948b3..b67d920 100644
--- a/src/AssetManager.hpp
+++ b/src/AssetManager.hpp
@@ -174,6 +174,8 @@ struct AssetScript : Asset {
namespace AssetManager {
void InitAssetManager();
+ void InitPostRml();
+
BlockFaces &GetBlockModelByBlockId(BlockId block);
std::string GetAssetNameByBlockId(BlockId block);
diff --git a/src/Plugin.cpp b/src/Plugin.cpp
index 13045e1..776ba8d 100644
--- a/src/Plugin.cpp
+++ b/src/Plugin.cpp
@@ -42,8 +42,8 @@ namespace PluginApi {
plugin["onRequestBlockInfo"].get_or(std::function<BlockInfo(Vector)>()),
};
plugins.push_back(nativePlugin);
+ LOG(INFO)<<"Loading plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name);
nativePlugin.onLoad();
-
LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name);
}
diff --git a/src/Render.cpp b/src/Render.cpp
index 39322f9..c0885e3 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -64,6 +64,8 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight,
glCheckError();
InitRml();
glCheckError();
+ AssetManager::InitPostRml();
+ glCheckError();
//Read settings
strcpy(fieldUsername, Settings::Read("username", "HelloOne").c_str());
@@ -545,19 +547,13 @@ void Render::InitRml() {
Rml::SetRenderInterface(rmlRender.get());
rmlRender->Update(renderState.WindowWidth, renderState.WindowHeight);
+ rmlFile = std::make_unique<RmlFileInterface>();
+ Rml::SetFileInterface(rmlFile.get());
+
if (!Rml::Initialise())
LOG(WARNING) << "Rml not initialized";
Rml::Lua::Initialise(PluginSystem::GetLuaState());
rmlContext = Rml::CreateContext("default", Rml::Vector2i(renderState.WindowWidth, renderState.WindowHeight));
-
- if (!Rml::LoadFontFace("OpenSans-Regular.ttf"))
- LOG(WARNING) << "Rml font not loaded";
-
- Rml::ElementDocument* document = rmlContext->LoadDocument("test.rml");
- if (document)
- document->Show();
- else
- LOG(WARNING) << "Rml document not loaded";
}
diff --git a/src/Render.hpp b/src/Render.hpp
index 7b2313c..a7b510a 100644
--- a/src/Render.hpp
+++ b/src/Render.hpp
@@ -15,6 +15,7 @@ class RendererWorld;
class Framebuffer;
class RmlRenderInterface;
class RmlSystemInterface;
+class RmlFileInterface;
namespace Rml
{
class Context;
@@ -51,8 +52,9 @@ class Render {
float fieldResolutionScale;
std::unique_ptr<RmlRenderInterface> rmlRender;
std::unique_ptr<RmlSystemInterface> rmlSystem;
+ std::unique_ptr<RmlFileInterface> rmlFile;
Rml::Context* rmlContext;
- unsigned short sdlKeyMods;
+ unsigned short sdlKeyMods = 0;
void SetMouseCapture(bool IsCaptured);
diff --git a/src/Rml.cpp b/src/Rml.cpp
index abec7db..cbc795f 100644
--- a/src/Rml.cpp
+++ b/src/Rml.cpp
@@ -162,3 +162,49 @@ void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHei
AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->SetUniform("fontTexture", 0);
glCheckError();
}
+
+Rml::FileHandle RmlFileInterface::Open(const Rml::String& path) {
+ Rml::FileHandle fileId = handles.rbegin() != handles.rend() ? handles.rbegin()->first + 1 : 1;
+ while (handles.find(fileId) != handles.end())
+ fileId++;
+
+ AssetHandle handle;
+ handle.fileName = path;
+ std::string assetName = path;
+ if (*assetName.begin() != '/')
+ assetName = "/" + assetName;
+ handle.assetPtr = AssetManager::GetAssetByAssetName(assetName);
+ handle.filePos = 0;
+
+ if (handle.assetPtr != nullptr)
+ handles.insert(std::make_pair(fileId, handle));
+ else
+ fileId = 0;
+ return fileId;
+}
+
+void RmlFileInterface::Close(Rml::FileHandle file) {
+ handles.erase(file);
+}
+
+size_t RmlFileInterface::Read(void* buffer, size_t size, Rml::FileHandle file) {
+ size_t readed = 0;
+ readed = _min(handles[file].assetPtr->data.size() - handles[file].filePos, size);
+ std::memcpy(buffer, handles[file].assetPtr->data.data() + handles[file].filePos, readed);
+ handles[file].filePos += readed;
+ return readed;
+}
+
+bool RmlFileInterface::Seek(Rml::FileHandle file, long offset, int origin) {
+ unsigned long long base = 0;
+ if (origin == SEEK_CUR)
+ base = handles[file].filePos;
+ else if (origin == SEEK_END)
+ base = handles[file].assetPtr->data.size();
+ handles[file].filePos = base + offset;
+ return true;
+}
+
+size_t RmlFileInterface::Tell(Rml::FileHandle file) {
+ return handles[file].filePos;
+}
diff --git a/src/Rml.hpp b/src/Rml.hpp
index 7b312c0..6e4d857 100644
--- a/src/Rml.hpp
+++ b/src/Rml.hpp
@@ -1,10 +1,15 @@
#pragma once
+#include <map>
+
#include <RmlUi/Core/SystemInterface.h>
#include <RmlUi/Core/RenderInterface.h>
+#include <RmlUi/Core/FileInterface.h>
#include "Renderer.hpp"
+class AssetTreeNode;
+
class RmlSystemInterface : public Rml::SystemInterface {
double totalTime;
public:
@@ -58,3 +63,24 @@ public:
void Update(unsigned int windowWidth, unsigned int windowHeight);
};
+
+class RmlFileInterface : public Rml::FileInterface {
+ struct AssetHandle {
+ std::string fileName;
+ unsigned long long filePos;
+ AssetTreeNode* assetPtr;
+ };
+ std::map<Rml::FileHandle, AssetHandle> handles;
+public:
+
+ virtual Rml::FileHandle Open(const Rml::String& path) override;
+
+ virtual void Close(Rml::FileHandle file) override;
+
+ virtual size_t Read(void* buffer, size_t size, Rml::FileHandle file) override;
+
+ virtual bool Seek(Rml::FileHandle file, long offset, int origin) override;
+
+ virtual size_t Tell(Rml::FileHandle file) override;
+
+};