diff --git a/src/client/client.cpp b/src/client/client.cpp index 05fae08..a5045db 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -3,6 +3,9 @@ #include #include +#include +#include "../../../beats-server/src/websockets.h" + #include "../../../beats-server/src/json.h" #include "../../../beats-server/src/json_ext.h" @@ -18,7 +21,12 @@ namespace games { libwebsocket_callback_on_writable(context, wsi); break; case LWS_CALLBACK_CLIENT_RECEIVE: - // std::cout << std::string(static_cast(in), len) << std::endl; + try { + auto l = from_json(parse_json(std::string((char*)in, len))); + current_client->add_line(l); + } catch(std::exception& e){ + throw websockets::runtime_error(e.what()); + } break; case LWS_CALLBACK_CLIENT_WRITEABLE:{ js::Object ret; @@ -47,6 +55,13 @@ namespace games { { NULL, NULL, 0 } }; + void update_pitches(Client& client){ + for(auto & l : client.lines){ + auto& sound = l->sound; + sound->set_pitch(pitch_for_midi_note(client.scale.note_for_length(std::sqrt(l->sqr_length())))); + } + } + void Client::add_line(cheap_line_type const & line_in){ auto line = create_instrument(line_in); line->sound.reset(new motor::al::Source(sounds[line->line_kind])); @@ -57,19 +72,13 @@ namespace games { } lines_changed = true; + update_pitches(*this); } bool Client::has_ended(){ return false; } - void update_pitches(Client& client){ - for(auto & l : client.lines){ - auto& sound = l->sound; - sound->set_pitch(pitch_for_midi_note(client.scale.note_for_length(std::sqrt(l->sqr_length())))); - } - } - Client::Client(int window_width_, int window_height_, std::shared_ptr& active_base_) : Base(window_width_, window_height_, active_base_) { @@ -78,15 +87,14 @@ namespace games { } current_client = this; + // sound + scale = Scale::load_from_file(*current_scale); + // simulation beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f}); cheap_line_type line{{{50.0f, 100.0f}, {150.0f, 200.0f}, simulation::kOneWay}}; add_line(line); - // sound - scale = Scale::load_from_file(*current_scale); - update_pitches(*this); - // websockets lws_context_creation_info info; memset(&info, 0, sizeof info); diff --git a/src/client/client.hpp b/src/client/client.hpp index 865e752..a41b98f 100644 --- a/src/client/client.hpp +++ b/src/client/client.hpp @@ -40,8 +40,8 @@ namespace games { ::bundle.get_sound_path() + "pentatonic" }; const std::vector> sounds{ - resource_cache.get_sound_buffer(::bundle.get_sound_path() + "marimba.wav"), - resource_cache.get_sound_buffer(::bundle.get_sound_path() + "guitar440.wav") + resource_cache.get_sound_buffer(::bundle.get_sound_path() + "guitar440.wav"), + resource_cache.get_sound_buffer(::bundle.get_sound_path() + "marimba.wav") }; std::vector::const_iterator current_scale{scales.begin()}; Scale scale; diff --git a/src/client/client_common.hpp b/src/client/client_common.hpp index 35608a6..2d6a8bc 100644 --- a/src/client/client_common.hpp +++ b/src/client/client_common.hpp @@ -103,6 +103,7 @@ struct cheap_line_type { IntVec2 end_point; int line_kind; + cheap_line_type() = default; cheap_line_type(Instrument const & l) : starting_point(l.starting_point) , end_point(l.end_point)