summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2021-07-04 14:47:44 +0200
committerGitHub <noreply@github.com>2021-07-04 14:47:44 +0200
commit8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a (patch)
tree1e9510a1c2ff7f6c0eaaba211c38e0a855b36ca9 /src
parentMerge pull request #64 from LaG1924/ftr/render-optimization (diff)
parentAdded screenshot-mode (diff)
downloadAltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.tar
AltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.tar.gz
AltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.tar.bz2
AltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.tar.lz
AltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.tar.xz
AltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.tar.zst
AltCraft-8a4075c58b4a078cdbbe8b394eb5ae761fdaff6a.zip
Diffstat (limited to 'src')
-rw-r--r--src/AssetManager.cpp26
-rw-r--r--src/AssetManager.hpp2
-rw-r--r--src/DebugInfo.cpp3
-rw-r--r--src/DebugInfo.hpp3
-rw-r--r--src/Game.hpp1
-rw-r--r--src/GameState.cpp20
-rw-r--r--src/GameState.hpp4
-rw-r--r--src/Plugin.cpp52
-rw-r--r--src/Plugin.hpp3
-rw-r--r--src/Render.cpp41
-rw-r--r--src/Render.hpp2
-rw-r--r--src/RendererWorld.cpp2
-rw-r--r--src/RendererWorld.hpp3
-rw-r--r--src/Rml.cpp10
-rw-r--r--src/Rml.hpp1
15 files changed, 119 insertions, 54 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 5f24404..9cbb6df 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -21,7 +21,6 @@ namespace fs = std::filesystem;
const fs::path pathToAssets = "./assets/";
const std::string pathToAssetsList = "./items.json";
-std::map<std::string, BlockId> assetIds;
std::map<BlockId, std::string> blockIdToBlockName;
std::unique_ptr<AssetTreeNode> assetTree;
std::unique_ptr<TextureAtlas> atlas;
@@ -29,7 +28,6 @@ std::map<BlockId, BlockFaces> blockIdToBlockFaces;
BlockFaces errorFaces;
-void LoadIds();
void LoadAssets();
void LoadTextures();
void LoadScripts();
@@ -62,7 +60,6 @@ void AssetManager::InitAssetManager()
LoadTextures();
- LoadIds();
ParseBlockModels();
PluginSystem::Init();
@@ -79,19 +76,6 @@ void AssetManager::InitPostRml() {
}
}
-void LoadIds() {
- std::ifstream in(pathToAssetsList);
- nlohmann::json index;
- in >> index;
- for (auto &it : index) {
- unsigned short id = it["type"].get<int>();
- unsigned char state = it["meta"].get<int>();
- std::string blockName = it["text_type"].get<std::string>();
- assetIds[blockName] = BlockId{ id, state };
- }
- LOG(INFO) << "Loaded " << assetIds.size() << " ids";
-}
-
void LoadAssets() {
assetTree = std::make_unique<AssetTreeNode>();
assetTree->name = "/";
@@ -661,16 +645,6 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) {
return blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)).first->second;
}
-std::string AssetManager::GetAssetNameByBlockId(BlockId block) {
- for (auto& it : assetIds) {
- BlockId value = it.second;
- value.state = 0;
- if (value == block)
- return it.first;
- }
- return "#NF";
-}
-
Asset *AssetManager::GetAssetPtr(const std::string & assetName) {
OPTICK_EVENT();
AssetTreeNode *node;
diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp
index b67d920..7f6c4fb 100644
--- a/src/AssetManager.hpp
+++ b/src/AssetManager.hpp
@@ -178,8 +178,6 @@ namespace AssetManager {
BlockFaces &GetBlockModelByBlockId(BlockId block);
- std::string GetAssetNameByBlockId(BlockId block);
-
Asset *GetAssetPtr(const std::string &assetName);
template <typename T>
diff --git a/src/DebugInfo.cpp b/src/DebugInfo.cpp
index 89e9425..37d181d 100644
--- a/src/DebugInfo.cpp
+++ b/src/DebugInfo.cpp
@@ -4,4 +4,5 @@ std::atomic_int DebugInfo::totalSections(0);
std::atomic_int DebugInfo::renderSections(0);
std::atomic_int DebugInfo::readyRenderer(0);
std::atomic_int DebugInfo::gameThreadTime(0);
-std::atomic_int DebugInfo::renderFaces(0); \ No newline at end of file
+std::atomic_int DebugInfo::renderFaces(0);
+std::atomic_int DebugInfo::culledSections(0);
diff --git a/src/DebugInfo.hpp b/src/DebugInfo.hpp
index e6aa17c..7ba1686 100644
--- a/src/DebugInfo.hpp
+++ b/src/DebugInfo.hpp
@@ -5,7 +5,8 @@
struct DebugInfo {
static std::atomic_int totalSections;
static std::atomic_int renderSections;
+ static std::atomic_int culledSections;
static std::atomic_int readyRenderer;
static std::atomic_int gameThreadTime;
static std::atomic_int renderFaces;
-}; \ No newline at end of file
+};
diff --git a/src/Game.hpp b/src/Game.hpp
index f7efd11..74eeea9 100644
--- a/src/Game.hpp
+++ b/src/Game.hpp
@@ -13,6 +13,7 @@ enum class State {
Paused,
Inventory,
Chat,
+ NeedRespawn,
};
void RunGame();
diff --git a/src/GameState.cpp b/src/GameState.cpp
index be408dd..89743e4 100644
--- a/src/GameState.cpp
+++ b/src/GameState.cpp
@@ -6,6 +6,8 @@
#include "Event.hpp"
#include "Packet.hpp"
+#include "Game.hpp"
+#include "Plugin.hpp"
void GameState::Update(double deltaTime) {
OPTICK_EVENT();
@@ -167,7 +169,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) {
case ChatMessageCB: {
auto packet = std::static_pointer_cast<PacketChatMessageCB>(ptr);
LOG(INFO) << "Message (" << int(packet->Position) << "): " << packet->JsonData.ToPlainText();
- PUSH_EVENT("ChatMessageReceived", std::make_tuple(packet->JsonData, packet->Position));
+ PluginSystem::CallOnChatMessage(packet->JsonData, packet->Position);
break;
}
@@ -383,7 +385,6 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) {
auto packetResponse = std::make_shared<PacketTeleportConfirm>(packet->TeleportId);
PUSH_EVENT("SendPacket", std::static_pointer_cast<Packet>(packetResponse));
-
break;
}
@@ -418,6 +419,9 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) {
gameStatus.dimension = packet->Dimension;
gameStatus.difficulty = packet->Difficulty;
gameStatus.levelType = packet->LevelType;
+ SetState(State::Loading);
+ gameStatus.isGameStarted = false;
+ receivedEnoughChunks = false;
break;
}
case EntityHeadLook:
@@ -453,9 +457,8 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) {
auto packet = std::static_pointer_cast<PacketUpdateHealth>(ptr);
playerStatus.health = packet->Health;
if (playerStatus.health <= 0) {
- LOG(INFO) << "Player is dead. Respawning...";
- auto packetPerformRespawn = std::make_shared<PacketClientStatus>(0);
- PUSH_EVENT("SendPacket", std::static_pointer_cast<Packet>(packetPerformRespawn));
+ LOG(INFO) << "Player is dead. Need respawn...";
+ SetState(State::NeedRespawn);
}
break;
}
@@ -678,4 +681,9 @@ void GameState::PlaceBlock() {
auto packet = std::static_pointer_cast<Packet>(packetPlace);
PUSH_EVENT("SendPacket", packet);
-} \ No newline at end of file
+}
+
+void GameState::PerformRespawn() {
+ auto packetPerformRespawn = std::make_shared<PacketClientStatus>(0);
+ PUSH_EVENT("SendPacket", std::static_pointer_cast<Packet>(packetPerformRespawn));
+}
diff --git a/src/GameState.hpp b/src/GameState.hpp
index 0ca858f..ca2ea81 100644
--- a/src/GameState.hpp
+++ b/src/GameState.hpp
@@ -75,6 +75,8 @@ class GameState {
bool receivedFirstPlayerPosAndLook = false;
+ std::shared_ptr<PacketRespawn> packetRespawn;
+
public:
void Update(double deltaTime);
@@ -89,6 +91,8 @@ public:
void PlaceBlock();
+ void PerformRespawn();
+
enum MoveType {
FORWARD, BACKWARD, LEFT, RIGHT, JUMP
};
diff --git a/src/Plugin.cpp b/src/Plugin.cpp
index ce995fc..98df8c3 100644
--- a/src/Plugin.cpp
+++ b/src/Plugin.cpp
@@ -11,6 +11,8 @@
#include "Event.hpp"
#include "AssetManager.hpp"
#include "Settings.hpp"
+#include "DebugInfo.hpp"
+#include "Chat.hpp"
struct Plugin {
@@ -22,6 +24,7 @@ struct Plugin {
const std::function<void(std::string)> onChangeState;
const std::function<void(double)> onTick;
const std::function<BlockInfo(Vector)> onRequestBlockInfo;
+ const std::function<void(Chat, int)> onChatMessage;
};
@@ -41,6 +44,7 @@ namespace PluginApi {
plugin["onChangeState"].get_or(std::function<void(std::string)>()),
plugin["onTick"].get_or(std::function<void(double)>()),
plugin["onRequestBlockInfo"].get_or(std::function<BlockInfo(Vector)>()),
+ plugin["onChatMessage"].get_or(std::function<void(Chat, int)>()),
};
plugins.push_back(nativePlugin);
LOG(INFO)<<"Loading plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name);
@@ -108,6 +112,29 @@ namespace PluginApi {
void SettingsUpdate() {
PUSH_EVENT("SettingsUpdate", 0);
}
+
+ int GetDebugValue(int valId) {
+ switch (valId) {
+ case 0:
+ return DebugInfo::totalSections;
+ case 1:
+ return DebugInfo::renderSections;
+ case 2:
+ return DebugInfo::readyRenderer;
+ case 3:
+ return DebugInfo::gameThreadTime;
+ case 4:
+ return DebugInfo::renderFaces;
+ case 5:
+ return DebugInfo::culledSections;
+ default:
+ return 0;
+ }
+ }
+
+ void SendChatMessage(const std::string& msg) {
+ PUSH_EVENT("SendChatMessage", msg);
+ }
}
int LoadFileRequire(lua_State* L) {
@@ -154,7 +181,8 @@ void PluginSystem::Init() {
"GetGameStatus", &GameState::GetGameStatus,
"GetPlayerStatus", &GameState::GetPlayerStatus,
"GetSelectionStatus", &GameState::GetSelectionStatus,
- "GetInventory", &GameState::GetInventory);
+ "GetInventory", &GameState::GetInventory,
+ "PerformRespawn", &GameState::PerformRespawn);
lua.new_usertype<TimeStatus>("TimeStatus",
"interpolatedTimeOfDay", &TimeStatus::interpolatedTimeOfDay,
@@ -194,6 +222,8 @@ void PluginSystem::Init() {
"GetEntitiesList", &World::GetEntitiesList,
"GetEntity",&World::GetEntityPtr,
"Raycast", &World::Raycast,
+ "GetBlockLight", sol::resolve<unsigned char(Vector)const>(&World::GetBlockLight),
+ "GetBlockSkyLight", sol::resolve<unsigned char(Vector)const>(&World::GetBlockSkyLight),
"GetBlockId", &World::GetBlockId,
"SetBlockId", &World::SetBlockId);
@@ -242,6 +272,9 @@ void PluginSystem::Init() {
"GetDeltaS", &LoopExecutionTimeController::GetDeltaS,
"GetRealDeltaS", &LoopExecutionTimeController::GetRealDeltaS);
+ lua.new_usertype<Chat>("Chat",
+ "ToPlainText", &Chat::ToPlainText);
+
sol::table apiTable = lua["AC"].get_or_create<sol::table>();
sol::table apiSettings = lua["AC"]["Settings"].get_or_create<sol::table>();
@@ -268,6 +301,9 @@ void PluginSystem::Init() {
apiSettings["WriteDouble"] = Settings::WriteDouble;
apiTable["SettingsUpdate"] = PluginApi::SettingsUpdate;
apiTable["GetTime"] = GetTime;
+ apiTable["GetBlockInfo"] = GetBlockInfo;
+ apiTable["GetDebugValue"] = PluginApi::GetDebugValue;
+ apiTable["SendChatMessage"] = PluginApi::SendChatMessage;
}
lua_State* PluginSystem::GetLuaState() {
@@ -331,3 +367,17 @@ BlockInfo PluginSystem::RequestBlockInfo(Vector blockPos) {
}
return ret;
}
+
+void PluginSystem::CallOnChatMessage(const Chat& chat, int position) {
+ OPTICK_EVENT();
+ for (Plugin& plugin : plugins) {
+ if (plugin.onRequestBlockInfo && plugin.errors < 10)
+ try {
+ plugin.onChatMessage(chat, position);
+ }
+ catch (sol::error& e) {
+ LOG(ERROR) << e.what();
+ plugin.errors++;
+ }
+ }
+}
diff --git a/src/Plugin.hpp b/src/Plugin.hpp
index 7af27a4..13b126e 100644
--- a/src/Plugin.hpp
+++ b/src/Plugin.hpp
@@ -6,6 +6,7 @@
class BlockInfo;
struct lua_State;
+class Chat;
namespace PluginSystem {
void Init();
@@ -19,4 +20,6 @@ namespace PluginSystem {
void CallOnTick(double deltaTime);
BlockInfo RequestBlockInfo(Vector blockPos);
+
+ void CallOnChatMessage(const Chat& chat, int position);
} \ No newline at end of file
diff --git a/src/Render.cpp b/src/Render.cpp
index bee8ffb..896a05f 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -4,6 +4,7 @@
#include <optick.h>
#include <RmlUi/Core.h>
#include <RmlUi/Lua.h>
+#include <RmlUi/Debugger.h>
#include "Shader.hpp"
#include "AssetManager.hpp"
@@ -28,7 +29,8 @@ const std::map<SDL_Keycode, Rml::Input::KeyIdentifier> keyMapping = {
{SDLK_RIGHT, Rml::Input::KI_RIGHT},
{SDLK_UP, Rml::Input::KI_UP},
{SDLK_DOWN, Rml::Input::KI_DOWN},
- {SDLK_TAB, Rml::Input::KI_TAB}
+ {SDLK_TAB, Rml::Input::KI_TAB},
+ {SDLK_RETURN, Rml::Input::KI_RETURN}
};
inline int ConvertKeymodsSdlToRml(unsigned short keyMods) {
@@ -287,9 +289,19 @@ void Render::HandleEvents() {
if (state == State::Playing) {
SetState(State::Chat);
}
- else if (state == State::Chat) {
- SetState(State::Playing);
- }
+ break;
+ }
+
+ case SDL_SCANCODE_F4:
+ hideRml = !hideRml;
+ break;
+
+ case SDL_SCANCODE_F8:
+ Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
+ break;
+
+ case SDL_SCANCODE_F7: {
+ SetMouseCapture(!isMouseCaptured);
break;
}
@@ -358,6 +370,11 @@ void Render::HandleEvents() {
break;
}
+ case SDL_MOUSEWHEEL: {
+ rmlContext->ProcessMouseWheel(-event.wheel.y, rmlKeymods);
+ break;
+ }
+
case SDL_TEXTINPUT: {
rmlContext->ProcessTextInput(Rml::String(event.text.text));
break;
@@ -417,7 +434,8 @@ void Render::Update() {
void Render::RenderGui() {
OPTICK_EVENT();
- rmlContext->Render();
+ if (!hideRml)
+ rmlContext->Render();
}
void Render::InitEvents() {
@@ -462,12 +480,6 @@ void Render::InitEvents() {
SetState(State::Loading);
});
- listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) {
- auto data = eventData.get<std::tuple<Chat, unsigned char>>();
- std::string msg = "(" + std::to_string((int)std::get<1>(data)) + ") " + (std::get<0>(data).ToPlainText());
- chatMessages.push_back(msg);
- });
-
listener.RegisterHandler("StateUpdated", [this](const Event& eventData) {
switch (GetState()) {
case State::Playing:
@@ -498,6 +510,10 @@ void Render::InitEvents() {
PluginSystem::CallOnChangeState("Chat");
SetMouseCapture(false);
break;
+ case State::NeedRespawn:
+ PluginSystem::CallOnChangeState("NeedRespawn");
+ SetMouseCapture(false);
+ break;
}
});
@@ -563,4 +579,7 @@ void Render::InitRml() {
Rml::Lua::Initialise(PluginSystem::GetLuaState());
rmlContext = Rml::CreateContext("default", Rml::Vector2i(renderState.WindowWidth, renderState.WindowHeight));
+
+ if (!Rml::Debugger::Initialise(rmlContext))
+ LOG(WARNING) << "Rml debugger not initialized";
}
diff --git a/src/Render.hpp b/src/Render.hpp
index 4f993c3..9a9feee 100644
--- a/src/Render.hpp
+++ b/src/Render.hpp
@@ -37,7 +37,6 @@ class Render {
float sensetivity = 0.1f;
bool isWireframe = false;
std::unique_ptr<Framebuffer> framebuffer;
- std::vector<std::string> chatMessages;
EventListener listener;
std::string stateString;
std::unique_ptr<RmlRenderInterface> rmlRender;
@@ -45,6 +44,7 @@ class Render {
std::unique_ptr<RmlFileInterface> rmlFile;
Rml::Context* rmlContext;
unsigned short sdlKeyMods = 0;
+ bool hideRml = false;
void SetMouseCapture(bool IsCaptured);
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp
index e3ef738..6996762 100644
--- a/src/RendererWorld.cpp
+++ b/src/RendererWorld.cpp
@@ -428,7 +428,7 @@ void RendererWorld::Render(RenderState & renderState) {
section.second.Render(renderState);
renderedFaces += section.second.numOfFaces;
}
- this->culledSections = culledSections;
+ DebugInfo::culledSections = culledSections;
DebugInfo::renderFaces = renderedFaces;
glCheckError();
}
diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp
index d031179..85cb736 100644
--- a/src/RendererWorld.hpp
+++ b/src/RendererWorld.hpp
@@ -60,5 +60,4 @@ public:
void Update(double timeToUpdate);
- int culledSections = 0;
-}; \ No newline at end of file
+};
diff --git a/src/Rml.cpp b/src/Rml.cpp
index 6ed83c1..179d4b9 100644
--- a/src/Rml.cpp
+++ b/src/Rml.cpp
@@ -117,11 +117,15 @@ void RmlRenderInterface::RenderGeometry(Rml::Vertex* vertices, int num_vertices,
}
void RmlRenderInterface::EnableScissorRegion(bool enable) {
-
+ if (enable)
+ glEnable(GL_SCISSOR_TEST);
+ else
+ glDisable(GL_SCISSOR_TEST);
}
void RmlRenderInterface::SetScissorRegion(int x, int y, int width, int height) {
-
+ glScissor(x, vpHeight - (y + height), width, height);
+ glCheckError();
}
bool RmlRenderInterface::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) {
@@ -161,6 +165,8 @@ void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHei
AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->SetUniform("viewportSize", windowWidth, windowHeight);
AssetManager::GetAsset<AssetShader>("/altcraft/shaders/rmltex")->shader->SetUniform("fontTexture", 0);
glCheckError();
+ vpWidth = windowWidth;
+ vpHeight = windowHeight;
}
Rml::FileHandle RmlFileInterface::Open(const Rml::String& path) {
diff --git a/src/Rml.hpp b/src/Rml.hpp
index 6e4d857..edcdc8b 100644
--- a/src/Rml.hpp
+++ b/src/Rml.hpp
@@ -34,6 +34,7 @@ class RmlRenderInterface : public Rml::RenderInterface {
GLuint Vao, Vbo, Ebo;
+ unsigned int vpWidth, vpHeight;
public:
RmlRenderInterface(RenderState &renderState);