From 666197087e4665284ce13f7e8d68eaa9e4fc24ff Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Tue, 23 Apr 2013 17:06:46 +0200 Subject: [PATCH] Added drum and stereo sound (reversed?) --- src/client/GameRenderer.cpp | 15 +++++++- src/client/client.cpp | 73 +++++++++++++++++++++++++++++++++++-- src/client/client.hpp | 14 ++++++- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/src/client/GameRenderer.cpp b/src/client/GameRenderer.cpp index ed10b24..6941a5d 100644 --- a/src/client/GameRenderer.cpp +++ b/src/client/GameRenderer.cpp @@ -111,13 +111,13 @@ void GameRenderer::draw(Client const & client){ gl::draw_arrays(GL_POINTS, 0, client.beat.notes.size()); } - if(!client.lines.empty()){ + if(!client.lines.empty() || client.kick || client.snare){ moggle::gl::blend_function(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); line_shader.s.use(); line_shader.s.uniform>("modelViewProjectionMatrix").set(modelViewProjectionMatrix); lines.clear(); - lines.reserve(client.lines.size() * 2); + lines.reserve(client.lines.size() * 2 + 2 * 2); for(auto const & l : client.lines){ Color color = (l->line_kind == simulation::kFallThrough) ? Color{1.0f, l->color_intensity, std::sin(l->color_intensity)} @@ -125,6 +125,17 @@ void GameRenderer::draw(Client const & client){ lines.push_back({l->starting_point, color}); lines.push_back({l->end_point, color}); } + + if(client.kick){ + Color color{0.6f + client.kick->color_intensity, 0.5f, 0.4f}; + lines.push_back({client.kick->starting_point, color}); + lines.push_back({client.kick->end_point, color}); + } + if(client.snare){ + Color color{0.6f + client.snare->color_intensity, 0.7f, 0.8f}; + lines.push_back({client.snare->starting_point, color}); + lines.push_back({client.snare->end_point, color}); + } if(debug_draw){ for(auto const & l : client.sim.lines){ diff --git a/src/client/client.cpp b/src/client/client.cpp index c27efe1..0ca4b6c 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -44,8 +44,18 @@ namespace games { case LWS_CALLBACK_CLIENT_WRITEABLE:{ js::Object ret; if(current_client->lines_changed){ + int extra = 0; + if(current_client->kick){ + current_client->lines.push_back(current_client->kick); + extra++; + } + if(current_client->snare){ + current_client->lines.push_back(current_client->snare); + extra++; + } ret["lines"] = vector_to_json(current_client->lines, [](std::shared_ptr const & i){return cheap_line_type(*i);}); current_client->lines_changed = false; + current_client->lines.erase(current_client->lines.end()-extra, current_client->lines.end()); } else { ret["balls"] = vector_to_json(current_client->sim.balls, [](ball_type const & b){return cheap_ball_type(b);}); } @@ -102,6 +112,28 @@ namespace games { lines_changed = true; } + void Client::add_drum(){ + kick = std::make_shared(math::Vec2{x1 - 25.0f, window_height - 100.0f}, math::Vec2{x1 + 25.0f, window_height - 50.0f}, 1, -1); + kick->sound.reset(new motor::al::Source(sounds[2])); + for(auto & l : kick->calculate_lines()){ + sim.lines.push_back(l); + } + snare = std::make_shared(math::Vec2{x3 - 25.0f, window_height - 50.0f}, math::Vec2{x3 + 25.0f, window_height - 100.0f}, 1, -2); + snare->sound.reset(new motor::al::Source(sounds[3])); + for(auto & l : snare->calculate_lines()){ + sim.lines.push_back(l); + } + } + + void Client::remove_drum(){ + auto& v = sim.lines; + v.erase(std::remove_if(v.begin(), v.end(), [this](line_type const &l2){ return kick.get() == l2.information; }), v.end()); + v.erase(std::remove_if(v.begin(), v.end(), [this](line_type const &l2){ return snare.get() == l2.information; }), v.end()); + + kick.reset(); + snare.reset(); + } + bool Client::has_ended(){ return false; } @@ -115,6 +147,10 @@ namespace games { } current_client = this; + x1 = window_width / 5.0f; + x2 = window_width / 2.0f; + x3 = 4.0f * window_width / 5.0f; + // sound for(auto & str : scale_files){ scales.push_back(Scale::load_from_file(str)); @@ -123,13 +159,20 @@ namespace games { chords.notes.emplace_back(note_type::kWholeNote, note_info{100.0f, 100.0f}); + add_drum(); + + alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); + alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f); + ALfloat atup[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f}; + alListenerfv(AL_ORIENTATION, atup); + // simulation sim.bounds.xmax = window_width_; sim.bounds.ymax = window_height_; - beat.notes.emplace_back(note_type::kQuarterNote, note_info{window_width / 5.0f, 10.0f}); - beat.notes.emplace_back(note_type::kWholeNote, note_info{window_width / 2.0f, 10.0f}); - beat.notes.emplace_back(note_type::kHalfNote, note_info{4.0f * window_width / 5.0f, 10.0f}); + beat.notes.emplace_back(note_type::kQuarterNote, note_info{x1, 10.0f}); + beat.notes.emplace_back(note_type::kWholeNote, note_info{x2, 10.0f}); + beat.notes.emplace_back(note_type::kHalfNote, note_info{x3, 10.0f}); // websockets lws_context_creation_info info; @@ -144,6 +187,7 @@ namespace games { if(!context) throw std::runtime_error("context could not be created."); std::string address = "127.0.0.1"; + // std::string address = "vadovas-studios.com"; wsi = libwebsocket_client_connect(context, address.c_str(), 7681, false, "/", address.c_str(), "origin", "uberclient", -1); if(!wsi) throw std::runtime_error("socket could not be created."); @@ -194,8 +238,25 @@ namespace games { } if(input.keys_went_down[SDLK_l]){ + bool restore_drum = false; + if(kick || snare){ + remove_drum(); + restore_drum = true; + } + lines.clear(); sim.lines.clear(); + + if(restore_drum) add_drum(); + lines_changed = true; + } + + if(input.keys_went_down[SDLK_d]){ + if(kick || snare){ + remove_drum(); + } else { + add_drum(); + } lines_changed = true; } @@ -242,9 +303,15 @@ namespace games { // simulation for(auto l : lines){ l->update(dt2); + if(kick) kick->update(dt2); + if(snare) snare->update(dt2); } for(auto x : sim.update(sim_speed * dt2)){ + float phi = x.ball.position.x / window_width - 0.5f; + float xpos = sin(M_PI * phi); + float zpos = cos(M_PI * phi); + x.line.information->sound->set_position({2.0f*xpos, 0.0f, 1.0f*zpos}); x.line.information->play(); } diff --git a/src/client/client.hpp b/src/client/client.hpp index 1dd73a6..7499c6f 100644 --- a/src/client/client.hpp +++ b/src/client/client.hpp @@ -21,6 +21,7 @@ q, w, e: music speed (= generation of balls) z, x, c: simulation speed a: toggle autopilot + d: toggle drum p: pause r: reload scales (aka chords) s: reload shaders @@ -41,6 +42,8 @@ namespace games { // simulation part int balluid{0}; std::vector> lines; + std::shared_ptr kick; + std::shared_ptr snare; simu_type sim; beat_type beat; @@ -52,6 +55,13 @@ namespace games { void add_line(cheap_line_type const & line); void remove_line(int ID); + void add_drum(); + void remove_drum(); + + float x1; + float x2; + float x3; + // Sound part const std::vector scale_files{ ::bundle.get_sound_path() + "pentatonic.txt", // 0 a @@ -78,7 +88,9 @@ namespace games { const std::vector> sounds{ resource_cache.get_sound_buffer(::bundle.get_sound_path() + "guitar440.wav"), - resource_cache.get_sound_buffer(::bundle.get_sound_path() + "marimba.wav") + resource_cache.get_sound_buffer(::bundle.get_sound_path() + "marimba.wav"), + resource_cache.get_sound_buffer(::bundle.get_sound_path() + "kick.wav"), + resource_cache.get_sound_buffer(::bundle.get_sound_path() + "snare.wav") }; float music_speed{1.0};