Fix some warnings

main
Cameron Murphy Reikes 2 years ago
parent db273d9b2d
commit e6e3e68a52

@ -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 };
int 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;
int 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
int 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, int 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, int 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);

@ -353,7 +353,7 @@ static void frame(void)
// and other validation instead of just casting to a struct // and other validation instead of just casting to a struct
// "Alignment of structure members can be different even among different compilers on the same platform, let alone different platforms." // "Alignment of structure members can be different even among different compilers on the same platform, let alone different platforms."
// ^^ need serialization strategy that accounts for this if multiple platforms is happening https://stackoverflow.com/questions/28455163/how-can-i-portably-send-a-c-struct-through-a-network-socket // ^^ need serialization strategy that accounts for this if multiple platforms is happening https://stackoverflow.com/questions/28455163/how-can-i-portably-send-a-c-struct-through-a-network-socket
struct ServerToClient msg = { ServerToClient msg = (ServerToClient){
.cur_gs = &gs, .cur_gs = &gs,
}; };
// @Robust @BeforeShip maximum acceptable message size? // @Robust @BeforeShip maximum acceptable message size?

@ -161,8 +161,8 @@ typedef struct GameState
// if you really need this, potentially refactor to store entity IDs instead of pointers // if you really need this, potentially refactor to store entity IDs instead of pointers
// in the shapes and bodies of chipmunk. Would require editing the library I think // in the shapes and bodies of chipmunk. Would require editing the library I think
Entity *entities; Entity *entities;
int max_entities; // maximum number of entities possible in the entities list unsigned int max_entities; // maximum number of entities possible in the entities list
int cur_next_entity; // next entity to pass on request of a new entity if the free list is empty unsigned int cur_next_entity; // next entity to pass on request of a new entity if the free list is empty
EntityID free_list; EntityID free_list;
} GameState; } GameState;
@ -212,8 +212,8 @@ void destroy(struct GameState *gs);
void process(struct GameState *gs, float dt); // does in place void process(struct GameState *gs, float dt); // does in place
Entity *closest_to_point_in_radius(struct GameState *gs, V2 point, float radius); Entity *closest_to_point_in_radius(struct GameState *gs, V2 point, float radius);
uint64_t tick(struct GameState *gs); uint64_t tick(struct GameState *gs);
void into_bytes(struct ServerToClient *gs, char *out_bytes, int *out_len, int max_len); void into_bytes(struct ServerToClient *gs, char *out_bytes, size_t * out_len, size_t max_len);
void from_bytes(struct ServerToClient *gs, char *bytes, int max_len); void from_bytes(struct ServerToClient *gs, char *bytes, size_t max_len);
// entities // entities
Entity *get_entity(struct GameState *gs, EntityID id); Entity *get_entity(struct GameState *gs, EntityID id);
@ -237,7 +237,7 @@ V2 grid_vel(Entity *grid);
V2 grid_local_to_world(Entity *grid, V2 local); V2 grid_local_to_world(Entity *grid, V2 local);
V2 grid_world_to_local(Entity *grid, V2 world); V2 grid_world_to_local(Entity *grid, V2 world);
V2 grid_snapped_box_pos(Entity *grid, V2 world); // returns the snapped pos in world coords V2 grid_snapped_box_pos(Entity *grid, V2 world); // returns the snapped pos in world coords
float grid_angular_velocity(Entity *grid); float entity_angular_velocity(Entity *grid);
V2 entity_shape_pos(Entity *box); V2 entity_shape_pos(Entity *box);
V2 box_pos(Entity *box); V2 box_pos(Entity *box);
float box_rotation(Entity *box); float box_rotation(Entity *box);
@ -313,8 +313,8 @@ static V2 V2project(V2 vec, V2 onto)
static V2 V2rotate(V2 vec, float theta) static V2 V2rotate(V2 vec, float theta)
{ {
return (V2){ return (V2){
.x = vec.x * cos(theta) - vec.y * sin(theta), .x = vec.x * cosf(theta) - vec.y * sinf(theta),
.y = vec.x * sin(theta) + vec.y * cos(theta), .y = vec.x * sinf(theta) + vec.y * cosf(theta),
}; };
} }
@ -339,7 +339,7 @@ static bool V2cmp(V2 a, V2 b, float eps)
static inline float clamp01(float f) static inline float clamp01(float f)
{ {
return fmax(0.0f, fmin(f, 1.0f)); return fmaxf(0.0f, fminf(f, 1.0f));
} }
static inline float clamp(float f, float minimum, float maximum) static inline float clamp(float f, float minimum, float maximum)
@ -373,8 +373,8 @@ static V2 V2lerp(V2 a, V2 b, float factor)
// for random generation // for random generation
static float hash11(float p) static float hash11(float p)
{ {
p = fract(p * .1031); p = fract(p * .1031f);
p *= p + 33.33; p *= p + 33.33f;
p *= p + p; p *= p + p;
return fract(p); return fract(p);
} }
@ -387,9 +387,9 @@ typedef struct Color
static Color colhex(int r, int g, int b) static Color colhex(int r, int g, int b)
{ {
return (Color){ return (Color){
.r = (float)r / 255.0, .r = (float)r / 255.0f,
.g = (float)g / 255.0, .g = (float)g / 255.0f,
.b = (float)b / 255.0, .b = (float)b / 255.0f,
.a = 1.0f, .a = 1.0f,
}; };
} }

Loading…
Cancel
Save