Skip to content

Commit

Permalink
First pass fixup. memdelete, class_name, use new data API
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Jan 8, 2025
1 parent eb1376a commit f097927
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 74 deletions.
13 changes: 7 additions & 6 deletions src/base_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
#ifndef BASECHUNK_CLASS_H
#define BASECHUNK_CLASS_H

#include "chunk_manager.h"
#include <godot_cpp/templates/vector.hpp>

#include "chunk_manager.h"
#include "constants.h"

using namespace godot;

class ChunkManager;

class BaseChunk : public Object {
GDCLASS(BaseChunk, Object);
CLASS_NAME();

public:
// Constants
static inline const char *__class__ = "Terrain3DBaseChunk";
public: // Constants

protected:
Vector2i _position = Vector2i(0, 0);
uint _size = 0;
unsigned int _size = 0;
ChunkManager *_manager = nullptr;

public:
Expand All @@ -29,7 +30,7 @@ class BaseChunk : public Object {
Vector2i get_position() { return _position; }

BaseChunk() {}
BaseChunk(ChunkManager *p_manager, uint p_size) {
BaseChunk(ChunkManager *p_manager, unsigned int p_size) {
_manager = p_manager;
_size = p_size;
}
Expand Down
4 changes: 2 additions & 2 deletions src/chunk_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "chunk_manager.h"
#include "logger.h"
#include "terrain_3d_storage.h"
#include "terrain_3d_data.h"

void ChunkManager::move(Vector3 p_camera_position) {
Vector2i pos_snapped = _snap_position(p_camera_position);
Expand Down Expand Up @@ -115,7 +115,7 @@ void ChunkManager::set_distance(float p_distance) {
_build();
}

void ChunkManager::set_chunk_size(uint p_size) {
void ChunkManager::set_chunk_size(unsigned int p_size) {
// Round up to nearest power of 2
{
p_size--;
Expand Down
21 changes: 12 additions & 9 deletions src/chunk_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
#ifndef CHUNKMANAGER_CLASS_H
#define CHUNKMANAGER_CLASS_H

#include "base_chunk.h"
#include <godot_cpp/classes/node3d.hpp>

#include "base_chunk.h"
#include "constants.h"


using namespace godot;

class BaseChunk;

class ChunkManager : public Node3D {
GDCLASS(ChunkManager, Node3D);
CLASS_NAME();

public: // Constants

public:
// Constants
static inline const char *__class__ = "Terrain3DChunkManager";

protected:
uint _chunk_size = 16;
unsigned int _chunk_size = 16;

private:
float _distance = 64.0;
uint _chunks_width = 0;
uint _chunk_count = 0;
unsigned int _chunks_width = 0;
unsigned int _chunk_count = 0;
Array _active_chunks = Array();
Array _old_chunks = Array();
Array _inactive_chunks = Array();
Expand All @@ -36,8 +39,8 @@ class ChunkManager : public Node3D {
public:
ChunkManager() {}

void set_chunk_size(uint p_size);
uint get_chunk_size() { return _chunk_size; }
void set_chunk_size(unsigned int p_size);
unsigned int get_chunk_size() { return _chunk_size; }
void set_distance(float p_distance);
float get_distance() { return _distance; }
void move(Vector3 p_camera_position);
Expand Down
9 changes: 5 additions & 4 deletions src/collision_chunk_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
#ifndef COLLISIONCHUNKMANAGER_CLASS_H
#define COLLISIONCHUNKMANAGER_CLASS_H

#include <godot_cpp/classes/static_body3d.hpp>

#include "chunk_manager.h"
#include "terrain_3d.h"
#include <godot_cpp/classes/static_body3d.hpp>

class Terrain3D;

using namespace godot;

class CollisionChunkManager : public ChunkManager {
GDCLASS(CollisionChunkManager, ChunkManager);
CLASS_NAME();

public:
// Constants
static inline const char *__class__ = "Terrain3DCollisionChunkManager";
public: // Constants

private:
Terrain3D *_terrain = nullptr;

public:
Expand Down
62 changes: 30 additions & 32 deletions src/editor_collision_chunk.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright © 2024 Lorenz Wildberg

#include "editor_collision_chunk.h"
#include "logger.h"
#include <godot_cpp/classes/height_map_shape3d.hpp>
#include <godot_cpp/classes/project_settings.hpp>

EditorCollisionChunk::EditorCollisionChunk(EditorCollisionChunkManager *p_manager, uint p_size) :
#include "editor_collision_chunk.h"
#include "logger.h"

EditorCollisionChunk::EditorCollisionChunk(EditorCollisionChunkManager *p_manager, unsigned int p_size) :
BaseChunk(p_manager, p_size) {
_col_shape = memnew(CollisionShape3D);
_col_shape->set_name("CollisionShape3D");
Expand Down Expand Up @@ -35,40 +36,37 @@ void EditorCollisionChunk::refill() {
}

inline PackedFloat32Array EditorCollisionChunk::fill_map(Transform3D *xform) {
Ref<Terrain3DStorage> storage = ((CollisionChunkManager *)_manager)->_terrain->get_storage();
float hole_const = NAN;
if (ProjectSettings::get_singleton()->get_setting("physics/3d/physics_engine") == "JoltPhysics3D") {
hole_const = __FLT_MAX__;
}

int region = storage->get_region_index(Vector3(_position.x, 0, _position.y));
int region_size = storage->get_region_size();
Ref<Terrain3DData> data = ((CollisionChunkManager *)_manager)->_terrain->get_data();
int region_size = _terrain->get_region_size();

PackedFloat32Array map_data = PackedFloat32Array();
map_data.resize(_size * _size);

Ref<Image> map, map_x, map_z, map_xz;
Ref<Image> cmap, cmap_x, cmap_z, cmap_xz;
map = storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap = storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
if (region >= 0) {
map = storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap = storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);

Ref<Terrain3DRegion> region = data->get_regionp(Vector3(_position.x, 0, _position.y));
if (region.is_null()) {
LOG(ERROR, "Region ", region_loc, " not found");
return;
}
region = storage->get_region_index(Vector3(_position.x + _size, 0, _position.y));
if (region >= 0) {
map_x = storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap_x = storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
map = region->get_map(TYPE_HEIGHT);
cmap = region->get_map(TYPE_CONTROL);

region = _data->get_regionp(Vector3(_position.x + _size, 0.f, _position.y) * _vertex_spacing);
if (region.is_valid()) {
map_x = region->get_map(TYPE_HEIGHT);
cmap_x = region->get_map(TYPE_CONTROL);
}
region = storage->get_region_index(Vector3(_position.x, 0, _position.y + _size));
if (region >= 0) {
map_z = storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap_z = storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
region = _data->get_regionp(Vector3(_position.x, 0.f, _position.y + _size) * _vertex_spacing);
if (region.is_valid()) {
map_z = region->get_map(TYPE_HEIGHT);
cmap_z = region->get_map(TYPE_CONTROL);
}
region = storage->get_region_index(Vector3(_position.x + _size, 0, _position.y + _size));
if (region >= 0) {
map_xz = storage->get_map_region(Terrain3DStorage::TYPE_HEIGHT, region);
cmap_xz = storage->get_map_region(Terrain3DStorage::TYPE_CONTROL, region);
region = _data->get_regionp(Vector3(_position.x + _size, 0.f, _position.y + _size) * _vertex_spacing);
if (region.is_valid()) {
map_xz = region->get_map(TYPE_HEIGHT);
cmap_xz = region->get_map(TYPE_CONTROL);
}

for (int z = 0; z < _size; z++) {
Expand All @@ -92,25 +90,25 @@ inline PackedFloat32Array EditorCollisionChunk::fill_map(Transform3D *xform) {
// Set heights on local map, or adjacent maps if on the last row/col
if (shape_global_x < region_size && shape_global_z < region_size) {
if (map.is_valid()) {
map_data[index] = (Util::is_hole(cmap->get_pixel(shape_global_x, shape_global_z).r)) ? hole_const : map->get_pixel(shape_global_x, shape_global_z).r;
map_data[index] = (is_hole(cmap->get_pixel(shape_global_x, shape_global_z).r)) ? NAN : map->get_pixel(shape_global_x, shape_global_z).r;
} else {
map_data[index] = 0.0;
}
} else if (shape_global_x == region_size && shape_global_z < region_size) {
if (map_x.is_valid()) {
map_data[index] = (Util::is_hole(cmap_x->get_pixel(0, shape_global_z).r)) ? hole_const : map_x->get_pixel(0, shape_global_z).r;
map_data[index] = (is_hole(cmap_x->get_pixel(0, shape_global_z).r)) ? NAN : map_x->get_pixel(0, shape_global_z).r;
} else {
map_data[index] = 0.0f;
}
} else if (shape_global_z == region_size && shape_global_x < region_size) {
if (map_z.is_valid()) {
map_data[index] = (Util::is_hole(cmap_z->get_pixel(shape_global_x, 0).r)) ? hole_const : map_z->get_pixel(shape_global_x, 0).r;
map_data[index] = (is_hole(cmap_z->get_pixel(shape_global_x, 0).r)) ? NAN : map_z->get_pixel(shape_global_x, 0).r;
} else {
map_data[index] = 0.0f;
}
} else if (shape_global_x == region_size && shape_global_z == region_size) {
if (map_xz.is_valid()) {
map_data[index] = (Util::is_hole(cmap_xz->get_pixel(0, 0).r)) ? hole_const : map_xz->get_pixel(0, 0).r;
map_data[index] = (is_hole(cmap_xz->get_pixel(0, 0).r)) ? NAN : map_xz->get_pixel(0, 0).r;
} else {
map_data[index] = 0.0f;
}
Expand Down
10 changes: 5 additions & 5 deletions src/editor_collision_chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
#ifndef EDITORCOLLISIONCHUNK_CLASS_H
#define EDITORCOLLISIONCHUNK_CLASS_H

#include <godot_cpp/classes/collision_shape3d.hpp>

#include "base_chunk.h"
#include "editor_collision_chunk_manager.h"
#include <godot_cpp/classes/collision_shape3d.hpp>

using namespace godot;

class EditorCollisionChunk : public BaseChunk {
GDCLASS(EditorCollisionChunk, BaseChunk);
CLASS_NAME();

public:
// Constants
static inline const char *__class__ = "Terrain3DEditorCollisionChunk";
public: // Constants

private:
CollisionShape3D *_col_shape = nullptr;

public:
EditorCollisionChunk() {}
EditorCollisionChunk(EditorCollisionChunkManager *p_manager, uint p_size);
EditorCollisionChunk(EditorCollisionChunkManager *p_manager, unsigned int p_size);
~EditorCollisionChunk();

void refill() override;
Expand Down
6 changes: 2 additions & 4 deletions src/editor_collision_chunk_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ EditorCollisionChunkManager::EditorCollisionChunkManager() {
}

EditorCollisionChunkManager::~EditorCollisionChunkManager() {
if (_body != nullptr) {
remove_child(_body);
memfree(_body);
}
remove_from_tree(_body);
memdelete_safely(_body);
}

BaseChunk *EditorCollisionChunkManager::create_chunk() {
Expand Down
6 changes: 2 additions & 4 deletions src/editor_collision_chunk_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ using namespace godot;

class EditorCollisionChunkManager : public CollisionChunkManager {
GDCLASS(EditorCollisionChunkManager, CollisionChunkManager);
CLASS_NAME();

public:
// Constants
static inline const char *__class__ = "Terrain3DEditorCollisionChunkManager";

public: // Constants
StaticBody3D *_body = nullptr;

public:
Expand Down
10 changes: 2 additions & 8 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,15 @@ void Terrain3D::_update_collision(Vector3 p_cam_pos) {
Vector3 cam_pos = p_cam_pos;
cam_pos.y = 0;
int time = Time::get_singleton()->get_ticks_usec();

Vector3 cam_pos = (_camera != nullptr) ? _camera->get_global_position() : Vector3();
_chunk_manager->move(cam_pos);

LOG(EXTREME, "Collision update time: ", Time::get_singleton()->get_ticks_usec() - time, " us");
}

void Terrain3D::_destroy_collision() {
// Free any RIDs allocated by the Physics Server
LOG(INFO, "Destroy Collision");

if (_chunk_manager != nullptr) {
remove_child(_chunk_manager);
memfree(_chunk_manager);
}
remove_from_tree(_chunk_manager);
memdelete_safely(_chunk_manager);
}

void Terrain3D::_build_meshes(const int p_mesh_lods, const int p_mesh_size) {
Expand Down

0 comments on commit f097927

Please sign in to comment.