Skip to content

Commit

Permalink
Add direct load mode
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Nov 22, 2024
1 parent 743dd54 commit d944fb2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
20 changes: 12 additions & 8 deletions core/extension/gdextension_spx_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,20 @@ static void gdextension_spx_platform_set_debug_mode(GdBool enable) {
static void gdextension_spx_platform_is_debug_mode(GdBool* ret_val) {
*ret_val = platformMgr->is_debug_mode();
}
static void gdextension_spx_res_get_bound_from_alpha(GdString path,GdRect2* ret_val) {
*ret_val = resMgr->get_bound_from_alpha(path);
static void gdextension_spx_res_set_load_mode(GdBool is_direct_mode) {
resMgr->set_load_mode(is_direct_mode);
}
static void gdextension_spx_res_get_image_size(GdString path,GdVec2* ret_val) {
*ret_val = resMgr->get_image_size(path);
static void gdextension_spx_res_get_bound_from_alpha(GdString p_path,GdRect2* ret_val) {
*ret_val = resMgr->get_bound_from_alpha(p_path);
}
static void gdextension_spx_res_read_all_text(GdString path,GdString* ret_val) {
*ret_val = resMgr->read_all_text(path);
static void gdextension_spx_res_get_image_size(GdString p_path,GdVec2* ret_val) {
*ret_val = resMgr->get_image_size(p_path);
}
static void gdextension_spx_res_has_file(GdString path,GdBool* ret_val) {
*ret_val = resMgr->has_file(path);
static void gdextension_spx_res_read_all_text(GdString p_path,GdString* ret_val) {
*ret_val = resMgr->read_all_text(p_path);
}
static void gdextension_spx_res_has_file(GdString p_path,GdBool* ret_val) {
*ret_val = resMgr->has_file(p_path);
}
static void gdextension_spx_scene_change_scene_to_file(GdString path) {
sceneMgr->change_scene_to_file(path);
Expand Down Expand Up @@ -642,6 +645,7 @@ void gdextension_spx_setup_interface() {
REGISTER_SPX_INTERFACE_FUNC(spx_platform_is_window_fullscreen);
REGISTER_SPX_INTERFACE_FUNC(spx_platform_set_debug_mode);
REGISTER_SPX_INTERFACE_FUNC(spx_platform_is_debug_mode);
REGISTER_SPX_INTERFACE_FUNC(spx_res_set_load_mode);
REGISTER_SPX_INTERFACE_FUNC(spx_res_get_bound_from_alpha);
REGISTER_SPX_INTERFACE_FUNC(spx_res_get_image_size);
REGISTER_SPX_INTERFACE_FUNC(spx_res_read_all_text);
Expand Down
9 changes: 5 additions & 4 deletions core/extension/gdextension_spx_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ typedef void (*GDExtensionSpxPlatformIsWindowFullscreen)(GdBool* ret_value);
typedef void (*GDExtensionSpxPlatformSetDebugMode)(GdBool enable);
typedef void (*GDExtensionSpxPlatformIsDebugMode)(GdBool* ret_value);
// SpxRes
typedef void (*GDExtensionSpxResGetBoundFromAlpha)(GdString path, GdRect2* ret_value);
typedef void (*GDExtensionSpxResGetImageSize)(GdString path, GdVec2* ret_value);
typedef void (*GDExtensionSpxResReadAllText)(GdString path, GdString* ret_value);
typedef void (*GDExtensionSpxResHasFile)(GdString path, GdBool* ret_value);
typedef void (*GDExtensionSpxResSetLoadMode)(GdBool is_direct_mode);
typedef void (*GDExtensionSpxResGetBoundFromAlpha)(GdString p_path, GdRect2* ret_value);
typedef void (*GDExtensionSpxResGetImageSize)(GdString p_path, GdVec2* ret_value);
typedef void (*GDExtensionSpxResReadAllText)(GdString p_path, GdString* ret_value);
typedef void (*GDExtensionSpxResHasFile)(GdString p_path, GdBool* ret_value);
// SpxScene
typedef void (*GDExtensionSpxSceneChangeSceneToFile)(GdString path);
typedef void (*GDExtensionSpxSceneReloadCurrentScene)(GdInt* ret_value);
Expand Down
8 changes: 3 additions & 5 deletions core/extension/spx_audio_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void SpxAudioMgr::on_update(float delta) {

void SpxAudioMgr::play_sfx(GdString path) {
auto path_str = SpxStr(path);
Ref<AudioStream> stream = resMgr->load_texture(path_str);
Ref<AudioStream> stream = resMgr->load_audio(path_str);

auto audio = memnew(AudioStreamPlayer2D);
audio->set_bus(STR_BUS_SFX);
Expand All @@ -109,10 +109,8 @@ GdBool SpxAudioMgr::is_music_playing() {
}

void SpxAudioMgr::play_music(GdString path) {
Ref<Resource> res = ResourceLoader::load(SpxStr(path));
ERR_FAIL_COND(res.is_null());
ERR_FAIL_COND(!res->is_class("AudioStream"));
Ref<AudioStream> stream = res;
auto path_str = SpxStr(path);
Ref<AudioStream> stream = resMgr->load_audio(path_str);

// set loop
Ref<AudioStreamMP3> mp3_stream = stream;
Expand Down
9 changes: 4 additions & 5 deletions core/extension/spx_res_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "spx_res_mgr.h"
#include "core/io/file_access.h"
#include "core/io/image.h"
#include "core/io/image_loader.h"
#include "editor/import/resource_importer_wav.h"
#include "modules/minimp3/audio_stream_mp3.h"
#include "modules/minimp3/resource_importer_mp3.h"
Expand Down Expand Up @@ -91,15 +92,13 @@ Ref<Texture2D> SpxResMgr::_load_texture_direct(const String &path) {

Ref<Image> image;
image.instantiate();
Error err = image->load(path);
Error err =ImageLoader::load_image(path, image);
if (err != OK) {
print_line("Failed to load image: " + String::num_int64(err));
print_line("Failed to load image: " + String::num_int64(err),path);
return Ref<Texture2D>();
}

Ref<ImageTexture> texture;
texture.instantiate();
texture->create_from_image(image);
Ref<ImageTexture> texture = ImageTexture::create_from_image(image);
cached_texture.insert(path, texture);
return texture;
}
Expand Down
20 changes: 12 additions & 8 deletions platform/web/godot_js_spx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,24 @@ void gdspx_platform_is_debug_mode(GdBool* ret_val) {
*ret_val = platformMgr->is_debug_mode();
}
EMSCRIPTEN_KEEPALIVE
void gdspx_res_get_bound_from_alpha(GdString* path,GdRect2* ret_val) {
*ret_val = resMgr->get_bound_from_alpha(*path);
void gdspx_res_set_load_mode(GdBool* is_direct_mode) {
resMgr->set_load_mode(*is_direct_mode);
}
EMSCRIPTEN_KEEPALIVE
void gdspx_res_get_image_size(GdString* path,GdVec2* ret_val) {
*ret_val = resMgr->get_image_size(*path);
void gdspx_res_get_bound_from_alpha(GdString* p_path,GdRect2* ret_val) {
*ret_val = resMgr->get_bound_from_alpha(*p_path);
}
EMSCRIPTEN_KEEPALIVE
void gdspx_res_read_all_text(GdString* path,GdString* ret_val) {
*ret_val = resMgr->read_all_text(*path);
void gdspx_res_get_image_size(GdString* p_path,GdVec2* ret_val) {
*ret_val = resMgr->get_image_size(*p_path);
}
EMSCRIPTEN_KEEPALIVE
void gdspx_res_has_file(GdString* path,GdBool* ret_val) {
*ret_val = resMgr->has_file(*path);
void gdspx_res_read_all_text(GdString* p_path,GdString* ret_val) {
*ret_val = resMgr->read_all_text(*p_path);
}
EMSCRIPTEN_KEEPALIVE
void gdspx_res_has_file(GdString* p_path,GdBool* ret_val) {
*ret_val = resMgr->has_file(*p_path);
}
EMSCRIPTEN_KEEPALIVE
void gdspx_scene_change_scene_to_file(GdString* path) {
Expand Down
24 changes: 16 additions & 8 deletions platform/web/js/engine/gdspx.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,40 +361,48 @@ function gdspx_platform_is_debug_mode() {
FreeGdBool(_retValue);
return _finalRetValue
}
function gdspx_res_get_bound_from_alpha(path) {
function gdspx_res_set_load_mode(is_direct_mode) {
_gdFuncPtr = GodotEngine.rtenv['_gdspx_res_set_load_mode'];

_arg0 = ToGdBool(is_direct_mode);
_gdFuncPtr(_arg0);
FreeGdBool(_arg0);

}
function gdspx_res_get_bound_from_alpha(p_path) {
_gdFuncPtr = GodotEngine.rtenv['_gdspx_res_get_bound_from_alpha'];
_retValue = AllocGdRect2();
_arg0 = ToGdString(path);
_arg0 = ToGdString(p_path);
_gdFuncPtr(_arg0, _retValue);
FreeGdString(_arg0);
_finalRetValue = ToJsRect2(_retValue);
FreeGdRect2(_retValue);
return _finalRetValue
}
function gdspx_res_get_image_size(path) {
function gdspx_res_get_image_size(p_path) {
_gdFuncPtr = GodotEngine.rtenv['_gdspx_res_get_image_size'];
_retValue = AllocGdVec2();
_arg0 = ToGdString(path);
_arg0 = ToGdString(p_path);
_gdFuncPtr(_arg0, _retValue);
FreeGdString(_arg0);
_finalRetValue = ToJsVec2(_retValue);
FreeGdVec2(_retValue);
return _finalRetValue
}
function gdspx_res_read_all_text(path) {
function gdspx_res_read_all_text(p_path) {
_gdFuncPtr = GodotEngine.rtenv['_gdspx_res_read_all_text'];
_retValue = AllocGdString();
_arg0 = ToGdString(path);
_arg0 = ToGdString(p_path);
_gdFuncPtr(_arg0, _retValue);
FreeGdString(_arg0);
_finalRetValue = ToJsString(_retValue);
FreeGdString(_retValue);
return _finalRetValue
}
function gdspx_res_has_file(path) {
function gdspx_res_has_file(p_path) {
_gdFuncPtr = GodotEngine.rtenv['_gdspx_res_has_file'];
_retValue = AllocGdBool();
_arg0 = ToGdString(path);
_arg0 = ToGdString(p_path);
_gdFuncPtr(_arg0, _retValue);
FreeGdString(_arg0);
_finalRetValue = ToJsBool(_retValue);
Expand Down

0 comments on commit d944fb2

Please sign in to comment.