@ -29,7 +29,7 @@ void __assert(bool cond, const char *file, int line, const char *cond_string)
static V2 cp_to_v2 ( cpVect v )
static V2 cp_to_v2 ( cpVect v )
{
{
return ( V2 ) { . x = v . x , . y = v . y } ;
return ( V2 ) { . x = ( float ) v . x , . y = ( float ) v . y } ;
}
}
static cpVect v2_to_cp ( V2 v )
static cpVect v2_to_cp ( V2 v )
@ -64,7 +64,7 @@ EntityID get_id(struct GameState *gs, Entity *e)
if ( e = = NULL )
if ( e = = NULL )
return ( EntityID ) { 0 } ;
return ( EntityID ) { 0 } ;
in t index = e - gs - > entities ;
size_ t index = e - gs - > entities ;
assert ( index > = 0 ) ;
assert ( index > = 0 ) ;
assert ( index < gs - > cur_next_entity ) ;
assert ( index < gs - > cur_next_entity ) ;
@ -340,10 +340,18 @@ static void grid_remove_box(struct GameState *gs, struct Entity *grid, struct En
V2 cur_local_pos = entity_shape_pos ( N ) ;
V2 cur_local_pos = entity_shape_pos ( N ) ;
const V2 dirs [ ] = {
const V2 dirs [ ] = {
( V2 ) { . x = - 1.0f , . y = 0.0f } ,
( V2 ) {
( V2 ) { . x = 1.0f , . y = 0.0f } ,
. x = - 1.0f , . y = 0.0f
( V2 ) { . x = 0.0f , . y = 1.0f } ,
} ,
( V2 ) { . x = 0.0f , . y = - 1.0f } ,
( V2 ) {
. x = 1.0f , . y = 0.0f
} ,
( V2 ) {
. x = 0.0f , . y = 1.0f
} ,
( V2 ) {
. x = 0.0f , . y = - 1.0f
} ,
} ;
} ;
int num_dirs = sizeof ( dirs ) / sizeof ( * dirs ) ;
int num_dirs = sizeof ( dirs ) / sizeof ( * dirs ) ;
@ -416,7 +424,7 @@ static void grid_remove_box(struct GameState *gs, struct Entity *grid, struct En
}
}
cpBodySetVelocity ( new_grid - > body , cpBodyGetVelocityAtWorldPoint ( grid - > body , v2_to_cp ( grid_com ( new_grid ) ) ) ) ;
cpBodySetVelocity ( new_grid - > body , cpBodyGetVelocityAtWorldPoint ( grid - > body , v2_to_cp ( grid_com ( new_grid ) ) ) ) ;
cpBodySetAngularVelocity ( new_grid - > body , grid _angular_velocity( grid ) ) ;
cpBodySetAngularVelocity ( new_grid - > body , entity _angular_velocity( grid ) ) ;
}
}
}
}
@ -517,11 +525,11 @@ V2 grid_snapped_box_pos(Entity *grid, V2 world)
}
}
float entity_rotation ( Entity * grid )
float entity_rotation ( Entity * grid )
{
{
return cpBodyGetAngle ( grid - > body ) ;
return ( float ) cpBodyGetAngle ( grid - > body ) ;
}
}
float grid_angular_velocity( Entity * grid )
float entity_angular_velocity( Entity * grid )
{
{
return cpBodyGetAngularVelocity ( grid - > body ) ;
return ( float ) cpBodyGetAngularVelocity ( grid - > body ) ;
}
}
Entity * box_grid ( Entity * box )
Entity * box_grid ( Entity * box )
{
{
@ -535,7 +543,7 @@ V2 entity_shape_pos(Entity *box)
float entity_shape_mass ( Entity * box )
float entity_shape_mass ( Entity * box )
{
{
assert ( box - > shape ! = NULL ) ;
assert ( box - > shape ! = NULL ) ;
return cpShapeGetMass ( box - > shape ) ;
return ( float ) cpShapeGetMass ( box - > shape ) ;
}
}
V2 box_pos ( Entity * box )
V2 box_pos ( Entity * box )
{
{
@ -544,7 +552,7 @@ V2 box_pos(Entity *box)
}
}
float box_rotation ( Entity * box )
float box_rotation ( Entity * box )
{
{
return cpBodyGetAngle ( cpShapeGetBody ( box - > shape ) ) ;
return ( float ) cpBodyGetAngle ( cpShapeGetBody ( box - > shape ) ) ;
}
}
struct BodyData
struct BodyData
@ -571,62 +579,64 @@ void update_from(cpBody *body, struct BodyData *data)
cpBodySetAngularVelocity ( body , data - > angular_velocity ) ;
cpBodySetAngularVelocity ( body , data - > angular_velocity ) ;
}
}
# define WRITE_VARNAMES true // debugging feature horrible for network
# define WRITE_VARNAMES // debugging feature horrible for network
typedef struct SerState
typedef struct SerState
{
{
char * bytes ;
char * bytes ;
bool serializing ;
bool serializing ;
in t cursor ; // points to next available byte, is the size of current message after serializing something
size_ t cursor ; // points to next available byte, is the size of current message after serializing something
in t max_size ;
size_ t max_size ;
} SerState ;
} SerState ;
# define SER_VAR_NAME(var_pointer, name) \
void ser_var ( SerState * ser , char * var_pointer , size_t var_size , const char * name )
{ \
{
const char * var_name = name ; \
const char * var_name = name ;
size_t var_name_len = 0 ; \
# ifdef WRITE_VARNAMES
if ( WRITE_VARNAMES ) \
size_t var_name_len = strlen ( var_name ) ;
{ \
# endif
var_name_len = strlen ( var_name ) ; \
if ( ser - > serializing )
} \
{
if ( ser - > serializing ) \
{ \
# ifdef WRITE_VARNAMES
if ( WRITE_VARNAMES ) \
memcpy ( ser - > bytes + ser - > cursor , var_name , var_name_len ) ;
{ \
ser - > cursor + = var_name_len ;
memcpy ( ser - > bytes + ser - > cursor , var_name , var_name_len ) ; \
# endif
ser - > cursor + = var_name_len ; \
for ( int b = 0 ; b < var_size ; b + + )
} \
{
for ( int b = 0 ; b < sizeof ( * var_pointer ) ; b + + ) \
ser - > bytes [ ser - > cursor ] = var_pointer [ b ] ;
{ \
ser - > cursor + = 1 ;
ser - > bytes [ ser - > cursor ] = ( ( char * ) var_pointer ) [ b ] ; \
assert ( ser - > cursor < ser - > max_size ) ;
ser - > cursor + = 1 ; \
}
assert ( ser - > cursor < ser - > max_size ) ; \
}
} \
else
} \
{
else \
# ifdef WRITE_VARNAMES
{ \
{
if ( WRITE_VARNAMES ) \
char read_name [ 1024 ] = { 0 } ;
{ \
char * read_name = malloc ( sizeof * read_name * ( var_name_len + 1 ) ) ; \
for ( int i = 0 ; i < var_name_len ; i + + )
for ( int i = 0 ; i < var_name_len ; i + + ) \
{
{ \
read_name [ i ] = ser - > bytes [ ser - > cursor ] ;
read_name [ i ] = ser - > bytes [ ser - > cursor ] ; \
ser - > cursor + = 1 ;
ser - > cursor + = 1 ; \
assert ( ser - > cursor < ser - > max_size ) ;
assert ( ser - > cursor < ser - > max_size ) ; \
}
} \
read_name [ var_name_len ] = ' \0 ' ;
read_name [ var_name_len ] = ' \0 ' ; \
if ( strcmp ( read_name , var_name ) ! = 0 )
if ( strcmp ( read_name , var_name ) ! = 0 ) \
{
{ \
printf ( " %s:%d | Expected variable %s but got %sn \n " , __FILE__ , __LINE__ , var_name , read_name ) ;
printf ( " %s:%d | Expected variable %s but got %s \n " , __FILE__ , __LINE__ , var_name , read_name ) ; \
* ( char * ) NULL = 0 ;
} \
}
free ( read_name ) ; \
}
} \
# endif
for ( int b = 0 ; b < sizeof ( * var_pointer ) ; b + + ) \
for ( int b = 0 ; b < var_size ; b + + )
{ \
{
( ( char * ) var_pointer ) [ b ] = ser - > bytes [ ser - > cursor ] ; \
var_pointer [ b ] = ser - > bytes [ ser - > cursor ] ;
ser - > cursor + = 1 ; \
ser - > cursor + = 1 ;
assert ( ser - > cursor < ser - > max_size ) ; \
assert ( ser - > cursor < ser - > max_size ) ;
} \
}
} \
}
}
}
# define SER_VAR_NAME(var_pointer, name) ser_var(ser, (char*)var_pointer, sizeof(var_pointer), name)
# define SER_VAR(var_pointer) SER_VAR_NAME(var_pointer, #var_pointer)
# define SER_VAR(var_pointer) SER_VAR_NAME(var_pointer, #var_pointer)
void ser_V2 ( SerState * ser , V2 * var )
void ser_V2 ( SerState * ser , V2 * var )
@ -708,7 +718,7 @@ void ser_entity(SerState *ser, struct GameState *gs, Entity *e)
float shape_mass ;
float shape_mass ;
if ( ser - > serializing )
if ( ser - > serializing )
shape_mass = entity_shape_mass ( e ) ;
shape_mass = entity_shape_mass ( e ) ;
SER_VAR ( & shape_mass )
SER_VAR ( & shape_mass ) ;
Entity * parent = get_entity ( gs , e - > shape_parent_entity ) ;
Entity * parent = get_entity ( gs , e - > shape_parent_entity ) ;
if ( parent = = NULL )
if ( parent = = NULL )
@ -831,7 +841,7 @@ void ser_server_to_client(SerState *ser, ServerToClient *s)
}
}
}
}
void into_bytes ( struct ServerToClient * msg , char * bytes , int * out_len , in t max_len )
void into_bytes ( struct ServerToClient * msg , char * bytes , size_t * out_len , size_ t max_len )
{
{
assert ( msg - > cur_gs ! = NULL ) ;
assert ( msg - > cur_gs ! = NULL ) ;
assert ( msg ! = NULL ) ;
assert ( msg ! = NULL ) ;
@ -847,7 +857,7 @@ void into_bytes(struct ServerToClient *msg, char *bytes, int *out_len, int max_l
* out_len = ser . cursor + 1 ; // @Robust not sure why I need to add one to cursor, ser.cursor should be the length..
* out_len = ser . cursor + 1 ; // @Robust not sure why I need to add one to cursor, ser.cursor should be the length..
}
}
void from_bytes ( struct ServerToClient * msg , char * bytes , in t max_len )
void from_bytes ( struct ServerToClient * msg , char * bytes , size_ t max_len )
{
{
assert ( msg - > cur_gs ! = NULL ) ;
assert ( msg - > cur_gs ! = NULL ) ;
assert ( msg ! = NULL ) ;
assert ( msg ! = NULL ) ;