Yay working Lua API
This commit is contained in:
parent
6e9ea2dfbf
commit
12c80a3eab
4
Makefile
4
Makefile
|
@ -1,6 +1,6 @@
|
|||
CFLAGS +=
|
||||
LDFLAGS := -lm -lcairo -lxcb
|
||||
OBJECTS := obj/rootwindow.o obj/cairo_context.o
|
||||
LDFLAGS := -lm -lcairo -lxcb -llua
|
||||
OBJECTS := obj/common.o obj/rootwindow.o obj/cairo_context.o obj/api_draw.o
|
||||
|
||||
livewp: lib
|
||||
$(CC) $(CFLAGS) $(OBJECTS) src/main.c $(LDFLAGS) -o livewp
|
||||
|
|
10
example.lua
10
example.lua
|
@ -1,11 +1,12 @@
|
|||
|
||||
local img = assert(Image.load("/home/hkc/images/wallpapers/wallpaper.png"))
|
||||
local font = assert(Font.load("/usr/share/fonts/TTF/TerminusTTF.ttf"))
|
||||
-- local img = assert(Image.load("/home/hkc/images/wallpapers/wallpaper.png"))
|
||||
-- local font = assert(Font.load("/usr/share/fonts/TTF/TerminusTTF.ttf"))
|
||||
|
||||
function tick()
|
||||
function tick(t)
|
||||
-- Background
|
||||
Screen.clear(0xFF131313)
|
||||
Draw.clear(math.floor(t * 255))
|
||||
|
||||
--[[
|
||||
-- Color ARGB, x, y
|
||||
Draw.pixel(0xFFFF0000, 30, 30)
|
||||
|
||||
|
@ -29,6 +30,7 @@ function tick()
|
|||
|
||||
-- image ptr x, y, w, h, src_x, src_y, src_w, src_h
|
||||
Draw.image( img, 0, 0) --> Rect
|
||||
]]
|
||||
end
|
||||
|
||||
function unload()
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
#include "common.h"
|
||||
#include "api_draw.h"
|
||||
#include <cairo/cairo.h>
|
||||
#include <lua5.3/lua.h>
|
||||
#include <lua5.3/lauxlib.h>
|
||||
|
||||
int api_draw_clear(lua_State *L);
|
||||
|
||||
bool loadapi_draw(lua_State *lua) {
|
||||
const struct luaL_Reg api_draw[] = {
|
||||
{ "clear", api_draw_clear },
|
||||
};
|
||||
lua_newtable(lua);
|
||||
luaL_setfuncs(lua, api_draw, 0);
|
||||
lua_setglobal(lua, "Draw");
|
||||
return true;
|
||||
}
|
||||
|
||||
int api_draw_clear(lua_State *L) {
|
||||
printf("Draw.clear()\n");
|
||||
int color = luaL_checkinteger(L, 1);
|
||||
printf("Draw.clear(0x%08x)\n", color);
|
||||
cairo_t *cr = global_context.cairo_context.cairo;
|
||||
int r = (color >> 16) & 0xFF,
|
||||
g = (color >> 8) & 0xFF,
|
||||
b = color & 0xFF;
|
||||
cairo_set_source_rgb(cr, r / 255.0, g / 255.0, b / 255.0);
|
||||
cairo_paint(cr);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef _API_DRAW_H_
|
||||
#define _API_DRAW_H_
|
||||
|
||||
#include <lua5.3/lua.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool loadapi_draw(lua_State *lua);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,3 @@
|
|||
#include "common.h"
|
||||
|
||||
struct global_context global_context = { 0 };
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef _COMMON_H_
|
||||
#define _COMMON_H_
|
||||
|
||||
#include <lua5.3/lua.h>
|
||||
#include "cairo_context.h"
|
||||
|
||||
extern struct global_context {
|
||||
lua_State *lua;
|
||||
struct cairo_ctx cairo_context;
|
||||
double time;
|
||||
} global_context;
|
||||
|
||||
#endif
|
39
src/main.c
39
src/main.c
|
@ -1,3 +1,7 @@
|
|||
#include <lua5.2/lualib.h>
|
||||
#include <lua5.3/lua.h>
|
||||
#include <lua5.3/luaconf.h>
|
||||
#include <lua5.3/lauxlib.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
@ -5,13 +9,14 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include "cairo_context.h"
|
||||
|
||||
struct cairo_ctx ctx;
|
||||
#include "common.h"
|
||||
#include "api_draw.h"
|
||||
|
||||
volatile bool running = true;
|
||||
|
||||
void cleanup(void) {
|
||||
cairo_ctx_close(&ctx);
|
||||
cairo_ctx_close(&global_context.cairo_context);
|
||||
lua_close(global_context.lua);
|
||||
}
|
||||
|
||||
void sighandler(int sig) {
|
||||
|
@ -19,21 +24,39 @@ void sighandler(int sig) {
|
|||
}
|
||||
|
||||
int main(void) {
|
||||
if (!cairo_ctx_create(NULL, &ctx)) {
|
||||
if (!cairo_ctx_create(NULL, &global_context.cairo_context)) {
|
||||
fprintf(stderr, "Failed creating window or something\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
global_context.lua = luaL_newstate();
|
||||
luaL_openlibs(global_context.lua);
|
||||
loadapi_draw(global_context.lua);
|
||||
if (luaL_dofile(global_context.lua, "example.lua") == LUA_OK) {
|
||||
lua_pop(global_context.lua, lua_gettop(global_context.lua));
|
||||
}
|
||||
|
||||
|
||||
atexit(cleanup);
|
||||
signal(SIGINT, sighandler);
|
||||
|
||||
cairo_t *cr = global_context.cairo_context.cairo;
|
||||
for (float t = 0.0; running; t += 1.0 / 60.0) {
|
||||
cairo_begin(&ctx);
|
||||
global_context.time = t;
|
||||
cairo_begin(&global_context.cairo_context);
|
||||
{
|
||||
cairo_set_source_rgb(ctx.cairo, 0.0, 0.5 - sin(t) * 0.5, 0.5 - cos(t) * 0.5);
|
||||
cairo_paint(ctx.cairo);
|
||||
lua_getglobal(global_context.lua, "tick");
|
||||
if (lua_isfunction(global_context.lua, -1)) {
|
||||
lua_pushnumber(global_context.lua, (lua_Number)t);
|
||||
if (lua_pcall(global_context.lua, 1, 0, 0) == LUA_OK) {
|
||||
lua_pop(global_context.lua, lua_gettop(global_context.lua));
|
||||
} else {
|
||||
printf("ERROR: %s\n", lua_tostring(global_context.lua, lua_gettop(global_context.lua)));
|
||||
lua_pop(global_context.lua, lua_gettop(global_context.lua));
|
||||
}
|
||||
}
|
||||
}
|
||||
cairo_flush(&ctx);
|
||||
cairo_flush(&global_context.cairo_context);
|
||||
usleep(1000000 / 60.0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue