Add ticks to gamestate

main
Cameron Murphy Reikes 2 years ago
parent 5c94921eb5
commit 160107f92a

@ -50,6 +50,7 @@
"type_traits": "c", "type_traits": "c",
"xmemory": "c", "xmemory": "c",
"xtr1common": "c", "xtr1common": "c",
"xtree": "c" "xtree": "c",
"stdint.h": "c"
} }
} }

@ -248,6 +248,16 @@ void des_float(char **in, float *f)
memread(in, f); memread(in, f);
} }
void ser_double(char **out, double d)
{
memwrite(out, d);
}
void des_double(char **in, double *d)
{
memread(in, d);
}
void ser_int(char **out, int i) void ser_int(char **out, int i)
{ {
memwrite(out, i); memwrite(out, i);
@ -393,10 +403,11 @@ void into_bytes(struct ServerToClient *msg, char *bytes, int *out_len, int max_l
ser_int(&bytes, msg->your_player); ser_int(&bytes, msg->your_player);
LEN_CHECK(); LEN_CHECK();
ser_float(&bytes, gs->time); ser_double(&bytes, gs->time);
LEN_CHECK(); LEN_CHECK();
ser_V2(&bytes, gs->goldpos); ser_V2(&bytes, gs->goldpos);
LEN_CHECK();
for (int i = 0; i < MAX_PLAYERS; i++) for (int i = 0; i < MAX_PLAYERS; i++)
{ {
@ -433,7 +444,7 @@ void from_bytes(struct ServerToClient *msg, char *bytes, int max_len)
des_int(&bytes, &msg->your_player); des_int(&bytes, &msg->your_player);
LEN_CHECK(); LEN_CHECK();
des_float(&bytes, &gs->time); des_double(&bytes, &gs->time);
LEN_CHECK(); LEN_CHECK();
des_V2(&bytes, &gs->goldpos); des_V2(&bytes, &gs->goldpos);
@ -494,6 +505,7 @@ struct Grid *closest_to_point_in_radius(struct GameState *gs, V2 point, float ra
return NULL; return NULL;
} }
// for random generation
static float hash11(float p) static float hash11(float p)
{ {
p = fract(p * .1031); p = fract(p * .1031);
@ -502,6 +514,11 @@ static float hash11(float p)
return fract(p); return fract(p);
} }
uint64_t tick(struct GameState *gs)
{
return (uint64_t)floor(gs->time / ((double)TIMESTEP));
}
void process(struct GameState *gs, float dt) void process(struct GameState *gs, float dt)
{ {
assert(gs->space != NULL); assert(gs->space != NULL);

@ -14,6 +14,8 @@
#include "types.h" #include "types.h"
#include <inttypes.h>
static struct GameState gs = {0}; static struct GameState gs = {0};
static int myplayer = -1; static int myplayer = -1;
static bool right_mouse_down = false; static bool right_mouse_down = false;
@ -318,6 +320,8 @@ static void frame(void)
// process(&gs, (float)sapp_frame_duration()); // process(&gs, (float)sapp_frame_duration());
} }
Log("Tick: %" PRIu64 "\n", tick(&gs));
// drawing // drawing
{ {
sgp_begin(width, height); sgp_begin(width, height);

@ -4,7 +4,6 @@
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> // int64 printing #include <inttypes.h> // int64 printing
#define TIMESTEP (1.0f / 60.0f)
// started in a thread from host // started in a thread from host
void server(void *data) void server(void *data)
@ -173,6 +172,7 @@ void server(void *data)
if (processed) if (processed)
{ {
Log("Tick: %"PRIu64"\n", tick(&gs));
#define MAX_BYTES_SIZE 2048 * 2 #define MAX_BYTES_SIZE 2048 * 2
static char bytes_buffer[MAX_BYTES_SIZE] = {0}; static char bytes_buffer[MAX_BYTES_SIZE] = {0};
for (int i = 0; i < server->peerCount; i++) for (int i = 0; i < server->peerCount; i++)

@ -7,9 +7,12 @@
#define MAX_GRIDS 32 #define MAX_GRIDS 32
#define MAX_BOXES_PER_GRID 32 #define MAX_BOXES_PER_GRID 32
#define BOX_MASS 1.0f #define BOX_MASS 1.0f
#define TIMESTEP (1.0f / 60.0f) // not required to simulate at this, but this defines what tick the game is on
// @Robust remove this include somehow, needed for sqrt and cos // @Robust remove this include somehow, needed for sqrt and cos
#include <math.h> #include <math.h>
#include <stdint.h> // tick is unsigned integer
// including headers from headers bad // including headers from headers bad
#ifndef SOKOL_GP_INCLUDED #ifndef SOKOL_GP_INCLUDED
@ -55,7 +58,7 @@ struct GameState
{ {
cpSpace *space; cpSpace *space;
float time; double time;
V2 goldpos; V2 goldpos;
@ -119,6 +122,7 @@ void initialize(struct GameState *gs); // must do this to place boxes into it an
void destroy(struct GameState *gs); 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
struct Grid *closest_to_point_in_radius(struct GameState *gs, V2 point, float radius); struct Grid *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 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 from_bytes(struct ServerToClient *gs, char *bytes, int max_len);

Loading…
Cancel
Save