For me working with lua from c is some king of magic. So i need some help
I need to iterate a color matrix and change value in stream.
Something like that but in native.
for y=1, self.height do
local pre_calc_y = (y-1) * pre_calc
local nodes_y = nodes[y]
for x=1, width do
local color = nodes_y[x]
stream[index] = color.x * 255
stream[index + 1] = color.y * 255
stream[index + 2] = color.z * 255
index = index + 3
end
end
colors is matrix for colors (colors[y][x] return color for pixel at (x,y))
local buffer = buffer.create(self.width * self.height, { {name=hash("rgb"), type=buffer.VALUE_TYPE_UINT8, count=3} } )
local stream = buffer.get_stream(buffer, hash("rgb"))
native_draw.draw_pixels({width = 45, height = 60, colors = colors, stream = stream})
The problem that i don’t understand how to get value from table that is in another table.
I try start with small function.This function should convert lua array to c++ array. vmath.vector3 should be converted to int color.But i have no idea how to get x,y,z from vmath.vector3(), i get only 0.0 value
local v = vmath.vector3
local t = {v(1),v(2),v(3),v(4),v(5),v(56)}
native_draw.draw_pixels(t)
// myextension.cpp
// Extension lib defines
#define EXTENSION_NAME NativeDraw
#define LIB_NAME "NativeDraw"
#define MODULE_NAME "native_draw"
// include the Defold SDK
#include <dmsdk/sdk.h>
static double* array_to_vector(lua_State* L){
if(lua_gettop(L) == 1 && lua_istable(L, 1))
{
// for each entry in the table
size_t len = lua_objlen(L, 1);
double *buff = (double*)malloc(len*sizeof(double));
for(int i=1;i <= len; i++)
{
// get the entry to stack
lua_pushinteger(L, i);
lua_gettable(L, 1);
lua_pushstring(L, "x");
lua_gettable(L, 1);
buff[i] = lua_tonumber(L, -1);
printf("%f\n",buff[i]);
printf("%f\n",lua_tonumber(L, 2));
// remove entry from stack
lua_pop(L,1);
}
return buff;
}
}
static int drawPixels(lua_State* L)
{
array_to_vector(L);
return 1;
}
// Functions exposed to Lua
static const luaL_reg Module_methods[] =
{
{"draw_pixels", drawPixels},
{0, 0}
};
static void LuaInit(lua_State* L)
{
int top = lua_gettop(L);
// Register lua names
luaL_register(L, MODULE_NAME, Module_methods);
lua_pop(L, 1);
assert(top == lua_gettop(L));
}
dmExtension::Result AppInitializeMyExtension(dmExtension::AppParams* params)
{
return dmExtension::RESULT_OK;
}
dmExtension::Result InitializeMyExtension(dmExtension::Params* params)
{
// Init Lua
LuaInit(params->m_L);
printf("Registered %s Extension\n", MODULE_NAME);
return dmExtension::RESULT_OK;
}
dmExtension::Result AppFinalizeMyExtension(dmExtension::AppParams* params)
{
return dmExtension::RESULT_OK;
}
dmExtension::Result FinalizeMyExtension(dmExtension::Params* params)
{
return dmExtension::RESULT_OK;
}
DM_DECLARE_EXTENSION(EXTENSION_NAME, LIB_NAME, AppInitializeMyExtension, AppFinalizeMyExtension, InitializeMyExtension, 0, 0, FinalizeMyExtension)