LuaParser rewrite and zeus.register_block Lua API.
- Using SOL2 instead of LuaC - Updated file.lua to include some block definitions.master
parent
67b3a5086e
commit
9812d6adcf
|
@ -9,6 +9,7 @@ include_directories(lib/glfw_linux/include)
|
||||||
include_directories(lib/glm)
|
include_directories(lib/glm)
|
||||||
include_directories(lib/stb_image)
|
include_directories(lib/stb_image)
|
||||||
include_directories(lib/cute)
|
include_directories(lib/cute)
|
||||||
|
include_directories(lib/sol)
|
||||||
#include_directories(Libraries/boost_1_68_0)
|
#include_directories(Libraries/boost_1_68_0)
|
||||||
|
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
|
@ -17,6 +18,54 @@ find_package(glfw3 REQUIRED)
|
||||||
|
|
||||||
link_directories(lib/glew/lib)
|
link_directories(lib/glew/lib)
|
||||||
|
|
||||||
add_executable(zeus zeus/Main.cpp zeus/engine/graphics/Mesh.cpp zeus/engine/graphics/Mesh.h zeus/engine/PerlinNoise.cpp zeus/engine/PerlinNoise.h zeus/engine/Entity.cpp zeus/engine/Entity.h zeus/engine/graphics/Shader.cpp zeus/engine/graphics/Shader.h zeus/engine/Window.cpp zeus/engine/Window.h zeus/engine/Camera.cpp zeus/engine/Camera.h zeus/engine/graphics/Texture.cpp zeus/engine/graphics/Texture.h zeus/mesh/MeshGenerator.cpp zeus/mesh/MeshGenerator.h zeus/engine/Timer.cpp zeus/engine/Timer.h zeus/blocks/BlockAtlas.cpp zeus/blocks/BlockAtlas.h zeus/blocks/BlockDef.cpp zeus/blocks/BlockDef.h zeus/mesh/MeshPart.cpp zeus/mesh/MeshPart.h zeus/mesh/MeshMod.h zeus/mesh/Vertex.cpp zeus/mesh/Vertex.h zeus/mesh/BlockModel.cpp zeus/mesh/BlockModel.h zeus/engine/TextureAtlas.cpp zeus/engine/TextureAtlas.h zeus/world/World.cpp zeus/world/World.h zeus/world/BlockChunk.cpp zeus/world/BlockChunk.h zeus/engine/helpers/ArrayTrans3D.h zeus/world/MeshChunk.cpp zeus/world/MeshChunk.h zeus/world/GameInstance.cpp zeus/world/GameInstance.h zeus/engine/graphics/Renderer.cpp zeus/engine/graphics/Renderer.h zeus/lua_api/LuaParser.cpp zeus/lua_api/LuaParser.h zeus/lua_api/LuaApiInterface.h zeus/lua_api/apis/l_Average.cpp zeus/lua_api/apis/l_Average.h)
|
add_executable(zeus
|
||||||
|
zeus/Main.cpp
|
||||||
|
zeus/engine/graphics/Mesh.cpp
|
||||||
|
zeus/engine/graphics/Mesh.h
|
||||||
|
zeus/engine/PerlinNoise.cpp
|
||||||
|
zeus/engine/PerlinNoise.h
|
||||||
|
zeus/engine/Entity.cpp
|
||||||
|
zeus/engine/Entity.h
|
||||||
|
zeus/engine/graphics/Shader.cpp
|
||||||
|
zeus/engine/graphics/Shader.h
|
||||||
|
zeus/engine/Window.cpp
|
||||||
|
zeus/engine/Window.h
|
||||||
|
zeus/engine/Camera.cpp
|
||||||
|
zeus/engine/Camera.h
|
||||||
|
zeus/engine/graphics/Texture.cpp
|
||||||
|
zeus/engine/graphics/Texture.h
|
||||||
|
zeus/mesh/MeshGenerator.cpp
|
||||||
|
zeus/mesh/MeshGenerator.h
|
||||||
|
zeus/engine/Timer.cpp
|
||||||
|
zeus/engine/Timer.h
|
||||||
|
zeus/blocks/BlockAtlas.cpp
|
||||||
|
zeus/blocks/BlockAtlas.h
|
||||||
|
zeus/blocks/BlockDef.cpp
|
||||||
|
zeus/blocks/BlockDef.h
|
||||||
|
zeus/mesh/MeshPart.cpp
|
||||||
|
zeus/mesh/MeshPart.h
|
||||||
|
zeus/mesh/MeshMod.h
|
||||||
|
zeus/mesh/Vertex.cpp
|
||||||
|
zeus/mesh/Vertex.h
|
||||||
|
zeus/mesh/BlockModel.cpp
|
||||||
|
zeus/mesh/BlockModel.h
|
||||||
|
zeus/engine/TextureAtlas.cpp
|
||||||
|
zeus/engine/TextureAtlas.h
|
||||||
|
zeus/world/World.cpp
|
||||||
|
zeus/world/World.h
|
||||||
|
zeus/world/BlockChunk.cpp
|
||||||
|
zeus/world/BlockChunk.h
|
||||||
|
zeus/engine/helpers/ArrayTrans3D.h
|
||||||
|
zeus/world/MeshChunk.cpp
|
||||||
|
zeus/world/MeshChunk.h
|
||||||
|
zeus/world/GameInstance.cpp
|
||||||
|
zeus/world/GameInstance.h
|
||||||
|
zeus/engine/graphics/Renderer.cpp
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(zeus ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread lua dl)
|
target_link_libraries(zeus ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread lua dl)
|
28
lua/file.lua
28
lua/file.lua
|
@ -1,9 +1,25 @@
|
||||||
print("----\nPrinting the Zeus Global table's contents:\n----")
|
--
|
||||||
|
-- Register basic blocks
|
||||||
|
--
|
||||||
|
|
||||||
for function_name, _ in pairs(zeus) do
|
-- Grass
|
||||||
print(function_name);
|
zeus.register_block("default:grass", {
|
||||||
end
|
name = "Grass",
|
||||||
|
textures = {
|
||||||
|
"default_grass_top",
|
||||||
|
"default_dirt",
|
||||||
|
"default_grass_side"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
print("----")
|
-- Dirt
|
||||||
|
zeus.register_block('default:dirt', {
|
||||||
|
name = "Dirt",
|
||||||
|
textures = {"default_dirt"}
|
||||||
|
})
|
||||||
|
|
||||||
print(zeus.average(1, 2, 3, 4, 5));
|
-- Stone
|
||||||
|
zeus.register_block('default:stone', {
|
||||||
|
name = "Stone",
|
||||||
|
textures = {"default_stone"}
|
||||||
|
})
|
||||||
|
|
|
@ -11,6 +11,7 @@ BlockAtlas::BlockAtlas(TextureAtlas *textureAtlas) {
|
||||||
|
|
||||||
void BlockAtlas::registerBlock(BlockDef* def) {
|
void BlockAtlas::registerBlock(BlockDef* def) {
|
||||||
definitions.push_back(def);
|
definitions.push_back(def);
|
||||||
|
std::cout << "Registered block" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockDef* BlockAtlas::getBlock(int id) {
|
BlockDef* BlockAtlas::getBlock(int id) {
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
//
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
//
|
||||||
|
// Created by aurailus on 18/12/18.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef ZEUS_LREGISTERBLOCK_H
|
||||||
|
#define ZEUS_LREGISTERBLOCK_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "../world/GameInstance.h"
|
||||||
|
|
||||||
|
class LRegisterBlock {
|
||||||
|
public:
|
||||||
|
explicit LRegisterBlock(GameInstance* game);
|
||||||
|
void regApi(sol::table* Z);
|
||||||
|
private:
|
||||||
|
void api(std::string identifer, sol::table data);
|
||||||
|
GameInstance* game;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ZEUS_LREGISTERBLOCK_H
|
|
@ -1,16 +0,0 @@
|
||||||
//
|
|
||||||
// Created by aurailus on 17/12/18.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef ZEUS_LUAAPIINTERFACE_H
|
|
||||||
#define ZEUS_LUAAPIINTERFACE_H
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include "LuaParser.h"
|
|
||||||
|
|
||||||
class LuaApiInterface {
|
|
||||||
public:
|
|
||||||
virtual void bind_methods(LuaParser* parser) {};
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //ZEUS_LUAAPIINTERFACE_H
|
|
|
@ -4,39 +4,22 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "LuaParser.h"
|
#include "LuaParser.h"
|
||||||
#include "apis/l_Average.h"
|
|
||||||
|
|
||||||
LuaParser::LuaParser(std::string luaFileLoc) {
|
//double LuaParser::average(int a, int b, int c) {
|
||||||
L = luaL_newstate();
|
// return (a + b + c) / (double)(3);
|
||||||
luaL_openlibs(L);
|
//}
|
||||||
|
|
||||||
regGlobalModule();
|
void LuaParser::init() {
|
||||||
|
L.open_libraries(sol::lib::base, sol::lib::package);
|
||||||
|
|
||||||
int err = luaL_dofile(L, luaFileLoc.c_str());
|
Z = L.create_table();
|
||||||
if (err != 0) {
|
L["zeus"] = Z;
|
||||||
std::cerr << "Failed to compile " << luaFileLoc << " with error code " << err << "." << std::endl;
|
|
||||||
std::cerr << lua_tostring(L, -1) << std::endl;
|
|
||||||
luaL_traceback(L, L, nullptr, 1);
|
|
||||||
std::cerr << lua_tostring(L, -1) << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
getchar(); // Just to pause
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaParser::regFunc(const char *functionName, lua_CFunction any) {
|
sol::table* LuaParser::getModule() {
|
||||||
lua_pushcfunction(L, any);
|
return &Z;
|
||||||
lua_setfield(L, -2, functionName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LuaParser::doFile(std::string file) {
|
||||||
void LuaParser::regGlobalModule() {
|
L.script_file(file);
|
||||||
lua_newtable(L);
|
|
||||||
|
|
||||||
l_Average().bind_methods(this);
|
|
||||||
|
|
||||||
lua_setglobal(L, "zeus");
|
|
||||||
}
|
|
||||||
|
|
||||||
LuaParser::~LuaParser() {
|
|
||||||
lua_close(L);
|
|
||||||
}
|
}
|
|
@ -5,24 +5,24 @@
|
||||||
#ifndef ZEUS_LUAPARSER_H
|
#ifndef ZEUS_LUAPARSER_H
|
||||||
#define ZEUS_LUAPARSER_H
|
#define ZEUS_LUAPARSER_H
|
||||||
|
|
||||||
#include <string>
|
//Should give tracebacks if there's a segfault
|
||||||
|
//#define SOL_CHECK_ARGUMENTS
|
||||||
|
|
||||||
extern "C" {
|
#include <string>
|
||||||
#include "lua.h"
|
#include <sol.hpp>
|
||||||
#include "lualib.h"
|
|
||||||
#include "lauxlib.h"
|
|
||||||
}
|
|
||||||
|
|
||||||
class LuaParser {
|
class LuaParser {
|
||||||
public:
|
public:
|
||||||
explicit LuaParser(std::string luaFileLoc);
|
LuaParser() = default;
|
||||||
|
|
||||||
void regGlobalModule();
|
void init();
|
||||||
void regFunc(const char *functionName, lua_CFunction any);
|
sol::table* getModule();
|
||||||
|
void doFile(std::string file);
|
||||||
|
|
||||||
~LuaParser();
|
~LuaParser() = default;
|
||||||
private:
|
private:
|
||||||
lua_State* L;
|
sol::state L;
|
||||||
|
sol::table Z;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
//
|
|
||||||
// Created by aurailus on 17/12/18.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "l_Average.h"
|
|
||||||
|
|
||||||
int average(lua_State *L) {
|
|
||||||
int n = lua_gettop(L);
|
|
||||||
double sum = 0;
|
|
||||||
|
|
||||||
for (int i = 1; i <= n; i++) {
|
|
||||||
sum += lua_tonumber(L, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushnumber(L, sum / n);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void l_Average::bind_methods(LuaParser* parser) {
|
|
||||||
parser->regFunc("average", &average);
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
//
|
|
||||||
// Created by aurailus on 17/12/18.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef ZEUS_AVERAGE_H
|
|
||||||
#define ZEUS_AVERAGE_H
|
|
||||||
|
|
||||||
|
|
||||||
#include "../LuaApiInterface.h"
|
|
||||||
|
|
||||||
class l_Average : public LuaApiInterface {
|
|
||||||
public:
|
|
||||||
void bind_methods(LuaParser* parser) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif //ZEUS_AVERAGE_H
|
|
|
@ -3,6 +3,7 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "GameInstance.h"
|
#include "GameInstance.h"
|
||||||
|
#include "../lua_api/LRegisterBlock.h"
|
||||||
|
|
||||||
GameInstance::GameInstance() = default;
|
GameInstance::GameInstance() = default;
|
||||||
|
|
||||||
|
@ -45,14 +46,16 @@ void GameInstance::initialize(Renderer* renderer) {
|
||||||
textureAtlas = new TextureAtlas("../tex");
|
textureAtlas = new TextureAtlas("../tex");
|
||||||
blockAtlas = new BlockAtlas(textureAtlas);
|
blockAtlas = new BlockAtlas(textureAtlas);
|
||||||
|
|
||||||
LuaParser p("../lua/file.lua");
|
LuaParser p;
|
||||||
|
p.init();
|
||||||
|
auto Z = p.getModule();
|
||||||
|
|
||||||
//TODO: Move block registration to lua code
|
//Register APIs here
|
||||||
auto* def = new BlockDef("grass", BlockModel::Square(
|
LRegisterBlock(this).regApi(Z);
|
||||||
"default_grass_top", "default_dirt", "default_grass_side",
|
|
||||||
"default_grass_side", "default_grass_side", "default_grass_side", textureAtlas));
|
|
||||||
blockAtlas->registerBlock(def);
|
|
||||||
|
|
||||||
|
p.doFile("../lua/file.lua");
|
||||||
|
|
||||||
|
//Build the world
|
||||||
//The world requires the blockAtlas for meshing and handling inputs.
|
//The world requires the blockAtlas for meshing and handling inputs.
|
||||||
world = new World(blockAtlas);
|
world = new World(blockAtlas);
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ public:
|
||||||
void draw();
|
void draw();
|
||||||
|
|
||||||
~GameInstance();
|
~GameInstance();
|
||||||
private:
|
public:
|
||||||
//The renderer contains the camera, window, and draw methods.
|
//The renderer contains the camera, window, and draw methods.
|
||||||
Renderer* renderer;
|
Renderer* renderer;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue