summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/Resource/Resource.bind.cpp
blob: a10ed6928f78ad0709f53973979889cf9ee9bd5a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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);
    if (channels == 1)
    {
        data->format = EPixelFormat::PixelFormat_R;
        data->type = EPixelElementType::PixelType_UNSIGNED_BYTE;
    }
    else if (channels == 3)
    {
        data->format = EPixelFormat::PixelFormat_RGB;
        data->type = EPixelElementType::PixelType_UNSIGNED_BYTE;
    }
    else if(channels == 4)
    {
        data->format = EPixelFormat::PixelFormat_RGBA;
        data->type = EPixelElementType::PixelType_UNSIGNED_BYTE;
    }
    else
    {
        Assert(false); // 暂时不支持其他格式
    }

	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;
}