Compare commits

..

2 Commits

Author SHA1 Message Date
a4b77f420a lua apicka 2023-12-25 18:48:16 +01:00
5f331dd548 shadery a scripty 2023-12-25 18:47:55 +01:00
35 changed files with 1733 additions and 156 deletions

4
.gitignore vendored
View File

@ -365,4 +365,6 @@ FodyWeavers.xsd
#TSR
main/
main/*
!main/scripts
!main/shaders

27
main/scripts/autoexec.lua Normal file
View File

@ -0,0 +1,27 @@
g_require("scripts/thread.lua")
g_require("scripts/input.lua")
g_require("scripts/player.lua")
print("autoexec")
inputmap["KEY_W"] = "forward"
inputmap["KEY_S"] = "backward"
inputmap["KEY_A"] = "left"
inputmap["KEY_D"] = "right"
local player = ent_spawn(-1, 1, 0, 0, 0, 0, 1, 1, 1)
mdl_set(player, "models/test_skeletal.mdl.json")
local playercamera = false
setupplayer(player)
onkeypress["KEY_T"] = function ()
if playercamera then
g_detachcamera()
else
g_attachcamera(player, 0, 1, 0)
end
playercamera = not playercamera
end

View File

@ -0,0 +1,140 @@
---@meta
--- Game
---@param interval number
---@param repeating boolean
---@param callback fun()
---@return number
function g_settimer(interval, repeating, callback) end
---@param timerid number
function g_cleartimer(timerid) end
---@return number
function g_getframe() end
---@return number
function g_gettps() end
---@param path string
---@return nil
function g_require(path) end
---@return number x
---@return number y
---@return number z
function g_getcameraforward() end
---@return number x
---@return number z
function g_getcameraforwardxz() end
---@return number x
---@return number z
function g_getcamerarightxz() end
---@param ent number
---@param x number
---@param y number
---@param z number
---@return nil
function g_attachcamera(ent, x, y, z) end
---@return nil
function g_detachcamera() end
--- Input
---@param callback fun(control: string, press: boolean)
---@return nil
function i_setcontrolcallback(callback) end
--- Entity
---@param x number
---@param y number
---@param z number
---@param pitch number
---@param yaw number
---@param roll number
---@param sx number
---@param sy number
---@param sz number
---@return number
function ent_spawn(x, y, z, pitch, yaw, roll, sx, sy, sz) end
--- Model
---@param ent number
---@param name string
---@return nil
function mdl_set(ent, name) end
---@param ent number
---@param name string
---@param t number
---@param fade number
---@return nil
function mdl_anim(ent, name, t, fade) end
---@param ent number
---@param name string
---@param fade number
---@return nil
function mdl_stopanim(ent, name, fade) end
---@param ent number
---@return nil
function mdl_remove(ent) end
-- CCT
---@param ent number
---@param height number
---@param radius number
---@return nil
function cct_add(ent, height, radius) end
---@param ent number
---@return nil
function cct_remove(ent) end
---@param ent number
---@param x number
---@param y number
---@param z number
---@return nil
function cct_setpos(ent, x, y, z) end
---@param ent number
---@return number x
---@return number y
---@return number z
function cct_getpos(ent) end
---@param ent number
---@return number x
---@return number z
function cct_getforwardxz(ent) end
---@param ent number
---@param x number
---@param y number
---@param z number
---@param speed number
---@return nil
function cct_turnto(ent, x, y, z, speed) end
---@param ent number
---@param angle number
---@param speed number
---@return nil
function cct_turntoangle(ent, angle, speed) end
---@param ent number
---@param x number
---@param y number
---@param z number
---@return nil
function cct_move(ent, x, y, z) end

29
main/scripts/input.lua Normal file
View File

@ -0,0 +1,29 @@
-- input utils
keyheld = {}
onkey = {}
onkeypress = {}
inputmap = {}
local function processcontrol(control, press)
if press then
keyheld[control] = true
else
keyheld[control] = nil
end
if onkey[control] then
onkey[control](press)
end
if press and onkeypress[control] then
onkeypress[control]()
end
end
i_setcontrolcallback(function (control, press)
processcontrol(control, press)
if (inputmap[control]) then
processcontrol(inputmap[control], press)
end
end)

76
main/scripts/player.lua Normal file
View File

@ -0,0 +1,76 @@
g_require("scripts/input.lua")
local tick = 1 / g_gettps()
local player = nil
local playertimer = nil
local standanim = "stand"
local walkanim = "walk2"
function setupplayer(ent)
player = ent
mdl_anim(player, "init", 0, 0)
mdl_anim(player, standanim, 0, 0)
cct_add(player, 0.25, 1.2)
local walking = false
playertimer = g_settimer(0, true, function ()
local fb = 0
local rl = 0
if keyheld["forward"] then fb = fb + 1 end
if keyheld["backward"] then fb = fb - 1 end
if keyheld["right"] then rl = rl + 1 end
if keyheld["left"] then rl = rl - 1 end
local move_x = 0
local move_y = -3.0
local move_z = 0
local movespeed = 3.0
if fb ~= 0 or rl ~= 0 then
local foward_x, forward_z = g_getcameraforwardxz()
local right_x, right_z = g_getcamerarightxz()
local x = foward_x * fb + right_x * rl
local z = forward_z * fb + right_z * rl
local angle = math.atan(x, z)
move_x, move_z = cct_getforwardxz(player)
cct_turntoangle(player, angle, 6)
if not walking then
mdl_anim(player, walkanim, 6, 0.2)
walking = true
end
-- print(angle * 57.2957795)
else
if walking then
mdl_stopanim(player, walkanim, 0.1)
walking = false
end
end
cct_move(player, move_x * tick * movespeed, move_y * tick, move_z * tick * movespeed)
end)
end
function cleanupplayer()
if playertimer ~= nil then
g_cleartimer(playertimer)
playertimer = nil
end
if player ~= nil then
cct_remove(player)
player = nil
end
end

BIN
main/scripts/player.luac Normal file

Binary file not shown.

34
main/scripts/thread.lua Normal file
View File

@ -0,0 +1,34 @@
-- thread utils
function wait(t)
coroutine.yield(t)
end
function thread(cb)
local co = coroutine.create(cb)
local function resumefun()
if coroutine.status(co) == "dead" then
return
end
local suc, time = coroutine.resume(co)
if not suc then
error(debug.traceback(co))
end
if suc and time then
g_settimer(time, false, resumefun)
end
end
resumefun()
return co
end
function killthread(co)
coroutine.close(co)
end

98
main/shaders/basic.fs Normal file
View File

@ -0,0 +1,98 @@
#version 330 core
in vec2 v_texcoord;
// in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
// uniform mat4 u_model;
// uniform mat4 u_view;
// uniform mat4 u_proj;
// uniform int u_no_light;
// uniform float u_alpha;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec4 object_color_4 = texture(u_tex, vec2(v_texcoord.x, 1.0 - v_texcoord.y));
if (object_color_4.a < 0.5) discard;
vec3 object_color = object_color_4.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(gl_FrontFacing ? v_normal : (v_normal * -1.0), 1.0);
// o_normal = vec4(v_normal, 1.0);
// o_normal = vec4(v_normal * -1.0, 1.0);
}
// //-----------------------------------
// #version 330 core
// //#define ALPHA_DITHER
// in vec2 Texcoord;
// in vec3 FragPosF;
// in vec3 Normal;
// // in float Dist;
// uniform sampler2D tex;
// uniform vec3 lightPos;
// uniform mat4 model;
// uniform mat4 view;
// uniform mat4 proj;
// uniform int NoLight;
// uniform float alpha;
// out vec4 FragColor;
// // out float FragPos;
// out vec4 FragNormal;
// #ifdef ALPHA_DITHER
// float bayer[64] = float[64] (
// 0.000000, 0.500000, 0.125000, 0.625000, 0.031250, 0.531250, 0.156250, 0.656250,
// 0.750000, 0.250000, 0.875000, 0.375000, 0.781250, 0.281250, 0.906250, 0.406250,
// 0.187500, 0.687500, 0.062500, 0.562500, 0.218750, 0.718750, 0.093750, 0.593750,
// 0.937500, 0.437500, 0.812500, 0.312500, 0.968750, 0.468750, 0.843750, 0.343750,
// 0.046875, 0.546875, 0.171875, 0.671875, 0.015625, 0.515625, 0.140625, 0.640625,
// 0.796875, 0.296875, 0.921875, 0.421875, 0.765625, 0.265625, 0.890625, 0.390625,
// 0.234375, 0.734375, 0.109375, 0.609375, 0.203125, 0.703125, 0.078125, 0.578125,
// 0.984375, 0.484375, 0.859375, 0.359375, 0.953125, 0.453125, 0.828125, 0.328125
// );
// int intmod(int a, int b) {
// return a - ((a / b) * b);
// }
// float map(float value, float min1, float max1, float min2, float max2) {
// return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
// }
// #endif
// void main() {
// vec4 objectColor4 = texture(tex, vec2(Texcoord.x, 1.0 - Texcoord.y));
// vec3 objectColor = objectColor4.rgb;
// #ifdef ALPHA_DITHER
// ivec2 iPoz = ivec2(gl_FragCoord.xy);
// int bx = intmod(iPoz.x, 8);
// int by = intmod(iPoz.y, 8);
// // if (objectColor4.a < 0.25 || alpha < bayer[bx * 8 + by]) discard;
// if (map(objectColor4.a, 0.0, 1.0, -0.05, 1.2) * alpha < bayer[bx * 8 + by]) discard;
// #else
// if (objectColor4.a < 0.25) discard;
// #endif
// FragColor = vec4(objectColor, 1.0);
// // FragPos = gl_FragCoord.z; //vec4(FragPosF, /*Dist*/ gl_FragCoord.z);
// FragNormal = vec4(Normal, NoLight == 1 ? 0.0 : 1.0);
// }

21
main/shaders/basic.vs Normal file
View File

@ -0,0 +1,21 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
uniform mat4 u_vp;
uniform mat4 u_model;
out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main() {
v_texcoord = a_texcoord;
v_normal = normalize(mat3(transpose(inverse(u_model))) * a_normal);
vec4 fragpos4 = u_model * vec4(a_position, 1.0);
v_fragpos = vec3(fragpos4);
gl_Position = u_vp * fragpos4;
}

View File

@ -0,0 +1,34 @@
#version 430 core
struct DrawCtx {
mat4 model;
vec4 color[4];
};
layout(std430, binding = 0) buffer InstanceBuffer {
DrawCtx instances[];
};
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
uniform mat4 u_vp;
// uniform mat4 u_model;
out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main() {
// uint index = gl_GlobalInvocationID.x;
mat4 u_model = instances[gl_InstanceID].model;
v_texcoord = a_texcoord;
v_normal = normalize(mat3(transpose(inverse(u_model))) * a_normal);
vec4 fragpos4 = u_model * vec4(a_position, 1.0);
v_fragpos = vec3(fragpos4);
gl_Position = u_vp * fragpos4;
}

14
main/shaders/debug.fs Normal file
View File

@ -0,0 +1,14 @@
#version 330 core
in vec3 v_color;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
o_color = vec4(v_color, 0.0);
o_normal = vec4(0.0);
// o_normal = vec4(v_normal, 1.0);
// o_normal = vec4(v_normal * -1.0, 1.0);
}

13
main/shaders/debug.vs Normal file
View File

@ -0,0 +1,13 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec3 a_color;
uniform mat4 u_vp;
out vec3 v_color;
void main() {
v_color = a_color;
gl_Position = u_vp * vec4(a_position, 1.0);
}

View File

@ -0,0 +1,135 @@
#version 330 core
uniform sampler2D u_tex;
uniform sampler2D u_tex_pos;
uniform sampler2D u_tex_normal;
uniform sampler2D u_tex_light;
uniform vec3 u_sun_color;
uniform vec3 u_sun_direction;
uniform vec3 u_ambient_color;
// uniform float u_farplane;
// uniform vec3 u_farplane_color;
// uniform float u_farplane_strength;
layout (location = 0) out vec4 o_color;
vec3 GetAmbientLight() {
return u_ambient_color;
}
vec3 GetSunLight(vec3 normal) {
float sun_intensity = max(dot(normal, u_sun_direction), 0.0);
return sun_intensity * u_sun_color;
}
void main() {
ivec2 int_pos = ivec2(gl_FragCoord.xy);
vec4 local_rgba = texelFetch(u_tex, int_pos, 0);
vec4 local_normal_4 = texelFetch(u_tex_normal, int_pos, 0);
vec4 local_light_4 = texelFetch(u_tex_light, int_pos, 0);
float local_dist = texelFetch(u_tex_pos, int_pos, 0).r;
vec3 local_rgb = local_rgba.rgb;
vec3 local_normal = local_normal_4.xyz;
vec3 local_light = local_light_4.rgb;
vec3 rr = GetAmbientLight() + GetSunLight(local_normal) + local_light;
// vec3 rr = max(GetAmbientLight(), max(GetSunLight(local_normal), local_light));
vec3 result = local_rgb * mix(vec3(1.0), rr, local_rgba.a);
o_color = vec4(result, 1.0);
// o_color = vec4(1.0);
// o_color = vec4(local_rgb, 1.0);
}
/*
// in vec2 Poz;
layout (pixel_center_integer) in vec4 gl_FragCoord;
uniform sampler2D tex;
uniform sampler2D tex_pos;
uniform sampler2D tex_normal;
uniform sampler2D tex_light;
uniform sampler2D tex_transparent;
uniform float fadeAlpha;
uniform vec3 sunColor;
uniform vec3 sunDirection;
uniform vec3 ambientColor;
uniform float ambientStrength;
uniform float farplane;
uniform vec3 farplaneColor;
uniform float farplaneStrength;
uniform float time;
out vec4 FragColor;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
vec3 noise(vec2 co, float t, float intensity) {
vec2 co_t = co * mod(t, 1.0);
return ((vec3(rand(co_t), rand(co_t * vec2(-1.0, 1.0)), rand(co_t * vec2(1.0, -1.0))) - 0.5) * intensity * 2.0);
}
float linearize_depth(float d, float zNear, float zFar) {
float z_n = 2.0 * d - 1.0;
return 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
}
void main() {
ivec2 iPoz = ivec2(gl_FragCoord.xy);
vec4 this_rgba = texelFetch(tex, iPoz, 0);
//vec4 this_posd = texelFetch(tex_pos, iPoz, 0);
vec4 this_normala = texelFetch(tex_normal, iPoz, 0);
vec4 this_lighta = texelFetch(tex_light, iPoz, 0);
// vec3 this_light = this_lighta.rgb / min(1.0, max(this_lighta.r, max(this_lighta.g, this_lighta.b)));
vec4 this_transparent = texelFetch(tex_transparent, iPoz, 0);
vec3 this_rgb = this_rgba.rgb;
//vec3 this_pos = this_posd.xyz;
vec3 this_normal = this_normala.xyz;
float Dist = texelFetch(tex_pos, iPoz, 0).r; //this_posd.w;
// vec3 this_pos = WorldPosFromDepth(Dist);
float LightAffected = this_normala.w;
// --- old fs
vec3 ambientResult = ambientStrength * ambientColor;
vec3 sunDir = normalize(sunDirection);
float sunIntensity = max(dot(this_normal, sunDir), 0.0);
vec3 sunResult = sunIntensity * sunColor;
// vec3 result = max(max(ambientResult, sunResult), this_lighta.rgb) * this_rgb;
vec3 result = (ambientResult + sunResult + this_lighta.rgb) * this_rgb;
vec3 litColor = mix(this_rgb, result, LightAffected);
//vec3 fragColor3 = litColor; //mix(litColor, farplaneColor, linearize_depth(Dist, 0.1, farplane) / farplane);
float fogIntensity = farplaneStrength * linearize_depth(Dist, 0.1, farplane) / farplane;
vec3 finalColor = mix(litColor, farplaneColor, fogIntensity);
vec3 fragColor3 = mix(finalColor, this_transparent.rgb, this_transparent.a);
FragColor = vec4(fragColor3 /*+ noise(gl_FragCoord.xy, time, 0.03)* /, 1.0);
}
*/

View File

@ -0,0 +1,7 @@
#version 330 core
layout (location = 0) in vec2 a_vert_pos;
void main() {
gl_Position = vec4(a_vert_pos, 0.0, 1.0);
}

19
main/shaders/skeletal.fs Normal file
View File

@ -0,0 +1,19 @@
#version 330 core
in vec2 v_texcoord;
in vec3 v_normal;
uniform sampler2D u_tex;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec4 object_color_4 = texture(u_tex, vec2(v_texcoord.x, 1.0 - v_texcoord.y));
if (object_color_4.a < 0.5) discard;
vec3 object_color = object_color_4.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

40
main/shaders/skeletal.vs Normal file
View File

@ -0,0 +1,40 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
layout (location = 3) in ivec4 a_bone_ids;
layout (location = 4) in vec4 a_bone_weights;
const int MAX_BONES = 240;
const int MAX_BONE_INFLUENCE = 4;
uniform mat4 u_bone_transforms[MAX_BONES];
uniform mat4 u_vp;
uniform mat4 u_model;
// out vec3 v_fragpos;
out vec2 v_texcoord;
out vec3 v_normal;
void main() {
mat4 bone_transform = mat4(0.0);
for (int i = 0; i < MAX_BONE_INFLUENCE; i++) {
if (a_bone_ids[i] == -1)
continue;
bone_transform += u_bone_transforms[a_bone_ids[i]] * a_bone_weights[i];
}
vec4 pos_l = bone_transform * vec4(a_position, 1.0);
vec4 fragpos4 = u_model * pos_l;
gl_Position = u_vp * fragpos4;
// v_fragpos = vec3(fragpos4);
v_texcoord = a_texcoord;
vec4 normal_l = bone_transform * vec4(a_normal, 0.0);
v_normal = normalize((u_model * normal_l).xyz);
}

13
main/shaders/skybox.fs Normal file
View File

@ -0,0 +1,13 @@
#version 330 core
in vec2 v_texcoord;
uniform sampler2D u_tex;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
o_color = vec4(texture(u_tex, vec2(v_texcoord.x, 1.0 - v_texcoord.y)).rgb, 0.0);
o_normal = vec4(0.0, 0.0, 0.0, 1.0);
}

14
main/shaders/skybox.vs Normal file
View File

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec2 a_texcoord;
layout (location = 2) in vec3 a_normal;
uniform mat4 u_vp;
out vec2 v_texcoord;
void main() {
v_texcoord = a_texcoord;
gl_Position = u_vp * vec4(a_position, 1.0);
}

View File

@ -0,0 +1,25 @@
#version 330 core
in vec2 v_texcoord;
// in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
uniform sampler2D u_tex1;
uniform float u_uv_mult;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec2 txc = vec2(v_texcoord.x, 1.0 - v_texcoord.y);
vec4 object_color_4 = texture(u_tex, txc) * texture(u_tex1, txc * u_uv_mult);
if (object_color_4.a < 0.5) discard;
vec3 object_color = object_color_4.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

27
main/shaders/xz.fs Normal file
View File

@ -0,0 +1,27 @@
#version 330 core
in vec2 v_texcoord;
in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
uniform vec2 u_xz_mult;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec2 tc = v_fragpos.xz * u_xz_mult.x;
vec4 color_xz = texture(u_tex, tc);
// vec4 object_color_4 = mix(color_xz1, color_xz2, color_mask.a);
// if (object_color_4.a < 0.5) discard;
vec3 object_color = color_xz.rgb;
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

View File

@ -0,0 +1,35 @@
#version 330 core
in vec2 v_texcoord;
in vec3 v_fragpos;
in vec3 v_normal;
uniform sampler2D u_tex;
uniform sampler2D u_tex1;
uniform sampler2D u_tex2;
uniform vec2 u_xz_mult;
layout (location = 0) out vec4 o_color;
layout (location = 1) out vec4 o_normal;
void main() {
vec2 txc = vec2(v_texcoord.x, 1.0 - v_texcoord.y);
vec2 tc1 = v_fragpos.xz * u_xz_mult.x;
vec2 tc2 = v_fragpos.xz * u_xz_mult.y;
vec4 color_mask = texture(u_tex, txc);
vec4 color_xz1 = texture(u_tex1, tc1);
vec4 color_xz2 = texture(u_tex2, tc2);
// vec4 object_color_4 = mix(color_xz1, color_xz2, color_mask.a);
// if (object_color_4.a < 0.5) discard;
vec3 object_color = mix(color_xz1.rgb, color_xz2.rgb * color_mask.rgb, color_mask.a);
o_color = vec4(object_color, 1.0);
o_normal = vec4(v_normal, 1.0);
}

33
src/tsr/entity.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "entity.hpp"
using namespace TSR;
entt::entity EntitySystem::Spawn(const Transform& trans) {
auto& reg = Game::Registry();
auto ent = reg.create();
auto& comp = reg.emplace<TransformComponent>(ent);
comp.trans = trans;
return ent;
}
void EntitySystem::Despawn(entt::entity ent) {
Game::Registry().destroy(ent);
}
void EntitySystem::RegisterLuaFunctions() {
LuaAPI::RegisterFunction("ent_spawn", [](float x, float y, float z, float pitch, float yaw, float roll, float sx, float sy, float sz) {
Transform trans;
trans.pos.x = x;
trans.pos.y = y;
trans.pos.z = z;
trans.rot = glm::quat(glm::vec3(pitch, yaw, roll));
trans.scl.x = sx;
trans.scl.y = sy;
trans.scl.z = sz;
return EntitySystem::Spawn(trans);
});
LuaAPI::RegisterFunction("ent_despawn", [](entt::entity ent) {
EntitySystem::Despawn(ent);
});
}

View File

@ -1,3 +1,23 @@
#pragma once
#include <entt/entt.hpp>
#include "game.hpp"
#include "game.hpp"
namespace TSR {
struct TransformComponent {
Transform trans;
};
class EntitySystem {
public:
static entt::entity Spawn(const Transform& trans);
static void Despawn(entt::entity ent);
static void RegisterLuaFunctions();
};
}

View File

@ -37,8 +37,10 @@ static glm::vec3 CCTGetPos(CCTComponent& comp) {
return glm::vec3(pos_px.x, pos_px.y, pos_px.z);
}
static bool CCTTurnToAngle(CCTComponent& comp, float target, float step) {
if (target < comp.angle)
static bool CCTTurnToAngle(CCTComponent& comp, float target, float speed) {
auto step = speed / Game::TPS();
while (target < comp.angle)
target += glm::two_pi<float>();
auto diff_angle = target - comp.angle;
@ -121,24 +123,30 @@ void CCTSystem::SetPos(entt::entity ent, const glm::vec3& pos) {
CCTSetPos(comp, pos);
}
glm::vec3 TSR::CCTSystem::GetPos(entt::entity ent) {
glm::vec3 CCTSystem::GetPos(entt::entity ent) {
auto& reg = Game::Registry();
auto& comp = reg.get<CCTComponent>(ent);
return CCTGetPos(comp);
}
bool CCTSystem::TurnTo(entt::entity ent, const glm::vec3& pos, float step) {
glm::vec3 CCTSystem::GetForward(entt::entity ent) {
auto& reg = Game::Registry();
auto& comp = reg.get<CCTComponent>(ent);
return glm::vec3(std::sin(comp.angle), 0.0f, std::cos(comp.angle));
}
bool CCTSystem::TurnTo(entt::entity ent, const glm::vec3& pos, float speed) {
auto& reg = Game::Registry();
auto& comp = reg.get<CCTComponent>(ent);
return CCTTurnTo(comp, pos, step);
return CCTTurnTo(comp, pos, speed);
}
bool TSR::CCTSystem::TurnToAngle(entt::entity ent, float target, float step) {
bool CCTSystem::TurnToAngle(entt::entity ent, float target, float speed) {
auto& reg = Game::Registry();
auto& comp = reg.get<CCTComponent>(ent);
return CCTTurnToAngle(comp, target, step);
return CCTTurnToAngle(comp, target, speed);
}
void CCTSystem::Update() {
@ -152,3 +160,42 @@ void CCTSystem::Update() {
}
}
void TSR::CCTSystem::RegisterLuaFunctions() {
LuaAPI::RegisterFunction("cct_add", [](entt::entity ent, float radius, float height) {
CCTSystem::AddCCT(ent, radius, height);
});
LuaAPI::RegisterFunction("cct_remove", [](entt::entity ent) {
CCTSystem::RemoveCCT(ent);
});
LuaAPI::RegisterFunction("cct_move", [](entt::entity ent, float x, float y, float z) {
auto disp = glm::vec3(x, y, z);
return CCTSystem::Move(ent, disp, 1.0f / Game::TPS());
});
LuaAPI::RegisterFunction("cct_setpos", [](entt::entity ent, float x, float y, float z) {
auto pos = glm::vec3(x, y, z);
CCTSystem::SetPos(ent, pos);
});
LuaAPI::RegisterFunction("cct_getpos", [](entt::entity ent) {
auto pos = CCTSystem::GetPos(ent);
return std::make_tuple(pos.x, pos.y, pos.z);
});
LuaAPI::RegisterFunction("cct_getforwardxz", [](entt::entity ent) {
auto forward = CCTSystem::GetForward(ent);
return std::make_tuple(forward.x, forward.z);
});
LuaAPI::RegisterFunction("cct_turnto", [](entt::entity ent, float x, float y, float z, float speed) {
auto pos = glm::vec3(x, y, z);
return CCTSystem::TurnTo(ent, pos, speed);
});
LuaAPI::RegisterFunction("cct_turntoangle", [](entt::entity ent, float angle, float speed) {
return CCTSystem::TurnToAngle(ent, angle, speed);
});
}

View File

@ -27,10 +27,15 @@ namespace TSR {
static int Move(entt::entity ent, const glm::vec3& disp, float dt);
static void SetPos(entt::entity ent, const glm::vec3& pos);
static glm::vec3 GetPos(entt::entity ent);
static glm::vec3 GetForward(entt::entity ent);
static bool TurnTo(entt::entity ent, const glm::vec3& pos, float step);
static bool TurnToAngle(entt::entity ent, float target, float step);
static void Update();
static void RegisterLuaFunctions();
};
}

View File

@ -36,7 +36,7 @@ void ModelSystem::AddModel(entt::entity ent, const std::string& name) {
}
}
void TSR::ModelSystem::RemoveModel(entt::entity ent) {
void ModelSystem::RemoveModel(entt::entity ent) {
auto& reg = Game::Registry();
reg.erase<ModelComponent>(ent);
//reg.erase<AnimationComponent>(ent);
@ -241,4 +241,22 @@ void ModelSystem::Render(TSR::Renderer& renderer) {
//World::Get().Reg()
}
}
void ModelSystem::RegisterLuaFunctions() {
LuaAPI::RegisterFunction("mdl_set", [](entt::entity ent, const std::string& name) {
ModelSystem::AddModel(ent, name);
});
LuaAPI::RegisterFunction("mdl_remove", [](entt::entity ent) {
ModelSystem::RemoveModel(ent);
});
LuaAPI::RegisterFunction("mdl_anim", [](entt::entity ent, const std::string& anim_name, float t, float fade_in_time) {
ModelSystem::Anim(ent, anim_name, t, fade_in_time);
});
LuaAPI::RegisterFunction("mdl_stopanim", [](entt::entity ent, const std::string& anim_name, float fade_out_time) {
ModelSystem::StopAnim(ent, anim_name, fade_out_time);
});
}

View File

@ -43,7 +43,7 @@ namespace TSR {
static void Animate();
static void Render(Renderer& renderer);
static void RegisterLuaFunctions();
};

View File

@ -8,6 +8,10 @@
#include "camera.hpp"
#include "worldmap.hpp"
#include "physics.hpp"
#include "input.hpp"
#include "entity.hpp"
#include "entity_model.hpp"
#include "entity_cct.hpp"
using namespace TSR;
@ -31,6 +35,10 @@ static AssetPtr<Model> s_skybox_model;
static uint32_t s_next_timer_id;
static bool s_camera_attached = false;
static entt::entity s_camera_attach_ent;
static glm::vec3 s_camera_attach_offset;
void Game::StartGame(const std::string& map_name) {
s_sv_frame = 0;
s_frame_t = 0.0f;
@ -44,36 +52,67 @@ void Game::StartGame(const std::string& map_name) {
LuaAPI::Init();
LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, sol::function callback) {
RegisterLuaFunctions();
Input::RegisterLuaFunctions();
EntitySystem::RegisterLuaFunctions();
ModelSystem::RegisterLuaFunctions();
CCTSystem::RegisterLuaFunctions();
LuaAPI::Load("scripts/autoexec.lua");
}
void Game::RegisterLuaFunctions() {
LuaAPI::RegisterFunction("g_settimer", [](float interval, bool repeat, LuaFunction callback) {
auto& timer = s_timers[++s_next_timer_id];
timer.last_frame = s_sv_frame;
timer.interval = interval;
timer.repeat = repeat;
timer.callback = callback;
return s_next_timer_id;
});
});
LuaAPI::RegisterFunction("g_cleartimer", [](uint32_t id) {
s_timers.erase(id);
});
LuaAPI::RegisterFunction("g_getframe", []() {
return s_sv_frame;
});
return s_sv_frame;
});
LuaAPI::RegisterFunction("g_gettps", []() {
return s_tps;
});
return s_tps;
});
LuaAPI::RegisterFunction("g_require", [](const std::string& name) {
LuaAPI::Load(name);
LuaAPI::Load(name);
});
LuaAPI::RegisterFunction("g_getcameraforward", []() {
return std::make_tuple(cam.forward_backward_vector.x, cam.forward_backward_vector.y, cam.forward_backward_vector.z);
});
LuaAPI::RegisterFunction("g_getcameraforwardxz", []() {
return std::make_tuple(cam.forward_backward_vector.x, cam.forward_backward_vector.z);
});
LuaAPI::RegisterFunction("g_getcamerarightxz", []() {
return std::make_tuple(cam.right_vector.x, cam.right_vector.z);
});
LuaAPI::RegisterFunction("g_attachcamera", [](entt::entity ent, float x, float y, float z) {
s_camera_attached = true;
s_camera_attach_ent = ent;
s_camera_attach_offset = glm::vec3(x, y, z);
});
LuaAPI::Load("scripts/autoexec.lua");
LuaAPI::RegisterFunction("g_detachcamera", []() {
s_camera_attached = false;
});
}
void Game::EndGame() {
s_timers.clear();
Input::Shutdown();
LuaAPI::Shutdown();
s_registry.clear();
@ -100,53 +139,6 @@ void Game::Run() {
s_skybox_model = AssetMap::Get<Model>("models/skybox.mdl.json");
//s_physics_scene->EnableDebug(false);
for (int i = 0; i < 10; ++i) {
auto ent = Game::Registry().create();
ModelSystem::AddModel(ent, "models/test_skeletal.mdl.json");
ModelSystem::Anim(ent, "init");
ModelSystem::Anim(ent, "ruce");
//ModelSystem::Anim(ent, "stand");
//ModelSystem::Anim(ent, "walk2", i * 13.0f);
glm::vec3 pos(2.0f * (i / 10), 0.0f, 2.0f * (i % 10));
auto& trans = Registry().emplace_or_replace<TransformComponent>(ent);
trans.trans.pos = pos;
trans.trans.rot = glm::quat(glm::vec3(0.0f));
trans.trans.scl = glm::vec3(1.0f);
}
{
ent1 = Game::Registry().create();
ModelSystem::AddModel(ent1, "models/skrin.mdl.json");
ModelSystem::Anim(ent1, "init");
ModelSystem::Anim(ent1, "open_left");
ModelSystem::Anim(ent1, "open_right");
auto& trans = Registry().emplace_or_replace<TransformComponent>(ent1);
glm::vec3 pos(0.0f, 5.0f, 0.0f);
trans.trans.pos = pos;
trans.trans.rot = glm::quat(glm::vec3(0.0f));
trans.trans.scl = glm::vec3(1.0f);
}
{
player = Game::Registry().create();
ModelSystem::AddModel(player, "models/test_skeletal.mdl.json");
ModelSystem::Anim(player, "init");
ModelSystem::Anim(player, "stand");
auto& trans = Registry().emplace_or_replace<TransformComponent>(player);
glm::vec3 pos(0.0f, 5.0f, 0.0f);
trans.trans.pos = pos;
trans.trans.rot = glm::quat(glm::vec3(0.0f));
trans.trans.scl = glm::vec3(1.0f);
CCTSystem::AddCCT(player, 0.25f, 1.2f);
}
cam.third_person_distance = 5.0f;
Window::SetInputMode(GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -171,23 +163,12 @@ void Game::Run() {
s_debug_draw = !s_debug_draw;
s_physics_scene->EnableDebug(s_debug_draw);
break;
case GLFW_KEY_E:
ModelSystem::Anim(ent1, "open_left");
ModelSystem::Anim(ent1, "open_right");
break;
case GLFW_KEY_Q:
ModelSystem::Anim(ent1, "close_left");
ModelSystem::Anim(ent1, "close_right");
break;
}
}
if (action == GLFW_PRESS || action == GLFW_RELEASE)
Input::AddControlEvent(Input::ControlFromGlfwKey(key), action == GLFW_PRESS);
});
cam.movement_speed *= 0.1f;
@ -215,7 +196,7 @@ void Game::Run() {
}
s_frame_t = (float)(real_time - time + frame_time_ms) / (float)frame_time_ms;
s_frame_t = glm::min((float)(real_time - time + frame_time_ms) / (float)frame_time_ms, 1.0f);
ClFrame(renderer, 0.0f);
}
@ -233,18 +214,7 @@ void Game::Run() {
void Game::SvFrame() {
float time = 1.0f / (float)s_tps;
//if (Window::KeyDown(GLFW_KEY_W))
// cam.ProcessMovement(Camera::Movement::FORWARD, time);
//if (Window::KeyDown(GLFW_KEY_S))
// cam.ProcessMovement(Camera::Movement::BACKWARD, time);
//if (Window::KeyDown(GLFW_KEY_A))
// cam.ProcessMovement(Camera::Movement::LEFT, time);
//if (Window::KeyDown(GLFW_KEY_D))
// cam.ProcessMovement(Camera::Movement::RIGHT, time);
//if (Window::KeyDown(GLFW_KEY_SPACE))
// cam.ProcessMovement(Camera::Movement::UP, time);
//if (Window::KeyDown(GLFW_KEY_LEFT_SHIFT))
// cam.ProcessMovement(Camera::Movement::DOWN, time);
Input::ProcessEvents();
// execute timers
for (auto it = s_timers.begin(); it != s_timers.end();) {
@ -252,8 +222,7 @@ void Game::SvFrame() {
if (s_sv_frame != timer.last_frame && s_sv_frame - timer.last_frame >= (uint32_t)(timer.interval * (float)s_tps)) {
timer.last_frame = s_sv_frame;
sol::protected_function func = timer.callback;
func();
LuaAPI::Call(timer.callback);
if (!timer.repeat) {
it = s_timers.erase(it);
@ -264,59 +233,6 @@ void Game::SvFrame() {
++it;
}
float move_forward = 0.0f;
float move_right = 0.0f;
if (Window::KeyDown(GLFW_KEY_W))
move_forward += 1.0f;
if (Window::KeyDown(GLFW_KEY_S))
move_forward -= 1.0f;
if (Window::KeyDown(GLFW_KEY_A))
move_right -= 1.0f;
if (Window::KeyDown(GLFW_KEY_D))
move_right += 1.0f;
static bool walking = false;
if (move_forward != 0.0f || move_right != 0.0f) {
auto move_vector = cam.forward_backward_vector * move_forward + cam.right_vector * move_right;
auto move_angle = std::atan2(move_vector.x, move_vector.z);
CCTSystem::TurnToAngle(player, move_angle, 6.0f / TPS());
auto& cctcomp = Registry().get<CCTComponent>(player);
glm::vec3 forward_vector(std::sin(cctcomp.angle), -1.0f, std::cos(cctcomp.angle));
CCTSystem::Move(player, forward_vector * (3.0f / TPS()), time);
if (!walking) {
walking = true;
ModelSystem::Anim(player, "walk2", 6.0f, 0.2f);
}
}
else {
if (walking) {
walking = false;
ModelSystem::StopAnim(player, "walk2", 0.1f);
}
}
static bool aiming = false;
if (Window::KeyDown(GLFW_KEY_E)) {
if (!aiming) {
aiming = true;
ModelSystem::Anim(player, "ruce", 0.0f, 0.2f);
}
}
else {
if (aiming) {
aiming = false;
ModelSystem::StopAnim(player, "ruce", 0.2f);
}
}
CCTSystem::Update();
ModelSystem::Animate();
@ -340,7 +256,10 @@ void Game::ClFrame(Renderer& renderer, float frame_t) {
if (s_skybox_model)
s_skybox_model->Draw(renderer, nullptr);
cam.position = glm::vec3(Registry().get<ModelComponent>(player).ctx.model_matrix[3]) + glm::vec3(0.0f, 1.0f, 0.0f);
if (s_camera_attached) {
cam.position = glm::vec3(Registry().get<ModelComponent>(s_camera_attach_ent).ctx.model_matrix[3]) + s_camera_attach_offset;
}
renderer.SetViewMatrix(cam.GetViewMatrix());
renderer.Render(w, h);

View File

@ -16,7 +16,7 @@ namespace TSR {
float interval = 0.0f;
uint32_t last_frame = 0;
bool repeat = false;
sol::reference callback;
LuaReference callback;
};
class Game {
@ -49,12 +49,9 @@ namespace TSR {
static float FrameT() { return s_frame_t; }
static int TPS() { return s_tps; }
static void RegisterLuaFunctions();
};
struct TransformComponent {
Transform trans;
};
}

535
src/tsr/input.cpp Normal file
View File

@ -0,0 +1,535 @@
#include "input.hpp"
using namespace TSR;
const Control Input::s_glfw_key_map[] = {
KEY_NONE, // 0
KEY_NONE, // 1
KEY_NONE, // 2
KEY_NONE, // 3
KEY_NONE, // 4
KEY_NONE, // 5
KEY_NONE, // 6
KEY_NONE, // 7
KEY_NONE, // 8
KEY_NONE, // 9
KEY_NONE, // 10
KEY_NONE, // 11
KEY_NONE, // 12
KEY_NONE, // 13
KEY_NONE, // 14
KEY_NONE, // 15
KEY_NONE, // 16
KEY_NONE, // 17
KEY_NONE, // 18
KEY_NONE, // 19
KEY_NONE, // 20
KEY_NONE, // 21
KEY_NONE, // 22
KEY_NONE, // 23
KEY_NONE, // 24
KEY_NONE, // 25
KEY_NONE, // 26
KEY_NONE, // 27
KEY_NONE, // 28
KEY_NONE, // 29
KEY_NONE, // 30
KEY_NONE, // 31
KEY_SPACE, // 32
KEY_NONE, // 33
KEY_NONE, // 34
KEY_NONE, // 35
KEY_NONE, // 36
KEY_NONE, // 37
KEY_NONE, // 38
KEY_APOSTROPHE, // 39
KEY_NONE, // 40
KEY_NONE, // 41
KEY_NONE, // 42
KEY_NONE, // 43
KEY_COMMA, // 44
KEY_MINUS, // 45
KEY_PERIOD, // 46
KEY_SLASH, // 47
KEY_0, // 48
KEY_1, // 49
KEY_2, // 50
KEY_3, // 51
KEY_4, // 52
KEY_5, // 53
KEY_6, // 54
KEY_7, // 55
KEY_8, // 56
KEY_9, // 57
KEY_NONE, // 58
KEY_SEMICOLON, // 59
KEY_NONE, // 60
KEY_EQUAL, // 61
KEY_NONE, // 62
KEY_NONE, // 63
KEY_NONE, // 64
KEY_A, // 65
KEY_B, // 66
KEY_C, // 67
KEY_D, // 68
KEY_E, // 69
KEY_F, // 70
KEY_G, // 71
KEY_H, // 72
KEY_I, // 73
KEY_J, // 74
KEY_K, // 75
KEY_L, // 76
KEY_M, // 77
KEY_N, // 78
KEY_O, // 79
KEY_P, // 80
KEY_Q, // 81
KEY_R, // 82
KEY_S, // 83
KEY_T, // 84
KEY_U, // 85
KEY_V, // 86
KEY_W, // 87
KEY_X, // 88
KEY_Y, // 89
KEY_Z, // 90
KEY_LEFT_BRACKET, // 91
KEY_BACKSLASH, // 92
KEY_RIGHT_BRACKET, // 93
KEY_NONE, // 94
KEY_NONE, // 95
KEY_GRAVE_ACCENT, // 96
KEY_NONE, // 97
KEY_NONE, // 98
KEY_NONE, // 99
KEY_NONE, // 100
KEY_NONE, // 101
KEY_NONE, // 102
KEY_NONE, // 103
KEY_NONE, // 104
KEY_NONE, // 105
KEY_NONE, // 106
KEY_NONE, // 107
KEY_NONE, // 108
KEY_NONE, // 109
KEY_NONE, // 110
KEY_NONE, // 111
KEY_NONE, // 112
KEY_NONE, // 113
KEY_NONE, // 114
KEY_NONE, // 115
KEY_NONE, // 116
KEY_NONE, // 117
KEY_NONE, // 118
KEY_NONE, // 119
KEY_NONE, // 120
KEY_NONE, // 121
KEY_NONE, // 122
KEY_NONE, // 123
KEY_NONE, // 124
KEY_NONE, // 125
KEY_NONE, // 126
KEY_NONE, // 127
KEY_NONE, // 128
KEY_NONE, // 129
KEY_NONE, // 130
KEY_NONE, // 131
KEY_NONE, // 132
KEY_NONE, // 133
KEY_NONE, // 134
KEY_NONE, // 135
KEY_NONE, // 136
KEY_NONE, // 137
KEY_NONE, // 138
KEY_NONE, // 139
KEY_NONE, // 140
KEY_NONE, // 141
KEY_NONE, // 142
KEY_NONE, // 143
KEY_NONE, // 144
KEY_NONE, // 145
KEY_NONE, // 146
KEY_NONE, // 147
KEY_NONE, // 148
KEY_NONE, // 149
KEY_NONE, // 150
KEY_NONE, // 151
KEY_NONE, // 152
KEY_NONE, // 153
KEY_NONE, // 154
KEY_NONE, // 155
KEY_NONE, // 156
KEY_NONE, // 157
KEY_NONE, // 158
KEY_NONE, // 159
KEY_NONE, // 160
KEY_WORLD_1, // 161
KEY_WORLD_2, // 162
KEY_NONE, // 163
KEY_NONE, // 164
KEY_NONE, // 165
KEY_NONE, // 166
KEY_NONE, // 167
KEY_NONE, // 168
KEY_NONE, // 169
KEY_NONE, // 170
KEY_NONE, // 171
KEY_NONE, // 172
KEY_NONE, // 173
KEY_NONE, // 174
KEY_NONE, // 175
KEY_NONE, // 176
KEY_NONE, // 177
KEY_NONE, // 178
KEY_NONE, // 179
KEY_NONE, // 180
KEY_NONE, // 181
KEY_NONE, // 182
KEY_NONE, // 183
KEY_NONE, // 184
KEY_NONE, // 185
KEY_NONE, // 186
KEY_NONE, // 187
KEY_NONE, // 188
KEY_NONE, // 189
KEY_NONE, // 190
KEY_NONE, // 191
KEY_NONE, // 192
KEY_NONE, // 193
KEY_NONE, // 194
KEY_NONE, // 195
KEY_NONE, // 196
KEY_NONE, // 197
KEY_NONE, // 198
KEY_NONE, // 199
KEY_NONE, // 200
KEY_NONE, // 201
KEY_NONE, // 202
KEY_NONE, // 203
KEY_NONE, // 204
KEY_NONE, // 205
KEY_NONE, // 206
KEY_NONE, // 207
KEY_NONE, // 208
KEY_NONE, // 209
KEY_NONE, // 210
KEY_NONE, // 211
KEY_NONE, // 212
KEY_NONE, // 213
KEY_NONE, // 214
KEY_NONE, // 215
KEY_NONE, // 216
KEY_NONE, // 217
KEY_NONE, // 218
KEY_NONE, // 219
KEY_NONE, // 220
KEY_NONE, // 221
KEY_NONE, // 222
KEY_NONE, // 223
KEY_NONE, // 224
KEY_NONE, // 225
KEY_NONE, // 226
KEY_NONE, // 227
KEY_NONE, // 228
KEY_NONE, // 229
KEY_NONE, // 230
KEY_NONE, // 231
KEY_NONE, // 232
KEY_NONE, // 233
KEY_NONE, // 234
KEY_NONE, // 235
KEY_NONE, // 236
KEY_NONE, // 237
KEY_NONE, // 238
KEY_NONE, // 239
KEY_NONE, // 240
KEY_NONE, // 241
KEY_NONE, // 242
KEY_NONE, // 243
KEY_NONE, // 244
KEY_NONE, // 245
KEY_NONE, // 246
KEY_NONE, // 247
KEY_NONE, // 248
KEY_NONE, // 249
KEY_NONE, // 250
KEY_NONE, // 251
KEY_NONE, // 252
KEY_NONE, // 253
KEY_NONE, // 254
KEY_NONE, // 255
KEY_ESCAPE, // 256
KEY_ENTER, // 257
KEY_TAB, // 258
KEY_BACKSPACE, // 259
KEY_INSERT, // 260
KEY_DELETE, // 261
KEY_RIGHT, // 262
KEY_LEFT, // 263
KEY_DOWN, // 264
KEY_UP, // 265
KEY_PAGE_UP, // 266
KEY_PAGE_DOWN, // 267
KEY_HOME, // 268
KEY_END, // 269
KEY_NONE, // 270
KEY_NONE, // 271
KEY_NONE, // 272
KEY_NONE, // 273
KEY_NONE, // 274
KEY_NONE, // 275
KEY_NONE, // 276
KEY_NONE, // 277
KEY_NONE, // 278
KEY_NONE, // 279
KEY_CAPS_LOCK, // 280
KEY_SCROLL_LOCK, // 281
KEY_NUM_LOCK, // 282
KEY_PRINT_SCREEN, // 283
KEY_PAUSE, // 284
KEY_NONE, // 285
KEY_NONE, // 286
KEY_NONE, // 287
KEY_NONE, // 288
KEY_NONE, // 289
KEY_F1, // 290
KEY_F2, // 291
KEY_F3, // 292
KEY_F4, // 293
KEY_F5, // 294
KEY_F6, // 295
KEY_F7, // 296
KEY_F8, // 297
KEY_F9, // 298
KEY_F10, // 299
KEY_F11, // 300
KEY_F12, // 301
KEY_F13, // 302
KEY_F14, // 303
KEY_F15, // 304
KEY_F16, // 305
KEY_F17, // 306
KEY_F18, // 307
KEY_F19, // 308
KEY_F20, // 309
KEY_F21, // 310
KEY_F22, // 311
KEY_F23, // 312
KEY_F24, // 313
KEY_F25, // 314
KEY_NONE, // 315
KEY_NONE, // 316
KEY_NONE, // 317
KEY_NONE, // 318
KEY_NONE, // 319
KEY_KP_0, // 320
KEY_KP_1, // 321
KEY_KP_2, // 322
KEY_KP_3, // 323
KEY_KP_4, // 324
KEY_KP_5, // 325
KEY_KP_6, // 326
KEY_KP_7, // 327
KEY_KP_8, // 328
KEY_KP_9, // 329
KEY_KP_DECIMAL, // 330
KEY_KP_DIVIDE, // 331
KEY_KP_MULTIPLY, // 332
KEY_KP_SUBTRACT, // 333
KEY_KP_ADD, // 334
KEY_KP_ENTER, // 335
KEY_KP_EQUAL, // 336
KEY_NONE, // 337
KEY_NONE, // 338
KEY_NONE, // 339
KEY_LEFT_SHIFT, // 340
KEY_LEFT_CONTROL, // 341
KEY_LEFT_ALT, // 342
KEY_LEFT_SUPER, // 343
KEY_RIGHT_SHIFT, // 344
KEY_RIGHT_CONTROL, // 345
KEY_RIGHT_ALT, // 346
KEY_RIGHT_SUPER, // 347
KEY_MENU, // 348
};
const Control Input::s_glfw_mouse_map[] = {
MOUSE_1, // 0
MOUSE_2, // 1
MOUSE_3, // 2
MOUSE_4, // 3
MOUSE_5, // 4
MOUSE_6, // 5
};
const char* const Input::s_control_names[] = {
"KEY_NONE",
"KEY_SPACE",
"KEY_APOSTROPHE",
"KEY_COMMA",
"KEY_MINUS",
"KEY_PERIOD",
"KEY_SLASH",
"KEY_0",
"KEY_1",
"KEY_2",
"KEY_3",
"KEY_4",
"KEY_5",
"KEY_6",
"KEY_7",
"KEY_8",
"KEY_9",
"KEY_SEMICOLON",
"KEY_EQUAL",
"KEY_A",
"KEY_B",
"KEY_C",
"KEY_D",
"KEY_E",
"KEY_F",
"KEY_G",
"KEY_H",
"KEY_I",
"KEY_J",
"KEY_K",
"KEY_L",
"KEY_M",
"KEY_N",
"KEY_O",
"KEY_P",
"KEY_Q",
"KEY_R",
"KEY_S",
"KEY_T",
"KEY_U",
"KEY_V",
"KEY_W",
"KEY_X",
"KEY_Y",
"KEY_Z",
"KEY_LEFT_BRACKET",
"KEY_BACKSLASH",
"KEY_RIGHT_BRACKET",
"KEY_GRAVE_ACCENT",
"KEY_WORLD_1",
"KEY_WORLD_2",
"KEY_ESCAPE",
"KEY_ENTER",
"KEY_TAB",
"KEY_BACKSPACE",
"KEY_INSERT",
"KEY_DELETE",
"KEY_RIGHT",
"KEY_LEFT",
"KEY_DOWN",
"KEY_UP",
"KEY_PAGE_UP",
"KEY_PAGE_DOWN",
"KEY_HOME",
"KEY_END",
"KEY_CAPS_LOCK",
"KEY_SCROLL_LOCK",
"KEY_NUM_LOCK",
"KEY_PRINT_SCREEN",
"KEY_PAUSE",
"KEY_F1",
"KEY_F2",
"KEY_F3",
"KEY_F4",
"KEY_F5",
"KEY_F6",
"KEY_F7",
"KEY_F8",
"KEY_F9",
"KEY_F10",
"KEY_F11",
"KEY_F12",
"KEY_F13",
"KEY_F14",
"KEY_F15",
"KEY_F16",
"KEY_F17",
"KEY_F18",
"KEY_F19",
"KEY_F20",
"KEY_F21",
"KEY_F22",
"KEY_F23",
"KEY_F24",
"KEY_F25",
"KEY_KP_0",
"KEY_KP_1",
"KEY_KP_2",
"KEY_KP_3",
"KEY_KP_4",
"KEY_KP_5",
"KEY_KP_6",
"KEY_KP_7",
"KEY_KP_8",
"KEY_KP_9",
"KEY_KP_DECIMAL",
"KEY_KP_DIVIDE",
"KEY_KP_MULTIPLY",
"KEY_KP_SUBTRACT",
"KEY_KP_ADD",
"KEY_KP_ENTER",
"KEY_KP_EQUAL",
"KEY_LEFT_SHIFT",
"KEY_LEFT_CONTROL",
"KEY_LEFT_ALT",
"KEY_LEFT_SUPER",
"KEY_RIGHT_SHIFT",
"KEY_RIGHT_CONTROL",
"KEY_RIGHT_ALT",
"KEY_RIGHT_SUPER",
"KEY_MENU",
"MOUSE_1",
"MOUSE_2",
"MOUSE_3",
"MOUSE_4",
"MOUSE_5",
"MOUSE_6",
};
std::vector<ControlEvent> Input::s_control_events;
LuaFunction Input::s_control_cb;
Control Input::ControlFromGlfwKey(int key) {
return s_glfw_key_map[key];
}
Control Input::ControlFromGlfwMouseButton(int button) {
return s_glfw_mouse_map[button];
}
void Input::AddControlEvent(Control control, bool press) {
s_control_events.push_back({ control, press });
}
void Input::RegisterLuaFunctions() {
LuaAPI::RegisterFunction("i_setcontrolcallback", [](LuaFunction callback) {
s_control_cb = callback;
});
}
void Input::ProcessEvents() {
if (s_control_cb) {
for (const auto& event : s_control_events) {
LuaAPI::Call(s_control_cb, s_control_names[event.control], event.press);
}
}
s_control_events.clear();
}
void Input::Shutdown() {
s_control_cb = LuaFunction();
s_control_events.clear();
}

168
src/tsr/input.hpp Normal file
View File

@ -0,0 +1,168 @@
#pragma once
#include <vector>
#include <functional>
#include "luaapi.hpp"
namespace TSR {
enum Control {
KEY_NONE,
KEY_SPACE,
KEY_APOSTROPHE,
KEY_COMMA,
KEY_MINUS,
KEY_PERIOD,
KEY_SLASH,
KEY_0,
KEY_1,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8,
KEY_9,
KEY_SEMICOLON,
KEY_EQUAL,
KEY_A,
KEY_B,
KEY_C,
KEY_D,
KEY_E,
KEY_F,
KEY_G,
KEY_H,
KEY_I,
KEY_J,
KEY_K,
KEY_L,
KEY_M,
KEY_N,
KEY_O,
KEY_P,
KEY_Q,
KEY_R,
KEY_S,
KEY_T,
KEY_U,
KEY_V,
KEY_W,
KEY_X,
KEY_Y,
KEY_Z,
KEY_LEFT_BRACKET,
KEY_BACKSLASH,
KEY_RIGHT_BRACKET,
KEY_GRAVE_ACCENT,
KEY_WORLD_1,
KEY_WORLD_2,
KEY_ESCAPE,
KEY_ENTER,
KEY_TAB,
KEY_BACKSPACE,
KEY_INSERT,
KEY_DELETE,
KEY_RIGHT,
KEY_LEFT,
KEY_DOWN,
KEY_UP,
KEY_PAGE_UP,
KEY_PAGE_DOWN,
KEY_HOME,
KEY_END,
KEY_CAPS_LOCK,
KEY_SCROLL_LOCK,
KEY_NUM_LOCK,
KEY_PRINT_SCREEN,
KEY_PAUSE,
KEY_F1,
KEY_F2,
KEY_F3,
KEY_F4,
KEY_F5,
KEY_F6,
KEY_F7,
KEY_F8,
KEY_F9,
KEY_F10,
KEY_F11,
KEY_F12,
KEY_F13,
KEY_F14,
KEY_F15,
KEY_F16,
KEY_F17,
KEY_F18,
KEY_F19,
KEY_F20,
KEY_F21,
KEY_F22,
KEY_F23,
KEY_F24,
KEY_F25,
KEY_KP_0,
KEY_KP_1,
KEY_KP_2,
KEY_KP_3,
KEY_KP_4,
KEY_KP_5,
KEY_KP_6,
KEY_KP_7,
KEY_KP_8,
KEY_KP_9,
KEY_KP_DECIMAL,
KEY_KP_DIVIDE,
KEY_KP_MULTIPLY,
KEY_KP_SUBTRACT,
KEY_KP_ADD,
KEY_KP_ENTER,
KEY_KP_EQUAL,
KEY_LEFT_SHIFT,
KEY_LEFT_CONTROL,
KEY_LEFT_ALT,
KEY_LEFT_SUPER,
KEY_RIGHT_SHIFT,
KEY_RIGHT_CONTROL,
KEY_RIGHT_ALT,
KEY_RIGHT_SUPER,
KEY_MENU,
MOUSE_1,
MOUSE_2,
MOUSE_3,
MOUSE_4,
MOUSE_5,
MOUSE_6,
CONTROL_COUNT
};
struct ControlEvent {
Control control;
bool press;
};
class Input {
static const Control s_glfw_key_map[];
static const Control s_glfw_mouse_map[];
static const char* const s_control_names[];
static std::vector<ControlEvent> s_control_events;
static LuaFunction s_control_cb;
public:
static Control ControlFromGlfwKey(int key);
static Control ControlFromGlfwMouseButton(int button);
static void AddControlEvent(Control control, bool press);
static void RegisterLuaFunctions();
static void ProcessEvents();
static void Shutdown();
};
}

View File

@ -20,6 +20,9 @@ void LuaAPI::Shutdown() {
}
void LuaAPI::Load(const std::string& name) {
if (s_loaded_scripts.contains(name))
return;
s_loaded_scripts.insert(name);
auto script = Filesystem::Read(name);

View File

@ -1,10 +1,15 @@
#pragma once
#define SOL_CHECK_ARGUMENTS
#include <sol/sol.hpp>
#include <string>
#include <memory>
#include <set>
namespace TSR {
using LuaReference = sol::reference;
using LuaFunction = sol::function;
class LuaAPI {
static std::unique_ptr<sol::state> s_lua;
@ -27,6 +32,18 @@ namespace TSR {
func(std::forward<Args>(args)...);
}
template <typename... Args>
static void Call(LuaReference& ref, Args&&... args) {
sol::protected_function func = ref;
auto res = func(std::forward<Args>(args)...);
if (!res.valid()) {
sol::error err = res;
throw std::runtime_error(err.what());
}
}
static sol::state& GetState() { return *s_lua; }
};

View File

@ -129,6 +129,8 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\tsr\entity.cpp" />
<ClCompile Include="src\tsr\input.cpp" />
<ClCompile Include="src\tsr\luaapi.cpp" />
<ClCompile Include="src\tsr\entity_cct.cpp" />
<ClCompile Include="src\tsr\physics.cpp" />
@ -145,6 +147,7 @@
<ClCompile Include="src\tsr\worldmap.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\tsr\input.hpp" />
<ClInclude Include="src\tsr\luaapi.hpp" />
<ClInclude Include="src\tsr\entity_cct.hpp" />
<ClInclude Include="src\tsr\physics.hpp" />

View File

@ -57,6 +57,12 @@
<ClCompile Include="src\tsr\luaapi.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tsr\input.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\tsr\entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\tsr\window.hpp">
@ -107,5 +113,8 @@
<ClInclude Include="src\tsr\luaapi.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\tsr\input.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>