summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/Resource/Resource.bind.cpp
blob: a09460e05a73a3a20371b2ce32d7f61cdebfbfa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Runtime/Lua/LuaHelper.h"
#include "Runtime/Debug/Log.h"
#include "Runtime/FileSystem/ImageJobs.h"
#include "Runtime/Graphics/ImageData.h"
#define STB_IMAGE_IMPLEMENTATION
#include "ThirdParty/stb/stb_image.h"

using namespace LuaBind;

// Resource.LoadImageDataAsync(path, callback)
int LoadImageDataJob(lua_State* L)
{
	LUA_BIND_STATE(L);
	LUA_BIND_CHECK(L, "SF");

	cc8* path = state.GetValue(1, "");

	ReadImageFileJob* job = new ReadImageFileJob(state.GetVM(), 2, path);

	JobSystem::Instance()->AddJobAtEnd(job);

	return 0;
}

int LoadImageData(lua_State* L)
{
	LUA_BIND_STATE(L);

	const char* path = state.GetValue<const char*>(1, "");

	stbi_set_flip_vertically_on_load(true);
	ImageData* data = new ImageData(state.GetVM());
	int channels;
	data->pixels = stbi_load(path, &data->width, &data->height, &channels, 0);
	data->format = EPixelFormat::PixelFormat_RGB;
	data->type = EPixelElementType::PixelType_UNSIGNED_BYTE;

	data->PushUserdata(state);
	return 1;
}

static luaL_Reg funcs[] = {
	{ "LoadImageData", LoadImageData },
	{ "LoadImageDataJob", LoadImageDataJob },
	{0, 0}
};

int luaopen_GameLab_Engine_Resource(lua_State* L)
{
	log_info_tag("Scripting", "luaopen_GameLab_Engine_Resource()");

	LUA_BIND_STATE(L);

	state.PushGlobalNamespace();
	state.PushNamespace("GameLab");
	state.PushNamespace("Engine");
	state.PushNamespace("Resource");

	state.RegisterMethods(funcs);

	return 1;
}