Réecriture des types lua custom + fixs divers
This commit is contained in:
parent
ab44eac4ee
commit
105095efbc
3 changed files with 155 additions and 76 deletions
|
@ -82,7 +82,8 @@ Texture2D mkmap_tex(Map *map){
|
||||||
Rectangle rectb = recta;
|
Rectangle rectb = recta;
|
||||||
rectb.x = x*32;
|
rectb.x = x*32;
|
||||||
rectb.y = y*32;
|
rectb.y = y*32;
|
||||||
ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb, WHITE);
|
ImageDraw(&mapimg, tile_index[tiles[map->width*y + x]], recta, rectb,
|
||||||
|
WHITE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Texture2D maptex = LoadTextureFromImage(mapimg);
|
Texture2D maptex = LoadTextureFromImage(mapimg);
|
||||||
|
|
219
src/lua_inter.c
219
src/lua_inter.c
|
@ -13,6 +13,69 @@
|
||||||
|
|
||||||
Game *gl_game;
|
Game *gl_game;
|
||||||
|
|
||||||
|
typedef void (lua_affect_t)(lua_State*, void *, int);
|
||||||
|
|
||||||
|
struct LuaAffect {
|
||||||
|
|
||||||
|
lua_affect_t *func;
|
||||||
|
int arg;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap {
|
||||||
|
|
||||||
|
char *type_name;
|
||||||
|
|
||||||
|
int n_fields;
|
||||||
|
char **field_names;
|
||||||
|
struct LuaAffect field_affect[];
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
void lua_index_custom(lua_State *L, struct TypeMap *map){
|
||||||
|
void **ptr = luaL_checkudata(L, 1, map->type_name);
|
||||||
|
const char *name = luaL_checkstring(L, 2);
|
||||||
|
for(int i = 0; i < map->n_fields; i++){
|
||||||
|
if(!strcmp(name,map->field_names[i])){
|
||||||
|
map->field_affect[i].func(L, *((char**)(ptr)), map->field_affect[i].arg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char tbuf[256];
|
||||||
|
snprintf(tbuf, 256, "Invalid index for %s", map->type_name);
|
||||||
|
luaL_error(L, tbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_index_u8(lua_State *L, void *base, int offset){
|
||||||
|
uint8_t *ptr = (void*)(((char*)base) + offset);
|
||||||
|
lua_pushinteger(L, *ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_index_u16(lua_State *L, void *base, int offset){
|
||||||
|
uint16_t *ptr = (void*)(((char*)base) + offset);
|
||||||
|
lua_pushinteger(L, *ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_index_i32(lua_State *L, void *base, int offset){
|
||||||
|
int32_t *ptr = (void*)(((char*)base) + offset);
|
||||||
|
lua_pushinteger(L, *ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_newindex_u8(lua_State *L, void *base, int offset){
|
||||||
|
uint8_t *ptr = (void*)(((char*)base) + offset);
|
||||||
|
*ptr = luaL_checkinteger(L, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_newindex_u16(lua_State *L, void *base, int offset){
|
||||||
|
uint16_t *ptr = (void*)(((char*)base) + offset);
|
||||||
|
*ptr = luaL_checkinteger(L, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_newindex_i32(lua_State *L, void *base, int offset){
|
||||||
|
uint16_t *ptr = (void*)(((char*)base) + offset);
|
||||||
|
*ptr = luaL_checkinteger(L, 3);
|
||||||
|
}
|
||||||
|
|
||||||
static int lua_V2d_index(lua_State *L){
|
static int lua_V2d_index(lua_State *L){
|
||||||
V2d **ptr = luaL_checkudata(L, 1, "V2d");
|
V2d **ptr = luaL_checkudata(L, 1, "V2d");
|
||||||
V2d *u = *ptr;
|
V2d *u = *ptr;
|
||||||
|
@ -69,90 +132,102 @@ static int lua_storage_newindex(lua_State *L){
|
||||||
uint8_t *storage = *ptr;
|
uint8_t *storage = *ptr;
|
||||||
int pos = luaL_checkinteger(L, 2);
|
int pos = luaL_checkinteger(L, 2);
|
||||||
int val = luaL_checkinteger(L, 3);
|
int val = luaL_checkinteger(L, 3);
|
||||||
if(pos >= 0 && pos < 5){
|
if(pos >= 0 && pos < 5)
|
||||||
storage[pos] = val;
|
storage[pos] = val;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
return luaL_error(L, "Invalid index for Storage");
|
return luaL_error(L, "Invalid index for Storage");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lua_machine_pos_index(lua_State *L, void *mach_, int offset){
|
||||||
|
(void)offset;
|
||||||
|
Machine *mach = mach_;
|
||||||
|
V2d **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
|
||||||
|
*ptr = &mach->pos;
|
||||||
|
luaL_newmetatable(L, "V2d");
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_machine_pos_newindex(lua_State *L, void *mach_, int offset){
|
||||||
|
(void)offset;
|
||||||
|
Machine *mach = mach_;
|
||||||
|
V2d **ptr = luaL_checkudata(L, 3, "V2d");
|
||||||
|
memmove(&mach->pos, *ptr, sizeof(V2d));
|
||||||
|
}
|
||||||
|
|
||||||
|
void lua_machine_storage_index(lua_State *L, void *mach_, int offset){
|
||||||
|
(void)offset;
|
||||||
|
Machine *mach = mach_;
|
||||||
|
uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
|
||||||
|
*ptr = mach->storage;
|
||||||
|
luaL_newmetatable(L, "Storage");
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define OFFSET(field) (size_t)((char*)(BASE.field)-(char*)(BASE))
|
||||||
|
|
||||||
|
Machine bmach;
|
||||||
|
|
||||||
|
#define BASE &bmach
|
||||||
|
|
||||||
|
char *mach_index_names[] = {
|
||||||
|
"pos",
|
||||||
|
"type",
|
||||||
|
"assign_workers",
|
||||||
|
"active",
|
||||||
|
"storage",
|
||||||
|
"id"
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap machine_index_map = {
|
||||||
|
.type_name = "Machine",
|
||||||
|
.n_fields = 6,
|
||||||
|
.field_names = mach_index_names,
|
||||||
|
.field_affect = {
|
||||||
|
{lua_machine_pos_index, 0},
|
||||||
|
{lua_index_u8, OFFSET(type)},
|
||||||
|
{lua_index_u8, OFFSET(assign_workers)},
|
||||||
|
{lua_index_u8, OFFSET(active)},
|
||||||
|
{lua_machine_storage_index, 0},
|
||||||
|
{lua_index_u16, OFFSET(id)}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TypeMap machine_newindex_map = {
|
||||||
|
.type_name = "Machine",
|
||||||
|
.n_fields = 6,
|
||||||
|
.field_names = mach_index_names,
|
||||||
|
.field_affect = {
|
||||||
|
{lua_machine_pos_newindex, 0},
|
||||||
|
{lua_newindex_u8, OFFSET(type)},
|
||||||
|
{lua_newindex_u8, OFFSET(assign_workers)},
|
||||||
|
{lua_newindex_u8, OFFSET(active)},
|
||||||
|
{NULL, 0}, // Shouldn't happen - TODO : Fix when it happens
|
||||||
|
{lua_newindex_u16, OFFSET(id)}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef BASE
|
||||||
|
|
||||||
|
|
||||||
|
#undef OFFSET
|
||||||
|
|
||||||
static int lua_machine_index(lua_State *L){
|
static int lua_machine_index(lua_State *L){
|
||||||
void **ptr = luaL_checkudata(L, 1, "Machine");
|
lua_index_custom(L, &machine_index_map);
|
||||||
Machine *mach = *ptr;
|
|
||||||
const char *name = luaL_checkstring(L, 2);
|
|
||||||
int ret;
|
|
||||||
if(!strcmp(name, "pos")){
|
|
||||||
V2d **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
|
|
||||||
*ptr = &mach->pos;
|
|
||||||
luaL_newmetatable(L, "V2d");
|
|
||||||
lua_setmetatable(L, -2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "type")){
|
|
||||||
ret = mach->type;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "assign_workers")){
|
|
||||||
ret = mach->assign_workers;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "active")){
|
|
||||||
ret = mach->active;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "storage")){
|
|
||||||
uint8_t **ptr = lua_newuserdatauv(L, sizeof(void*), 0);
|
|
||||||
*ptr = mach->storage;
|
|
||||||
luaL_newmetatable(L, "Storage");
|
|
||||||
lua_setmetatable(L, -2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "id")){
|
|
||||||
ret = mach->id;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return luaL_error(L, "Invalid index for Machine");
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lua_machine_newindex(lua_State *L){
|
static int lua_machine_newindex(lua_State *L){
|
||||||
void **ptr = luaL_checkudata(L, 1, "Machine");
|
lua_index_custom(L, &machine_newindex_map);
|
||||||
Machine *mach = *ptr;
|
|
||||||
const char *name = luaL_checkstring(L, 2);
|
|
||||||
int prm;
|
|
||||||
V2d *pos = NULL;
|
|
||||||
if(lua_isinteger(L, -1))
|
|
||||||
prm = luaL_checkinteger(L, 3);
|
|
||||||
else{
|
|
||||||
V2d **ptr = luaL_checkudata(L, 3, "V2d");
|
|
||||||
pos = *ptr;
|
|
||||||
}
|
|
||||||
if(!strcmp(name, "pos") && pos){
|
|
||||||
memmove(&mach->pos, pos, sizeof(V2d));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "type")){
|
|
||||||
mach->type = prm;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "assign_workers")){
|
|
||||||
mach->assign_workers = prm;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "active")){
|
|
||||||
mach->active = prm;
|
|
||||||
}
|
|
||||||
else if(!strcmp(name, "id")){
|
|
||||||
mach->id = prm;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return luaL_error(L, "Invalid index for Machine");
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int lua_worker_index(lua_State *L){
|
||||||
|
(void)L;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// add_machine(x, y)
|
// add_machine(x, y)
|
||||||
static int lua_add_machine(lua_State *L){
|
static int lua_add_machine(lua_State *L){
|
||||||
int x = luaL_checkinteger(L, 1);
|
int x = luaL_checkinteger(L, 1);
|
||||||
|
@ -282,7 +357,7 @@ void lua_onmapload(Game *game, int map){
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua_mt_init(lua_State *L){
|
void lua_mt_init(lua_State *L){
|
||||||
for(int i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
for(size_t i = 0; i < sizeof(exported_funcs)/sizeof(*exported_funcs); i++)
|
||||||
lua_register(L, exported_funcs[i].name, exported_funcs[i].fn);
|
lua_register(L, exported_funcs[i].name, exported_funcs[i].fn);
|
||||||
|
|
||||||
luaL_newmetatable(L, "Machine");
|
luaL_newmetatable(L, "Machine");
|
||||||
|
@ -308,7 +383,7 @@ void script_init(Game *game){
|
||||||
char tmpbuf[4096];
|
char tmpbuf[4096];
|
||||||
gl_game = game;
|
gl_game = game;
|
||||||
|
|
||||||
char *cwd = GetApplicationDirectory();
|
const char *cwd = GetApplicationDirectory();
|
||||||
snprintf(tmpbuf, 4096, "%sscript", cwd);
|
snprintf(tmpbuf, 4096, "%sscript", cwd);
|
||||||
FilePathList scripts = LoadDirectoryFilesEx(tmpbuf,".lua",true);
|
FilePathList scripts = LoadDirectoryFilesEx(tmpbuf,".lua",true);
|
||||||
|
|
||||||
|
@ -318,7 +393,7 @@ void script_init(Game *game){
|
||||||
puts("Fail 318");
|
puts("Fail 318");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(int i = 0; i < scripts.count; i++){
|
for(uint i = 0; i < scripts.count; i++){
|
||||||
lstates[i] = luaL_newstate();
|
lstates[i] = luaL_newstate();
|
||||||
lua_mt_init(lstates[i]);
|
lua_mt_init(lstates[i]);
|
||||||
|
|
||||||
|
|
9
src/ui.c
9
src/ui.c
|
@ -263,9 +263,12 @@ void init_ui(){
|
||||||
// Android exclusive key -> no exit key
|
// Android exclusive key -> no exit key
|
||||||
SetExitKey(KEY_MENU);
|
SetExitKey(KEY_MENU);
|
||||||
|
|
||||||
gui_info = (GUIInfo){{&game_widget, &main_menu, &worker_widget, &machine_widget, &pause_menu},
|
gui_info = (GUIInfo){
|
||||||
{false, true, false, false, false},
|
{&game_widget, &main_menu, &worker_widget, &machine_widget, &pause_menu},
|
||||||
NULL};
|
{false, true, false, false, false},
|
||||||
|
NULL,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_widgets(Game *game){
|
void draw_widgets(Game *game){
|
||||||
|
|
Loading…
Add table
Reference in a new issue