1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-06-29 11:56:44 +02:00

Add networking to game engine / add multiplayer chess

This commit is contained in:
jfreegman
2021-01-17 13:29:13 -05:00
parent 60bdcf0ba5
commit 7aeb1a0aac
18 changed files with 1186 additions and 234 deletions

View File

@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "game_util.h"
#include "toxic.h"
@ -156,3 +157,38 @@ int game_util_random_colour(void)
return RED;
}
}
static size_t net_pack_u16(uint8_t *bytes, uint16_t v)
{
bytes[0] = (v >> 8) & 0xff;
bytes[1] = v & 0xff;
return sizeof(v);
}
static size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
{
uint8_t hi = bytes[0];
uint8_t lo = bytes[1];
*v = ((uint16_t)hi << 8) | lo;
return sizeof(*v);
}
size_t game_util_pack_u32(uint8_t *bytes, uint32_t v)
{
uint8_t *p = bytes;
p += net_pack_u16(p, (v >> 16) & 0xffff);
p += net_pack_u16(p, v & 0xffff);
return p - bytes;
}
size_t game_util_unpack_u32(const uint8_t *bytes, uint32_t *v)
{
const uint8_t *p = bytes;
uint16_t hi;
uint16_t lo;
p += net_unpack_u16(p, &hi);
p += net_unpack_u16(p, &lo);
*v = ((uint32_t)hi << 16) | lo;
return p - bytes;
}