Skip to content

Commit

Permalink
Replaced nbnet with custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Mar 31, 2024
1 parent e0ccdba commit ff72f65
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 96 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "vendor/raylib"]
path = vendor/raylib
url = https://github.com/raysan5/raylib
[submodule "vendor/nbnet"]
path = vendor/nbnet
url = https://github.com/nathhB/nbnet
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ 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 Down Expand Up @@ -84,7 +83,6 @@ define CPPFLAGS
$(DISABLE_ASSERTS_OPTS) \
-D _POSIX_C_SOURCE=199309L \
-isystem $(RAYLIB_SRC_DIR) \
-isystem $(NBNET_DIR) \
-I $(HDR_DIR)
endef
define RAYLIB_CFLAGS
Expand Down
3 changes: 3 additions & 0 deletions include/sk_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include <stdint.h>

#define sk_xstr(s) sk_str(s)
#define sk_str(s) #s

typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
Expand Down
1 change: 1 addition & 0 deletions include/sk_gametypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ typedef enum {
typedef struct State {
Scene current_scene;
sk_player player;
u8 is_online;
} State;
7 changes: 3 additions & 4 deletions include/sk_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
#include <raylib.h>
#include <sk_weapon.h>

// fwd-decl
struct State;

typedef enum {
SK_PLAYER_KIND_JETT
} sk_player_kind;

typedef struct sk_player {
i8 lobby_id;
i8 lobby_slot_idx;
sk_player_kind kind;
Camera3D camera;
Model model;
Expand All @@ -43,7 +42,7 @@ static const char * const sk_player_kinds[] = {
[SK_PLAYER_KIND_JETT] = "jett"
};

void sk_player_create(struct State *s, sk_player_kind kind);
sk_player sk_player_create(sk_player_kind kind);

void sk_player_destroy(sk_player *p);

Expand Down
6 changes: 5 additions & 1 deletion include/sk_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#define sk_renderer_loop while (!WindowShouldClose())

void sk_renderer_init(void);
void sk_renderer_create(void);

void sk_renderer_destroy(void);

void sk_renderer_update(State *s);

void sk_renderer_draw(State *s);
14 changes: 5 additions & 9 deletions include/sk_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@

#include <sk_defines.h>

#define SK_SERVER_NAME "sparky-server"
#define SK_SERVER_PORT 27015
#define SK_SERVER_TICK_RATE 128

#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__)
#define SK_SERVER_NAME "sparky-server"
#define SK_SERVER_PORT 27015
#define SK_SERVER_TICK_RATE 128
#define SK_SERVER_MSG_CONN_REQ "ping::" SK_SERVER_NAME
#define SK_SERVER_MSG_CONN_RES "pong::" SK_SERVER_NAME

u8 sk_server_run(void);
5 changes: 1 addition & 4 deletions include/sk_weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
#include <raylib.h>
#include <sk_defines.h>

// fwd-decl
struct sk_player;

typedef enum {
SK_WEAPON_KIND_7MM
} sk_weapon_kind;
Expand All @@ -44,7 +41,7 @@ static const char * const sk_weapon_kinds[] = {
[SK_WEAPON_KIND_7MM] = "7mm"
};

void sk_weapon_create(struct sk_player *p, sk_weapon_kind kind);
sk_weapon sk_weapon_create(sk_weapon_kind kind);

void sk_weapon_destroy(sk_weapon *w);

Expand Down
70 changes: 55 additions & 15 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,77 @@
*/


#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <raylib.h>
#include <arpa/inet.h>
#include <sk_config.h>
#include <sk_client.h>
#include <sk_server.h>
#include <sys/socket.h>
#include <sk_renderer.h>
#include <sk_gametypes.h>

static State state = {0};

static u8 destroy(void) {
sk_player_destroy(&state.player);
CloseAudioDevice();
CloseWindow();
state = (State) {0};
TraceLog(LOG_INFO, "%s closed successfully", SK_CLIENT_NAME);
return 0;
}

u8 sk_client_run(const char *ip) {
TraceLog(LOG_INFO, "Initializing %s", SK_CLIENT_NAME);
int sock_fd;
if (!ip) TraceLog(LOG_WARNING, "Running in offline mode");
else TraceLog(LOG_INFO, "Connected to server @ %s", ip);

else {
TraceLog(LOG_INFO, "Connecting to `%s` ...", ip);
const struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_port = htons(SK_SERVER_PORT),
.sin_addr.s_addr = inet_addr(ip)
};
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock_fd == -1) {
TraceLog(LOG_ERROR, "socket(2) :: %s", strerror(errno));
return 1;
}
if (sendto(sock_fd,
SK_SERVER_MSG_CONN_REQ,
strlen(SK_SERVER_MSG_CONN_REQ),
0,
(const struct sockaddr *) &server_addr,
sizeof(server_addr)) == -1) {
TraceLog(LOG_ERROR, "sendto(2) :: %s", strerror(errno));
close(sock_fd);
return 1;
}
char pong_msg[1024];
int pong_msg_n = recv(sock_fd, pong_msg, sizeof(pong_msg), MSG_WAITALL);
if (pong_msg_n == -1) {
TraceLog(LOG_ERROR, "recv(2) :: %s", strerror(errno));
close(sock_fd);
return 1;
}
pong_msg[pong_msg_n] = 0;
if (!strcmp(pong_msg, SK_SERVER_MSG_CONN_RES)) {
state.is_online = 1;
}
if (!state.is_online) {
TraceLog(LOG_ERROR, "Unable to communicate with `%s`. Exiting...", ip);
close(sock_fd);
return 1;
}
TraceLog(LOG_INFO, "Connected successfully to `%s`", ip);
}
if (!ChangeDirectory(GetApplicationDirectory())) {
TraceLog(LOG_WARNING, "Could not change CWD to the game's root directory");
}
sk_renderer_init();
sk_renderer_create();
state.current_scene = SCENE_MAIN_MENU;
sk_player_create(&state, SK_PLAYER_KIND_JETT);

state.player = sk_player_create(SK_PLAYER_KIND_JETT);
sk_renderer_loop {
sk_renderer_update(&state);
sk_renderer_draw(&state);
}
return destroy();
sk_player_destroy(&state.player);
sk_renderer_destroy();
if (state.is_online) close(sock_fd);
TraceLog(LOG_INFO, "%s closed successfully", SK_CLIENT_NAME);
return 0;
}
23 changes: 13 additions & 10 deletions src/sk_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <stdio.h>
#include <sk_player.h>
#include <sk_config.h>
#include <sk_gametypes.h>

#define FILEPATH_BUFFER_MAX_SIZE 64
#define PLAYER_HEIGHT 2
Expand All @@ -31,15 +30,19 @@
#define PEEK_ACCELERATION 1.5
#define WALK_VELOCITY 0.1

void sk_player_create(State *s, sk_player_kind kind) {
s->player.camera = (Camera3D) {
.position = (Vector3) { 0, PLAYER_HEIGHT, 4 },
.target = (Vector3) { 0, 2, 0 },
.up = (Vector3) { 0, 1, 0 },
.fovy = SK_CONFIG_CLIENT_FOV,
.projection = CAMERA_PERSPECTIVE
sk_player sk_player_create(sk_player_kind kind) {
return (sk_player) {
.lobby_id = -1,
.lobby_slot_idx = -1,
.kind = kind,
.camera = (Camera3D) {
.position = (Vector3) { 0, PLAYER_HEIGHT, 4 },
.target = (Vector3) { 0, 2, 0 },
.up = (Vector3) { 0, 1, 0 },
.fovy = SK_CONFIG_CLIENT_FOV,
.projection = CAMERA_PERSPECTIVE
}
};
s->player.kind = kind;
}

void sk_player_destroy(sk_player *p) {
Expand All @@ -52,7 +55,7 @@ void sk_player_load(sk_player *p, sk_weapon_kind initial_weapon_kind) {
char filepath[FILEPATH_BUFFER_MAX_SIZE] = {0};
sprintf(filepath, "assets/models/%s.glb", sk_player_kinds[p->kind]);
p->model = LoadModel(filepath);
sk_weapon_create(p, initial_weapon_kind);
p->weapon = sk_weapon_create(initial_weapon_kind);
}

void sk_player_jump(sk_player *p) {
Expand Down
14 changes: 6 additions & 8 deletions src/sk_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@
#include <sk_client.h>
#include <sk_renderer.h>

static inline void __open_window(void) {
void sk_renderer_create(void) {
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(SK_CONFIG_CLIENT_WIN_WIDTH,
SK_CONFIG_CLIENT_WIN_HEIGHT,
SK_CLIENT_NAME);
assert(IsWindowReady());
}

static inline void __init_audio(void) {
InitAudioDevice();
assert(IsAudioDeviceReady());
SetTargetFPS(SK_CONFIG_CLIENT_FPS);
}

void sk_renderer_init(void) {
__open_window();
__init_audio();
SetTargetFPS(SK_CONFIG_CLIENT_FPS);
void sk_renderer_destroy(void) {
CloseAudioDevice();
CloseWindow();
}
28 changes: 22 additions & 6 deletions src/sk_renderer_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include <assert.h>
#include <sk_config.h>
#include <sk_client.h>
#include <sk_server.h>
#include <sk_defines.h>
#include <sk_renderer.h>

static void __draw_main_menu(void) {
ClearBackground(BLACK);
const char *subtitle = "Press <ENTER> to start";
const char * const subtitle = "Press <ENTER> to start";
u8 subtitle_size = 30;
DrawText(SK_CLIENT_MAIN_MENU_TITLE,
(GetScreenWidth() - MeasureText(SK_CLIENT_MAIN_MENU_TITLE,
Expand All @@ -42,6 +43,11 @@ static void __draw_main_menu(void) {
((GetScreenHeight() - MeasureText(subtitle, subtitle_size)) / 2) + 115,
subtitle_size,
RAYWHITE);
DrawText("v" sk_xstr(SK_VERSION),
10,
GetScreenHeight() - 25,
20,
RAYWHITE);
}

static inline void __draw_floor(void) {
Expand Down Expand Up @@ -73,9 +79,15 @@ static inline void __draw_fps(void) {
}

static inline void __draw_frametime(void) {
char dt[10];
snprintf(dt, sizeof(dt), "%.4f ms", GetFrameTime() * 1000);
DrawText(dt, 10, 33, 20, LIME);
DrawText(TextFormat("%.4f ms", GetFrameTime() * 1000), 10, 33, 20, LIME);
}

static inline void __draw_ping(void) {
DrawText(TextFormat("N/A ms"), 10, 50, 20, LIME);
}

static inline void __draw_bandwidth(void) {
DrawText(TextFormat("(d) N/A bps | (u) N/A bps"), 10, 70, 20, LIME);
}

static inline void __draw_crosshair(void) {
Expand All @@ -89,9 +101,13 @@ static inline void __draw_crosshair(void) {
WHITE);
}

static void __draw_gameplay_hud(void) {
static void __draw_gameplay_hud(State *s) {
__draw_fps();
__draw_frametime();
if (s->is_online) {
__draw_ping();
__draw_bandwidth();
}
__draw_crosshair();
}

Expand All @@ -103,7 +119,7 @@ void sk_renderer_draw(State *s) {
break;
case SCENE_GAMEPLAY:
__draw_gameplay_world(s);
__draw_gameplay_hud();
__draw_gameplay_hud(s);
break;
default:
assert(0 && "Unreachable");
Expand Down
Loading

0 comments on commit ff72f65

Please sign in to comment.