添加LibFS
This commit is contained in:
parent
84eda56087
commit
8d7d52677a
@ -6,6 +6,7 @@
|
|||||||
#include "SDL2/include/SDL_mixer.h"
|
#include "SDL2/include/SDL_mixer.h"
|
||||||
#include "SDL2/include/SDL_ttf.h"
|
#include "SDL2/include/SDL_ttf.h"
|
||||||
#include "SDL2/include/SDL_image.h"
|
#include "SDL2/include/SDL_image.h"
|
||||||
|
#include "PlatAPI.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void check(lua_State* L, const vector<int>& typearr)
|
void check(lua_State* L, const vector<int>& typearr)
|
||||||
@ -706,6 +707,37 @@ struct MusicPlayer
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct LibFS
|
||||||
|
{
|
||||||
|
static int listdir(lua_State* L)
|
||||||
|
{
|
||||||
|
check(L, { LUA_TSTRING });
|
||||||
|
vector<FileInfo> vec = ListDir(lua_tostring(L, 1));
|
||||||
|
int sz = vec.size();
|
||||||
|
lua_newtable(L);
|
||||||
|
for (lua_Integer i = 0; i < sz; i++)
|
||||||
|
{
|
||||||
|
const FileInfo& info = vec[i];
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_pushstring(L, info.name.c_str());
|
||||||
|
lua_setfield(L, -2, "name");
|
||||||
|
lua_pushstring(L, info.type ? "dir" : "file");
|
||||||
|
lua_setfield(L, -2, "type");
|
||||||
|
lua_pushinteger(L, info.size);
|
||||||
|
lua_setfield(L, -2, "size");
|
||||||
|
lua_seti(L, -2, i + 1);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
static int create(lua_State* L)
|
||||||
|
{
|
||||||
|
lua_newtable(L);
|
||||||
|
setfn(L, listdir, "listdir");
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#define reg_in_lua(L, cfunc, name) lua_pushcfunction(L, cfunc); lua_setglobal(L, name)
|
#define reg_in_lua(L, cfunc, name) lua_pushcfunction(L, cfunc); lua_setglobal(L, name)
|
||||||
|
|
||||||
void check_init(lua_State* L, const char* name, int ret, int expected = 0)
|
void check_init(lua_State* L, const char* name, int ret, int expected = 0)
|
||||||
@ -732,12 +764,13 @@ int InitEngine(lua_State* L)
|
|||||||
check_init(L, "AUDIO", ret);
|
check_init(L, "AUDIO", ret);
|
||||||
Mix_AllocateChannels(16);
|
Mix_AllocateChannels(16);
|
||||||
|
|
||||||
lua_newtable(L);
|
|
||||||
reg_in_lua(L, Window::create, "Window");
|
reg_in_lua(L, Window::create, "Window");
|
||||||
reg_in_lua(L, Renderer::create, "Renderer");
|
reg_in_lua(L, Renderer::create, "Renderer");
|
||||||
reg_in_lua(L, Font::create, "Font");
|
reg_in_lua(L, Font::create, "Font");
|
||||||
reg_in_lua(L, Music::create, "Music");
|
reg_in_lua(L, Music::create, "Music");
|
||||||
reg_in_lua(L, MusicPlayer::create, "MusicPlayer");
|
reg_in_lua(L, MusicPlayer::create, "MusicPlayer");
|
||||||
|
|
||||||
|
LibFS::create(L); lua_setglobal(L, "fs");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
40
PlatAPI.cpp
Normal file
40
PlatAPI.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "PlatAPI.h"
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<FileInfo> ListDir(const std::string& dirName)
|
||||||
|
{
|
||||||
|
vector<FileInfo> vec;
|
||||||
|
WIN32_FIND_DATAA findData;
|
||||||
|
LARGE_INTEGER filesize;
|
||||||
|
string patternName = dirName + "\\*";
|
||||||
|
HANDLE hand = FindFirstFileA(patternName.c_str(), &findData);
|
||||||
|
if (hand == INVALID_HANDLE_VALUE)
|
||||||
|
{
|
||||||
|
cout << "Failed to ListDir: " << GetLastError() << endl;
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
do
|
||||||
|
{
|
||||||
|
FileInfo f;
|
||||||
|
f.name = std::string(findData.cFileName);
|
||||||
|
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||||
|
{
|
||||||
|
f.type = 1;
|
||||||
|
f.size = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
f.type = 0;
|
||||||
|
filesize.LowPart = findData.nFileSizeLow;
|
||||||
|
filesize.HighPart = findData.nFileSizeHigh;
|
||||||
|
f.size = filesize.QuadPart;
|
||||||
|
}
|
||||||
|
vec.push_back(f);
|
||||||
|
|
||||||
|
} while (FindNextFileA(hand, &findData));
|
||||||
|
|
||||||
|
FindClose(hand);
|
||||||
|
return vec;
|
||||||
|
}
|
12
PlatAPI.h
Normal file
12
PlatAPI.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct FileInfo
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
int type;
|
||||||
|
int size;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<FileInfo> ListDir(const std::string& dirName);
|
@ -1,8 +1,20 @@
|
|||||||
wnd = Window("Hello", 1024, 768)
|
wnd = Window("Hello", 1024, 768)
|
||||||
rnd = Renderer(wnd)
|
rnd = Renderer(wnd)
|
||||||
font = Font("asserts/msyh.ttf", 18)
|
font = Font("asserts/msyh.ttf", 18)
|
||||||
music = Music("asserts/mp3/bgm1.mp3")
|
|
||||||
musicPlayer = MusicPlayer()
|
musicPlayer = MusicPlayer()
|
||||||
|
music = Music("asserts/mp3/bgm1.mp3")
|
||||||
|
|
||||||
|
|
||||||
|
all_bqb = fs.listdir("asserts/bqb_all")
|
||||||
|
|
||||||
|
for i, info in ipairs(all_bqb) do
|
||||||
|
print (i, info.name, info.type, info.size)
|
||||||
|
end
|
||||||
|
|
||||||
|
texture_track = {}
|
||||||
|
setmetatable(texture_track, {
|
||||||
|
__mode = "k"
|
||||||
|
})
|
||||||
|
|
||||||
wnd:setTimeout(function()
|
wnd:setTimeout(function()
|
||||||
print("Playing music")
|
print("Playing music")
|
||||||
@ -12,24 +24,38 @@ end, 3000)
|
|||||||
|
|
||||||
wnd:on('click', function(x, y)
|
wnd:on('click', function(x, y)
|
||||||
print("Clicked", x, y)
|
print("Clicked", x, y)
|
||||||
rnd:clear()
|
local t = font:renderText(rnd, string.format("%.0f,%.0f", x, y), {r=255,g=255,b=255,a=0,type="color"})
|
||||||
local t = font:renderText(rnd, string.format("%f-%f", x, y), {r=255,g=255,b=255,a=0,type="color"})
|
texture_track[t] = true
|
||||||
rnd:copyTo(t, {x=x,y=y,type="point"})
|
rnd:copyTo(t, {x=x,y=y,type="point"})
|
||||||
|
t:close()
|
||||||
rnd:update()
|
rnd:update()
|
||||||
end)
|
end)
|
||||||
wnd:on('quit', function()
|
wnd:on('quit', function()
|
||||||
print("before quit")
|
print("before quit")
|
||||||
end)
|
end)
|
||||||
wnd:on('keydown', function(key)
|
wnd:on('keydown', function(key)
|
||||||
|
if key == string.byte('q') then
|
||||||
|
local current = collectgarbage("count")
|
||||||
|
collectgarbage("collect")
|
||||||
|
print("Before GC: ", current)
|
||||||
|
print("After GC: ", collectgarbage("count"))
|
||||||
|
return
|
||||||
|
elseif key == string.byte("w") then
|
||||||
|
local cnt = 0
|
||||||
|
for k in pairs(texture_track) do
|
||||||
|
cnt = cnt + 1
|
||||||
|
end
|
||||||
|
print ("Tracked texture: ", cnt)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
print("Keydown", key)
|
print("Keydown", key)
|
||||||
rnd:clear()
|
rnd:clear()
|
||||||
|
local t = rnd:loadTexture("asserts/bqb_all/" .. all_bqb[math.random(#all_bqb)].name)
|
||||||
|
texture_track[t] = true
|
||||||
|
rnd:copyTo(t, {x=0,y=0,type="point"})
|
||||||
|
t:close()
|
||||||
rnd:update()
|
rnd:update()
|
||||||
wnd:setTimeout(function()
|
|
||||||
print("timer callback.")
|
|
||||||
rnd:clear()
|
|
||||||
rnd:copyTo(rnd:loadTexture("asserts/bqb/1.jpg"), {x=0,y=0,type="point"})
|
|
||||||
rnd:update()
|
|
||||||
end, 1000)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
xpcall(function()
|
xpcall(function()
|
||||||
@ -37,3 +63,4 @@ xpcall(function()
|
|||||||
end, function()
|
end, function()
|
||||||
print(debug.traceback())
|
print(debug.traceback())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user