- Luaの公式サイトからLuaのソースファイルをダウンロード
- Visual Studio の新規プロジェクトで「Win32コンソールアプリケーション」のプロジェクトを作成する
- Luaの公式からダウンロードしたLuaのsrcフォルダにあるファイル(lua.c/luac.c以外)をプロジェクトにコピー(こうすることでスタティックライブラリのリンクを使わなくてもOKになります)
- プロジェクトの文字コードを「マルチバイト文字列を使用する」(リリース&デバッグ)、「コード生成」で「マルチスレッドデバッグ(デバッグ)」「マルチスレッド(リリース)」に設定して、「全般」をクリックした後に「追加のインクルード」のところでLua.hppがあるフォルダを指定する
- あとはビルドをするだけです。
- 添付ファイルのように表示されたら成功となります。
サンプルプログラム
//main.cpp
#include
#include "lua.hpp"
#include "PrintStack.h"
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
//Luaのスタックに各種プッシュしてみる
lua_pushnumber(L, 10.5); //数値をプッシュ
lua_pushboolean(L, true); //ブール値をプシュ
lua_pushstring(L, "Hello world!"); //文字列をプッシュ
lua_pushnil(L); //nilをプッシュ
lua_pushliteral(L, "リテラル文字列の表示"); //リテラル文字列をプッシュ
//Luaのスタックを表示
PrintStack(L);
lua_close(L);
return 0;
}
//PrintStack.cpp
#include "PrintStack.h"
// スタックの指定インデックスのアイテムの内容を表示する
static void PrintStackItem(lua_State *L, int idx)
{
int type = lua_type(L, idx);
switch (type){
case LUA_TSTRING:
// 文字列アイテムの内容表示
printf("index %2d : type = %s : \"%s\"\n",
idx, lua_typename(L, type), lua_tostring(L, idx));
break;
case LUA_TNUMBER:
// 数値アイテムの内容表示
printf("index %2d : type = %s : %f\n",
idx, lua_typename(L, type), lua_tonumber(L, idx));
break;
case LUA_TBOOLEAN:
// ブール値アイテムの内容表示
printf("index %2d : type = %s : \"%s\"\n",
idx, lua_typename(L, type), lua_toboolean(L, idx) ? "true" : "false");
break;
default:
// その他ならば型だけ表示
printf("index %2d : type = %s\n", idx, lua_typename(L, type));
break;
}
}
// スタックのアイテムの内容を一覧で出力する
void PrintStack(lua_State *L)
{
printf("----- stack -----\n");
int top = lua_gettop(L);
// 正のインデックスで指定
for (int i = top; i >= 1; i--){
PrintStackItem(L, i);
}
printf("-----------------\n");
for (int i = -1; i >= -top; i--){
PrintStackItem(L, i);
}
printf("-----------------\n");
}