Skip to content

Commit

Permalink
Added nbnet as net lib
Browse files Browse the repository at this point in the history
Also, improved A LOT the entrypoint, the CLI argument handling, the
Makefile structure (+ some bugs fixed there as well), etc.
  • Loading branch information
iWas-Coder committed Mar 27, 2024
1 parent 809c693 commit d018524
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "vendor/raylib"]
path = vendor/raylib
url = https://github.com/raysan5/raylib
[submodule "vendor/nbnet"]
path = vendor/nbnet
url = https://github.com/nathhB/nbnet
70 changes: 50 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ NAME = sparky
include config.mk

# Version formatting
EXTRAVERSION_COMPUTED := $(or $(and $(wildcard .git/), $(and $(shell which git 2>/dev/null), -git+$(shell which git &>/dev/null && git rev-parse --short HEAD))), -dev)
ifndef EXTRAVERSION
EXTRAVERSION += $(EXTRAVERSION_COMPUTED)
ifeq ($(SUBLEVEL), 0)
DIST_VERSION = $(VERSION).$(PATCHLEVEL)
else
DIST_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
ifeq ($(wildcard .git/), .git/)
ifneq ($(shell which git 2>/dev/null),)
GIT_SHA := -git+$(shell git rev-parse --short HEAD)
endif
FULL_VERSION = $(DIST_VERSION)$(EXTRAVERSION)
DEVEXTRAVERSION := $(or $(GIT_SHA), -dev)
endif
ifeq ($(SUBLEVEL), 0)
DIST_VERSION = $(VERSION).$(PATCHLEVEL)$(EXTRAVERSION)
else
EXTRAVERSION += $(EXTRAVERSION_COMPUTED)
DIST_VERSION = $(VERSION).$(PATCHLEVEL)$(EXTRAVERSION)
FULL_VERSION = $(DIST_VERSION)
DIST_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
endif
FULL_VERSION = $(DIST_VERSION)$(DEVEXTRAVERSION)

# Pretty Printing Output (PPO)
PPO_MKDIR = MKDIR
Expand All @@ -54,6 +52,7 @@ SRC_DIR = src
HDR_DIR = include
BUILD_DIR = build
VENDOR_DIR = vendor
NBNET_DIR = $(VENDOR_DIR)/nbnet
RAYLIB_SRC_DIR = $(VENDOR_DIR)/raylib/$(SRC_DIR)
RAYLIB_BUILD_DIR = $(BUILD_DIR)/raylib

Expand All @@ -64,8 +63,8 @@ SRCS := $(wildcard $(SRC_DIR)/*.c)
OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS))

# Build flags
CC = gcc
AR = ar -rc
CC = gcc
AR = ar -rc
ifdef D
DEBUG_SYM_OPTS = -ggdb
else
Expand All @@ -74,11 +73,42 @@ else
RELEASE_OPTS = -pipe -O3
STRIP_OPTS = -s
endif
RAYLIB_CPPFLAGS = $(DISABLE_ASSERTS_OPTS) -D PLATFORM_DESKTOP -I $(RAYLIB_SRC_DIR) -I $(RAYLIB_SRC_DIR)/external/glfw/$(HDR_DIR)
CPPFLAGS = $(DISABLE_ASSERTS_OPTS) -I $(RAYLIB_SRC_DIR) -I $(HDR_DIR)
RAYLIB_CFLAGS = -std=gnu99 $(HIDE_WARNS_OPTS) $(DEBUG_SYM_OPTS) $(RELEASE_OPTS)
CFLAGS = -std=c17 -Wall -Wextra -pedantic $(DEBUG_SYM_OPTS) $(RELEASE_OPTS)
LDFLAGS = -Wl,--build-id $(STRIP_OPTS) $(RELEASE_OPTS) -L $(BUILD_DIR) -lraylib -lm
define RAYLIB_CPPFLAGS
$(DISABLE_ASSERTS_OPTS) \
-D PLATFORM_DESKTOP \
-isystem $(RAYLIB_SRC_DIR) \
-isystem $(RAYLIB_SRC_DIR)/external/glfw/$(HDR_DIR)
endef
define CPPFLAGS
-D SK_VERSION=$(DIST_VERSION) \
$(DISABLE_ASSERTS_OPTS) \
-D _POSIX_C_SOURCE=199309L \
-isystem $(RAYLIB_SRC_DIR) \
-isystem $(NBNET_DIR) \
-I $(HDR_DIR)
endef
define RAYLIB_CFLAGS
-std=gnu99 \
$(HIDE_WARNS_OPTS) \
$(DEBUG_SYM_OPTS) \
$(RELEASE_OPTS)
endef
define CFLAGS
-std=c17 \
-Wall \
-Wextra \
-pedantic \
$(DEBUG_SYM_OPTS) \
$(RELEASE_OPTS)
endef
define LDFLAGS
-Wl,--build-id \
$(STRIP_OPTS) \
$(RELEASE_OPTS) \
-L $(BUILD_DIR) \
-lraylib \
-lm
endef

# Build output
RAYLIB_OUT = $(BUILD_DIR)/libraylib.a
Expand Down Expand Up @@ -150,8 +180,8 @@ help:
@echo "* game :: Build the bare game"
@echo " clean :: Remove the 'build' directory"
@echo " mrproper :: Remove and cleans everything"
@echo " version :: Shows the current version string"
@echo " help :: Shows this help and usage panel"
@echo " version :: Show the current version string"
@echo " help :: Show this help and usage panel"
@echo
@echo "Execute 'make' or 'make all' to build all targets marked with [*]"
@echo "For further info see the ./README.org file"
2 changes: 1 addition & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
VERSION = 0
PATCHLEVEL = 1
SUBLEVEL = 0
EXTRAVERSION =
EXTRAVERSION = -alpha1
# Quiet
Q = @
# Debug
Expand Down
2 changes: 1 addition & 1 deletion include/sk_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

#include <sk_defines.h>

u8 sk_client_run(void);
u8 sk_client_run(const char *ip);
32 changes: 32 additions & 0 deletions include/sk_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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/>.
*/


#pragma once

#include <sk_defines.h>

#define NBN_LogTrace(...) TraceLog(LOG_TRACE, __VA_ARGS__)
#define NBN_LogDebug(...) TraceLog(LOG_DEBUG, __VA_ARGS__)
#define NBN_LogInfo(...) TraceLog(LOG_INFO, __VA_ARGS__)
#define NBN_LogWarning(...) TraceLog(LOG_WARNING, __VA_ARGS__)
#define NBN_LogError(...) TraceLog(LOG_ERROR, __VA_ARGS__)

u8 sk_server_run(void);
4 changes: 3 additions & 1 deletion src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ static u8 __shutdown(void) {
return 0;
}

u8 sk_client_run(void) {
u8 sk_client_run(const char *ip) {
TraceLog(LOG_INFO, "Initializing %s", SK_CLIENT_NAME);
if (!ip) TraceLog(LOG_WARNING, "Running in offline mode");
else TraceLog(LOG_INFO, "Connected to server @ %s", ip);
__init();
sk_client_renderer_loop {
sk_client_renderer_update(&state);
Expand Down
77 changes: 77 additions & 0 deletions src/sk_server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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/>.
*/


#include <raylib.h>
#include <signal.h>
#include <sk_server.h>

#define NBNET_IMPL
#include <nbnet.h>
#include <net_drivers/udp.h>

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <winsock2.h>
#include <synchapi.h>
#else
#include <time.h>
#endif

static volatile u8 running = 1;

static inline void __handle_interrupt(int signum) {
if (signum == SIGINT) running = 0;
}

static void __wait_next_tick(double t) {
#if defined(_WIN32) || defined(_WIN64)
Sleep(1000 * t);
#else
long ns_t = 1e9 * t;
struct timespec ts = {
.tv_sec = ns_t / (long) (1e9 - 1),
.tv_nsec = ns_t % (long) (1e9 - 1)
};
nanosleep(&ts, &ts);
#endif
}

static inline u8 __shutdown(void) {
NBN_GameServer_Stop();
TraceLog(LOG_INFO, "%s closed successfully", SK_SERVER_NAME);
return 0;
}

u8 sk_server_run(void) {
TraceLog(LOG_INFO, "Initializing %s", SK_SERVER_NAME);
NBN_UDP_Register();
if (NBN_GameServer_StartEx(SK_SERVER_NAME, SK_SERVER_PORT, 1) == -1) {
TraceLog(LOG_ERROR, "Unable to start the server");
return 1;
}
// TODO: register msgs
double dt = 1 / SK_SERVER_TICK_RATE;
signal(SIGINT, __handle_interrupt);
while (running) {
__wait_next_tick(dt);
}
return __shutdown();
}
40 changes: 34 additions & 6 deletions src/sparky.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,54 @@
*/


#include <stdio.h>
#include <string.h>
#include <raylib.h>
#include <sk_client.h>
#include <sk_server.h>

#define xstr(s) str(s)
#define str(s) #s

static const char *help_msg = "Usage: %s [OPTION]\n"
"GNU Sparky --- A 5v5 character-based libre tactical shooter\n\n"
"If no option is provided, the client will start in offline mode.\n\n"
"Options:\n"
" -i, --ip <IP> \t start the client and connect to the server at <IP>\n"
" -s, --server \t\t start the server\n"
" -h, --help \t\t display this help and exit\n"
" -v, --version \t output version information and exit\n\n"
"Report bugs to: <https://github.com/iWas-Coder/sparky/issues>\n"
"GNU Sparky home page: <https://github.com/iWas-Coder/sparky>\n"
"General help using GNU software: <https://www.gnu.org/gethelp/>\n";
static const char *version_msg = "GNU Sparky %s\n"
"Copyright (C) 2024 Wasym A. Alonso\n"
"License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl-3.0>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n\n"
"Written by Wasym A. Alonso.\n";
static const char *unrecog_opt_msg = "%s: unrecognized option\n"
"Try '%s --help' for more information.\n";

int main(int argc, char **argv) {
#ifndef NDEBUG
#define NBN_DEBUG
SetTraceLogLevel(LOG_DEBUG);
TraceLog(LOG_WARNING, "Debug mode is enabled");
TraceLog(LOG_WARNING, "Running in debug mode");
#endif

if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
TraceLog(LOG_INFO, "usage: %s [--server]", argv[0]);
printf(help_msg, argv[0]);
return 0;
}
if (argc == 2 && !strcmp(argv[1], "--server")) {
TraceLog(LOG_WARNING, "Server not implemented yet. Exiting...");
if (argc == 2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
printf(version_msg, xstr(SK_VERSION));
return 0;
}
if (argc == 1) return sk_client_run();
if (argc == 2 && (!strcmp(argv[1], "-s") || !strcmp(argv[1], "--server"))) return sk_server_run();
if (argc == 3 && (!strcmp(argv[1], "-i") || !strcmp(argv[1], "--ip"))) return sk_client_run(argv[2]);
if (argc == 1) return sk_client_run(0);

TraceLog(LOG_ERROR, "usage: %s [--server]", argv[0]);
fprintf(stderr, unrecog_opt_msg, argv[0], argv[0]);
return 1;
}
1 change: 1 addition & 0 deletions vendor/nbnet
Submodule nbnet added at 36088b

0 comments on commit d018524

Please sign in to comment.