polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

resource.h (1762B)


      1 
      2 #ifndef RESOURCE_H
      3 #define RESOURCE_H
      4 
      5 #include "common.h"
      6 
      7 #define STATIC_UUID 0x5A51C00000000000
      8 
      9 enum refresh_status {
     10     RESOURCE_DELETED,
     11     REFRESH_NOT_NEEDED,
     12     REFRESHED_ID
     13 };
     14 
     15 struct resource_id {
     16     u64 uuid;
     17     u32 index;
     18     u32 generation;
     19 };
     20 
     21 struct resource_manager {
     22     u8 *resources;
     23     struct resource_id *ids;
     24     u32 static_elems;
     25     u32 resource_count;
     26     u32 generation;
     27     u32 elem_size;
     28     u32 slots_used;
     29     u32 max_capacity;
     30     u32 current_capacity;
     31 
     32     const char *name;
     33 };
     34 
     35 #define ideq(a, b) ((a)->uuid == (b)->uuid)
     36 
     37 void init_id(struct resource_id *id);
     38 void *get_resource(struct resource_manager *r, struct resource_id *id);
     39 void *get_all_resources(struct resource_manager *, u32 *count, struct resource_id **ids);
     40 void destroy_resource(struct resource_manager *, struct resource_id *id);
     41 void destroy_resource_manager(struct resource_manager *);
     42 void *new_resource(struct resource_manager *, struct resource_id *id);
     43 void *new_static_resource(struct resource_manager *, struct resource_id *id);
     44 void print_id(struct resource_id *, int nl);
     45 void null_id(struct resource_id *id);
     46 
     47 void init_resource_manager(struct resource_manager *r, u32 elem_size,
     48                            u32 initial_elements, u32 max_elements, const char *name,
     49                            int static_resources
     50                            );
     51 
     52 
     53 static inline struct resource_id make_static_id(u32 index)
     54 {
     55     return (struct resource_id){
     56         .uuid = STATIC_UUID,
     57         .index = index,
     58         .generation = -1
     59     };
     60 }
     61 
     62 static inline int is_static_resource(struct resource_id *id)
     63 {
     64     return id->uuid == STATIC_UUID;
     65 }
     66 
     67 static inline int is_null_id(struct resource_id *id)
     68 {
     69     return id->generation == 0;
     70 }
     71 
     72 
     73 #endif /* RESOURCE_H */