Began working on the lua API for the map

The basic framework is done.
This commit is contained in:
Kayne Ruse
2014-03-28 03:24:14 +11:00
parent 47684380a9
commit 38b603fc8f
15 changed files with 263 additions and 14 deletions
+1
View File
@@ -22,6 +22,7 @@ all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ)
$(MAKE) -C graphics
$(MAKE) -C map
$(MAKE) -C script
$(MAKE) -C network
$(MAKE) -C ui
+19 -2
View File
@@ -21,10 +21,27 @@
*/
#include "map_file_format.hpp"
void MapFileFormat::Load(Region** const ptr, int x, int y) {
void DummyFormat::Load(Region** const ptr, int x, int y) {
//TODO
}
void MapFileFormat::Save(Region* const ptr) {
void DummyFormat::Save(Region* const ptr) {
//TODO
}
/*
void VerboseFormat::Load(Region** const ptr, int x, int y) {
//TODO
}
void VerboseFormat::Save(Region* const ptr) {
//TODO
}
void CompactFormat::Load(Region** const ptr, int x, int y) {
//TODO
}
void CompactFormat::Save(Region* const ptr) {
//TODO
}
*/
+17 -1
View File
@@ -24,7 +24,15 @@
#include "region.hpp"
class MapFileFormat {
class DummyFormat {
public:
void Load(Region** const, int x, int y);
void Save(Region* const);
private:
//
};
/*
class VerboseFormat {
public:
void Load(Region** const, int x, int y);
void Save(Region* const);
@@ -32,4 +40,12 @@ private:
//
};
class CompactFormat {
public:
void Load(Region** const, int x, int y);
void Save(Region* const);
private:
//
};
*/
#endif
+23 -2
View File
@@ -21,10 +21,31 @@
*/
#include "map_generator.hpp"
void MapGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
void BlankGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y);
}
void MapGenerator::Unload(Region* const ptr) {
void BlankGenerator::Unload(Region* const ptr) {
delete ptr;
}
/*
void PerlinGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y);
}
void PerlinGenerator::Unload(Region* const ptr) {
delete ptr;
}
*/
void LuaGenerator::Create(Region** const ptr, int width, int height, int depth, int x, int y) {
(*ptr) = new Region(width, height, depth, x, y);
//generate the lua-driven maps
lua_getglobal(state, "CreateRegion");
lua_pushlightuserdata(state, ptr);
lua_pcall(state, 1, 0, 0);
}
void LuaGenerator::Unload(Region* const ptr) {
delete ptr;
}
+22 -1
View File
@@ -24,12 +24,33 @@
#include "region.hpp"
class MapGenerator {
#include "lua/lua.hpp"
class BlankGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
private:
//
};
/*
class PerlinGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
private:
//
};
*/
class LuaGenerator {
public:
void Create(Region** const, int width, int height, int depth, int x, int y);
void Unload(Region* const);
lua_State* SetLuaState(lua_State* L) { return state = L; }
lua_State* GetLuaState() { return state; }
private:
lua_State* state = nullptr;
};
#endif
+72
View File
@@ -0,0 +1,72 @@
/*
** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h
*/
/* Modified for use in Tortuga, renamed to linit.cpp
*/
/*
** If you embed Lua in your program and need to open the standard
** libraries, call luaL_openlibs in your program. If you need a
** different set of libraries, copy this file to your project and edit
** it to suit your needs.
*/
#define linit_c
#define LUA_LIB
#include "lua/lua.hpp"
#include "region_api.hpp"
/*
** these libs are loaded by lua.c and are readily available to any Lua
** program
*/
static const luaL_Reg loadedlibs[] = {
/* Standard libs */
{"_G", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_BITLIBNAME, luaopen_bit32},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_DBLIBNAME, luaopen_debug},
/* custom libs */
{LUA_REGIONLIBNAME, luaopen_regionapi},
{NULL, NULL}
};
/*
** these libs are preloaded and must be required before used
*/
static const luaL_Reg preloadedlibs[] = {
{NULL, NULL}
};
LUALIB_API void luaL_openlibs (lua_State *L) {
const luaL_Reg *lib;
/* call open functions from 'loadedlibs' and set results to global table */
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
/* add open functions from 'preloadedlibs' into 'package.preload' table */
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
for (lib = preloadedlibs; lib->func; lib++) {
lua_pushcfunction(L, lib->func);
lua_setfield(L, -2, lib->name);
}
lua_pop(L, 1); /* remove _PRELOAD table */
}
+43
View File
@@ -0,0 +1,43 @@
#config
INCLUDES+=. .. ../map
LIBS+=
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
#source
CXXSRC=$(wildcard *.cpp)
CSRC=$(wildcard *.c)
#objects
OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
#output
OUTDIR=../..
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
#targets
all: $(OBJ) $(OUT)
ar -crs $(OUT) $(OBJ)
$(OBJ): | $(OBJDIR)
$(OUT): | $(OUTDIR)
$(OBJDIR):
mkdir $(OBJDIR)
$(OUTDIR):
mkdir $(OUTDIR)
$(OBJDIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
$(RM) *.o *.a *.exe
rebuild: clean all
+27
View File
@@ -0,0 +1,27 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "region_api.hpp"
LUAMOD_API int luaopen_regionapi(lua_State* L) {
//TODO: stuff
return 1;
}
+30
View File
@@ -0,0 +1,30 @@
/* Copyright: (c) Kayne Ruse 2014
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef REGIONAPI_HPP_
#define REGIONAPI_HPP_
#include "lua/lua.hpp"
#define LUA_REGIONLIBNAME "region"
LUAMOD_API int luaopen_regionapi(lua_State* L);
#endif