From eb096f1a7a2d60b291ed9826ff43ad503ac37542 Mon Sep 17 00:00:00 2001 From: aurailus <100Toby1@gmail.com> Date: Wed, 2 Jan 2019 01:34:32 -0800 Subject: [PATCH] Update Lua Api C++ code to make it more extendable. Added register_blockmodel to the api, unfinished. --- CMakeLists.txt | 6 +- lua/file.lua | 139 +++++++++++++++++- zeus/game/GameInstance.cpp | 13 +- zeus/game/GameInstance.h | 1 + zeus/lua_api/LRegisterBlock.cpp | 55 ------- zeus/lua_api/LuaApi.cpp | 10 ++ zeus/lua_api/LuaApi.h | 21 +++ zeus/lua_api/LuaParser.cpp | 6 +- zeus/lua_api/LuaParser.h | 3 + zeus/lua_api/l_register_block.cpp | 59 ++++++++ .../{LRegisterBlock.h => l_register_block.h} | 9 +- zeus/lua_api/l_register_blockmodel.cpp | 41 ++++++ zeus/lua_api/l_register_blockmodel.h | 19 +++ 13 files changed, 307 insertions(+), 75 deletions(-) delete mode 100644 zeus/lua_api/LRegisterBlock.cpp create mode 100644 zeus/lua_api/LuaApi.cpp create mode 100644 zeus/lua_api/LuaApi.h create mode 100644 zeus/lua_api/l_register_block.cpp rename zeus/lua_api/{LRegisterBlock.h => l_register_block.h} (56%) create mode 100644 zeus/lua_api/l_register_blockmodel.cpp create mode 100644 zeus/lua_api/l_register_blockmodel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 57cd5dde..690a504b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,12 +64,12 @@ add_executable(zeus zeus/engine/graphics/Renderer.h zeus/lua_api/LuaParser.cpp zeus/lua_api/LuaParser.h - zeus/lua_api/LRegisterBlock.cpp - zeus/lua_api/LRegisterBlock.h + zeus/lua_api/l_register_block.cpp + zeus/lua_api/l_register_block.h zeus/engine/graphics/TextBuilder.cpp zeus/engine/graphics/TextBuilder.h zeus/engine/graphics/TextBuilder.cpp zeus/engine/graphics/HudText.cpp - zeus/engine/graphics/HudText.h zeus/game/gui/DebugGui.cpp zeus/game/gui/DebugGui.h zeus/game/world/Player.cpp zeus/game/world/Player.h zeus/engine/Ray.cpp zeus/engine/Ray.h) + zeus/engine/graphics/HudText.h zeus/game/gui/DebugGui.cpp zeus/game/gui/DebugGui.h zeus/game/world/Player.cpp zeus/game/world/Player.h zeus/engine/Ray.cpp zeus/engine/Ray.h zeus/lua_api/l_register_blockmodel.cpp zeus/lua_api/l_register_blockmodel.h zeus/lua_api/LuaApi.cpp zeus/lua_api/LuaApi.h) target_link_libraries(zeus ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread lua dl) \ No newline at end of file diff --git a/lua/file.lua b/lua/file.lua index 11517a4c..4191f1d8 100644 --- a/lua/file.lua +++ b/lua/file.lua @@ -1,16 +1,141 @@ --- --- Register basic blocks --- +-- Dump function +function dump(tbl, indent) + if not indent then indent = 0 end + for k, v in pairs(tbl) do + if type(k) == "number" then + k = "[" .. k .. "]" + end + local indentString = string.rep(" ", indent); + formatting = indentString .. k .. " = " + if type(v) == "table" then + print(formatting .. "{") + dump(v, indent+1) + print(indentString .. "}") + elseif type(v) == 'boolean' then + print(formatting .. tostring(v)) + else + print(formatting .. v) + end + end +end +--[[ + +**Mesh Creation** + +The function zeus.register_blockmodel creates a blockmodel for use by blocks. +The first argument is the name of the model, which should be prefixed by the mod name and then a colon. +The next argument is a table of mesh parts. A mesh part is a table with mesh information in it. +The allowed information is as follows + +face: which part of the block this mesh part is for, accepted strings are: +"top", "bottom", "left", "right", "front", "back", "nocull" + +tex: The index of the textures array to use for texturing the mesh part + +points: the points defining the mesh part. The format is defined by the mode property. + +mode: Must be one of the following, default is "simple" + +- simple: +The first five points are the top left vertice, the next five are the bottom right. + +- vertex: +Define 5 positional coordinates for each face. + +--]] + +-- print(#zeus.registered_blockmodels); + +-- Create cube model +zeus.register_blockmodel("default:cube", { + { + face = "left", + tex = 2, + points = { + 0, 0, 0, 0, 1, + 0, 0, 1, 1, 1, + 0, 1, 1, 1, 0, + 0, 1, 0, 0, 0 + } + }, { + face = "right", + tex = 3, + points = { + 1, 0, 0, 1, 0, + 0, 0, 1, 1, 1, + 0, 1, 1, 1, 0, + 0, 1, 0, 0, 0 + } + }, { + face = "top", + tex = 0, + points = { + 0, 1, 0, 0, 0, + 0, 1, 1, 0, 1, + 1, 1, 1, 1, 1, + 1, 1, 0, 1, 0 + } + }, { + face = "bottom", + tex = 1, + points = { + 0, 0, 0, 0, 0, + 1, 0, 0, 1, 0, + 1, 0, 1, 1, 1, + 0, 0, 1, 0, 1 + } + }, { + face = "front", + tex = 4, + points = { + 0, 0, 1, 0, 1, + 1, 0, 1, 1, 1, + 1, 1, 1, 1, 0, + 0, 1, 1, 0, 0 + } + }, { + face = "back", + tex = 5, + points = { + 0, 0, 0, 0, 1, + 0, 1, 0, 0, 0, + 1, 1, 0, 1, 0, + 1, 0, 0, 1, 1 + } + } +}) + +-- dump(zeus.registered_blockmodels["default:cube"]); + +--[[ + +**Block Creation** + +The function zeus.register_block creates a block in the game. +The first argument is the name of the block, which should be prefixed by the mod name and a colon. +The next argument is a table of properties. These are the following accepted properties: + +name: The display name of the block + +model: The blockModel of the block, will default to "default:cube" + +textures: The list of textures for the blockmodel. Which textures go where are defined by the blockmodel. + +--]] + +-- Ignore this, it is temporary -- Air zeus.register_block("_:air", { - name = "Air", - textures = {"_missing"} + name = "Air", + model = "default:cube", + textures = {"_missing"} }) -- Grass zeus.register_block("default:grass", { name = "Grass", + model = "default:cube", textures = { "default_grass_top", "default_dirt", @@ -21,11 +146,13 @@ zeus.register_block("default:grass", { -- Dirt zeus.register_block('default:dirt', { name = "Dirt", + model = "default:cube", textures = {"default_dirt"} }) -- Stone zeus.register_block('default:stone', { name = "Stone", + model = "default:cube", textures = {"default_cobblestone"} -}) \ No newline at end of file +}) diff --git a/zeus/game/GameInstance.cpp b/zeus/game/GameInstance.cpp index 29051f47..9cb3dec1 100644 --- a/zeus/game/GameInstance.cpp +++ b/zeus/game/GameInstance.cpp @@ -3,8 +3,8 @@ // #include "GameInstance.h" -#include "../lua_api/LRegisterBlock.h" -#include "../engine/Ray.h" +#include "../lua_api/l_register_block.h" +#include "../lua_api/l_register_blockmodel.h" GameInstance::GameInstance() = default; @@ -16,10 +16,10 @@ void GameInstance::initialize(Renderer* renderer) { LuaParser p; p.init(); - auto Z = p.getModule(); //Register APIs here - LRegisterBlock(this).regApi(Z); + l_register_block(this, &p); + l_register_blockmodel(this, &p); p.doFile("../lua/file.lua"); @@ -66,7 +66,10 @@ void GameInstance::initialize(Renderer* renderer) { delete crossVerts; delete crossInds; - crosshair->setPosition(glm::vec3(renderer->getWindow()->getBufferWidth()/2, renderer->getWindow()->getBufferHeight()/2, 0)); + float xx = renderer->getWindow()->getBufferWidth()/2; + float yy = renderer->getWindow()->getBufferHeight()/2; + + crosshair->setPosition(glm::vec3(xx, yy, 0)); crosshair->setScale(22); guiEntities.push_back(crosshair); diff --git a/zeus/game/GameInstance.h b/zeus/game/GameInstance.h index 01f9c811..f862d3c7 100644 --- a/zeus/game/GameInstance.h +++ b/zeus/game/GameInstance.h @@ -16,6 +16,7 @@ #include "../engine/graphics/HudText.h" #include "gui/DebugGui.h" #include "world/Player.h" +#include "../engine/Ray.h" class GameInstance { public: diff --git a/zeus/lua_api/LRegisterBlock.cpp b/zeus/lua_api/LRegisterBlock.cpp deleted file mode 100644 index 1f87bf52..00000000 --- a/zeus/lua_api/LRegisterBlock.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// -// Created by aurailus on 18/12/18. -// - -#include "LRegisterBlock.h" - -LRegisterBlock::LRegisterBlock(GameInstance *game) { - this->game = game; -} - -void LRegisterBlock::regApi(sol::table *Z) { - Z->set_function("register_block", &LRegisterBlock::api, this); -} - -//Api Function -void LRegisterBlock::api(std::string identifier, sol::table data) { - if (identifier.length() == 0) { - printf("Tried to initialize block without identifier!\n"); - return; - } - - auto name = data.get>("name"); - auto texTable = data.get>("textures"); - - if (name && texTable) { -// printf("Registering block %s!\n", name->c_str()); - - auto textures = std::vector(); - - int size = texTable->size(); - - //LUA is 1-indexed - for (int i = 1; i <= 6; i++) { - //Get either the texture for this index or the last texture. - //This is done so you can implicitly specify how your block is textured like so: - //1 Texture: all sides take this texture - //3 Textures: top and bottom have seperate textures, all side faces share a third texture - //6 Textures: each face has it's own texture. - - auto texture = texTable->get(min(i, size)); - textures.push_back(texture); - } - - BlockModel* model = BlockModel::Square(textures[0].c_str(), textures[1].c_str(), textures[2].c_str(), - textures[3].c_str(), textures[4].c_str(), textures[5].c_str(), game->textureAtlas); - BlockDef* def = new BlockDef(identifier, model); - - game->blockAtlas->registerBlock(def); - - return; - } - else { - printf("Tried to initialize block without required arguments!\n"); - } -} diff --git a/zeus/lua_api/LuaApi.cpp b/zeus/lua_api/LuaApi.cpp new file mode 100644 index 00000000..77a8a21c --- /dev/null +++ b/zeus/lua_api/LuaApi.cpp @@ -0,0 +1,10 @@ +// +// Created by aurailus on 01/01/19. +// + +#include "LuaApi.h" + +LuaApi::LuaApi(GameInstance *game, LuaParser *parser) { + this->game = game; + this->parser = parser; +} diff --git a/zeus/lua_api/LuaApi.h b/zeus/lua_api/LuaApi.h new file mode 100644 index 00000000..35ad2cb1 --- /dev/null +++ b/zeus/lua_api/LuaApi.h @@ -0,0 +1,21 @@ +// +// Created by aurailus on 01/01/19. +// + +#ifndef ZEUS_LUAAPI_H +#define ZEUS_LUAAPI_H + + +#include "../game/GameInstance.h" +#include "LuaParser.h" + +class LuaApi { +public: + LuaApi(GameInstance* game, LuaParser* parser); + + GameInstance* game; + LuaParser* parser; +}; + + +#endif //ZEUS_LUAAPI_H diff --git a/zeus/lua_api/LuaParser.cpp b/zeus/lua_api/LuaParser.cpp index 79893e7f..f72985e9 100644 --- a/zeus/lua_api/LuaParser.cpp +++ b/zeus/lua_api/LuaParser.cpp @@ -10,7 +10,7 @@ //} void LuaParser::init() { - L.open_libraries(sol::lib::base, sol::lib::package); + L.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string); Z = L.create_table(); L["zeus"] = Z; @@ -20,6 +20,10 @@ sol::table* LuaParser::getModule() { return &Z; } +sol::state* LuaParser::getState() { + return &L; +} + void LuaParser::doFile(std::string file) { L.script_file(file); } \ No newline at end of file diff --git a/zeus/lua_api/LuaParser.h b/zeus/lua_api/LuaParser.h index fc2a51d0..4c7eab5f 100644 --- a/zeus/lua_api/LuaParser.h +++ b/zeus/lua_api/LuaParser.h @@ -16,7 +16,10 @@ public: LuaParser() = default; void init(); + sol::table* getModule(); + sol::state* getState(); + void doFile(std::string file); ~LuaParser() = default; diff --git a/zeus/lua_api/l_register_block.cpp b/zeus/lua_api/l_register_block.cpp new file mode 100644 index 00000000..72060cdb --- /dev/null +++ b/zeus/lua_api/l_register_block.cpp @@ -0,0 +1,59 @@ +// +// Created by aurailus on 18/12/18. +// + +#include "l_register_block.h" + +//Add a block to the game. +//The first variable is the identifer, which is used internally and by mods to reference the block. +//The second variable is a table containing all the block's data. This is used to define properties of the nodeblock. +void l_register_block::api(std::string identifier, sol::table data) { + + if (identifier.length() == 0) { + printf("Tried to initialize block without identifier!\n"); + return; + } + + auto name = data.get>("name"); + auto texTable = data.get>("textures"); + auto modelName = data.get_or("model", "default:cube"); + + if (!name || !texTable) { + printf("Tried to initialize block without data."); + return; + } + + sol::table models = (*parser->getModule())["registered_blockmodels"]; + auto modelOptional = models.get>(modelName); + + if (!modelOptional) { + printf("Tried to initialize using invalid model %s\n", modelName.c_str()); + return; + } + + auto textures = std::vector(); + int size = texTable->size(); + + //LUA is 1-indexed + for (int i = 1; i <= 6; i++) { + //Get either the texture for this index or the last texture. + //This is done so you can implicitly specify how your block is textured like so: + //1 Texture: all sides take this texture + //3 Textures: top and bottom have seperate textures, all side faces share a third texture + //6 Textures: each face has it's own texture. + + auto texture = texTable->get(min(i, size)); + textures.push_back(texture); + } + + BlockModel* model = BlockModel::Square(textures[0].c_str(), textures[1].c_str(), textures[2].c_str(), + textures[3].c_str(), textures[4].c_str(), textures[5].c_str(), game->textureAtlas); + + BlockDef* def = new BlockDef(identifier, model); + + game->blockAtlas->registerBlock(def); +} + +l_register_block::l_register_block(GameInstance *game, LuaParser *parser) : LuaApi(game, parser) { + parser->getModule()->set_function("register_block", &l_register_block::api, this); +} diff --git a/zeus/lua_api/LRegisterBlock.h b/zeus/lua_api/l_register_block.h similarity index 56% rename from zeus/lua_api/LRegisterBlock.h rename to zeus/lua_api/l_register_block.h index eed1d799..d748578a 100644 --- a/zeus/lua_api/LRegisterBlock.h +++ b/zeus/lua_api/l_register_block.h @@ -5,15 +5,14 @@ #ifndef ZEUS_LREGISTERBLOCK_H #define ZEUS_LREGISTERBLOCK_H -#include "../game/GameInstance.h" +#include "LuaApi.h" -class LRegisterBlock { +class l_register_block : LuaApi { public: - explicit LRegisterBlock(GameInstance* game); - void regApi(sol::table* Z); + l_register_block(GameInstance* game, LuaParser* parser); + private: void api(std::string identifer, sol::table data); - GameInstance* game; }; diff --git a/zeus/lua_api/l_register_blockmodel.cpp b/zeus/lua_api/l_register_blockmodel.cpp new file mode 100644 index 00000000..5c8b03af --- /dev/null +++ b/zeus/lua_api/l_register_blockmodel.cpp @@ -0,0 +1,41 @@ +// +// Created by aurailus on 31/12/18. +// + +#include "l_register_blockmodel.h" + +void l_register_blockmodel::api(std::string identifier, sol::table data) { + if (identifier.length() == 0) { + printf("Tried to initialize block without identifier!\n"); + return; + } + + (*parser->getModule())["registered_blockmodels"][identifier] = data; + +// for (auto it : data) { +// sol::table meshPart = it.second; +// +// auto pointsOptional = meshPart.get>("points"); +// +// if (pointsOptional) { +// +// sol::table points = *pointsOptional; +// float tex = meshPart.get_or("tex", 0); +// std::string face = meshPart.get_or("face", "nocull"); +// std::string mode = meshPart.get_or("mode", "simple"); +// +// std::cout << meshPart.get("face") << std::endl; +// } +// else { +// printf("Invalid MeshPart!\n"); +// } +// } +} + +l_register_blockmodel::l_register_blockmodel(GameInstance* game, LuaParser* parser) : LuaApi(game, parser) { + + auto registered_blockmodels = parser->getState()->create_table(); + (*parser->getModule())["registered_blockmodels"] = registered_blockmodels; + + parser->getModule()->set_function("register_blockmodel", &l_register_blockmodel::api, this); +} \ No newline at end of file diff --git a/zeus/lua_api/l_register_blockmodel.h b/zeus/lua_api/l_register_blockmodel.h new file mode 100644 index 00000000..6b41a640 --- /dev/null +++ b/zeus/lua_api/l_register_blockmodel.h @@ -0,0 +1,19 @@ +// +// Created by aurailus on 31/12/18. +// + +#ifndef ZEUS_LREGISTERMODEL_H +#define ZEUS_LREGISTERMODEL_H + +#include "LuaApi.h" + +class l_register_blockmodel : LuaApi { +public: + l_register_blockmodel(GameInstance* game, LuaParser* parser); + +private: + void api(std::string identifer, sol::table data); +}; + + +#endif //ZEUS_LREGISTERMODEL_H