Update Lua Api C++ code to make it more extendable.

Added register_blockmodel to the api, unfinished.
master
aurailus 2019-01-02 01:34:32 -08:00
parent 2d9a00d0ef
commit eb096f1a7a
13 changed files with 307 additions and 75 deletions

View File

@ -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)

View File

@ -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"}
})
})

View File

@ -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);

View File

@ -16,6 +16,7 @@
#include "../engine/graphics/HudText.h"
#include "gui/DebugGui.h"
#include "world/Player.h"
#include "../engine/Ray.h"
class GameInstance {
public:

View File

@ -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<sol::optional<std::string>>("name");
auto texTable = data.get<sol::optional<sol::table>>("textures");
if (name && texTable) {
// printf("Registering block %s!\n", name->c_str());
auto textures = std::vector<std::string>();
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<std::string>(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");
}
}

10
zeus/lua_api/LuaApi.cpp Normal file
View File

@ -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;
}

21
zeus/lua_api/LuaApi.h Normal file
View File

@ -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

View File

@ -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);
}

View File

@ -16,7 +16,10 @@ public:
LuaParser() = default;
void init();
sol::table* getModule();
sol::state* getState();
void doFile(std::string file);
~LuaParser() = default;

View File

@ -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<sol::optional<std::string>>("name");
auto texTable = data.get<sol::optional<sol::table>>("textures");
auto modelName = data.get_or<std::string>("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<sol::optional<sol::table>>(modelName);
if (!modelOptional) {
printf("Tried to initialize using invalid model %s\n", modelName.c_str());
return;
}
auto textures = std::vector<std::string>();
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<std::string>(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);
}

View File

@ -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;
};

View File

@ -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<sol::optional<sol::table>>("points");
//
// if (pointsOptional) {
//
// sol::table points = *pointsOptional;
// float tex = meshPart.get_or<float>("tex", 0);
// std::string face = meshPart.get_or<std::string>("face", "nocull");
// std::string mode = meshPart.get_or<std::string>("mode", "simple");
//
// std::cout << meshPart.get<std::string>("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);
}

View File

@ -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