Scarab
A thin, game-agnostic 2D engine host built on the sunlight tile-map engine
Loading...
Searching...
No Matches
luaengineutil.h
Go to the documentation of this file.
1/*
2 * Copyright (c) since 2021 by PopolonY2k and Leidson Campos A. Ferreira
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 */
20
21#ifndef __LUAENGINEUTIL_H__
22#define __LUAENGINEUTIL_H__
23
24#include <cstddef>
25#include <cstdint>
26#include <map>
27#include <mutex>
28
29extern "C"
30{
31 #include "lua.h"
32}
33
34#include "tilemap/itilemap.h"
35#include "drawsurface/idrawsurface.h"
36#include "sound/soundmanager.h"
37#include "engine/spritepool.h"
38#include "scripting/scriptprocessor.h"
39#include "concurrent/timer.h"
41
42
43namespace Scarab {
44 namespace Engine {
45 namespace Lua {
46
51 const char *szName;
52 int nValue;
53 };
54
61 const char *szName;
62 double fValue;
63 };
64
70 typedef std :: map<int, SunLight :: Concurrent :: Timer*> TimerMap;
71
80
81 public:
82
83 /*
84 * SunLight::Concurrent::Timer::Start() runs it's callback on
85 * it's own background thread (see LuaTimerApi::SetTimer), and
86 * that callback calls lua_pcall directly on the shared
87 * lua_State - lua_State is not thread-safe, so every entry
88 * point that can touch it from the main thread
89 * (LuaEngine::CallOnUpdate/TryDispatchMoveSpritesToScreen/
90 * TryDispatchLoadStage/GetActiveEnemyCount/RunFile) must
91 * serialize against the timer thread through this same
92 * mutex, or the two threads racing on the interpreter's
93 * internal state corrupts it and crashes (seen in practice as
94 * a SIGSEGV inside Lua's own error-formatting code).
95 * Lua-callable static functions (LuaScriptingApi::Wait,
96 * LuaSpriteApi::*, etc.) do NOT need to lock it themselves -
97 * they only ever run reentrantly on the main thread, already
98 * inside a lua_pcall an entry point above is holding the lock
99 * for. The timer callback itself must use try_lock rather
100 * than a blocking lock: LuaTimerApi::ResetTimer runs
101 * reentrantly on the main thread and calls Timer::Stop(),
102 * which joins the timer thread - if the timer thread were
103 * blocked waiting on this same mutex at that moment, the join
104 * would deadlock against the thread that holds it. Lives here
105 * (not on LuaEngine) since LuaTimerApi needs it too, and this
106 * is the one class every Lua primitive API module already
107 * depends on.
108 */
109 static std :: mutex s_LuaMutex;
110
111 static Engine :: IRendererProvider* GetRendererProvider( lua_State *pLuaState );
112 static void EnsureRenderer( lua_State *pLuaState );
113 static SunLight :: TileMap :: ITileMap* GetTileMap( lua_State *pLuaState );
114 static SunLight :: DrawSurface :: IDrawSurface* GetDrawSurface( lua_State *pLuaState );
115 static SunLight :: Sound :: SoundManager* GetSoundManager( lua_State *pLuaState );
116 static Engine :: SpritePool* GetSpritePool( lua_State *pLuaState );
117 static SunLight :: Scripting :: ScriptProcessor* GetScriptProcessor( lua_State *pLuaState );
118 static void AddOneParmCommandScript( lua_State *pLuaState, SunLight :: Scripting :: Commands cmd );
119 static void RegisterConstants( lua_State *pLuaState, const stNamedConstant *pTable, size_t nCount );
120 static void RegisterRealConstants( lua_State *pLuaState, const stNamedRealConstant *pTable, size_t nCount );
121 };
122 }
123 }
124}
125
126#endif /* __LUAENGINEUTIL_H__ */
Shared helpers used by every Lua primitive API module - and, for s_LuaMutex specifically,...
Definition luaengineutil.h:79
static SunLight::Sound::SoundManager * GetSoundManager(lua_State *pLuaState)
Fetch the engine's SoundManager instance stashed as a Lua light userdata global.
Definition luaengineutil.cpp:109
static SunLight::Scripting::ScriptProcessor * GetScriptProcessor(lua_State *pLuaState)
Fetch the engine's ScriptProcessor instance stashed as a Lua light userdata global.
Definition luaengineutil.cpp:133
static void RegisterRealConstants(lua_State *pLuaState, const stNamedRealConstant *pTable, size_t nCount)
Register a table of named real-valued constants as Lua globals (see stNamedRealConstant).
Definition luaengineutil.cpp:182
static void RegisterConstants(lua_State *pLuaState, const stNamedConstant *pTable, size_t nCount)
Register a table of named integer constants as Lua globals.
Definition luaengineutil.cpp:166
static SunLight::TileMap::ITileMap * GetTileMap(lua_State *pLuaState)
The engine's ITileMap - creating the default renderer first if the script has not created one yet (se...
Definition luaengineutil.cpp:83
static std::mutex s_LuaMutex
Definition luaengineutil.h:109
static Engine::SpritePool * GetSpritePool(lua_State *pLuaState)
Fetch the engine's SpritePool instance stashed as a Lua light userdata global.
Definition luaengineutil.cpp:121
static void EnsureRenderer(lua_State *pLuaState)
Make sure a renderer exists, creating the default one (and opening its window) if the script hasn't -...
Definition luaengineutil.cpp:72
static SunLight::DrawSurface::IDrawSurface * GetDrawSurface(lua_State *pLuaState)
The engine's IDrawSurface - screen-space text/rectangle drawing and window-state primitives (see sunl...
Definition luaengineutil.cpp:99
static void AddOneParmCommandScript(lua_State *pLuaState, SunLight ::Scripting ::Commands cmd)
Add an one parameter command to the ScriptProcessor's queue; shared by every Lua primitive that queue...
Definition luaengineutil.cpp:149
static Engine::IRendererProvider * GetRendererProvider(lua_State *pLuaState)
Fetch the renderer provider stashed as a Lua light userdata global - the one object every renderer-ne...
Definition luaengineutil.cpp:38
std ::map< int, SunLight ::Concurrent ::Timer * > TimerMap
Shared type for LuaEngine's m_Timers member and LuaTimerApi's casts of the timerMapPtr global - both ...
Definition luaengineutil.h:70
Definition irendererprovider.h:32
A named integer constant, used to register enum values as Lua globals.
Definition luaengineutil.h:50
int nValue
Definition luaengineutil.h:52
const char * szName
Definition luaengineutil.h:51
A named real-valued constant, used to register non-integer values (eg. the zoom step and factor limit...
Definition luaengineutil.h:60
const char * szName
Definition luaengineutil.h:61
double fValue
Definition luaengineutil.h:62