12 inline T *getPtrFromUpvalue(lua_State *L,
int uvInd)
14 int idx = lua_upvalueindex(uvInd);
15 ASSERT(lua_type(L, idx) == LUA_TLIGHTUSERDATA);
16 void *p = lua_touserdata(L, idx);
17 return static_cast<T *
>(p);
21 inline int exceptToLuaError(lua_State *L, F proc)
26 catch (
const std::exception &ex) {
29 return luaL_error(L,
"%s", ex.what());
34 using lim = std::numeric_limits<T>;
36 static_assert(lim<lua_Integer>::min() <= lim<int>::min(),
"Lua type check");
37 static_assert(lim<lua_Integer>::max() >= lim<int>::max(),
"Lua type check");
38 static_assert(lim<lua_Number>::max() >= lim<float>::max(),
"Lua type check");
40 inline int luaintToInt(lua_State *L,
int arg,
41 lua_Integer val,
int min,
int max)
43 luaL_argcheck(L, val >= min, arg,
"number too small");
44 luaL_argcheck(L, val <= max, arg,
"number too large");
45 return static_cast<int>(val);
48 inline int getInt(lua_State *L,
int arg,
49 int min = std::numeric_limits<int>::min(),
50 int max = std::numeric_limits<int>::max())
52 lua_Integer val = luaL_checkinteger(L, arg);
53 return luaintToInt(L, arg, val, min, max);
56 inline int getOptInt(lua_State *L,
int arg,
int def,
57 int min = std::numeric_limits<int>::min(),
58 int max = std::numeric_limits<int>::max())
60 lua_Integer val = luaL_optinteger(L, arg, def);
61 return luaintToInt(L, arg, val, min, max);
64 inline float luanumToFloat(lua_State *L,
int arg,
65 lua_Number val,
float min,
float max)
67 luaL_argcheck(L, val >= min, arg,
"number too small or NaN");
68 luaL_argcheck(L, val <= max, arg,
"number too large or NaN");
69 return static_cast<float>(val);
72 inline double luanumToDouble(lua_State *L,
int arg,
73 lua_Number val,
double min,
double max)
75 luaL_argcheck(L, val >= min, arg,
"number too small or NaN");
76 luaL_argcheck(L, val <= max, arg,
"number too large or NaN");
77 return static_cast<double>(val);
80 inline float getFloat(lua_State *L,
int arg,
81 float min = std::numeric_limits<float>::lowest(),
82 float max = std::numeric_limits<float>::max())
84 lua_Number val = luaL_checknumber(L, arg);
85 return luanumToFloat(L, arg, val, min, max);
88 inline float getOptFloat(lua_State *L,
int arg,
float def,
89 float min = std::numeric_limits<float>::lowest(),
90 float max = std::numeric_limits<float>::max())
92 lua_Number val = luaL_optnumber(L, arg, def);
93 return luanumToFloat(L, arg, val, min, max);
96 inline double getDouble(lua_State *L,
int arg,
97 double min = std::numeric_limits<double>::lowest(),
98 double max = std::numeric_limits<double>::max())
100 lua_Number val = luaL_checknumber(L, arg);
101 return luanumToDouble(L, arg, val, min, max);
104 inline double getOptDouble(lua_State *L,
int arg,
double def,
105 double min = std::numeric_limits<double>::lowest(),
106 double max = std::numeric_limits<double>::max())
108 lua_Number val = luaL_optnumber(L, arg, def);
109 return luanumToDouble(L, arg, val, min, max);
135 return exceptToLuaError(L, [L]() {
136 int argc = lua_gettop(L);
137 for (
int i = 1; i <= argc; i++) {
138 const char *
str = lua_tostring(L, i);
139 if (str !=
nullptr) {
142 else if (lua_type(L, i) == LUA_TBOOLEAN) {
143 int b = lua_toboolean(L, i);
168 return exceptToLuaError(L, [L]() {
169 int argc = lua_gettop(L);
170 for (
int i = 1; i <= argc; i++) {
171 const char *
str = ::lua_tostring(L, i);
172 if (str !=
nullptr) {
205 return exceptToLuaError(L, [L]() {
206 auto *lua = getPtrFromUpvalue<Lua>(L, 1);
208 int argc = lua_gettop(L);
209 for (
int i = 1; i <= argc; i++) {
210 const char *fileName = ::lua_tostring(L, i);
211 luaL_argcheck(L, fileName !=
nullptr, i,
"string needed");
251 return exceptToLuaError(L, [L]() {
252 const char *fileName = luaL_checkstring(L, 1);
254 FILE *tmpfp =
nullptr;
255 errno_t err = ::fopen_s(&tmpfp, fileName,
"r");
261 luaL_error(L,
"File open error: %s, %d", fileName, err);
269 while (std::fgets(buf,
sizeof(buf), fp.get()) !=
nullptr) {
271 char *pnl = std::strchr(buf,
'\n');
272 if (pnl !=
nullptr) {
281 if (std::fgets(disc,
sizeof(disc), fp.get()) ==
nullptr) {
284 }
while (std::strchr(disc,
'\n') ==
nullptr);
286 lua_pushstring(L, buf);
319 return exceptToLuaError(L, [L]() {
320 const char *fileName = luaL_checkstring(L, 1);
322 FILE *tmpfp =
nullptr;
323 errno_t err = ::fopen_s(&tmpfp, fileName,
"w");
325 luaL_error(L,
"File open error: %s, %d", fileName, err);
329 int argc = lua_gettop(L);
330 for (
int i = 2; i <= argc; i++) {
332 const char *line = luaL_checkstring(L, i);
333 std::fputs(line, fp.get());
334 std::fputc(
'\n', fp.get());
357 return exceptToLuaError(L, [L]() {
359 lua_pushinteger(L, seed);
381 return exceptToLuaError(L, [L]() {
382 auto seed =
static_cast<unsigned int>(luaL_checkinteger(L, 1));
403 return exceptToLuaError(L, [L]() {
404 int a = getOptInt(L, 1, 0);
405 int b = getOptInt(L, 2, std::numeric_limits<int>::max());
406 luaL_argcheck(L, a <= b, 1,
"Must be a <= b");
410 lua_pushinteger(L, rnum);
430 return exceptToLuaError(L, [L]() {
431 double a = getOptDouble(L, 1, 0.0);
432 double b = getOptDouble(L, 2, 1.0);
433 luaL_argcheck(L, a <= b, 1,
"Must be a <= b");
437 lua_pushnumber(L, rnum);
460 return exceptToLuaError(L, [L]() {
461 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
462 int setId = getInt(L, 1, 0);
463 const char *resId = luaL_checkstring(L, 2);
464 const char *path = luaL_checkstring(L, 3);
466 app->addTextureResource(setId, resId,
util::utf82wc(path).
get());
485 return exceptToLuaError(L, [L]() {
486 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
487 int setId = getInt(L, 1, 0);
488 const char *resId = luaL_checkstring(L, 2);
489 const char *fontName = luaL_checkstring(L, 3);
490 const char *startCharStr = luaL_checkstring(L, 4);
491 luaL_argcheck(L, *startCharStr !=
'\0', 4,
"empty string is NG");
492 const char *endCharStr = luaL_checkstring(L, 4);
493 luaL_argcheck(L, *endCharStr !=
'\0', 5,
"empty string is NG");
494 int w = getInt(L, 7, 0);
495 int h = getInt(L, 8, 0);
500 app->addFontResource(setId, resId,
util::utf82wc(fontName).
get(),
501 startChar, endChar, w, h);
520 return exceptToLuaError(L, [L]() {
521 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
522 int setId = getInt(L, 1, 0);
523 const char *resId = luaL_checkstring(L, 2);
524 const char *path = luaL_checkstring(L, 3);
545 return exceptToLuaError(L, [L]() {
546 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
547 int setId = getInt(L, 1, 0);
548 const char *resId = luaL_checkstring(L, 2);
549 const char *path = luaL_checkstring(L, 3);
551 app->addBgmResource(setId, resId,
util::utf82wc(path).
get());
575 return exceptToLuaError(L, [L]() {
576 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
579 app->getGraphicsParam(¶m);
581 lua_pushinteger(L, param.w);
582 lua_pushinteger(L, param.h);
583 lua_pushinteger(L, param.refreshRate);
584 lua_pushboolean(L, param.vsync);
604 return exceptToLuaError(L, [L]() {
605 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
606 int setId = getInt(L, 1, 0);
607 const char *resId = luaL_checkstring(L, 2);
609 const auto &pTex = app->getTexture(setId, resId);
611 lua_pushinteger(L, pTex->w);
612 lua_pushinteger(L, pTex->h);
651 return exceptToLuaError(L, [L]() {
652 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
653 int setId = getInt(L, 1, 0);
654 const char *resId = luaL_checkstring(L, 2);
655 int dx = getInt(L, 3);
656 int dy = getInt(L, 4);
658 bool lrInv = lua_toboolean(L, 5) != 0;
659 bool udInv = lua_toboolean(L, 6) != 0;
660 int sx = getOptInt(L, 7, 0);
661 int sy = getOptInt(L, 8, 0);
662 int sw = getOptInt(L, 9, -1);
663 int sh = getOptInt(L, 10, -1);
664 int cx = getOptInt(L, 11, 0);
665 int cy = getOptInt(L, 12, 0);
666 float angle = getOptFloat(L, 13, 0.0f);
667 float scaleX = getOptFloat(L, 14, 1.0f);
668 float scaleY = getOptFloat(L, 15, 1.0f);
669 float alpha = getOptFloat(L, 16, 1.0f);
671 const auto &pTex = app->getTexture(setId, resId);
672 app->graph().drawTexture(pTex, dx, dy, lrInv, udInv, sx, sy, sw, sh, cx, cy,
673 angle, scaleX, scaleY, alpha);
703 return exceptToLuaError(L, [L]() {
704 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
705 int setId = getInt(L, 1, 0);
706 const char *resId = luaL_checkstring(L, 2);
707 const char *
str = luaL_checkstring(L, 3);
708 int dx = getInt(L, 4);
709 int dy = getInt(L, 5);
711 int color = getOptInt(L, 6, 0x000000);
712 int ajustX = getOptInt(L, 7, 0);
713 float scaleX = getOptFloat(L, 8, 1.0f);
714 float scaleY = getOptFloat(L, 9, 1.0f);
715 float alpha = getOptFloat(L, 10, 1.0f);
717 const auto &pFont = app->getFont(setId, resId);
718 app->graph().drawString(pFont,
util::utf82wc(str).
get(), dx, dy,
719 color, ajustX, scaleX, scaleY, alpha);
741 return exceptToLuaError(L, [L]() {
742 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
743 int setId = getInt(L, 1, 0);
744 const char *resId = luaL_checkstring(L, 2);
746 const auto &pSoundEffect = app->getSoundEffect(setId, resId);
747 app->sound().playSoundEffect(pSoundEffect);
765 return exceptToLuaError(L, [L]() {
766 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
767 int setId = getInt(L, 1, 0);
768 const char *resId = luaL_checkstring(L, 2);
770 auto &pBgm = app->getBgm(setId, resId);
771 app->sound().playBgm(pBgm);
787 return exceptToLuaError(L, [L]() {
788 auto *app = getPtrFromUpvalue<framework::Application>(L, 1);
790 app->sound().stopBgm();
int nextInt(int a, int b)
Get next int random number.
static int addSe(lua_State *L)
効果音リソースを登録する。
double nextDouble(double a, double b)
Get next double random number.
static int addBgm(lua_State *L)
BGMリソースを登録する。
std::unique_ptr< FILE, FileDeleter > FilePtr
unique_ptr of FILE with FileDeleter.
void setSeed(unsigned int seed)
Set random seed.
void writef(const wchar_t *fmt,...) noexcept
Write debug message using format string like printf.
static int getTextureSize(lua_State *L)
テクスチャのサイズを得る。
#define ASSERT(x)
Assertion which uses debug framework.
static int nextInt(lua_State *L)
次の整数乱数を生成する。
static int playSe(lua_State *L)
効果音再生を開始する。
static int drawString(lua_State *L)
文字列を描画する。
static int readFile(lua_State *L)
ファイルを読む。
static int stopBgm(lua_State *L)
BGM 再生を停止する。
void writeLine(const wchar_t *str=L"") noexcept
Write debug string and new line.
static int nextDouble(lua_State *L)
次の浮動小数点乱数を生成する。
void write(const char *str)
Write string to buffer.
static int getParam(lua_State *L)
テクスチャのサイズを得る。
static int writeFile(lua_State *L)
ファイルを書く。
static int setSeed(lua_State *L)
乱数のシード値を設定する。
unsigned int generateRandomSeed()
Generate nondeterministic random number for seed.
static int playBgm(lua_State *L)
BGM 再生を開始する。
std::unique_ptr< wchar_t[]> utf82wc(const char *in)
UTF-8 to wide char.
static int write(lua_State *L)
デバッグ出力する。
static int include(lua_State *L)
別の Lua ソースファイルを実行する。
static int addTexture(lua_State *L)
テクスチャリソースを登録する。
static int perf(lua_State *L)
メモリ上のバッファに高速なログ出力を行う。
static int addFont(lua_State *L)
フォントリソースを登録する。
static int generateSeed(lua_State *L)
乱数シード用の値を生成する。
static int drawTexture(lua_State *L)
テクスチャを描画する。