little less overloaded core functionality

This commit is contained in:
BlackMATov
2020-01-09 13:26:29 +07:00
parent 349d88bc05
commit 0766d75ba7
18 changed files with 117 additions and 196 deletions

View File

@@ -83,12 +83,9 @@ namespace e2d
audio(debug& d);
~audio() noexcept;
[[nodiscard]] sound_stream_ptr preload_stream(
[[nodiscard]] sound_stream_ptr create_stream(
buffer_view sound_data);
[[nodiscard]] sound_stream_ptr preload_stream(
const input_stream_uptr& file_stream);
[[nodiscard]] sound_stream_ptr create_stream(
input_stream_uptr file_stream);

View File

@@ -8,8 +8,6 @@
#include "_core.hpp"
#include "window.hpp"
namespace e2d
{
class mouse final : private noncopyable {
@@ -120,16 +118,4 @@ namespace e2d
class internal_state;
std::unique_ptr<internal_state> state_;
};
class window_input_source : public window::event_listener {
public:
window_input_source(input& input) noexcept;
void on_input_char(char32_t uchar) noexcept final;
void on_move_cursor(const v2f& pos) noexcept final;
void on_mouse_scroll(const v2f& delta) noexcept final;
void on_mouse_button(mouse_button btn, mouse_button_action act) noexcept final;
void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept final;
private:
input& input_;
};
}

View File

@@ -911,16 +911,9 @@ namespace e2d
buffer_view vertex_source,
buffer_view fragment_source);
shader_ptr create_shader(
const input_stream_uptr& vertex_stream,
const input_stream_uptr& fragment_stream);
texture_ptr create_texture(
const image& image);
texture_ptr create_texture(
const input_stream_uptr& image_stream);
texture_ptr create_texture(
const v2u& size,
const pixel_declaration& decl);

View File

@@ -55,10 +55,10 @@ namespace e2d
input_stream_uptr read(const url& url) const;
output_stream_uptr write(const url& url, bool append) const;
bool load(const url& url, buffer& dst) const;
std::optional<buffer> load(const url& url) const;
stdex::promise<buffer> load_async(const url& url) const;
bool load_as_string(const url& url, str& dst) const;
std::optional<str> load_as_string(const url& url) const;
stdex::promise<str> load_as_string_async(const url& url) const;
template < typename Iter >

View File

@@ -89,9 +89,9 @@ namespace e2d
std::unique_ptr<state> state_;
};
class window_trace_event_listener final : public window::event_listener {
class window_event_tracer final : public window::event_listener {
public:
window_trace_event_listener(debug& debug) noexcept;
window_event_tracer(debug& debug) noexcept;
void on_input_char(char32_t uchar) noexcept final;
void on_move_cursor(const v2f& pos) noexcept final;
void on_mouse_scroll(const v2f& delta) noexcept final;
@@ -105,6 +105,18 @@ namespace e2d
private:
debug& debug_;
};
class window_input_source final : public window::event_listener {
public:
window_input_source(input& input) noexcept;
void on_input_char(char32_t uchar) noexcept final;
void on_move_cursor(const v2f& pos) noexcept final;
void on_mouse_scroll(const v2f& delta) noexcept final;
void on_mouse_button(mouse_button btn, mouse_button_action act) noexcept final;
void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept final;
private:
input& input_;
};
}
ENUM_HPP_REGISTER_TRAITS(e2d::window::cursor_shapes)

View File

@@ -96,13 +96,20 @@ namespace
the<vfs>().register_scheme_alias(
"ships",
url("piratepack://PNG/Retina/Ships"));
image texture1_image;
if ( !images::try_load_image(texture1_image, the<vfs>().read(url("ships://ship (2).png"))) ) {
return false;
}
shader_ = the<render>().create_shader(
vs_source_cstr, fs_source_cstr);
texture1_ = the<render>().create_texture(
the<vfs>().read(url("ships://ship (2).png")));
texture2_ = the<render>().create_texture(
the<vfs>().read(url("ships://ship (19).png")));
image texture2_image;
if ( !images::try_load_image(texture2_image, the<vfs>().read(url("ships://ship (19).png"))) ) {
return false;
}
shader_ = the<render>().create_shader(vs_source_cstr, fs_source_cstr);
texture1_ = the<render>().create_texture(texture1_image);
texture2_ = the<render>().create_texture(texture2_image);
if ( !shader_ || !texture1_ || !texture2_ ) {
return false;

View File

@@ -156,12 +156,14 @@ namespace
the<vfs>().register_scheme_alias(
"ships",
url("piratepack://PNG/Retina/Ships"));
image texture_image;
if ( !images::try_load_image(texture_image, the<vfs>().read(url("ships://ship (3).png"))) ) {
return false;
}
shader_ = the<render>().create_shader(
vs_source_cstr, fs_source_cstr);
texture_ = the<render>().create_texture(
the<vfs>().read(url("ships://ship (3).png")));
shader_ = the<render>().create_shader(vs_source_cstr, fs_source_cstr);
texture_ = the<render>().create_texture(texture_image);
if ( !shader_ || !texture_ ) {
return false;

View File

@@ -111,12 +111,14 @@ namespace
the<vfs>().register_scheme_alias(
"ships",
url("piratepack://PNG/Retina/Ships"));
image texture_image;
if ( !images::try_load_image(texture_image, the<vfs>().read(url("ships://ship (3).png"))) ) {
return false;
}
shader_ = the<render>().create_shader(
vs_source_cstr, fs_source_cstr);
texture_ = the<render>().create_texture(
the<vfs>().read(url("ships://ship (3).png")));
shader_ = the<render>().create_shader(vs_source_cstr, fs_source_cstr);
texture_ = the<render>().create_texture(texture_image);
if ( !shader_ || !texture_ ) {
return false;

View File

@@ -20,8 +20,8 @@ namespace
"audio",
url("rpgaudio://Audio"));
auto sstream1 = the<audio>().preload_stream(the<vfs>().read(url("audio://chop.ogg")));
auto sstream2 = the<audio>().create_stream(the<vfs>().read(url("audio://footstep00.ogg")));
auto sstream1 = the<audio>().create_stream(the<vfs>().read(url("audio://chop.ogg")));
auto sstream2 = the<audio>().create_stream(*the<vfs>().load(url("audio://footstep00.ogg")));
if ( !sstream1 || !sstream2 ) {
return false;

View File

@@ -143,7 +143,7 @@ namespace e2d
audio::~audio() noexcept {
}
sound_stream_ptr audio::preload_stream(
sound_stream_ptr audio::create_stream(
buffer_view sound_data)
{
if ( !state_->initialized() ) {
@@ -176,23 +176,6 @@ namespace e2d
state_->dbg(), sample, nullptr));
}
sound_stream_ptr audio::preload_stream(
const input_stream_uptr& file_stream)
{
if ( !state_->initialized() ) {
state_->dbg().error("AUDIO: Not initialized");
return nullptr;
}
buffer file_data;
if ( !streams::try_read_tail(file_data, file_stream) ) {
state_->dbg().error("AUDIO: Failed to read file");
return nullptr;
}
return preload_stream(buffer_view(file_data));
}
sound_stream_ptr audio::create_stream(
input_stream_uptr file_stream)
{

View File

@@ -119,20 +119,13 @@ namespace e2d
audio::~audio() noexcept {
}
sound_stream_ptr audio::preload_stream(
sound_stream_ptr audio::create_stream(
buffer_view sound_data)
{
E2D_UNUSED(sound_data);
return nullptr;
}
sound_stream_ptr audio::preload_stream(
const input_stream_uptr& file_stream)
{
E2D_UNUSED(file_stream);
return nullptr;
}
sound_stream_ptr audio::create_stream(
input_stream_uptr file_stream)
{

View File

@@ -370,32 +370,4 @@ namespace e2d
state_->mouse.state_->frame_tick();
state_->keyboard.state_->frame_tick();
}
//
// class window_input_source
//
window_input_source::window_input_source(input& input) noexcept
: input_(input) {}
void window_input_source::on_input_char(char32_t uchar) noexcept {
input_.post_event(input::input_char_event{uchar});
}
void window_input_source::on_move_cursor(const v2f& pos) noexcept {
input_.post_event(input::move_cursor_event{pos});
}
void window_input_source::on_mouse_scroll(const v2f& delta) noexcept {
input_.post_event(input::mouse_scroll_event{delta});
}
void window_input_source::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
input_.post_event(input::mouse_button_event{btn, act});
}
void window_input_source::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
E2D_UNUSED(scancode);
input_.post_event(input::keyboard_key_event{key, act});
}
}

View File

@@ -188,24 +188,11 @@ namespace e2d
return nullptr;
}
shader_ptr render::create_shader(
const input_stream_uptr& vertex_stream,
const input_stream_uptr& fragment_stream)
{
E2D_UNUSED(vertex_stream, fragment_stream);
return nullptr;
}
texture_ptr render::create_texture(const image& image) {
E2D_UNUSED(image);
return nullptr;
}
texture_ptr render::create_texture(const input_stream_uptr& image_stream) {
E2D_UNUSED(image_stream);
return nullptr;
}
texture_ptr render::create_texture(const v2u& size, const pixel_declaration& decl) {
E2D_UNUSED(size, decl);
return nullptr;

View File

@@ -542,19 +542,6 @@ namespace e2d
str_view(reinterpret_cast<const char*>(fragment_source.data()), fragment_source.size()));
}
shader_ptr render::create_shader(
const input_stream_uptr& vertex,
const input_stream_uptr& fragment)
{
E2D_ASSERT(is_in_main_thread());
str vertex_source, fragment_source;
return streams::try_read_tail(vertex_source, vertex)
&& streams::try_read_tail(fragment_source, fragment)
? create_shader(vertex_source, fragment_source)
: nullptr;
}
texture_ptr render::create_texture(
const image& image)
{
@@ -633,18 +620,6 @@ namespace e2d
state_->dbg(), std::move(id), image.size(), decl));
}
texture_ptr render::create_texture(
const input_stream_uptr& image_stream)
{
E2D_ASSERT(is_in_main_thread());
image image;
if ( !images::try_load_image(image, image_stream) ) {
return nullptr;
}
return create_texture(image);
}
texture_ptr render::create_texture(
const v2u& size,
const pixel_declaration& decl)

View File

@@ -172,12 +172,10 @@ namespace e2d
}, output_stream_uptr());
}
bool vfs::load(const url& url, buffer& dst) const {
return load_async(url)
.then([&dst](auto&& src){
dst = std::forward<decltype(src)>(src);
return true;
}).get_or_default(false);
std::optional<buffer> vfs::load(const url& url) const {
return load_async(url).then([](auto&& src){
return std::optional(std::forward<decltype(src)>(src));
}).get_or_default(std::nullopt);
}
stdex::promise<buffer> vfs::load_async(const url& url) const {
@@ -191,12 +189,10 @@ namespace e2d
});
}
bool vfs::load_as_string(const url& url, str& dst) const {
return load_as_string_async(url)
.then([&dst](auto&& src){
dst = std::forward<decltype(src)>(src);
return true;
}).get_or_default(false);
std::optional<str> vfs::load_as_string(const url& url) const {
return load_as_string_async(url).then([](auto&& src){
return std::optional(std::forward<decltype(src)>(src));
}).get_or_default(std::nullopt);
}
stdex::promise<str> vfs::load_as_string_async(const url& url) const {

View File

@@ -7,13 +7,10 @@
#include <enduro2d/core/window.hpp>
#include <enduro2d/core/debug.hpp>
#include <enduro2d/core/input.hpp>
namespace e2d
{
//
// class window::event_listener
//
void window::event_listener::on_input_char(char32_t uchar) noexcept {
E2D_UNUSED(uchar);
}
@@ -52,56 +49,77 @@ namespace e2d
void window::event_listener::on_window_minimize(bool minimized) noexcept {
E2D_UNUSED(minimized);
}
}
//
// class trace_window_event_listener
//
window_trace_event_listener::window_trace_event_listener(debug& debug) noexcept
namespace e2d
{
window_event_tracer::window_event_tracer(debug& debug) noexcept
: debug_(debug) {}
void window_trace_event_listener::on_input_char(char32_t uchar) noexcept {
void window_event_tracer::on_input_char(char32_t uchar) noexcept {
debug_.trace("WINDOW: on_input_char(uchar: %0)", str32_view(&uchar, 1));
}
void window_trace_event_listener::on_move_cursor(const v2f& pos) noexcept {
void window_event_tracer::on_move_cursor(const v2f& pos) noexcept {
debug_.trace("WINDOW: on_move_cursor(pos: %0)", pos);
}
void window_trace_event_listener::on_mouse_scroll(const v2f& delta) noexcept {
void window_event_tracer::on_mouse_scroll(const v2f& delta) noexcept {
debug_.trace("WINDOW: on_scroll(delta: %0)", delta);
}
void window_trace_event_listener::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
debug_.trace("WINDOW: on_mouse_button(btn: %0 act: %1)",
btn,
act);
void window_event_tracer::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
debug_.trace("WINDOW: on_mouse_button(btn: %0 act: %1)", btn, act);
}
void window_trace_event_listener::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
debug_.trace("WINDOW: on_keyboard_key(key: %0 scancode: %1 act: %2)",
key,
scancode,
act);
void window_event_tracer::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
debug_.trace("WINDOW: on_keyboard_key(key: %0 scancode: %1 act: %2)", key, scancode, act);
}
void window_trace_event_listener::on_window_size(const v2u& size) noexcept {
void window_event_tracer::on_window_size(const v2u& size) noexcept {
debug_.trace("WINDOW: on_window_size(size: %0)", size);
}
void window_trace_event_listener::on_framebuffer_size(const v2u& size) noexcept {
void window_event_tracer::on_framebuffer_size(const v2u& size) noexcept {
debug_.trace("WINDOW: on_framebuffer_size(size: %0)", size);
}
void window_trace_event_listener::on_window_close() noexcept {
void window_event_tracer::on_window_close() noexcept {
debug_.trace("WINDOW: on_window_close()");
}
void window_trace_event_listener::on_window_focus(bool focused) noexcept {
void window_event_tracer::on_window_focus(bool focused) noexcept {
debug_.trace("WINDOW: on_window_focus(focused: %0)", focused);
}
void window_trace_event_listener::on_window_minimize(bool minimized) noexcept {
void window_event_tracer::on_window_minimize(bool minimized) noexcept {
debug_.trace("WINDOW: on_window_minimize(minimized: %0)", minimized);
}
}
namespace e2d
{
window_input_source::window_input_source(input& input) noexcept
: input_(input) {}
void window_input_source::on_input_char(char32_t uchar) noexcept {
input_.post_event(input::input_char_event{uchar});
}
void window_input_source::on_move_cursor(const v2f& pos) noexcept {
input_.post_event(input::move_cursor_event{pos});
}
void window_input_source::on_mouse_scroll(const v2f& delta) noexcept {
input_.post_event(input::mouse_scroll_event{delta});
}
void window_input_source::on_mouse_button(mouse_button btn, mouse_button_action act) noexcept {
input_.post_event(input::mouse_button_event{btn, act});
}
void window_input_source::on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept {
E2D_UNUSED(scancode);
input_.post_event(input::keyboard_key_event{key, act});
}
}

View File

@@ -77,7 +77,7 @@ namespace
return library.load_asset_async<binary_asset>(sound_address)
.then([](const binary_asset::load_result& sound_data){
return the<deferrer>().do_in_worker_thread([sound_data](){
sound_stream_ptr content = the<audio>().preload_stream(
sound_stream_ptr content = the<audio>().create_stream(
sound_data->content());
if ( !content ) {
throw sound_asset_loading_exception();

View File

@@ -33,15 +33,15 @@ TEST_CASE("vfs"){
REQUIRE(v.read({"file", nofile_path}) == input_stream_uptr());
}
{
buffer b0;
REQUIRE(v.load({"file", file_path}, b0));
auto b0 = v.load({"file", file_path});
REQUIRE(b0);
REQUIRE(b0 == buffer{"hello", 5});
auto b1 = v.load_async({"file", file_path}).get();
REQUIRE(b1 == buffer{"hello", 5});
str b2;
REQUIRE(v.load_as_string({"file", file_path}, b2));
auto b2 = v.load_as_string({"file", file_path});
REQUIRE(b2);
REQUIRE(b2 == "hello");
auto b3 = v.load_as_string_async({"file", file_path}).get();
@@ -135,15 +135,15 @@ TEST_CASE("vfs"){
REQUIRE(b == buffer("hello", 5));
}
{
buffer b0;
REQUIRE(v.load(url("archive://test.txt"), b0));
auto b0 = v.load(url("archive://test.txt"));
REQUIRE(b0);
REQUIRE(b0 == buffer("hello", 5));
auto b1 = v.load_async(url("archive://test.txt")).get();
REQUIRE(b1 == buffer("hello", 5));
str b2;
REQUIRE(v.load_as_string(url("archive://test.txt"), b2));
auto b2 = v.load_as_string(url("archive://test.txt"));
REQUIRE(b2);
REQUIRE(b2 == "hello");
auto b3 = v.load_as_string_async(url("archive://test.txt")).get();
@@ -157,15 +157,15 @@ TEST_CASE("vfs"){
REQUIRE(b == buffer("world", 5));
}
{
buffer b0;
REQUIRE(v.load(url("archive://folder/file.txt"), b0));
auto b0 = v.load(url("archive://folder/file.txt"));
REQUIRE(b0);
REQUIRE(b0 == buffer("world", 5));
auto b1 = v.load_async(url("archive://folder/file.txt")).get();
REQUIRE(b1 == buffer("world", 5));
str b2;
REQUIRE(v.load_as_string(url("archive://folder/file.txt"), b2));
auto b2 = v.load_as_string(url("archive://folder/file.txt"));
REQUIRE(b2);
REQUIRE(b2 == "world");
auto b3 = v.load_as_string_async(url("archive://folder/file.txt")).get();
@@ -174,17 +174,15 @@ TEST_CASE("vfs"){
{
REQUIRE(v.read(url("archive://TEst.txt")) == input_stream_uptr());
buffer b0;
REQUIRE_FALSE(v.load(url("archive://TEst.txt"), b0));
REQUIRE(b0.empty());
auto b0 = v.load(url("archive://TEst.txt"));
REQUIRE_FALSE(b0);
REQUIRE_THROWS_AS(
v.load_async(url("archive://TEst.txt")).get(),
vfs_load_async_exception);
str b2;
REQUIRE_FALSE(v.load_as_string(url("archive://TEst.txt"), b2));
REQUIRE(b2.empty());
auto b2 = v.load_as_string(url("archive://TEst.txt"));
REQUIRE_FALSE(b2);
REQUIRE_THROWS_AS(
v.load_as_string_async(url("archive://TEst.txt")).get(),