Skip to content

Commit

Permalink
foundation for vector lua type + reverted keys in device context
Browse files Browse the repository at this point in the history
  • Loading branch information
gabekz committed Jul 31, 2023
1 parent 3ff66ce commit a4c668b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
file(GLOB_RECURSE API_SOURCES CONFIGURE_DEPENDS
"${API_DIR}/common.h"
"${API_DIR}/llib_goodsack.c"
"${API_DIR}/llib_vector.c"
"${API_DIR}/goodsack/llib_time.c"
"${API_DIR}/goodsack/llib_input.cpp")

Expand Down
11 changes: 10 additions & 1 deletion demo/demo_hot/Resources/scripts/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ package.path = '../demo/demo_hot/Resources/scripts/?.lua;' .. package.path
SYSTEM_RUN('systems.weapon_controller')
SYSTEM_RUN('systems.weapon_sway')

print("package.path: "..package.path.."")
print("package.path: "..package.path.."")

-- test creating a FOO class

local foo = Vector.new(12)
local bar = Vector(24)

print(Vector[13])
print(foo:DoSomething())
print(bar:DoSomething())
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local pos_standby = {
}

local fov_aiming = 30
local fov_standby = 55
local fov_standby = 45
local fov_lerp_speed = 10

-------------------------------------
Expand Down Expand Up @@ -79,6 +79,10 @@ function system.update(entity)
handle_input(entity, new_rotation)
else
entity.Transform.position = pos_standby

-- restore FOV
local entity_camera = entity.Weapon.entity_camera
entity_camera.Camera.fov = math.Lerp(entity_camera.Camera.fov, fov_standby, time.DeltaTime() * fov_lerp_speed);
end

-- update rotation
Expand Down
74 changes: 74 additions & 0 deletions src/api/llib/llib_vector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "common.h"
#define VECTOR_LIB "goodsack.vector"

#include <stdio.h>
#include <stdlib.h>

#include <util/logger.h>

typedef struct // Vector_lua_t
{
int x;
} Vector;

static int
vector_gc(lua_State *L)
{
LOG_TRACE("## __gc\n");
Vector *foo = *(Vector **)luaL_checkudata(L, 1, VECTOR_LIB);
free(foo);
return 0;
}

static int
vector_DoSomething(lua_State *L)
{
LOG_TRACE("## DoSomething\n");
Vector *foo = *(Vector **)luaL_checkudata(L, 1, VECTOR_LIB);
lua_pushinteger(L, foo->x * 20);
return 1;
}

static int
vector_new(lua_State *L)
{
LOG_TRACE("## new\n");
Vector *foo = malloc(sizeof(Vector));
int i = 1 + lua_istable(L, 1);
foo->x = !lua_isnoneornil(L, i) ? luaL_checkinteger(L, i) : 0;
*(Vector **)lua_newuserdata(L, sizeof(Vector *)) = foo;
luaL_setmetatable(L, VECTOR_LIB);
return 1;
}

static int
vector_index(lua_State *L)
{
LOG_TRACE("## index\n");
int i = luaL_checkinteger(L, 2);
lua_pushinteger(L, i);
return 1;
}

int
luaopen_goodsack_vector(lua_State *L)
{
// instance functions
static const luaL_Reg meta[] = {{"__gc", vector_gc}, {NULL, NULL}};
static const luaL_Reg meth[] = {{"DoSomething", vector_DoSomething},
{NULL, NULL}};
luaL_newmetatable(L, VECTOR_LIB);
luaL_setfuncs(L, meta, 0);
luaL_newlib(L, meth);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);

// static functions
static const luaL_Reg static_meta[] = {
{"__index", vector_index}, {"__call", vector_new}, {NULL, NULL}};
static const luaL_Reg static_meth[] = {{"new", vector_new}, {NULL, NULL}};
luaL_newlib(L, static_meth);
luaL_newlib(L, static_meta);
lua_setmetatable(L, -2);
return 1;
}
2 changes: 2 additions & 0 deletions src/api/lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ end
input = require('goodsack.input')
time = require('goodsack.time')

Vector = require('goodsack.vector')


--[[---------------------------------------------------------
Extensions
Expand Down
4 changes: 2 additions & 2 deletions src/core/device/device_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ static void
_key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
// Quit the application
if (key == GLFW_KEY_F1 && action == GLFW_PRESS) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}

// Toggle cursor state
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
if (key == GLFW_KEY_F1 && action == GLFW_PRESS) {
Input deviceInput = device_getInput();
device_setCursorState(!deviceInput.cursor_state.is_locked,
!deviceInput.cursor_state.is_visible);
Expand Down
3 changes: 3 additions & 0 deletions src/wrapper/lua/lua_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <entity/lua/reg_system.hpp>

#include <wrapper/lua/lua_debug.h>
#include <wrapper/lua/lua_reg_print.h>>

#include <entity/v1/ecs.h>

Expand All @@ -19,6 +20,8 @@ LuaInit(const char *file, ECS *ecs)
lua_State *L = luaL_newstate();
luaL_openlibs(L);

luaopen_luaprintlib(L);

entity::LuaEventStore::Initialize(L, ecs);

// add the global C function (register system) to lua
Expand Down

0 comments on commit a4c668b

Please sign in to comment.