gdscript->c++ conversions.

This commit is contained in:
Relintai 2019-11-19 14:42:21 +01:00
parent 0594215929
commit d2566ab82b
6 changed files with 191 additions and 0 deletions

View File

@ -1,5 +1,14 @@
#include "voxelman_level_generator.h" #include "voxelman_level_generator.h"
void VoxelmanLevelGenerator::generate_chunk_bind(Node *chunk) {
generate_chunk(Object::cast_to<VoxelChunk>(chunk));
}
void VoxelmanLevelGenerator::generate_chunk(VoxelChunk *chunk) {
if (has_method("_generate_chunk")) {
call("_generate_chunk", chunk);
}
}
VoxelmanLevelGenerator::VoxelmanLevelGenerator() { VoxelmanLevelGenerator::VoxelmanLevelGenerator() {
} }
@ -7,4 +16,7 @@ VoxelmanLevelGenerator::~VoxelmanLevelGenerator() {
} }
void VoxelmanLevelGenerator::_bind_methods() { void VoxelmanLevelGenerator::_bind_methods() {
BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
ClassDB::bind_method(D_METHOD("generate_chunk", "chunk"), &VoxelmanLevelGenerator::generate_chunk_bind);
} }

View File

@ -3,10 +3,15 @@
#include "core/resource.h" #include "core/resource.h"
#include "../world/voxel_chunk.h"
class VoxelmanLevelGenerator : public Resource { class VoxelmanLevelGenerator : public Resource {
GDCLASS(VoxelmanLevelGenerator, Resource); GDCLASS(VoxelmanLevelGenerator, Resource);
public: public:
void generate_chunk_bind(Node *chunk);
void generate_chunk(VoxelChunk *chunk);
VoxelmanLevelGenerator(); VoxelmanLevelGenerator();
~VoxelmanLevelGenerator(); ~VoxelmanLevelGenerator();

View File

@ -2,6 +2,13 @@
#include "voxel_world.h" #include "voxel_world.h"
_FORCE_INLINE_ bool VoxelChunk::get_is_generating() const {
return _is_generating;
}
_FORCE_INLINE_ void VoxelChunk::set_is_generating(bool value) {
_is_generating = value;
}
_FORCE_INLINE_ bool VoxelChunk::get_dirty() const { _FORCE_INLINE_ bool VoxelChunk::get_dirty() const {
return _dirty; return _dirty;
} }
@ -457,6 +464,8 @@ void VoxelChunk::finalize_mesh() {
void VoxelChunk::build() { void VoxelChunk::build() {
if (_current_build_phase == BUILD_PHASE_DONE) { if (_current_build_phase == BUILD_PHASE_DONE) {
_is_generating = true;
next_phase(); next_phase();
} }
} }
@ -566,6 +575,8 @@ void VoxelChunk::next_phase() {
if (_current_build_phase >= BUILD_PHASE_MAX) { if (_current_build_phase >= BUILD_PHASE_MAX) {
_current_build_phase = BUILD_PHASE_DONE; _current_build_phase = BUILD_PHASE_DONE;
_is_generating = false;
emit_signal("mesh_generation_finished", this); emit_signal("mesh_generation_finished", this);
return; return;
@ -1046,6 +1057,7 @@ void VoxelChunk::free_chunk() {
} }
VoxelChunk::VoxelChunk() { VoxelChunk::VoxelChunk() {
_is_generating = false;
_dirty = false; _dirty = false;
_state = VOXEL_CHUNK_STATE_OK; _state = VOXEL_CHUNK_STATE_OK;
@ -1104,6 +1116,10 @@ void VoxelChunk::_bind_methods() {
ClassDB::bind_method(D_METHOD("_create_mesher"), &VoxelChunk::_create_mesher); ClassDB::bind_method(D_METHOD("_create_mesher"), &VoxelChunk::_create_mesher);
ClassDB::bind_method(D_METHOD("get_is_generating"), &VoxelChunk::get_is_generating);
ClassDB::bind_method(D_METHOD("set_is_generating", "value"), &VoxelChunk::set_is_generating);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_generating"), "set_is_generating", "get_is_generating");
ClassDB::bind_method(D_METHOD("get_dirty"), &VoxelChunk::get_dirty); ClassDB::bind_method(D_METHOD("get_dirty"), &VoxelChunk::get_dirty);
ClassDB::bind_method(D_METHOD("set_dirty", "value"), &VoxelChunk::set_dirty); ClassDB::bind_method(D_METHOD("set_dirty", "value"), &VoxelChunk::set_dirty);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dirty"), "set_dirty", "get_dirty"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dirty"), "set_dirty", "get_dirty");

View File

@ -76,6 +76,9 @@ public:
}; };
public: public:
bool get_is_generating() const;
void set_is_generating(bool value);
bool get_dirty() const; bool get_dirty() const;
void set_dirty(bool value); void set_dirty(bool value);
@ -254,6 +257,7 @@ public:
protected: protected:
static void _bind_methods(); static void _bind_methods();
bool _is_generating;
bool _dirty; bool _dirty;
int _state; int _state;

View File

@ -23,6 +23,27 @@ void VoxelWorld::set_chunk_size_z(const int value) {
_chunk_size_z = value; _chunk_size_z = value;
} }
int VoxelWorld::get_current_seed() const {
return _current_seed;
}
void VoxelWorld::set_current_seed(const int value) {
_current_seed = value;
}
bool VoxelWorld::get_use_threads() {
return _use_threads;
}
void VoxelWorld::set_use_threads(bool value) {
_use_threads = OS::get_singleton()->can_use_threads() ? value : false;
}
uint32_t VoxelWorld::get_max_concurrent_generations() {
return _max_concurrent_generations;
}
void VoxelWorld::set_max_concurrent_generations(uint32_t value) {
_max_concurrent_generations = OS::get_singleton()->can_use_threads() ? value : 1;
}
Ref<VoxelmanLibrary> VoxelWorld::get_library() const { Ref<VoxelmanLibrary> VoxelWorld::get_library() const {
return _library; return _library;
} }
@ -142,10 +163,46 @@ void VoxelWorld::clear_chunks() {
_chunks.clear(); _chunks.clear();
} }
void VoxelWorld::create_chunk(int x, int y, int z) {
call("_create_chunk");
}
void VoxelWorld::_create_chunk(int x, int y, int z) {
}
void VoxelWorld::generate_chunk_bind(Node *p_chunk) {
generate_chunk(Object::cast_to<VoxelChunk>(p_chunk));
}
void VoxelWorld::generate_chunk(VoxelChunk *p_chunk) {
ERR_FAIL_COND(!ObjectDB::instance_validate(p_chunk));
p_chunk->set_is_generating(true);
if (has_method("_prepare_chunk_for_generation"))
call("_prepare_chunk_for_generation", p_chunk);
call("_generate_chunk", p_chunk);
}
void VoxelWorld::_generate_chunk(Node *p_chunk) {
VoxelChunk *chunk = Object::cast_to<VoxelChunk>(p_chunk);
ERR_FAIL_COND(!ObjectDB::instance_validate(chunk));
ERR_FAIL_COND(!_level_generator.is_valid());
_level_generator->generate_chunk(chunk);
}
VoxelWorld::VoxelWorld() { VoxelWorld::VoxelWorld() {
_chunk_size_x = 16; _chunk_size_x = 16;
_chunk_size_y = 16; _chunk_size_y = 16;
_chunk_size_z = 16; _chunk_size_z = 16;
_current_seed = 0;
set_use_threads(true);
set_max_concurrent_generations(3);
_voxel_scale = 1; _voxel_scale = 1;
_chunk_spawn_range = 4; _chunk_spawn_range = 4;
@ -160,9 +217,67 @@ VoxelWorld ::~VoxelWorld() {
_world_areas.clear(); _world_areas.clear();
_library.unref(); _library.unref();
_level_generator.unref();
_player = NULL;
_generation_queue.clear();
_generating.clear();;
}
void VoxelWorld::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
set_process_internal(false);
if (!Engine::get_singleton()->is_editor_hint() && _library.is_valid())
_library->refresh_rects();
}
case NOTIFICATION_INTERNAL_PROCESS: {
if (_generation_queue.empty() && _generating.empty()) {
call("_generation_finished");
emit_signal("generation_finished");
set_process_internal(false);
return;
}
for (int i = 0; i < _generating.size(); ++i) {
VoxelChunk *chunk = _generating.get(i);
if (!ObjectDB::instance_validate(chunk) || chunk->get_is_generating()) {
_generating.remove(i);
--i;
continue;
}
}
if (_generating.size() >= _max_concurrent_generations)
return;
if (_generation_queue.size == 0)
return;
VoxelChunk *chunk = _generation_queue.get(0);
_generation_queue.remove(0);
ERR_FAIL_COND(!ObjectDB::instance_validate(chunk));
_generating.push_back(chunk);
generate_chunk(chunk);
}
}
} }
void VoxelWorld::_bind_methods() { void VoxelWorld::_bind_methods() {
ADD_SIGNAL(MethodInfo("generation_finished"));
BIND_VMETHOD(MethodInfo("_generation_finished"));
BIND_VMETHOD(MethodInfo("_prepare_chunk_for_generation", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
ClassDB::bind_method(D_METHOD("get_chunk_size_x"), &VoxelWorld::get_chunk_size_x); ClassDB::bind_method(D_METHOD("get_chunk_size_x"), &VoxelWorld::get_chunk_size_x);
ClassDB::bind_method(D_METHOD("set_chunk_size_x", "value"), &VoxelWorld::set_chunk_size_x); ClassDB::bind_method(D_METHOD("set_chunk_size_x", "value"), &VoxelWorld::set_chunk_size_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_x"), "set_chunk_size_x", "get_chunk_size_x"); ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_x"), "set_chunk_size_x", "get_chunk_size_x");
@ -175,6 +290,18 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_chunk_size_z", "value"), &VoxelWorld::set_chunk_size_z); ClassDB::bind_method(D_METHOD("set_chunk_size_z", "value"), &VoxelWorld::set_chunk_size_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_z"), "set_chunk_size_z", "get_chunk_size_z"); ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_z"), "set_chunk_size_z", "get_chunk_size_z");
ClassDB::bind_method(D_METHOD("get_current_seed"), &VoxelWorld::get_current_seed);
ClassDB::bind_method(D_METHOD("set_current_seed", "value"), &VoxelWorld::set_current_seed);
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_seed"), "set_current_seed", "get_current_seed");
ClassDB::bind_method(D_METHOD("get_use_threads"), &VoxelWorld::get_use_threads);
ClassDB::bind_method(D_METHOD("set_use_threads", "value"), &VoxelWorld::set_use_threads);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "get_use_threads");
ClassDB::bind_method(D_METHOD("get_max_concurrent_generations"), &VoxelWorld::get_max_concurrent_generations);
ClassDB::bind_method(D_METHOD("set_max_concurrent_generations", "value"), &VoxelWorld::set_max_concurrent_generations);
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_concurrent_generations"), "set_max_concurrent_generations", "get_max_concurrent_generations");
ClassDB::bind_method(D_METHOD("get_library"), &VoxelWorld::get_library); ClassDB::bind_method(D_METHOD("get_library"), &VoxelWorld::get_library);
ClassDB::bind_method(D_METHOD("set_library", "library"), &VoxelWorld::set_library); ClassDB::bind_method(D_METHOD("set_library", "library"), &VoxelWorld::set_library);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanLibrary"), "set_library", "get_library"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanLibrary"), "set_library", "get_library");
@ -213,4 +340,6 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_chunk_count"), &VoxelWorld::get_chunk_count); ClassDB::bind_method(D_METHOD("get_chunk_count"), &VoxelWorld::get_chunk_count);
ClassDB::bind_method(D_METHOD("clear_chunks"), &VoxelWorld::clear_chunks); ClassDB::bind_method(D_METHOD("clear_chunks"), &VoxelWorld::clear_chunks);
ClassDB::bind_method(D_METHOD("generate_chunk", "chunk"), &VoxelWorld::generate_chunk_bind);
} }

View File

@ -8,6 +8,8 @@
#include "../level_generator/voxelman_level_generator.h" #include "../level_generator/voxelman_level_generator.h"
#include "../areas/world_area.h" #include "../areas/world_area.h"
#include "core/os/os.h"
class VoxelChunk; class VoxelChunk;
class VoxelWorld : public Navigation { class VoxelWorld : public Navigation {
@ -22,6 +24,15 @@ public:
int get_chunk_size_z() const; int get_chunk_size_z() const;
void set_chunk_size_z(const int value); void set_chunk_size_z(const int value);
int get_current_seed() const;
void set_current_seed(const int value);
bool get_use_threads();
void set_use_threads(bool value);
uint32_t get_max_concurrent_generations();
void set_max_concurrent_generations(uint32_t value);
Ref<VoxelmanLibrary> get_library() const; Ref<VoxelmanLibrary> get_library() const;
void set_library(const Ref<VoxelmanLibrary> library); void set_library(const Ref<VoxelmanLibrary> library);
@ -58,10 +69,18 @@ public:
void clear_chunks(); void clear_chunks();
void create_chunk(int x, int y, int z);
void _create_chunk(int x, int y, int z);
void generate_chunk_bind(Node *p_chunk);
void generate_chunk(VoxelChunk *p_chunk);
void _generate_chunk(Node *p_chunk);
VoxelWorld(); VoxelWorld();
~VoxelWorld(); ~VoxelWorld();
protected: protected:
virtual void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
public: public:
@ -95,6 +114,7 @@ private:
int _chunk_size_x; int _chunk_size_x;
int _chunk_size_y; int _chunk_size_y;
int _chunk_size_z; int _chunk_size_z;
int _current_seed;
Ref<VoxelmanLibrary> _library; Ref<VoxelmanLibrary> _library;
Ref<VoxelmanLevelGenerator> _level_generator; Ref<VoxelmanLevelGenerator> _level_generator;
@ -108,6 +128,11 @@ private:
NodePath _player_path; NodePath _player_path;
Spatial *_player; Spatial *_player;
bool _use_threads;
uint32_t _max_concurrent_generations;
Vector<VoxelChunk *> _generation_queue;
Vector<VoxelChunk *> _generating;
}; };
_FORCE_INLINE_ bool operator==(const VoxelWorld::IntPos &a, const VoxelWorld::IntPos &b) { _FORCE_INLINE_ bool operator==(const VoxelWorld::IntPos &a, const VoxelWorld::IntPos &b) {