Skip to content

Commit

Permalink
new iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed May 27, 2020
1 parent 2b531e8 commit a4655ca
Show file tree
Hide file tree
Showing 22 changed files with 20,937 additions and 234 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ charset = utf-8
# 4 space indentation
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.h linguist-language=C
vendor/* linguist-vendored
code/vendor/* linguist-vendored
35 changes: 18 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# Folders
build/*
node_modules/
build-win/*
build-lin/*
pkg/

# vs shit
*.dir/
x64/

# Other
.DS_store

# local build scripts
build.sh
build.bat
# Folders
build/*
misc/deploy/
node_modules/
build-win/*
build-lin/*
pkg/

# vs shit
*.dir/
x64/

# Other
.DS_store

# local build scripts
build.sh
build.bat
60 changes: 60 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
CC=gcc
CXX=g++
STDC=-std=gnu11

ifeq ($(OS),Windows_NT)
CFLAGS += -DWIN32
else
OSDEF := $(shell uname -s)
ifeq ($(OSDEF),Linux)
LDFLAGS += -pthread -ldl -lm
endif
ifeq ($(OSDEF),OpenBSD)
STDC=-std=c11
CC=clang
CXX=clang++
LDFLAGS += -pthread -lm
endif
ifeq ($(OSDEF),FreeBSD)
STDC=-std=c11
CC=clang
CXX=clang++
LDFLAGS+=-pthread -lm
endif
endif

WARNS = -Wall -Wextra -Werror -Wno-missing-field-initializers -Wno-unused-value -Wno-unused-function -Wno-missing-braces
CFLAGS += -g $(STDC) -Icode $(WARNS)
CXXFLAGS += -g -std=c++11 -Icode $(WARNS)

APPS += $(patsubst %.c,%,$(wildcard code/apps/*.c))
APPS += $(patsubst %.cc,%,$(wildcard code/apps/*.cc))

BUILD_FILES = $(wildcard build/*)

.PHONY: all clean apps test

all: clean apps test

test: clean code/tests/unit
@echo '> Building unit tests'
build/unit

apps: $(APPS)
@echo '> Building apps'

clean:
ifneq ($(BUILD_FILES),)
@echo '> Cleaning up files'
@rm -r $(BUILD_FILES)
endif

% : %.c
@mkdir -p build
@echo '=> Building $(@F)'
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o build/$(@F)

% : %.cc
@mkdir -p build
@echo '=> Building $(@F)'
$(CXX) $(CXXFLAGS) $^ $(LDFLAGS) -o build/$(@F)
138 changes: 138 additions & 0 deletions code/apps/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include <assert.h>

#define LIBRG_IMPL
#include "librg.h"


/* usage */
#if 0
int main() {

librg_config_chunksize_set(ctx, 16, 16, 16);
librg_config_worldsize_set(ctx, 256, 256, 256);

/* called by a librg_world_write, to encode the data for a child */
librg_event_set(ctx, LIBRG_PARENT_CREATE, [](librg_event *e) {
/* returning > 0 - amount of data we've written */
/* return 0 - no data written */
/* return < 0 - return an error, will be pased to the parent world_write call */
return 0;
});

/* called by librg_world_read, to decode the data from a parent */
librg_event_set(ctx, LIBRG_CHILD_CREATE, [](librg_event *e) {
/* return 0 - ok */
/* return < 0 - return an error, will be pased to the parent world_read call */
return 0;
});

/* create a few simple entities */
librg_entity_set(ctx, 13);
librg_entity_type_set(ctx, 13, LIBRG_STATIC); /*static entity - no update events will ever be generated for it*/

/* additionaly, set a refresh rate */
librg_entity_set(ctx, 14);
librg_entity_type_set(ctx, 15, LIBRG_DYNAMIC); /* dynamic entity - entity always will be kept updated (default value) */
librg_entity_refresh_set(ctx, 14, LIBRG_LINEAR, 20.0f); /*amount of updates shown to a specific observer*/
/*will divided by 2, every 20.0f units depending how far they are*/
/* add an observer entity */
librg_entity_set(ctx, 15);
librg_entity_owner_set(ctx, 15, 100, 1); /* set owner and, mark it as observable */
librg_entity_chunkrange_set(ctx, 15, 5); /* chunk range sets a viewable range by our observer */
/* owner id can be anything, a ID from a newtork connection, a random number, or a ptr to a instance of a class */

/* set its position */
librg_entity_chunk_set(ctx, 15, librg_chunk_from_realpos(ctx, 15.0f, 25.0f, 353.0f));

/* write AoI POV to buffer */
char buffer[1024] = {0};
int res = librg_world_write(ctx, 100, buffer, 1024);
/* res can be one of following: */
/* > 0: success, and amount of actual bytes written */
/* == 0: success, empty result, no data written, empty view */
/* < 0: error with code: */
/* LIBRG_ERROR_NO_OBSERVERS - owner does not have any observer entities */
/* LIBRG_ERROR_BUFFER_TOO_SMALL - unable to continue writing since buffer is too small */

{
/* create our 2nd instance */
librg_ctx *ctx2 = librg_context_create();

librg_config_chunksize_set(ctx2, 16, 16, 16);
librg_config_worldsize_set(ctx2, 256, 256, 256);

/* rerad the world, and re-create our limited PoV local view of the world */
int code = librg_world_read(ctx2, LIBRG_UNDEFINED_OWNER, buffer, res);

/* modify some entity 15 internal data */

memset(buffer, 0, sizeof(char) * 1024);
res = librg_world_write(ctx2, LIBRG_UNDEFINED_OWNER, buffer, 1024);
}


/* back to main instance, read the incoming data */
int code = librg_data_read(ctx, 100, buffer, res);

return 0;
}
#endif
/* impl part*/

size_t _parent_create(librg_ctx *ctx, librg_event *event) {
printf("_parent_create %p %d\n", ctx, event->type);
return 0;
}

size_t _child_create(librg_ctx *ctx, librg_event *event) {
printf("_child_create %p %d\n", ctx, event->type);
return 0;
}

int main() {
librg_ctx *ctx = librg_context_create();
assert(librg_context_valid(ctx));

librg_config_worldsize_set(ctx, 9, 9, 9);
librg_config_chunksize_set(ctx, 16, 16, 16);
librg_config_chunkoffset_set(ctx, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID);

librg_event_set(ctx, LIBRG_PARENT_CREATE, _parent_create);
librg_event_set(ctx, LIBRG_CHILD_CREATE, _child_create);

const int myId = 24;

librg_entity_track(ctx, myId);
librg_entity_owner_set(ctx, myId, 423, 1);

for (int i=0;i<100;i++) {
librg_entity_track(ctx, i);
librg_entity_chunk_set(ctx, i, i);
}

assert(librg_entity_tracked(ctx, myId) == LIBRG_TRUE);

librg_entity_userdata_set(ctx, myId, (void *)124);
assert(librg_entity_userdata_get(ctx, myId) == (void *)124);

librg_entity_type_set(ctx, myId, LIBRG_ENTITY_STATIC);
assert(librg_entity_type_get(ctx, myId) == LIBRG_ENTITY_STATIC);

// librg_entity_chunk_set(ctx, myId, librg_chunk_from_realpos(ctx, 0.f, 0.f, 0.f));
librg_entity_chunk_set(ctx, myId, 3);
printf("entity chunk: %lld\n", librg_entity_chunk_get(ctx, myId));

int64_t results[255] = {0};
int amount = librg_world_query(ctx, 423, results, 255);
printf("query found: %d results\n", amount);
for (int i=0; i<amount; i++) printf("result #%d: %lld\n", i, results[i]);

librg_entity_dimension_set(ctx, myId, 1);
assert(librg_entity_dimension_get(ctx, myId) == 1);

librg_entity_untrack(ctx, myId);
assert(librg_entity_tracked(ctx, myId) == LIBRG_FALSE);

librg_context_destroy(ctx);
return 0;
}
43 changes: 43 additions & 0 deletions code/header/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// file: header/context.h

#ifdef LIBRG_EDITOR
#include <librg.h>
#endif

LIBRG_BEGIN_C_DECLS

// =======================================================================//
// !
// ! Context methods
// !
// =======================================================================//

LIBRG_API librg_ctx * librg_context_create();
LIBRG_API int8_t librg_context_destroy(librg_ctx *);
LIBRG_API int8_t librg_context_valid(librg_ctx *);
LIBRG_API int8_t librg_context_userdata_set(librg_ctx *, void *data);
LIBRG_API void * librg_context_userdata_get(librg_ctx *);

// =======================================================================//
// !
// ! Configuration methods
// !
// =======================================================================//

LIBRG_API int8_t librg_config_worldsize_set(librg_ctx *, uint16_t x, uint16_t y, uint16_t z);
LIBRG_API int8_t librg_config_worldsize_get(librg_ctx *, uint16_t *x, uint16_t *y, uint16_t *z);
LIBRG_API int8_t librg_config_chunksize_set(librg_ctx *, uint16_t x, uint16_t y, uint16_t z);
LIBRG_API int8_t librg_config_chunksize_get(librg_ctx *, uint16_t *x, uint16_t *y, uint16_t *z);
LIBRG_API int8_t librg_config_chunkoffset_set(librg_ctx *, int16_t x, int16_t y, int16_t z);
LIBRG_API int8_t librg_config_chunkoffset_get(librg_ctx *, int16_t *x, int16_t *y, int16_t *z);

// =======================================================================//
// !
// ! Utility methods
// !
// =======================================================================//

LIBRG_ALWAYS_INLINE LIBRG_API librg_chunk librg_chunk_from_realpos(librg_ctx *, double x, double y, double z);
LIBRG_ALWAYS_INLINE LIBRG_API librg_chunk librg_chunk_from_chunkpos(librg_ctx *, int16_t chunk_x, int16_t chunk_y, int16_t chunk_z);

LIBRG_END_C_DECLS
58 changes: 58 additions & 0 deletions code/header/entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// file: header/entity.h

#ifdef LIBRG_EDITOR
#include <librg.h>
#endif

LIBRG_BEGIN_C_DECLS

typedef enum librg_entity_type {
LIBRG_ENTITY_STATIC,
LIBRG_ENTITY_DYNAMIC,
} librg_entity_type;

typedef enum librg_entity_refreshing {
LIBRG_TICK, /* int argument, update entity every Nth tick */
LIBRG_LINEAR, /* float argument, linearly decrease update interval based on distance step (argument) */
LIBRG_CUBIC, /* float argument, cubicly decrease update inverval based on distance step (argument) */
} librg_entity_refreshing;

// =======================================================================//
// !
// ! Basic entity manipulation
// !
// =======================================================================//

LIBRG_API int8_t librg_entity_track(librg_ctx *, int64_t entity_id);
LIBRG_API int8_t librg_entity_untrack(librg_ctx *, int64_t entity_id);
LIBRG_API int8_t librg_entity_tracked(librg_ctx *, int64_t entity_id);
LIBRG_API int8_t librg_entity_userdata_set(librg_ctx *, int64_t entity_id, void *data);
LIBRG_API void * librg_entity_userdata_get(librg_ctx *, int64_t entity_id);

// =======================================================================//
// !
// ! Simple entity data methods
// !
// =======================================================================//

LIBRG_API int8_t librg_entity_type_set(librg_ctx *, int64_t entity_id, uint8_t type);
LIBRG_API int16_t librg_entity_type_get(librg_ctx *, int64_t entity_id);
LIBRG_API int8_t librg_entity_chunk_set(librg_ctx *, int64_t entity_id, librg_chunk);
LIBRG_API librg_chunk librg_entity_chunk_get(librg_ctx *, int64_t entity_id);
LIBRG_API int8_t librg_entity_owner_set(librg_ctx *, int64_t entity_id, int64_t owner_id, int8_t observed_chunk_radius);
LIBRG_API int64_t librg_entity_owner_get(librg_ctx *, int64_t entity_id);
LIBRG_API int8_t librg_entity_dimension_set(librg_ctx *, int64_t entity_id, int32_t dimension);
LIBRG_API int32_t librg_entity_dimension_get(librg_ctx *, int64_t entity_id);

// =======================================================================//
// !
// ! Advanced entity data methods
// !
// =======================================================================//

LIBRG_API int8_t librg_entity_chunkarray_set(librg_ctx *, int64_t entity_id, librg_chunk *chunks, size_t chunk_amount);
LIBRG_API size_t librg_entity_chunkarray_get(librg_ctx *, int64_t entity_id, LIBRG_OUT librg_chunk *chunks, size_t buffer_limit);
LIBRG_API int8_t librg_entity_refresh_set(librg_ctx *, int64_t entity_id, int type, float value); // ??
LIBRG_API int8_t librg_entity_refresh_get(librg_ctx *, int64_t entity_id, int *type, float *value);

LIBRG_END_C_DECLS
Loading

0 comments on commit a4655ca

Please sign in to comment.