Browse Source

Lua improvements

1. A conformant `print`.
2. `drawString`.
3. `OnGameDrawComplete` event for drawing things on screen.
pull/6710/head
Gleb Mazovetskiy 3 years ago
parent
commit
5b62bf7eec
  1. 5
      .editorconfig
  2. 2
      3rdParty/sol2/CMakeLists.txt
  3. 1
      Packaging/resources/assets/lua/init.lua
  4. 3
      Source/engine/render/scrollrt.cpp
  5. 41
      Source/utils/lua.cpp

5
.editorconfig

@ -17,6 +17,11 @@ end_of_line = lf
[*.po] [*.po]
end_of_line = lf end_of_line = lf
[*.lua]
indent_style = space
indent_size = 2
end_of_line = lf
[*.py] [*.py]
indent_style = space indent_style = space
indent_size = 4 indent_size = 4

2
3rdParty/sol2/CMakeLists.txt vendored

@ -10,4 +10,4 @@ FetchContent_Declare(sol2
FetchContent_MakeAvailableExcludeFromAll(sol2) FetchContent_MakeAvailableExcludeFromAll(sol2)
target_include_directories(sol2 SYSTEM BEFORE INTERFACE ${CMAKE_CURRENT_LIST_DIR}/sol_config) target_include_directories(sol2 SYSTEM BEFORE INTERFACE ${CMAKE_CURRENT_LIST_DIR}/sol_config)
target_compile_definitions(sol2 INTERFACE -DSOL_NO_EXCEPTIONS) target_compile_definitions(sol2 INTERFACE SOL_NO_EXCEPTIONS=1)

1
Packaging/resources/assets/lua/init.lua

@ -24,3 +24,4 @@ end
Events:RegisterEvent("OnGameBoot") Events:RegisterEvent("OnGameBoot")
Events:RegisterEvent("OnGameStart") Events:RegisterEvent("OnGameStart")
Events:RegisterEvent("OnGameDrawComplete")

3
Source/engine/render/scrollrt.cpp

@ -46,6 +46,7 @@
#include "utils/display.h" #include "utils/display.h"
#include "utils/endian.hpp" #include "utils/endian.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include "utils/lua.hpp"
#include "utils/str_cat.hpp" #include "utils/str_cat.hpp"
#ifndef USE_SDL1 #ifndef USE_SDL1
@ -1654,6 +1655,8 @@ void DrawAndBlit()
DrawFPS(out); DrawFPS(out);
LuaEvent("OnGameDrawComplete");
DrawMain(out, hgt, drawInfoBox, drawHealth, drawMana, drawBelt, drawControlButtons); DrawMain(out, hgt, drawInfoBox, drawHealth, drawMana, drawBelt, drawControlButtons);
RedrawComplete(); RedrawComplete();

41
Source/utils/lua.cpp

@ -6,6 +6,8 @@
#include <sol/sol.hpp> #include <sol/sol.hpp>
#include "engine/assets.hpp" #include "engine/assets.hpp"
#include "engine/dx.h"
#include "engine/render/text_render.hpp"
#include "plrmsg.h" #include "plrmsg.h"
#include "utils/console.h" #include "utils/console.h"
#include "utils/log.hpp" #include "utils/log.hpp"
@ -18,24 +20,16 @@ std::optional<sol::state> luaState;
int LuaPrint(lua_State *state) int LuaPrint(lua_State *state)
{ {
int nargs = lua_gettop(state); const int n = lua_gettop(state);
if (nargs >= 1 && lua_isstring(state, 1)) { for (int i = 1; i <= n; i++) {
std::string msg = lua_tostring(state, 1); size_t l;
msg += "\n"; const char *s = luaL_tolstring(state, i, &l);
printInConsole(msg); if (i > 1)
printInConsole("\t");
printInConsole(std::string_view(s, l));
lua_pop(state, 1);
} }
printNewlineInConsole();
return 0;
}
int LuaPlayerMessage(lua_State *state)
{
int nargs = lua_gettop(state);
if (nargs >= 1 && lua_isstring(state, 1)) {
std::string_view msg = lua_tostring(state, 1);
EventPlrMsg(msg, UiFlags::ColorRed);
}
return 0; return 0;
} }
@ -80,14 +74,14 @@ void LuaPanic(sol::optional<std::string> maybe_msg)
} // namespace } // namespace
void Sol2DebugPrintStack(lua_State *L) void Sol2DebugPrintStack(lua_State *state)
{ {
LogDebug("{}", sol::detail::debug::dump_types(L)); LogDebug("{}", sol::detail::debug::dump_types(state));
} }
void Sol2DebugPrintSection(const std::string &message, lua_State *L) void Sol2DebugPrintSection(const std::string &message, lua_State *state)
{ {
LogDebug("-- {} -- [ {} ]", message, sol::detail::debug::dump_types(L)); LogDebug("-- {} -- [ {} ]", message, sol::detail::debug::dump_types(state));
} }
void LuaInitialize() void LuaInitialize()
@ -115,7 +109,10 @@ void LuaInitialize()
// Registering devilutionx object table // Registering devilutionx object table
lua.create_named_table( lua.create_named_table(
"devilutionx", "devilutionx",
"message", LuaPlayerMessage); "message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); }
// TODO: Re-enable once https://github.com/bebbo/amiga-gcc/issues/363 is fixed.
// "drawString", [](std::string_view text, int x, int y) { DrawString(GlobalBackBuffer(), text, { x, y }); }
);
RunScript("lua/init.lua"); RunScript("lua/init.lua");
RunScript("lua/user.lua"); RunScript("lua/user.lua");

Loading…
Cancel
Save