Skip to content

Commit 8161487

Browse files
committed
IT'S ALIVE!
1 parent b4b2430 commit 8161487

File tree

8 files changed

+425
-17
lines changed

8 files changed

+425
-17
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(SRCS
3636
src/imgui-SFML.cpp
3737
src/filesys.cpp
3838
src/limgui.cpp
39+
src/lua_buffers.cpp
3940
${IMGUI}/imgui.cpp
4041
${IMGUI}/imgui_draw.cpp
4142
${IMGUI}/imgui_demo.cpp
@@ -48,6 +49,7 @@ set(HDRS
4849
src/limgui.h
4950
src/stb_image.h
5051
src/stb_image_write.h
52+
src/lua_buffers.h
5153
${LUA}/lua.hpp
5254
${LUA}/lualib.h
5355
)

projects/circle.lua

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +0,0 @@
1-
for i=1,10 do
2-
print(i)
3-
end
4-
color={0,0,0,0}
5-
6-
function update( )
7-
imgui.Begin("Hello")
8-
local changed
9-
changed,color=imgui.ColorEdit3("thing",color)
10-
imgui.End()
11-
end

projects/spirograph.lua

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
--[[ Example:
2+
imgui_config=make_config{
3+
{"debug_display",false},
4+
{"complexity",0.5,type="float"}, --implied min=0,max=1
5+
{"shapes","3"},
6+
{"w",3,type="int",min=1,max=5},
7+
}
8+
9+
Begin
10+
End
11+
Bullet
12+
BulletText
13+
RadioButton
14+
CollapsingHeader
15+
SliderFloat
16+
SliderAngle
17+
SliderInt
18+
InputText
19+
]]
20+
function make_config(tbl,defaults)
21+
local ret={}
22+
defaults=defaults or {}
23+
for i,v in ipairs(tbl) do
24+
ret[v[1]]=defaults[v[1]] or v[2]
25+
ret[i]=v
26+
end
27+
return ret
28+
end
29+
30+
img_buf=img_buf or buffers.Make("color")
31+
tick=tick or 0
32+
config=make_config({
33+
{"color",{0.5,0,0,1},type="color"},
34+
{"k",0.2,type="float"},
35+
{"l",0.4,type="float"},
36+
{"R",400,type="int",min=0,max=512},
37+
{"ticking",100,type="float",min=1,max=10000},
38+
},config)
39+
40+
function draw_config( tbl )
41+
for _,entry in ipairs(tbl) do
42+
local name=entry[1]
43+
local v=tbl[name]
44+
local k=name
45+
if type(v)=="boolean" then
46+
if imgui.Button(k) then
47+
tbl[k]=not tbl[k]
48+
end
49+
elseif type(v)=="string" then
50+
local changing
51+
changing,tbl[k]=imgui.InputText(k,tbl[k])
52+
entry.changing=changing
53+
else --if type(v)~="table" then
54+
55+
if entry.type=="int" then
56+
local changing
57+
changing,tbl[k]=imgui.SliderInt(k,tbl[k],entry.min or 0,entry.max or 100)
58+
entry.changing=changing
59+
elseif entry.type=="float" then
60+
local changing
61+
changing,tbl[k]=imgui.SliderFloat(k,tbl[k],entry.min or 0,entry.max or 1)
62+
entry.changing=changing
63+
elseif entry.type=="angle" then
64+
local changing
65+
changing,tbl[k]=imgui.SliderAngle(k,tbl[k],entry.min or 0,entry.max or 360)
66+
entry.changing=changing
67+
elseif entry.type=="color" then
68+
local changing
69+
changing,tbl[k]=imgui.ColorEdit4(k,tbl[k],true)
70+
entry.changing=changing
71+
end
72+
73+
end
74+
end
75+
end
76+
function pos( t )
77+
local k=config.k
78+
local l=config.l
79+
return config.R*((1-k)*math.cos(t)+l*k*math.cos(((1-k)/k)*t)),
80+
config.R*((1-k)*math.sin(t)-l*k*math.sin(((1-k)/k)*t))
81+
end
82+
function update( )
83+
imgui.Begin("Hello")
84+
85+
draw_config(config)
86+
local c_u8={config.color[1]*255,config.color[2]*255,config.color[3]*255,config.color[4]*255}
87+
if imgui.Button("Clear image") then
88+
local s=STATE.size
89+
for x=0,s[1]-1 do
90+
for y=0,s[2]-1 do
91+
img_buf:set(x,y,{0,0,0,0})
92+
end
93+
end
94+
end
95+
imgui.End()
96+
for i=1,config.ticking do
97+
local x,y=pos(tick/config.ticking);
98+
img_buf:set(x+512,y+512,c_u8)
99+
tick=tick+1
100+
end
101+
buffers.Present(img_buf)
102+
end

src/limgui.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <imgui.h>
44
#include <vector>
5+
6+
#include "lua.hpp"
7+
#include "lualib.h"
8+
#include "lauxlib.h"
59
int lua_absindex(lua_State *L, int i) {
610
if (i < 0 && i > LUA_REGISTRYINDEX)
711
i += lua_gettop(L) + 1;

src/limgui.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#pragma once
22

3-
#include "lua.hpp"
4-
#include "lualib.h"
5-
#include "lauxlib.h"
6-
3+
struct lua_State;
74
int lua_open_imgui(lua_State* L);
8-
5+
//NOTE: call this because on error, there might be unmatched imgui::begin/end(s)
96
void fixup_imgui_state();

src/lua_buffers.cpp

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#include "lua_buffers.h"
2+
3+
#include "SFML\Graphics.hpp"
4+
5+
#include "lua.hpp"
6+
#include "lualib.h"
7+
#include "lauxlib.h"
8+
9+
#include <unordered_map>
10+
enum class buffer_type {
11+
vector_u8x4,
12+
vector_float,
13+
};
14+
struct u8x4 {
15+
uint8_t r, g, b, a;
16+
};
17+
struct buffer_entry
18+
{
19+
int w;
20+
buffer_type t;
21+
};
22+
std::unordered_map<void*, buffer_entry> buffer_registry;
23+
24+
void get_current_size(lua_State* L, int& x, int& y)
25+
{
26+
lua_getglobal(L, "STATE");
27+
lua_getfield(L, -1, "size");
28+
29+
lua_rawgeti(L, -1, 1);
30+
x=lua_tointeger(L, -1);
31+
lua_pop(L, 1);
32+
33+
lua_rawgeti(L, -1, 2);
34+
y = lua_tointeger(L, -1);
35+
lua_pop(L, 1);
36+
37+
lua_pop(L, 2);
38+
}
39+
40+
41+
42+
template <typename T>
43+
struct buffer_value_access{
44+
static constexpr char* name();
45+
static int push(lua_State* L, const T& v);
46+
static T to_element(lua_State* L, int id);
47+
static std::vector<T>* check(lua_State* L, int id) { return *reinterpret_cast<std::vector<T>**>(luaL_checkudata(L, id, name())); }
48+
static int get_buffer(lua_State* L)
49+
{
50+
auto ptr = buffer_value_access<T>::check(L, 1);
51+
int x = luaL_checkinteger(L, 2);
52+
int y = luaL_checkinteger(L, 3);
53+
auto e = buffer_registry[ptr];
54+
auto& v = ptr->at(x*e.w + y); \
55+
return buffer_value_access<T>::push(L, v);
56+
}
57+
static int set_buffer(lua_State* L) {
58+
auto ptr = buffer_value_access<T>::check(L, 1);
59+
int x = luaL_checkinteger(L, 2);
60+
int y = luaL_checkinteger(L, 3);
61+
auto new_value = buffer_value_access<T>::to_element(L, 4);
62+
auto e = buffer_registry[ptr];
63+
ptr->at(x*e.w + y) = new_value;
64+
return 0;
65+
}
66+
static int del_buffer(lua_State* L) {
67+
auto ptr= check(L, 1);
68+
buffer_registry.erase(ptr); delete ptr;
69+
return 0;
70+
}
71+
static int len_buffer(lua_State* L) {
72+
auto ptr = check(L, 1);
73+
lua_pushnumber(L, ptr->size());
74+
return 1;
75+
}
76+
static int index_buffer(lua_State* L) {
77+
auto ptr = check(L, 1);
78+
int id = luaL_checkinteger(L, 2);
79+
auto& v = ptr->at(id);
80+
return push(L, v);
81+
}
82+
static int newindex_buffer(lua_State* L){
83+
auto ptr = check(L, 1);
84+
int id = luaL_checkinteger(L, 2);
85+
auto new_value = to_element(L, 3);
86+
ptr->at(id) = new_value;
87+
return 0;
88+
}
89+
static void resize_buffer(void* d, int w, int h){
90+
auto p = reinterpret_cast<std::vector<T>*>(d);
91+
p->resize(w*h);
92+
buffer_registry[d].w = w;
93+
}
94+
static int make_buffer(lua_State* L, int w, int h){
95+
auto ret = new std::vector<T>(w*h);
96+
buffer_registry[ret].w = w;
97+
auto np=lua_newuserdata(L,sizeof(ret));
98+
*reinterpret_cast<std::vector<T>**>(np)=ret;
99+
if (luaL_newmetatable(L, name()))
100+
{
101+
lua_pushcfunction(L, del_buffer);
102+
lua_setfield(L, -2, "__gc");
103+
lua_pushcfunction(L, len_buffer);
104+
lua_setfield(L, -2, "__len");
105+
106+
lua_pushcfunction(L, get_buffer);
107+
lua_setfield(L, -2, "get");
108+
lua_pushcfunction(L, set_buffer);
109+
lua_setfield(L, -2, "set");
110+
lua_pushvalue(L, -1);
111+
lua_setfield(L, -2, "__index");
112+
}
113+
lua_setmetatable(L, -2);
114+
return 1;
115+
}
116+
};
117+
118+
static int make_lua_auto_buffer(lua_State* L)
119+
{
120+
const char* buf_type = luaL_checkstring(L, 1);
121+
int x, y;
122+
get_current_size(L, x, y);
123+
124+
if (strcmp(buf_type, "color")==0)
125+
{
126+
return buffer_value_access<u8x4>::make_buffer(L,x,y);
127+
} else if (strcmp(buf_type, "float") == 0)
128+
{
129+
return buffer_value_access<float>::make_buffer(L, x, y);
130+
}
131+
}
132+
static int present_buffer(lua_State* L)
133+
{
134+
auto ptr= buffer_value_access<u8x4>::check(L, 1);
135+
lua_getglobal(L, "STATE");
136+
lua_getfield(L, -1, "texture");
137+
138+
auto tex=reinterpret_cast<sf::Texture*>(lua_touserdata(L, -1));
139+
lua_pop(L, 2);
140+
tex->update(reinterpret_cast<const sf::Uint8*>(ptr->data()));
141+
return 0;
142+
}
143+
static const luaL_Reg lua_buffers_lib[] = {
144+
{ "Make",make_lua_auto_buffer },
145+
{ "Present",present_buffer},
146+
{ NULL, NULL }
147+
};
148+
149+
int lua_open_buffers(lua_State * L)
150+
{
151+
luaL_newlib(L, lua_buffers_lib);
152+
153+
lua_setglobal(L, "buffers");
154+
155+
return 1;
156+
}
157+
158+
void resize_lua_buffers(int w, int h)
159+
{
160+
for (auto& v : buffer_registry)
161+
{
162+
switch (v.second.t)
163+
{
164+
#define DO_BUFFER_RESIZE(tname,name) case buffer_type::vector_##tname: buffer_value_access<tname>::resize_buffer(v.first,w,h);break
165+
DO_BUFFER_RESIZE(u8x4, color);
166+
DO_BUFFER_RESIZE(float, float);
167+
default:
168+
break;
169+
}
170+
}
171+
}
172+
#undef DO_BUFFER_RESIZE
173+
174+
template<>
175+
static constexpr char * buffer_value_access<u8x4>::name()
176+
{
177+
return "color_buffer";
178+
}
179+
180+
template<>
181+
static int buffer_value_access<u8x4>::push(lua_State * L, const u8x4& v)
182+
{
183+
lua_newtable(L);
184+
185+
lua_pushinteger(L, v.r);
186+
lua_rawseti(L, -2, 1);
187+
188+
lua_pushinteger(L, v.g);
189+
lua_rawseti(L, -2, 2);
190+
191+
lua_pushinteger(L, v.b);
192+
lua_rawseti(L, -2, 3);
193+
194+
lua_pushinteger(L, v.a);
195+
lua_rawseti(L, -2, 4);
196+
197+
return 1;
198+
}
199+
200+
template<>
201+
static u8x4 buffer_value_access<u8x4>::to_element(lua_State * L, int id)
202+
{
203+
u8x4 ret;
204+
luaL_checktype(L, id, LUA_TTABLE);
205+
lua_rawgeti(L, id, 1); ret.r = lua_tointeger(L, -1); lua_pop(L, 1);
206+
lua_rawgeti(L, id, 2); ret.g = lua_tointeger(L, -1); lua_pop(L, 1);
207+
lua_rawgeti(L, id, 3); ret.b = lua_tointeger(L, -1); lua_pop(L, 1);
208+
lua_rawgeti(L, id, 4); ret.a = lua_tointeger(L, -1); lua_pop(L, 1);
209+
return ret;
210+
}
211+
212+
template<>
213+
static constexpr char * buffer_value_access<float>::name()
214+
{
215+
return "float_buffer";
216+
}
217+
218+
template<>
219+
static int buffer_value_access<float>::push(lua_State * L, const float& v)
220+
{
221+
lua_pushnumber(L, v);
222+
return 1;
223+
}
224+
225+
template<>
226+
static float buffer_value_access<float>::to_element(lua_State * L, int id)
227+
{
228+
return luaL_checknumber(L, id);
229+
}

src/lua_buffers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
struct lua_State;
4+
int lua_open_buffers(lua_State* L);
5+
6+
void resize_lua_buffers(int w,int h);

0 commit comments

Comments
 (0)