Compare commits
10 commits
36d4c9298f
...
4135928a9f
Author | SHA1 | Date | |
---|---|---|---|
4135928a9f | |||
c5a98c282b | |||
cc6a8f553e | |||
f41fc40995 | |||
969de84c13 | |||
0b3e29dea9 | |||
f05141f1f7 | |||
12cf10a35c | |||
2453bcdcc1 | |||
1b250f668b |
28 changed files with 481 additions and 82 deletions
|
@ -4,7 +4,7 @@ project(RPGEDIT CXX)
|
|||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(SDL2 REQUIRED sdl2 SDL2_image)
|
||||
pkg_check_modules(LUA REQUIRED lua>=5.2)
|
||||
|
||||
find_package(Lua 5.3 EXACT REQUIRED)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
|
3
resources/script/interact.lua
Normal file
3
resources/script/interact.lua
Normal file
|
@ -0,0 +1,3 @@
|
|||
function interact(entity, time)
|
||||
print('Interact! ' .. time)
|
||||
end
|
|
@ -1,14 +1,20 @@
|
|||
function print_table(foo, bar)
|
||||
local function print_table(foo, bar)
|
||||
for k, v in pairs(foo) do
|
||||
print(bar .. k, v)
|
||||
|
||||
if type(v) == 'table' and bar .. k ~= '_G' then
|
||||
print_table(v, bar .. k .. '.')
|
||||
end
|
||||
--if type(v) == 'table' and bar .. k ~= '_G' then
|
||||
-- print_table(v, bar .. k .. '.')
|
||||
--end
|
||||
end
|
||||
end
|
||||
|
||||
print_table(_G, '')
|
||||
--print(getmetatable(_G))
|
||||
--setmetatable(_G, {})
|
||||
--print_table(_G, '')
|
||||
|
||||
print(getmetatable('').__index)
|
||||
print(string)
|
||||
print(bar)
|
||||
print(getmetatable(bar))
|
||||
|
||||
--print(getmetatable('').bar)
|
||||
--getmetatable('').bar = 'bar'
|
||||
--print(getmetatable('').bar)
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
include_directories(${SDL2_INCLUDE_DIRS} ${LUA_INCLUDE_DIRS})
|
||||
link_directories(${SDL2_LIBRARY_DIRS} ${LUA_LIBRARY_DIRS})
|
||||
include_directories(${SDL2_INCLUDE_DIRS} ${LUA_INCLUDE_DIR})
|
||||
link_directories(${SDL2_LIBRARY_DIRS})
|
||||
|
||||
add_executable(rpgedit
|
||||
rpgedit.cpp
|
||||
control/MapContext.cpp
|
||||
control/RPGEdit.cpp
|
||||
control/ScriptContext.cpp
|
||||
model/Map.cpp
|
||||
model/Scriptable.cpp
|
||||
view/MapView.cpp
|
||||
view/SpriteCache.cpp
|
||||
)
|
||||
set_target_properties(rpgedit PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall ${SDL2_CFLAGS_OTHER} ${LUA_CFLAGS_OTHER}")
|
||||
set_target_properties(rpgedit PROPERTIES LINK_FLAGS "${SDL2_LDFLAGS_OTHER} ${LUA_LDFLAGS_OTHER}")
|
||||
set_target_properties(rpgedit PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall ${SDL2_CFLAGS_OTHER}")
|
||||
set_target_properties(rpgedit PROPERTIES LINK_FLAGS "${SDL2_LDFLAGS_OTHER}")
|
||||
target_link_libraries(rpgedit ${SDL2_LIBRARIES} ${LUA_LIBRARIES})
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -31,10 +31,13 @@ namespace RPGEdit {
|
|||
|
||||
namespace Control {
|
||||
|
||||
MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<View::Window> &window, const Model::Map &map0)
|
||||
: eventBus(eventBus0), inputHandler(inputHandler0), map(map0) {
|
||||
MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, ScriptContext *scriptContext0, const std::shared_ptr<View::Window> &window, const Model::Map &map0)
|
||||
: eventBus(eventBus0), inputHandler(inputHandler0), scriptContext(scriptContext0), map(map0) {
|
||||
view = std::unique_ptr<View::MapView>(new View::MapView(window, map.getTileset()));
|
||||
|
||||
Model::Entity *square = map.addEntity("square", Model::Position<int>{10, 10});
|
||||
square->setScriptInteract("interact", "interact");
|
||||
|
||||
playerEntity = map.addEntity("square", Model::Position<int>{8, 8});
|
||||
|
||||
view->updateEntities(map.getEntities());
|
||||
|
@ -80,7 +83,11 @@ void MapContext::interact(uint64_t time) {
|
|||
if (!target)
|
||||
return;
|
||||
|
||||
target->interact(time);
|
||||
const std::pair<std::string, std::string> &interactScript = target->getScriptInteract();
|
||||
if (interactScript.first.empty())
|
||||
return;
|
||||
|
||||
scriptContext->run(interactScript.first, interactScript.second, nullptr, time);
|
||||
}
|
||||
|
||||
void MapContext::keyPressed(uint16_t key, uint64_t time) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -28,6 +28,7 @@
|
|||
|
||||
#include "EventBus.hpp"
|
||||
#include "InputHandler.hpp"
|
||||
#include "ScriptContext.hpp"
|
||||
#include "../model/Map.hpp"
|
||||
#include "../view/MapView.hpp"
|
||||
|
||||
|
@ -43,6 +44,7 @@ class MapContext {
|
|||
private:
|
||||
EventBus *const eventBus;
|
||||
InputHandler *const inputHandler;
|
||||
ScriptContext *const scriptContext;
|
||||
|
||||
std::unique_ptr<View::MapView> view;
|
||||
|
||||
|
@ -59,7 +61,7 @@ private:
|
|||
}
|
||||
|
||||
public:
|
||||
MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<View::Window> &window, const Model::Map &map0);
|
||||
MapContext(EventBus *eventBus0, InputHandler *inputHandler0, ScriptContext *scriptContext0, const std::shared_ptr<View::Window> &window, const Model::Map &map0);
|
||||
|
||||
void render(uint64_t time) {
|
||||
view->render(&map, getViewPosition(time), time);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -26,7 +26,6 @@
|
|||
|
||||
#include "RPGEdit.hpp"
|
||||
#include "MapContext.hpp"
|
||||
#include "ScriptContext.hpp"
|
||||
#include "../view/MapView.hpp"
|
||||
|
||||
|
||||
|
@ -106,7 +105,7 @@ void RPGEdit::run() {
|
|||
|
||||
window = std::make_shared<View::Window>();
|
||||
|
||||
ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, window, *map);
|
||||
ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, &scriptContext, window, *map);
|
||||
|
||||
eventThread = std::thread([this] { eventLoop(); });
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -46,6 +46,8 @@ private:
|
|||
EventBus eventBus;
|
||||
InputHandler inputHandler;
|
||||
|
||||
ScriptContext scriptContext;
|
||||
|
||||
std::shared_ptr<View::Window> window;
|
||||
|
||||
std::shared_ptr<MapContext> ctx;
|
||||
|
|
92
src/control/ScriptContext.cpp
Normal file
92
src/control/ScriptContext.cpp
Normal file
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "ScriptContext.hpp"
|
||||
|
||||
|
||||
namespace RPGEdit {
|
||||
|
||||
namespace Control {
|
||||
|
||||
void ScriptContext::setupEnv() {
|
||||
const std::pair<const char *, lua_CFunction> libs[] = {
|
||||
{"_G", luaopen_base},
|
||||
{"math", luaopen_math},
|
||||
{"string", luaopen_string},
|
||||
{"table", luaopen_table},
|
||||
{"utf8", luaopen_utf8},
|
||||
};
|
||||
|
||||
for (auto &lib : libs) {
|
||||
lua_pushcfunction(L, lib.second);
|
||||
lua_pushstring(L, lib.first);
|
||||
lua_call(L, 1, 1);
|
||||
lua_setglobal(L, lib.first);
|
||||
}
|
||||
|
||||
for (const char *f : {"dofile", "loadfile", "require"}) {
|
||||
lua_pushnil(L);
|
||||
lua_setglobal(L, f);
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptContext::cleanupEnv() {
|
||||
lua_pushglobaltable(L);
|
||||
|
||||
lua_pushnil(L);
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, -2) != 0) {
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushnil(L);
|
||||
|
||||
lua_rawset(L, -4);
|
||||
}
|
||||
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
void ScriptContext::load(const std::string &script) {
|
||||
if (loadedScripts.count(script))
|
||||
return;
|
||||
|
||||
std::string filename = "../resources/script/" + script + ".lua";
|
||||
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, this);
|
||||
lua_pushstring(L, script.c_str());
|
||||
luaL_loadfile(L, filename.c_str());
|
||||
lua_rawset(L, -3);
|
||||
lua_pop(L, 1);
|
||||
|
||||
loadedScripts.insert(script);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -26,6 +26,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../model/ScriptValue.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
|
@ -34,6 +36,8 @@ extern "C" {
|
|||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace RPGEdit {
|
||||
|
@ -44,43 +48,44 @@ class ScriptContext {
|
|||
private:
|
||||
lua_State *L;
|
||||
|
||||
void setupEnv() {
|
||||
const std::pair<const char *, lua_CFunction> libs[] = {
|
||||
{"_G", luaopen_base},
|
||||
{"bit32", luaopen_bit32},
|
||||
{"math", luaopen_math},
|
||||
{"string", luaopen_string},
|
||||
{"table", luaopen_table},
|
||||
};
|
||||
std::unordered_set<std::string> loadedScripts;
|
||||
|
||||
for (auto &lib : libs) {
|
||||
luaL_requiref(L, lib.first, lib.second, 1);
|
||||
lua_pop(L, 1);
|
||||
void setupEnv();
|
||||
void cleanupEnv();
|
||||
|
||||
void load(const std::string &script);
|
||||
|
||||
|
||||
void pushArg(std::nullptr_t __attribute__((unused)) v) {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
for (const char *f : {"dofile", "loadfile", "require"}) {
|
||||
lua_pushnil(L);
|
||||
lua_setglobal(L, f);
|
||||
}
|
||||
void pushArg(const std::string &v) {
|
||||
lua_pushstring(L, v.c_str());
|
||||
}
|
||||
|
||||
void cleanupEnv() {
|
||||
lua_pushglobaltable(L);
|
||||
|
||||
lua_pushnil(L);
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, -2) != 0) {
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_pushvalue(L, -1);
|
||||
lua_pushnil(L);
|
||||
|
||||
lua_rawset(L, -4);
|
||||
template<typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
|
||||
void pushArg(T v) {
|
||||
lua_pushnumber(L, v);
|
||||
}
|
||||
|
||||
lua_pop(L, 1);
|
||||
void pushArg(bool v) {
|
||||
lua_pushboolean(L, v);
|
||||
}
|
||||
|
||||
void pushArg(Model::ScriptValue &v) {
|
||||
v.push(L);
|
||||
}
|
||||
|
||||
|
||||
size_t pushArgs() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
size_t pushArgs(T v, Args ...args) {
|
||||
pushArg(v);
|
||||
return pushArgs(args...) + 1;
|
||||
}
|
||||
|
||||
public:
|
||||
|
@ -100,25 +105,26 @@ public:
|
|||
lua_close(L);
|
||||
}
|
||||
|
||||
void load(const std::string &name) {
|
||||
std::string filename = "../resources/script/" + name + ".lua";
|
||||
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, this);
|
||||
lua_pushstring(L, name.c_str());
|
||||
luaL_loadfile(L, filename.c_str());
|
||||
lua_rawset(L, -3);
|
||||
lua_pop(L, 1);
|
||||
void setGlobal(const std::string &key, Model::ScriptValue *value) {
|
||||
value->push(L);
|
||||
lua_setglobal(L, key.c_str());
|
||||
}
|
||||
|
||||
void run(const std::string &name) {
|
||||
template<typename... Args>
|
||||
void run(const std::string &script, const std::string &name, Args ...args) {
|
||||
load(script);
|
||||
|
||||
setupEnv();
|
||||
|
||||
lua_rawgetp(L, LUA_REGISTRYINDEX, this);
|
||||
lua_getfield(L, -1, name.c_str());
|
||||
lua_getfield(L, -1, script.c_str());
|
||||
lua_remove(L, -2);
|
||||
|
||||
lua_call(L, 0, 0);
|
||||
|
||||
lua_getglobal(L, name.c_str());
|
||||
lua_call(L, pushArgs(args...), 0);
|
||||
|
||||
cleanupEnv();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -28,7 +28,7 @@
|
|||
|
||||
|
||||
#include "Direction.hpp"
|
||||
#include "Position.hpp"
|
||||
#include "Scriptable.hpp"
|
||||
|
||||
|
||||
#include <string>
|
||||
|
@ -39,10 +39,12 @@ namespace RPGEdit {
|
|||
|
||||
namespace Model {
|
||||
|
||||
class Entity {
|
||||
class Entity : public Scriptable {
|
||||
private:
|
||||
std::string name;
|
||||
|
||||
std::pair<std::string, std::string> scriptInteract;
|
||||
|
||||
Direction direction;
|
||||
|
||||
public:
|
||||
|
@ -62,8 +64,13 @@ public:
|
|||
direction = dir;
|
||||
}
|
||||
|
||||
void interact(uint64_t time) {
|
||||
std::fprintf(stderr, "Tried to interact with `%s' entity at %llu\n", name.c_str(), (unsigned long long)time);
|
||||
const std::pair<std::string, std::string> & getScriptInteract() const {
|
||||
return scriptInteract;
|
||||
}
|
||||
|
||||
void setScriptInteract(const std::string &script, const std::string &name) {
|
||||
scriptInteract.first = script;
|
||||
scriptInteract.second = name;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -35,6 +35,7 @@
|
|||
|
||||
#include "CollisionType.hpp"
|
||||
#include "Entity.hpp"
|
||||
#include "Position.hpp"
|
||||
|
||||
|
||||
namespace RPGEdit {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
143
src/model/ScriptValue.hpp
Normal file
143
src/model/ScriptValue.hpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
}
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace RPGEdit {
|
||||
|
||||
namespace Model {
|
||||
|
||||
class ScriptValue {
|
||||
public:
|
||||
virtual void push(lua_State *L) = 0;
|
||||
|
||||
virtual ~ScriptValue() {}
|
||||
};
|
||||
|
||||
|
||||
class ScriptBoolean : public ScriptValue {
|
||||
private:
|
||||
bool value;
|
||||
|
||||
public:
|
||||
ScriptBoolean(bool value0) : value(value0) {
|
||||
}
|
||||
|
||||
ScriptBoolean & operator=(bool newValue) {
|
||||
value = newValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
virtual void push(lua_State *L) {
|
||||
lua_pushboolean(L, value);
|
||||
}
|
||||
};
|
||||
|
||||
class ScriptNumber : public ScriptValue {
|
||||
private:
|
||||
lua_Number value;
|
||||
|
||||
public:
|
||||
ScriptNumber(lua_Number value0) : value(value0) {
|
||||
}
|
||||
|
||||
ScriptNumber & operator=(lua_Number newValue) {
|
||||
value = newValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator lua_Number() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
virtual void push(lua_State *L) {
|
||||
lua_pushnumber(L, value);
|
||||
}
|
||||
};
|
||||
|
||||
class ScriptString : public ScriptValue {
|
||||
private:
|
||||
std::string value;
|
||||
|
||||
public:
|
||||
ScriptString(std::string &value0) : value(value0) {
|
||||
}
|
||||
|
||||
ScriptString & operator=(std::string &newValue) {
|
||||
value = newValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator std::string &() {
|
||||
return value;
|
||||
}
|
||||
|
||||
virtual void push(lua_State *L) {
|
||||
lua_pushstring(L, value.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
class ScriptTable : public ScriptValue {
|
||||
public:
|
||||
typedef std::unordered_map<std::shared_ptr<ScriptValue>, std::shared_ptr<ScriptValue>> MapType;
|
||||
|
||||
private:
|
||||
MapType value;
|
||||
|
||||
public:
|
||||
std::shared_ptr<ScriptValue> & operator[](const std::shared_ptr<ScriptValue> &key) {
|
||||
return value[key];
|
||||
}
|
||||
|
||||
virtual void push(lua_State *L) {
|
||||
lua_createtable(L, 0, value.size());
|
||||
|
||||
for (const auto &entry : value) {
|
||||
entry.first->push(L);
|
||||
entry.second->push(L);
|
||||
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
58
src/model/Scriptable.cpp
Normal file
58
src/model/Scriptable.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "Scriptable.hpp"
|
||||
|
||||
|
||||
namespace RPGEdit {
|
||||
|
||||
namespace Model {
|
||||
|
||||
int Scriptable::gc(lua_State *L) {
|
||||
std::shared_ptr<ScriptValue> *value = static_cast<std::shared_ptr<ScriptValue> *>(lua_touserdata(L, -1));
|
||||
|
||||
value->~shared_ptr<ScriptValue>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Scriptable::setupMetatable(lua_State *L) {
|
||||
if (luaL_newmetatable(L, "RPGEdit::Model::Scriptable")) {
|
||||
lua_pushstring(L, "__metatable");
|
||||
lua_pushstring(L, "protected");
|
||||
lua_rawset(L, -3);
|
||||
|
||||
lua_pushstring(L, "__gc");
|
||||
lua_pushcfunction(L, &gc);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
57
src/model/Scriptable.hpp
Normal file
57
src/model/Scriptable.hpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "ScriptValue.hpp"
|
||||
|
||||
#include <new>
|
||||
|
||||
|
||||
namespace RPGEdit {
|
||||
|
||||
namespace Model {
|
||||
|
||||
class Scriptable : public ScriptValue, public std::enable_shared_from_this<Scriptable> {
|
||||
private:
|
||||
static int gc(lua_State *L);
|
||||
|
||||
void setupMetatable(lua_State *L);
|
||||
|
||||
public:
|
||||
virtual void push(lua_State *L) {
|
||||
|
||||
void *ptr = lua_newuserdata(L, sizeof(std::shared_ptr<ScriptValue>));
|
||||
setupMetatable(L);
|
||||
|
||||
new (ptr) std::shared_ptr<ScriptValue>(shared_from_this());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -27,15 +27,27 @@
|
|||
#include "control/RPGEdit.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_main.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
extern "C"
|
||||
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *argv[]) {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
std::cerr << "Unable to initialize SDL" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
|
||||
std::cerr << "Unable to initialize PNG loader" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
RPGEdit::Control::RPGEdit().run();
|
||||
|
||||
IMG_Quit();
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
@ -70,6 +70,8 @@ SpriteCache::sprite_value SpriteCache::load(const std::string &id, unsigned rota
|
|||
|
||||
uint8_t *src = reinterpret_cast<uint8_t *>(base->pixels);
|
||||
uint8_t *dst = reinterpret_cast<uint8_t *>(surface->pixels);
|
||||
int pitch = base->pitch;
|
||||
int pitch2 = surface->pitch;
|
||||
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
|
@ -89,7 +91,7 @@ SpriteCache::sprite_value SpriteCache::load(const std::string &id, unsigned rota
|
|||
y2 = x;
|
||||
}
|
||||
|
||||
std::memcpy(dst + d * (y2*w2 + x2), src + d * (y*w + x), d);
|
||||
std::memcpy(dst + y2*pitch2 + d*x2, src + y*pitch + d*x, d);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
Copyright (c) 2014-2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
Reference in a new issue