From d15eb8c82ce2edcff7698c7ebe9de0ef710f3809 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Fri, 9 Dec 2022 10:39:45 +0800 Subject: [PATCH] factor out resource loading into header --- example/pubspec.lock | 63 +++++++++---------- .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 9 +++ .../polyvox_filament/resource_loader.hpp | 63 +++++++++++++++++++ linux/polyvox_filament_plugin.cc | 48 +------------- 5 files changed, 104 insertions(+), 82 deletions(-) create mode 100644 linux/include/polyvox_filament/resource_loader.hpp diff --git a/example/pubspec.lock b/example/pubspec.lock index e02f334d..979bbf1c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.8.2" + version: "2.9.0" boolean_selector: dependency: transitive description: @@ -21,28 +21,21 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" - charcode: - dependency: transitive - description: - name: charcode - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.1" + version: "1.2.1" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.1.1" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" cupertino_icons: dependency: "direct main" description: @@ -56,7 +49,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.1" flutter: dependency: "direct main" description: flutter @@ -87,28 +80,28 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.11" + version: "0.12.12" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" - mimetic_filament: - dependency: "direct main" - description: - path: ".." - relative: true - source: path - version: "0.0.1" + version: "1.8.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.2" plugin_platform_interface: dependency: transitive description: @@ -116,6 +109,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + polyvox_filament: + dependency: "direct main" + description: + path: ".." + relative: true + source: path + version: "0.0.1" sky_engine: dependency: transitive description: flutter @@ -127,7 +127,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.9.0" stack_trace: dependency: transitive description: @@ -148,35 +148,28 @@ packages: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.1.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.2.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.3" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.12" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.2" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.17.1 <3.0.0" flutter: ">=1.20.0" diff --git a/example/windows/flutter/generated_plugin_registrant.cc b/example/windows/flutter/generated_plugin_registrant.cc index 8b6d4680..1b57d394 100644 --- a/example/windows/flutter/generated_plugin_registrant.cc +++ b/example/windows/flutter/generated_plugin_registrant.cc @@ -6,6 +6,9 @@ #include "generated_plugin_registrant.h" +#include void RegisterPlugins(flutter::PluginRegistry* registry) { + PolyvoxFilamentPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("PolyvoxFilamentPlugin")); } diff --git a/example/windows/flutter/generated_plugins.cmake b/example/windows/flutter/generated_plugins.cmake index 4d10c251..6e17d71a 100644 --- a/example/windows/flutter/generated_plugins.cmake +++ b/example/windows/flutter/generated_plugins.cmake @@ -3,6 +3,10 @@ # list(APPEND FLUTTER_PLUGIN_LIST + polyvox_filament +) + +list(APPEND FLUTTER_FFI_PLUGIN_LIST ) set(PLUGIN_BUNDLED_LIBRARIES) @@ -13,3 +17,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST}) list(APPEND PLUGIN_BUNDLED_LIBRARIES $) list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) endforeach(plugin) + +foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin}) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries}) +endforeach(ffi_plugin) diff --git a/linux/include/polyvox_filament/resource_loader.hpp b/linux/include/polyvox_filament/resource_loader.hpp new file mode 100644 index 00000000..6263d33c --- /dev/null +++ b/linux/include/polyvox_filament/resource_loader.hpp @@ -0,0 +1,63 @@ +#ifndef POLYVOX_FILAMENT_LINUX_RESOURCE_LOADER_H +#define POLYVOX_FILAMENT_LINUX_RESOURCE_LOADER_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ResourceBuffer.hpp" + +using namespace std; + +static map _file_assets; +static uint32_t _i = 0; + +ResourceBuffer loadResource(const char* name) { + + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) != NULL) { + std::cout << "Current working dir: " << cwd << std::endl; + } + + string name_str(name); + auto id = _i++; + + if (name_str.rfind("file://", 0) == 0) { + name_str = name_str.substr(7); + } + + name_str = string(cwd) + string("/") + name_str; + + std::cout << "Loading resource at " << name_str.c_str() << std::endl; + + streampos length; + ifstream is(name_str, ios::binary); + if(!is) { + std::cout << "Failed to find resource at file path " << name_str.c_str() << std::endl; + return ResourceBuffer(nullptr, 0, -1); + } + is.seekg (0, ios::end); + length = is.tellg(); + char * buffer; + buffer = new char [length]; + is.seekg (0, ios::beg); + is.read (buffer, length); + is.close(); + _file_assets[id] = buffer; + return ResourceBuffer(buffer, length, id); +} + +void freeResource(uint32_t id) { + std::cout << "Freeing resource " << id << std::endl; + auto it = _file_assets.find(id); + if (it != _file_assets.end()) { + free(it->second); + } +} + +#endif \ No newline at end of file diff --git a/linux/polyvox_filament_plugin.cc b/linux/polyvox_filament_plugin.cc index 4defdf60..6b2cc0bf 100644 --- a/linux/polyvox_filament_plugin.cc +++ b/linux/polyvox_filament_plugin.cc @@ -5,7 +5,6 @@ #include #include - #include #include @@ -18,6 +17,7 @@ #include "include/polyvox_filament/filament_texture.h" #include "include/polyvox_filament/filament_pb_texture.h" +#include "include/polyvox_filament/resource_loader.hpp" #include "FilamentViewer.hpp" extern "C" { @@ -44,52 +44,6 @@ struct _PolyvoxFilamentPlugin { G_DEFINE_TYPE(PolyvoxFilamentPlugin, polyvox_filament_plugin, g_object_get_type()) -static map _file_assets; -static uint32_t _i = 0; - -static ResourceBuffer loadResource(const char* name) { - - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - std::cout << "Current working dir: " << cwd << std::endl; - } - - string name_str(name); - auto id = _i++; - - if (name_str.rfind("file://", 0) == 0) { - name_str = name_str.substr(7); - } - - name_str = string(cwd) + string("/") + name_str; - - std::cout << "Loading resource at " << name_str.c_str() << std::endl; - - streampos length; - ifstream is(name_str, ios::binary); - if(!is) { - std::cout << "Failed to find resource at file path " << name_str.c_str() << std::endl; - return ResourceBuffer(nullptr, 0, -1); - } - is.seekg (0, ios::end); - length = is.tellg(); - char * buffer; - buffer = new char [length]; - is.seekg (0, ios::beg); - is.read (buffer, length); - is.close(); - _file_assets[id] = buffer; - return ResourceBuffer(buffer, length, id); -} - -static void freeResource(uint32_t id) { - std::cout << "Freeing resource " << id << std::endl; - auto it = _file_assets.find(id); - if (it != _file_assets.end()) { - free(it->second); - } -} - // Called when a method call is received from Flutter. static void polyvox_filament_plugin_handle_method_call( PolyvoxFilamentPlugin* self,