Skip to content

Commit

Permalink
Lua improvements
Browse files Browse the repository at this point in the history
1. A conformant `print`.
2. `drawString`.
3. `OnGameDrawComplete` event for drawing things on screen.
  • Loading branch information
glebm committed Oct 15, 2023
1 parent 457b186 commit 0cbd4fc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ end_of_line = lf
[*.po]
end_of_line = lf

[*.lua]
indent_style = space
indent_size = 2
end_of_line = lf

[*.py]
indent_style = space
indent_size = 4
Expand Down
2 changes: 1 addition & 1 deletion 3rdParty/sol2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ FetchContent_Declare(sol2
FetchContent_MakeAvailableExcludeFromAll(sol2)

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 change: 1 addition & 0 deletions Packaging/resources/assets/lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ end

Events:RegisterEvent("OnGameBoot")
Events:RegisterEvent("OnGameStart")
Events:RegisterEvent("OnGameDrawComplete")
3 changes: 3 additions & 0 deletions Source/engine/render/scrollrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "utils/display.h"
#include "utils/endian.hpp"
#include "utils/log.hpp"
#include "utils/lua.hpp"
#include "utils/str_cat.hpp"

#ifndef USE_SDL1
Expand Down Expand Up @@ -1654,6 +1655,8 @@ void DrawAndBlit()

DrawFPS(out);

LuaEvent("OnGameDrawComplete");

DrawMain(out, hgt, drawInfoBox, drawHealth, drawMana, drawBelt, drawControlButtons);

RedrawComplete();
Expand Down
41 changes: 19 additions & 22 deletions Source/utils/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <sol/sol.hpp>

#include "engine/assets.hpp"
#include "engine/dx.h"
#include "engine/render/text_render.hpp"
#include "plrmsg.h"
#include "utils/console.h"
#include "utils/log.hpp"
Expand All @@ -18,24 +20,16 @@ std::optional<sol::state> luaState;

int LuaPrint(lua_State *state)
{
int nargs = lua_gettop(state);
if (nargs >= 1 && lua_isstring(state, 1)) {
std::string msg = lua_tostring(state, 1);
msg += "\n";
printInConsole(msg);
const int n = lua_gettop(state);
for (int i = 1; i <= n; i++) {
size_t l;
const char *s = luaL_tolstring(state, i, &l);
if (i > 1)
printInConsole("\t");
printInConsole(std::string_view(s, l));
lua_pop(state, 1);
}

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

printNewlineInConsole();
return 0;
}

Expand Down Expand Up @@ -80,14 +74,14 @@ void LuaPanic(sol::optional<std::string> maybe_msg)

} // 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()
Expand Down Expand Up @@ -115,7 +109,10 @@ void LuaInitialize()
// Registering devilutionx object table
lua.create_named_table(
"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/user.lua");
Expand Down

0 comments on commit 0cbd4fc

Please sign in to comment.