]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_terrain.h
Fix client terrain crashes and performance
[dragonblocks_alpha.git] / src / client / client_terrain.h
1 #ifndef _CLIENT_TERRAIN_H_
2 #define _CLIENT_TERRAIN_H_
3
4 #include <stdatomic.h>
5 #include <stdbool.h>
6 #include "client/model.h"
7 #include "terrain.h"
8 #include "types.h"
9
10 #define CHUNK_MODE_NOCREATE 2
11
12 typedef enum {
13         CHUNK_STATE_INIT,
14         CHUNK_STATE_RECV,
15         CHUNK_STATE_DEPS,
16         CHUNK_STATE_DIRTY,
17         CHUNK_STATE_CLEAN,
18 } TerrainChunkState;
19
20 typedef struct {
21         bool queue;
22         TerrainChunkState state;
23         pthread_rwlock_t lock_state;
24
25         // accessed only by recv thread
26         TerrainChunk *neighbors[6];
27         unsigned int num_neighbors;
28
29         // write is protected by mtx_model, read is atomic
30         atomic_bool depends[6];
31
32         bool has_model;
33         Model *model;
34         pthread_mutex_t mtx_model;
35
36         // protected by chunk data lock
37         bool empty;
38
39         // accessed only by sync thread
40         u64 sync;
41 } TerrainChunkMeta;
42
43 extern Terrain *client_terrain;
44
45 void client_terrain_init();                                          // called on startup
46 void client_terrain_deinit();                                        // called on shutdown
47 void client_terrain_set_load_distance(u32 dist);                     // update load distance
48 u32 client_terrain_get_load_distance();                              // return load distance
49 void client_terrain_start();                                         // start meshgen and sync threads
50 void client_terrain_stop();                                          // stop meshgen and sync threads
51 void client_terrain_meshgen_task(TerrainChunk *chunk, bool changed); // enqueue chunk to mesh update queue
52 void client_terrain_receive_chunk(void *peer, ToClientChunk *pkt);   // callback to deserialize chunk from network
53
54 #endif