Skip to content

Commit

Permalink
Add new rect packer
Browse files Browse the repository at this point in the history
  • Loading branch information
EIREXE committed Oct 15, 2024
1 parent cc07112 commit 7b86526
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 274 deletions.
1 change: 1 addition & 0 deletions modules/hbnative/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ env_hbnative.Prepend(CPPDEFINES=["UNICODE"])
env_hbnative.add_source_files(module_obj, "*.cpp") # Add all cpp files to the build
env_hbnative.add_source_files(module_obj, "process/*.cpp")
env_hbnative.add_source_files(module_obj, "diva/*.cpp")
env_hbnative.add_source_files(module_obj, "rectpack/*.cpp")

env.modules_sources += module_obj

Expand Down
65 changes: 65 additions & 0 deletions modules/hbnative/rectpack/rectpack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "rectpack.h"
#include "core/error/error_list.h"
#include "core/math/rect2i.h"
#include "core/object/class_db.h"
#include "core/typedefs.h"
#include "core/variant/dictionary.h"
#include "core/variant/typed_array.h"

stbrp_node HBRectPack::packer_temp_storage[MAX_RECT_SIZE] = {};

void HBRectPack::_bind_methods() {
ClassDB::bind_static_method("HBRectPack", D_METHOD("pack_rects", "rects", "starting_pack_size"), &HBRectPack::pack_rects);
}

Dictionary HBRectPack::pack_rects(PackedVector2Array p_rects, Vector2i p_starting_pack_size) {
LocalVector<stbrp_rect> rects;
rects.resize(p_rects.size());
for (int i = 0; i < p_rects.size(); i++) {
rects[i].id = i;
Vector2i rect = p_rects[i];
rects[i].w = rect.x;
rects[i].h = rect.y;
}

Vector2i pack_size = Vector2(
nearest_power_of_2_templated(p_starting_pack_size.x),
nearest_power_of_2_templated(p_starting_pack_size.y));

bool packed = false;

stbrp_context context;

while (!packed && pack_size != Vector2i(MAX_RECT_SIZE, MAX_RECT_SIZE)) {
int min_axis = pack_size.min_axis_index();
pack_size.coord[min_axis] <<= 1;

stbrp_init_target(&context, pack_size.x, pack_size.y, packer_temp_storage, MAX_RECT_SIZE);
int result = stbrp_pack_rects(&context, rects.ptr(), rects.size());
if (result == 1) {
packed = true;
break;
}
}

Dictionary out;

if (!packed) {
out["result"] = FAILED;
return out;
}

TypedArray<Vector2i> out_points;
out_points.resize(rects.size());

for (size_t i = 0; i < rects.size(); i++) {
const Vector2i pos = Vector2i(rects[i].x, rects[i].y);
out_points[i] = pos;
}

out["size"] = pack_size;
out["result"] = OK;
out["points"] = out_points;

return out;
}
22 changes: 22 additions & 0 deletions modules/hbnative/rectpack/rectpack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef RECTPACK_H
#define RECTPACK_H

#include "core/math/rect2.h"
#include "core/object/class_db.h"
#include "core/variant/dictionary.h"
#include "core/variant/typed_array.h"
#include "thirdparty/misc/stb_rect_pack.h"

class HBRectPack : public Object {
GDCLASS(HBRectPack, Object);
static constexpr int MAX_RECT_SIZE = 8192;
static stbrp_node packer_temp_storage[MAX_RECT_SIZE];

protected:
static void _bind_methods();

public:
static Dictionary pack_rects(PackedVector2Array p_rects, Vector2i p_starting_pack_size);
};

#endif // RECTPACK_H
3 changes: 3 additions & 0 deletions modules/hbnative/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "register_types.h"

#include "core/config/engine.h"
#include "core/object/class_db.h"
#include "diva/bone_db.h"
#include "diva/diva_object.h"
#include "interval_tree.h"
Expand All @@ -22,6 +23,7 @@
#include "process/process_windows.h"
#endif

#include "rectpack/rectpack.h"
#include "threen.h"

static PHAudioStreamPreviewGenerator *preview_generator_ptr = NULL;
Expand Down Expand Up @@ -78,6 +80,7 @@ void initialize_hbnative_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(DIVABoneDB);
GDREGISTER_CLASS(DIVASkeleton);
GDREGISTER_CLASS(DIVAObjectSet);
GDREGISTER_ABSTRACT_CLASS(HBRectPack);
Engine::get_singleton()->add_singleton(Engine::Singleton("PHAudioStreamPreviewGenerator", PHAudioStreamPreviewGenerator::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("PHNative", PHNative::get_singleton()));
}
Expand Down
274 changes: 0 additions & 274 deletions modules/hbnative/tests/test_replays.h

This file was deleted.

0 comments on commit 7b86526

Please sign in to comment.