Skip to content

Commit

Permalink
Value check in config.lua fields + separated raylib bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Apr 23, 2024
1 parent 7fa071a commit f5662d4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
10 changes: 1 addition & 9 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@
--


local MOUSE_BUTTON_LEFT = 0
local KEY_SPACE = 32
local KEY_A = 65
local KEY_D = 68
local KEY_E = 69
local KEY_Q = 81
local KEY_R = 82
local KEY_S = 83
local KEY_W = 87
require("include/raylib")

Crosshair = {
radius = 2.5
Expand Down
30 changes: 30 additions & 0 deletions include/raylib.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--
-- GNU Sparky --- A 5v5 character-based libre tactical shooter
-- Copyright (C) 2024 Wasym A. Alonso
--
-- This file is part of Sparky.
--
-- Sparky is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- Sparky is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with Sparky. If not, see <http://www.gnu.org/licenses/>.
--


MOUSE_BUTTON_LEFT = 0
KEY_SPACE = 32
KEY_A = 65
KEY_D = 68
KEY_E = 69
KEY_Q = 81
KEY_R = 82
KEY_S = 83
KEY_W = 87
45 changes: 30 additions & 15 deletions src/sk_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,55 +69,70 @@ void sk_config_load(const char *filepath, sk_config *c) {
}
lua_getglobal(L, "Crosshair");
lua_getfield(L, -1, "radius");
c->crosshair.radius = (f32) lua_tonumber(L, -1);
if ((f32) lua_tonumber(L, -1) > 0) c->crosshair.radius = (f32) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Crosshair.radius` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_pop(L, 1);
lua_getglobal(L, "Video");
lua_getfield(L, -1, "win_width");
c->video.win_width = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->video.win_width = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Video.win_width` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "win_height");
c->video.win_height = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->video.win_height = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Video.win_height` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "fps_limit");
c->video.fps_limit = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->video.fps_limit = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Video.fps_limit` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_pop(L, 1);
lua_getglobal(L, "General");
lua_getfield(L, -1, "fov");
c->general.fov = (u8) lua_tonumber(L, -1);
if ((i8) lua_tonumber(L, -1) > 0) c->general.fov = (u8) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `General.fov` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "mouse_sensitivity");
c->general.mouse_sensitivity = (f32) lua_tonumber(L, -1);
if ((f32) lua_tonumber(L, -1) > 0) c->general.mouse_sensitivity = (f32) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `General.mouse_sensitivity` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_pop(L, 1);
lua_getglobal(L, "Controls");
lua_getfield(L, -1, "move_forward");
c->controls.move_forward = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.move_forward = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.move_forward` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "move_backwards");
c->controls.move_backwards = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.move_backwards = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.move_backwards` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "move_left");
c->controls.move_left = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.move_left = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.move_left` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "move_right");
c->controls.move_right = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.move_right = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.move_right` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "peek_left");
c->controls.peek_left = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.peek_left = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.peek_left` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "peek_right");
c->controls.peek_right = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.peek_right = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.peek_right` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "jump");
c->controls.jump = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.jump = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.jump` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "shoot");
c->controls.shoot = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) >= 0) c->controls.shoot = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.jump` needs to be >= 0. Using default instead");
lua_pop(L, 1);
lua_getfield(L, -1, "reload");
c->controls.reload = (u16) lua_tonumber(L, -1);
if ((i16) lua_tonumber(L, -1) > 0) c->controls.reload = (u16) lua_tonumber(L, -1);
else SK_LOG_WARN("sk_config_load :: `Controls.reload` needs to be > 0. Using default instead");
lua_pop(L, 1);
lua_pop(L, 1);
lua_close(L);
Expand Down

0 comments on commit f5662d4

Please sign in to comment.