|
| 1 | +#include "olcPixelGameEngine.h" |
| 2 | +#include "olcPGEX_MiniAudio.h" |
| 3 | +#include <cmath> |
| 4 | + |
| 5 | +constexpr float thirtyFramesPerSecond = 1.0f / 30.f; |
| 6 | + |
| 7 | +class Demo : public olc::PixelGameEngine |
| 8 | +{ |
| 9 | +public: |
| 10 | + Demo() |
| 11 | + { |
| 12 | + sAppName = "Demo MiniAudio"; |
| 13 | + } |
| 14 | + |
| 15 | +public: |
| 16 | + bool OnUserCreate() override |
| 17 | + { |
| 18 | + ma.SetBackgroundPlay(true); |
| 19 | + |
| 20 | + song1 = ma.LoadSound("assets/sounds/song1.mp3"); |
| 21 | + ma_sound_set_position(ma.GetSound(song1), 0.0f, 0.0f, 0.0f); |
| 22 | + ma_sound_set_attenuation_model(ma.GetSound(song1), ma_attenuation_model_linear); |
| 23 | + ma_sound_set_min_distance(ma.GetSound(song1), 0.0f); |
| 24 | + ma_sound_set_max_distance(ma.GetSound(song1), 20.0f); |
| 25 | + ma_sound_set_max_gain(ma.GetSound(song1), 1.0f); |
| 26 | + ma_sound_set_min_gain(ma.GetSound(song1), 0.0f); |
| 27 | + |
| 28 | + centerScreen = GetScreenSize() / 2; |
| 29 | + return true; |
| 30 | + } |
| 31 | + |
| 32 | + olc::vf2d position{0.0f, 0.0f}; |
| 33 | + float direction = 0.0f; |
| 34 | + olc::vf2d centerScreen; |
| 35 | + |
| 36 | + bool OnUserUpdate(float fElapsedTime) override |
| 37 | + { |
| 38 | + fElapsedTime = (fElapsedTime > thirtyFramesPerSecond) ? thirtyFramesPerSecond : fElapsedTime; |
| 39 | + |
| 40 | + if(GetKey(olc::SPACE).bPressed) |
| 41 | + { |
| 42 | + ma.Toggle(song1); |
| 43 | + } |
| 44 | + |
| 45 | + float forwardVelocity = 0.0f; |
| 46 | + float rotationVelocity = 0.0f; |
| 47 | + |
| 48 | + if(GetKey(olc::W).bHeld || GetKey(olc::UP).bHeld) forwardVelocity += 1.0f; |
| 49 | + if(GetKey(olc::S).bHeld || GetKey(olc::DOWN).bHeld) forwardVelocity -= 1.0f; |
| 50 | + if(GetKey(olc::A).bHeld || GetKey(olc::LEFT).bHeld) rotationVelocity -= 1.0f; |
| 51 | + if(GetKey(olc::D).bHeld || GetKey(olc::RIGHT).bHeld) rotationVelocity += 1.0f; |
| 52 | + |
| 53 | + direction += rotationVelocity * 5.0f * fElapsedTime; |
| 54 | + olc::vf2d directionVector = olc::vf2d{ cosf(direction), sinf(direction) }; |
| 55 | + olc::vf2d velocity = directionVector * forwardVelocity * 20.0f * fElapsedTime; |
| 56 | + position += velocity; |
| 57 | + |
| 58 | + ma_engine_listener_set_direction(ma.GetEngine(), 0, directionVector.x, 0.0f, directionVector.y); |
| 59 | + ma_engine_listener_set_position(ma.GetEngine(), 0, position.x, 0.0f, position.y); |
| 60 | + ma_engine_listener_set_velocity(ma.GetEngine(), 0, velocity.x, 0.0f, velocity.y); |
| 61 | + |
| 62 | + Clear(olc::BLACK); |
| 63 | + |
| 64 | + DrawCircle(centerScreen, 5, olc::YELLOW); |
| 65 | + DrawCircle(centerScreen, 15, olc::MAGENTA); |
| 66 | + |
| 67 | + DrawCircle(centerScreen + position, 5, olc::WHITE); |
| 68 | + DrawLine(centerScreen + position, centerScreen + position + (directionVector * 5), olc::WHITE); |
| 69 | + |
| 70 | + |
| 71 | + DrawStringDecal({5, 5}, \ |
| 72 | + "-------- INFO --- CONTROLS -\n" "\n" |
| 73 | + "Forward/Backward (" + std::to_string(forwardVelocity) + ") W,S or UP,DOWN\n" "\n" |
| 74 | + "Rotation (" + std::to_string(direction) + ") A,D or LEFT,RIGHT\n" "\n" |
| 75 | + "Position " + position.str() + "\n" "\n" |
| 76 | + "Position Mag (" + std::to_string(position.mag()) + ")\n" "\n" |
| 77 | + "Toggle Sound (" + (ma.IsPlaying(song1) ? "Playing) " : "Not Playing)") + " SPACE\n" |
| 78 | + , |
| 79 | + olc::WHITE, {0.5f, 0.5f}); |
| 80 | + |
| 81 | + DrawStringDecal({5, 160}, \ |
| 82 | + "Music: Joy Ride [Full version] by MusicLFiles\n" |
| 83 | + "Free download: https://filmmusic.io/song/11627-joy-ride-full-version\n" |
| 84 | + "Licensed under CC BY 4.0: https://filmmusic.io/standard-license\n", \ |
| 85 | + olc::WHITE, {0.5f, 0.5f}); |
| 86 | + |
| 87 | + #if defined(__EMSCRIPTEN__) |
| 88 | + return true; |
| 89 | + #else |
| 90 | + return !GetKey(olc::ESCAPE).bPressed; |
| 91 | + #endif |
| 92 | + } |
| 93 | + |
| 94 | + olc::MiniAudio ma; |
| 95 | + int song1; |
| 96 | +}; |
| 97 | + |
| 98 | +int main() |
| 99 | +{ |
| 100 | + Demo demo; |
| 101 | + if (demo.Construct(320, 180, 4, 4)) |
| 102 | + demo.Start(); |
| 103 | + return 0; |
| 104 | +} |
0 commit comments