Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

115 rindas
2.7 KiB

  1. //
  2. // Lol Engine — Sound tutorial
  3. //
  4. // Copyright © 2011—2016 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include <functional>
  17. using namespace lol;
  18. class sound_demo : public WorldEntity
  19. {
  20. public:
  21. sound_demo()
  22. {
  23. for (auto &val : m_streams)
  24. val = -1;
  25. m_controller = new Controller("Default");
  26. m_profile << InputProfile::Keyboard(0, "Space")
  27. << InputProfile::MouseKey(1, "Left");
  28. m_controller->Init(m_profile);
  29. m_mouse = InputDevice::GetMouse();
  30. m_text = new Text("SPACE for sine wave, Left Click for white noise",
  31. "data/font/ascii.png");
  32. m_text->SetPos(vec3(5, 5, 1));
  33. Ticker::Ref(m_text);
  34. }
  35. ~sound_demo()
  36. {
  37. Ticker::Unref(m_text);
  38. }
  39. void synth(int mode, void *buf, int bytes)
  40. {
  41. uint16_t *stream = (uint16_t *)buf;
  42. for (int i = 0; i < bytes / 2; ++i)
  43. {
  44. switch (mode)
  45. {
  46. case 0: // sine wave
  47. stream[i] = 400 * lol::sin(12 * i * F_TAU / bytes);
  48. break;
  49. case 1: // white noise
  50. stream[i] = lol::rand(-120, 120);
  51. break;
  52. }
  53. }
  54. }
  55. virtual void TickGame(float seconds)
  56. {
  57. WorldEntity::TickGame(seconds);
  58. for (int i = 0; i < 2; ++i)
  59. {
  60. if (!m_controller->WasKeyPressedThisFrame(i))
  61. continue;
  62. if (m_streams[i] < 0)
  63. {
  64. auto f = std::bind(&sound_demo::synth, this, i,
  65. std::placeholders::_1,
  66. std::placeholders::_2);
  67. m_streams[i] = audio::start_streaming(f);
  68. }
  69. else
  70. {
  71. audio::stop_streaming(m_streams[i]);
  72. m_streams[i] = -1;
  73. }
  74. }
  75. }
  76. virtual void TickDraw(float seconds, Scene &scene)
  77. {
  78. WorldEntity::TickDraw(seconds, scene);
  79. }
  80. private:
  81. int m_streams[2];
  82. InputDevice *m_mouse;
  83. Controller *m_controller;
  84. InputProfile m_profile;
  85. Text *m_text;
  86. };
  87. int main(int argc, char **argv)
  88. {
  89. sys::init(argc, argv);
  90. Application app("Tutorial 9: Sound", ivec2(640, 480), 60.0f);
  91. new sound_demo();
  92. app.Run();
  93. return EXIT_SUCCESS;
  94. }