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)
{
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)
@ -64,7 +64,7 @@ EntityID get_id(struct GameState *gs, Entity *e)
if (e == NULL)
return (EntityID) { 0 };
int index = e - gs->entities;
size_t index = e - gs->entities;
assert(index >= 0);
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);
const V2 dirs[] = {
(V2){.x = -1.0f, .y = 0.0f},
(V2){.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 = 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);
@ -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))));
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)
{
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)
{
@ -535,7 +543,7 @@ V2 entity_shape_pos(Entity *box)
float entity_shape_mass(Entity* box)
{
assert(box->shape != NULL);
return cpShapeGetMass(box->shape);
return (float)cpShapeGetMass(box->shape);
}
V2 box_pos(Entity* box)
{
@ -544,7 +552,7 @@ V2 box_pos(Entity *box)
}
float box_rotation(Entity* box)
{
return cpBodyGetAngle(cpShapeGetBody(box->shape));
return (float)cpBodyGetAngle(cpShapeGetBody(box->shape));
}
struct BodyData
@ -571,62 +579,64 @@ void update_from(cpBody *body, struct BodyData *data)
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
{
char* bytes;
bool serializing;
int cursor; // points to next available byte, is the size of current message after serializing something
int max_size;
size_t cursor; // points to next available byte, is the size of current message after serializing something
size_t max_size;
} SerState;
#define SER_VAR_NAME(var_pointer, name) \
{ \
const char *var_name = name; \
size_t var_name_len = 0; \
if (WRITE_VARNAMES) \
{ \
var_name_len = strlen(var_name); \
} \
if (ser->serializing) \
{ \
if (WRITE_VARNAMES) \
{ \
memcpy(ser->bytes + ser->cursor, var_name, var_name_len); \
ser->cursor += var_name_len; \
} \
for (int b = 0; b < sizeof(*var_pointer); b++) \
{ \
ser->bytes[ser->cursor] = ((char *)var_pointer)[b]; \
ser->cursor += 1; \
assert(ser->cursor < ser->max_size); \
} \
} \
else \
{ \
if (WRITE_VARNAMES) \
{ \
char *read_name = malloc(sizeof *read_name * (var_name_len + 1)); \
for (int i = 0; i < var_name_len; i++) \
{ \
read_name[i] = ser->bytes[ser->cursor]; \
ser->cursor += 1; \
assert(ser->cursor < ser->max_size); \
} \
read_name[var_name_len] = '\0'; \
if (strcmp(read_name, var_name) != 0) \
{ \
printf("%s:%d | Expected variable %s but got %s\n", __FILE__, __LINE__, var_name, read_name); \
} \
free(read_name); \
} \
for (int b = 0; b < sizeof(*var_pointer); b++) \
{ \
((char *)var_pointer)[b] = ser->bytes[ser->cursor]; \
ser->cursor += 1; \
assert(ser->cursor < ser->max_size); \
} \
} \
void ser_var(SerState* ser, char* var_pointer, size_t var_size, const char* name)
{
const char* var_name = name;
#ifdef WRITE_VARNAMES
size_t var_name_len = strlen(var_name);
#endif
if (ser->serializing)
{
#ifdef WRITE_VARNAMES
memcpy(ser->bytes + ser->cursor, var_name, var_name_len);
ser->cursor += var_name_len;
#endif
for (int b = 0; b < var_size; b++)
{
ser->bytes[ser->cursor] = var_pointer[b];
ser->cursor += 1;
assert(ser->cursor < ser->max_size);
}
}
else
{
#ifdef WRITE_VARNAMES
{
char read_name[1024] = { 0 };
for (int i = 0; i < var_name_len; i++)
{
read_name[i] = ser->bytes[ser->cursor];
ser->cursor += 1;
assert(ser->cursor < ser->max_size);
}
read_name[var_name_len] = '\0';
if (strcmp(read_name, var_name) != 0)
{
printf("%s:%d | Expected variable %s but got %sn\n", __FILE__, __LINE__, var_name, read_name);
*(char*)NULL = 0;
}
}
#endif
for (int b = 0; b < var_size; b++)
{
var_pointer[b] = ser->bytes[ser->cursor];
ser->cursor += 1;
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)
void ser_V2(SerState* ser, V2* var)
@ -708,7 +718,7 @@ void ser_entity(SerState *ser, struct GameState *gs, Entity *e)
float shape_mass;
if (ser->serializing)
shape_mass = entity_shape_mass(e);
SER_VAR(&shape_mass)
SER_VAR(&shape_mass);
Entity* parent = get_entity(gs, e->shape_parent_entity);
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 != 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..
}
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 != NULL);

@ -353,7 +353,7 @@ static void frame(void)
// 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."
// ^^ 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,
};
// @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
// in the shapes and bodies of chipmunk. Would require editing the library I think
Entity *entities;
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 max_entities; // maximum number of entities possible in the entities list
unsigned int cur_next_entity; // next entity to pass on request of a new entity if the free list is empty
EntityID free_list;
} GameState;
@ -212,8 +212,8 @@ void destroy(struct GameState *gs);
void process(struct GameState *gs, float dt); // does in place
Entity *closest_to_point_in_radius(struct GameState *gs, V2 point, float radius);
uint64_t tick(struct GameState *gs);
void into_bytes(struct ServerToClient *gs, char *out_bytes, int *out_len, int max_len);
void from_bytes(struct ServerToClient *gs, char *bytes, 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, size_t max_len);
// entities
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_world_to_local(Entity *grid, V2 world);
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 box_pos(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)
{
return (V2){
.x = vec.x * cos(theta) - vec.y * sin(theta),
.y = vec.x * sin(theta) + vec.y * cos(theta),
.x = vec.x * cosf(theta) - vec.y * sinf(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)
{
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)
@ -373,8 +373,8 @@ static V2 V2lerp(V2 a, V2 b, float factor)
// for random generation
static float hash11(float p)
{
p = fract(p * .1031);
p *= p + 33.33;
p = fract(p * .1031f);
p *= p + 33.33f;
p *= p + p;
return fract(p);
}
@ -387,9 +387,9 @@ typedef struct Color
static Color colhex(int r, int g, int b)
{
return (Color){
.r = (float)r / 255.0,
.g = (float)g / 255.0,
.b = (float)b / 255.0,
.r = (float)r / 255.0f,
.g = (float)g / 255.0f,
.b = (float)b / 255.0f,
.a = 1.0f,
};
}

Loading…
Cancel
Save