Douglas A пре 1 година
родитељ
комит
3ec7398bbd
6 измењених фајлова са 266 додато и 314 уклоњено
  1. 11 21
      src/App.cpp
  2. 9 11
      src/App.h
  3. 73 83
      src/Audio.cpp
  4. 34 37
      src/Audio.h
  5. 110 131
      src/BaseWindow.cpp
  6. 29 31
      src/BaseWindow.h

+ 11 - 21
src/App.cpp

@@ -1,29 +1,19 @@
 #include "App.h"
-#include "Audio.h"
-#include "BaseWindow.h"
 
-#include <memory>
 #include <iostream>
+#include <memory>
 
-App::App()
-{
-    
-    m_audio = std::make_unique<Audio>();
-    m_window = std::make_unique<BaseWindow>(
-        800, 600, "Music", grey, m_audio.get()
-    );
-
-
-    //BaseWindow *base = new BaseWindow(800, 600, "Music", grey, *m_audio);
-    //base->run();
+#include "Audio.h"
+#include "BaseWindow.h"
 
-    //delete base;
- //   m_window = std::make_unique<BaseWindow>(BaseWindow(800, 600, "Music", grey, *m_audio));
+App::App() {
+  m_audio = std::make_unique<Audio>();
+  m_window =
+      std::make_unique<BaseWindow>(800, 600, "Music", grey, m_audio.get());
 }
 
-void App::Run()
-{
-    if (m_window->run()) {
-        std::cout << "Something wrong happened. Quitting.\n";
-    }
+void App::Run() {
+  if (m_window->run()) {
+    std::cout << "Something wrong happened. Quitting.\n";
+  }
 }

+ 9 - 11
src/App.h

@@ -1,19 +1,17 @@
 #pragma once
-#include "BaseWindow.h"
-#include "Audio.h"
-
 #include <memory>
 
-static Color grey = {0.45, 0.45, 0.45, 1.0};
+#include "Audio.h"
+#include "BaseWindow.h"
 
+static Color grey = {0.45, 0.45, 0.45, 1.0};
 
 class App {
+ private:
+  std::unique_ptr<BaseWindow> m_window;
+  std::unique_ptr<Audio> m_audio;
 
-    private:
-    std::unique_ptr<BaseWindow> m_window;
-    std::unique_ptr<Audio> m_audio;
-
-    public:
-    App();
-    void Run();
+ public:
+  App();
+  void Run();
 };

+ 73 - 83
src/Audio.cpp

@@ -3,113 +3,103 @@
 #define MINIAUDIO_IMPLEMENTATION
 #include <miniaudio/miniaudio.h>
 
-#include <iostream>
 #include <filesystem>
+#include <iostream>
 
-void Audio::data_callback(ma_device *device, void *output, const void *input, ma_uint32 frame_count)
-{
-    DecoderState* decoder = (DecoderState*)device->pUserData;
-    if(decoder == NULL) {
-        std::cout << "could not find suitable decoder\n";
-        return;
-    }
-
-    ma_result result = ma_decoder_read_pcm_frames(&decoder->decoder, output, frame_count, NULL);
-    decoder->is_finished = false;
-    if (result == MA_AT_END) {
-        std::cout << "end of the song... next.\n";
-        decoder->is_finished = true;
-    }
+void Audio::data_callback(ma_device *device, void *output, const void *input,
+                          ma_uint32 frame_count) {
+  DecoderState *decoder = (DecoderState *)device->pUserData;
+  if (decoder == NULL) {
+    std::cout << "could not find suitable decoder\n";
+    return;
+  }
+
+  ma_result result =
+      ma_decoder_read_pcm_frames(&decoder->decoder, output, frame_count, NULL);
+  decoder->is_finished = false;
+  if (result == MA_AT_END) {
+    std::cout << "end of the song... next.\n";
+    decoder->is_finished = true;
+  }
 }
 
-Audio::Audio()
-{
-
-    m_device_config = ma_device_config_init(ma_device_type_playback);
-    m_device_config.dataCallback = data_callback;
+Audio::Audio() {
+  m_device_config = ma_device_config_init(ma_device_type_playback);
+  m_device_config.dataCallback = data_callback;
 }
 
-Audio::~Audio()
-{
-    ma_device_uninit(&m_device);
-    ma_decoder_uninit(&m_decoder.decoder);
-    return;
+Audio::~Audio() {
+  ma_device_uninit(&m_device);
+  ma_decoder_uninit(&m_decoder.decoder);
+  return;
 }
 
-void Audio::load_file(std::filesystem::path file_path)
-{
-    m_loaded_files.push_back(file_path);
-    std::cout << "Loaded file: " << file_path.string() << "\n";
-
+void Audio::load_file(std::filesystem::path file_path) {
+  m_loaded_files.push_back(file_path);
+  std::cout << "Loaded file: " << file_path.string() << "\n";
 }
 
+void Audio::play() {
+  if (is_playing()) {
+    stop();
+  }
 
-void Audio::play()
-{
-    if (is_playing()) {
-        stop();
-    }
-       
+  m_selected_file = m_loaded_files[m_selected_file_idx];
 
-    m_selected_file = m_loaded_files[m_selected_file_idx];
-
-    ma_result result = ma_decoder_init_file(m_selected_file.string().c_str(), NULL, &m_decoder.decoder);
-    if (result != MA_SUCCESS) {
-        std::cout << "could not load file: " << m_selected_file.string().c_str() << "\n";
-        return;
-    }
+  ma_result result = ma_decoder_init_file(m_selected_file.string().c_str(),
+                                          NULL, &m_decoder.decoder);
+  if (result != MA_SUCCESS) {
+    std::cout << "could not load file: " << m_selected_file.string().c_str()
+              << "\n";
+    return;
+  }
 
-    m_device_config.playback.format = m_decoder.decoder.outputFormat;
-    m_device_config.playback.channels = m_decoder.decoder.outputChannels;
-    m_device_config.sampleRate = m_decoder.decoder.outputSampleRate;
-    m_device_config.pUserData = &m_decoder;
+  m_device_config.playback.format = m_decoder.decoder.outputFormat;
+  m_device_config.playback.channels = m_decoder.decoder.outputChannels;
+  m_device_config.sampleRate = m_decoder.decoder.outputSampleRate;
+  m_device_config.pUserData = &m_decoder;
 
-    if (ma_device_init(NULL, &m_device_config, &m_device) != MA_SUCCESS) {
-        std::cout << "failed to open playback device.\n";
-        return;
-    }
+  if (ma_device_init(NULL, &m_device_config, &m_device) != MA_SUCCESS) {
+    std::cout << "failed to open playback device.\n";
+    return;
+  }
 
-    if (ma_device_start(&m_device) != MA_SUCCESS) {
-        std::cout << "could not start playback device" << "\n";
-        return;
-    }
+  if (ma_device_start(&m_device) != MA_SUCCESS) {
+    std::cout << "could not start playback device"
+              << "\n";
+    return;
+  }
 
-    m_is_playing = true;
+  m_is_playing = true;
 }
 
-void Audio::stop()
-{
-    if (is_playing()) {
-        ma_device_uninit(&m_device);
-        ma_decoder_uninit(&m_decoder.decoder);
-        m_is_playing = false;
-    }
+void Audio::stop() {
+  if (is_playing()) {
+    ma_device_uninit(&m_device);
+    ma_decoder_uninit(&m_decoder.decoder);
+    m_is_playing = false;
+  }
 }
 
-void Audio::select_file(std::filesystem::path file)
-{
-    
-    for (size_t i = 0; i < m_loaded_files.size(); i++) {
-        if (file == m_loaded_files[i]) {
-            m_selected_file = file;
-            m_selected_file_idx = i;
-        }
+void Audio::select_file(std::filesystem::path file) {
+  for (size_t i = 0; i < m_loaded_files.size(); i++) {
+    if (file == m_loaded_files[i]) {
+      m_selected_file = file;
+      m_selected_file_idx = i;
     }
-    //m_select_file_idx = m_loaded_files
+  }
+  // m_select_file_idx = m_loaded_files
 }
 
-void Audio::next_music()
-{
-    m_selected_file_idx += 1;
-    if (m_loaded_files.size() <= m_selected_file_idx) {
-        m_selected_file_idx = 0;
-    }
+void Audio::next_music() {
+  m_selected_file_idx += 1;
+  if (m_loaded_files.size() <= m_selected_file_idx) {
+    m_selected_file_idx = 0;
+  }
 
-    if (is_playing())
-        play();
+  if (is_playing()) play();
 }
 
-std::vector<std::filesystem::path> Audio::loaded_files()
-{
-    return m_loaded_files;
+std::vector<std::filesystem::path> Audio::loaded_files() {
+  return m_loaded_files;
 }

+ 34 - 37
src/Audio.h

@@ -1,46 +1,43 @@
 #pragma once
 
-#include <vector>
-#include <filesystem>
-
-
 #include <miniaudio/miniaudio.h>
 
+#include <filesystem>
+#include <vector>
+
 struct DecoderState {
-    ma_decoder decoder{};
-    bool is_finished = false;
-    ma_decoder_config m_decoder_config{};
+  ma_decoder decoder{};
+  bool is_finished = false;
+  ma_decoder_config m_decoder_config{};
 };
 
 class Audio {
-    
-private:
-    ma_device m_device{};
-    ma_device_config m_device_config{};
-
-    DecoderState m_decoder{};
-
-
-    std::vector<std::filesystem::path> m_loaded_files = std::vector<std::filesystem::path>();
-    std::filesystem::path m_selected_file = std::filesystem::path();
-    size_t m_selected_file_idx = 0;
-    bool m_is_playing = false;
-
-    static void data_callback(ma_device* device, void* output, const void* input, ma_uint32 frame_count);
-
-public:
-    Audio();
-    ~Audio();
-
-    void load_file(std::filesystem::path file_path);
-    void play();
-    void stop();
-    void select_file(std::filesystem::path file);
-    bool is_playing() { return m_is_playing; }
-    void next_music();
-    DecoderState decoder() { return m_decoder; }
-
-
-
-    std::vector<std::filesystem::path> loaded_files();
+ private:
+  ma_device m_device{};
+  ma_device_config m_device_config{};
+
+  DecoderState m_decoder{};
+
+  std::vector<std::filesystem::path> m_loaded_files =
+      std::vector<std::filesystem::path>();
+  std::filesystem::path m_selected_file = std::filesystem::path();
+  size_t m_selected_file_idx = 0;
+  bool m_is_playing = false;
+
+  static void data_callback(ma_device *device, void *output, const void *input,
+                            ma_uint32 frame_count);
+
+ public:
+  Audio();
+  ~Audio();
+
+  void load_file(std::filesystem::path file_path);
+  void play();
+  void stop();
+  void select_file(std::filesystem::path file);
+  bool is_playing() { return m_is_playing; }
+  void next_music();
+  DecoderState decoder() { return m_decoder; }
+
+  std::vector<std::filesystem::path> loaded_files();
 };

+ 110 - 131
src/BaseWindow.cpp

@@ -1,154 +1,133 @@
 
 #include "BaseWindow.h"
+
 #include <imfilebrowser.h>
 
 #include <iostream>
 
-void window_resize_callback(GLFWwindow *, int width, int height)
-{
-    glViewport(0, 0, width, height);
+void window_resize_callback(GLFWwindow *, int width, int height) {
+  glViewport(0, 0, width, height);
 }
 
-BaseWindow::BaseWindow(int width, int height, const char* title, Color clear_color, Audio* audio_handler)
-    :   m_clear_color(clear_color),
-        m_window_width(width),
-        m_window_height(height),
-        m_window_title(title),
-        m_audio_handler(audio_handler)
-    {
-
-
-
-          std::cout << "Hello from constructor\n";
-
-    }
-
-BaseWindow::~BaseWindow()
-{
-    ImGui_ImplOpenGL3_Shutdown();
-    ImGui_ImplGlfw_Shutdown();
-    ImGui::DestroyContext();
-    glfwDestroyWindow(m_window);
-    glfwTerminate();
+BaseWindow::BaseWindow(int width, int height, const char *title,
+                       Color clear_color, Audio *audio_handler)
+    : m_clear_color(clear_color),
+      m_window_width(width),
+      m_window_height(height),
+      m_window_title(title),
+      m_audio_handler(audio_handler) {}
+
+BaseWindow::~BaseWindow() {
+  ImGui_ImplOpenGL3_Shutdown();
+  ImGui_ImplGlfw_Shutdown();
+  ImGui::DestroyContext();
+  glfwDestroyWindow(m_window);
+  glfwTerminate();
 }
 
-int BaseWindow::run()
-{
-    glfwInit();
-
-    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
-    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
-    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
+int BaseWindow::run() {
+  glfwInit();
 
-    m_window = glfwCreateWindow(m_window_width, m_window_height, m_window_title, NULL, NULL);
-    if (m_window == NULL)
-    {
-        std::cout << "could not intialize GLFW Window"
-                  << "\n";
-        glfwTerminate();
-        return 1;
-    }
+  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
 
-    glfwMakeContextCurrent(m_window);
-
-    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
-    {
-        std::cout << "Failed to initialize GLAD" << std::endl;
-        return 1;
-    }
-
-    glfwSetFramebufferSizeCallback(m_window, window_resize_callback);
-    IMGUI_CHECKVERSION();
-    ImGui::CreateContext();
-    ImGuiIO &io = ImGui::GetIO();
-    (void)io;
-    ImGui::StyleColorsDark();
-    ImGui_ImplGlfw_InitForOpenGL(m_window, true);
-    ImGui_ImplOpenGL3_Init("#version 330");
-
-    ImGui::FileBrowser fileDialog;
-    fileDialog.SetTitle("title");
-    fileDialog.SetTypeFilters({".mp3"});
-
-    // Main game loop
-    while (!glfwWindowShouldClose(m_window))
+  m_window = glfwCreateWindow(m_window_width, m_window_height, m_window_title,
+                              NULL, NULL);
+  if (m_window == NULL) {
+    std::cout << "could not intialize GLFW Window"
+              << "\n";
+    glfwTerminate();
+    return 1;
+  }
+
+  glfwMakeContextCurrent(m_window);
+
+  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
+    std::cout << "Failed to initialize GLAD" << std::endl;
+    return 1;
+  }
+
+  glfwSetFramebufferSizeCallback(m_window, window_resize_callback);
+  IMGUI_CHECKVERSION();
+  ImGui::CreateContext();
+  ImGuiIO &io = ImGui::GetIO();
+  (void)io;
+  ImGui::StyleColorsDark();
+  ImGui_ImplGlfw_InitForOpenGL(m_window, true);
+  ImGui_ImplOpenGL3_Init("#version 330");
+
+  ImGui::FileBrowser fileDialog;
+  fileDialog.SetTitle("title");
+  fileDialog.SetTypeFilters({".mp3"});
+
+  // Main game loop
+  while (!glfwWindowShouldClose(m_window)) {
+    // Create new imgui frames
+    ImGui_ImplOpenGL3_NewFrame();
+    ImGui_ImplGlfw_NewFrame();
+    ImGui::NewFrame();
+
+    // Draw IMGUI
+    ImGui::Begin("Hello!");
     {
-
-        // Create new imgui frames
-        ImGui_ImplOpenGL3_NewFrame();
-        ImGui_ImplGlfw_NewFrame();
-        ImGui::NewFrame();
-
-        // Draw IMGUI
-        ImGui::Begin("Hello!");
-        {
-            if (ImGui::Button("Add Music"))
-            {
-                fileDialog.Open();
+      if (ImGui::Button("Add Music")) {
+        fileDialog.Open();
+      }
+
+      static std::filesystem::path selected;
+      if (m_audio_handler->loaded_files().size() > 0) {
+        if (ImGui::BeginListBox(
+                "##",
+                ImVec2(-FLT_MIN, 5 * ImGui::GetTextLineHeightWithSpacing()))) {
+          for (auto file : m_audio_handler->loaded_files()) {
+            bool is_selected = selected.compare(file);
+            if (ImGui::Selectable(file.string().c_str(), is_selected)) {
+              selected = file;
+              m_audio_handler->select_file(file);
             }
 
-            static std::filesystem::path selected;
-            if (m_audio_handler->loaded_files().size() > 0)
-            {
-                if (ImGui::BeginListBox("##", ImVec2(-FLT_MIN, 5 * ImGui::GetTextLineHeightWithSpacing())))
-                {
-
-                    for (auto file : m_audio_handler->loaded_files())
-                    {
-                        bool is_selected = selected.compare(file);
-                        if (ImGui::Selectable(file.string().c_str(), is_selected))
-                        {
-                            selected = file;
-                            m_audio_handler->select_file(file);
-                        }
-
-                        if (is_selected)
-                        {
-                            ImGui::SetItemDefaultFocus();
-                        }
-                    }
-                }
-                ImGui::EndListBox();
+            if (is_selected) {
+              ImGui::SetItemDefaultFocus();
             }
+          }
         }
-        ImGui::End();
-        fileDialog.Display();
-        if (fileDialog.HasSelected())
-        {
-            m_audio_handler->load_file(fileDialog.GetSelected());
-            m_audio_handler->select_file(fileDialog.GetSelected());
-            fileDialog.ClearSelected();
-        }
+        ImGui::EndListBox();
+      }
+    }
+    ImGui::End();
+    fileDialog.Display();
+    if (fileDialog.HasSelected()) {
+      m_audio_handler->load_file(fileDialog.GetSelected());
+      m_audio_handler->select_file(fileDialog.GetSelected());
+      fileDialog.ClearSelected();
+    }
 
-        ImGui::Begin("Player controls");
-        {
-            if (ImGui::Button("Play"))
-            {
-                m_audio_handler->play();
-            }
-            if (ImGui::Button("Stop"))
-            {
-                m_audio_handler->stop();
-            }
-        }
-        ImGui::End();
+    ImGui::Begin("Player controls");
+    {
+      if (ImGui::Button("Play")) {
+        m_audio_handler->play();
+      }
+      if (ImGui::Button("Stop")) {
+        m_audio_handler->stop();
+      }
+    }
+    ImGui::End();
 
-        if (m_audio_handler->decoder().is_finished)
-        {
-            m_audio_handler->next_music();
-        }
+    if (m_audio_handler->decoder().is_finished) {
+      m_audio_handler->next_music();
+    }
 
-        ImGui::Render();
+    ImGui::Render();
 
-        glClearColor(m_clear_color.r, m_clear_color.g, m_clear_color.b, m_clear_color.a);
-        glClear(GL_COLOR_BUFFER_BIT);
-        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
-        glfwSwapBuffers(m_window);
-        glfwPollEvents();
-    }
+    glClearColor(m_clear_color.r, m_clear_color.g, m_clear_color.b,
+                 m_clear_color.a);
+    glClear(GL_COLOR_BUFFER_BIT);
+    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+    glfwSwapBuffers(m_window);
+    glfwPollEvents();
+  }
 
-    // Unload and destroy
-    
-    return 0;
+  return 0;
 }

+ 29 - 31
src/BaseWindow.h

@@ -2,45 +2,43 @@
 
 #include <glad/glad.h>
 #include <GLFW/glfw3.h>
+
 #include <imgui.h>
 #include <imgui_impl_glfw.h>
 #include <imgui_impl_opengl3.h>
 
-
-
 #include "Audio.h"
 
-
 struct Color {
-    float r;
-    float g;
-    float b;
-    float a;
-
-    static Color color_from_short(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha) {
-        Color c;
-        c.r = r/256;
-        c.g = g/256;
-        c.b = b/256;
-        c.a = alpha/256;
-
-        return c;
-    }
+  float r;
+  float g;
+  float b;
+  float a;
+
+  static Color color_from_short(uint8_t r, uint8_t g, uint8_t b,
+                                uint8_t alpha) {
+    Color c;
+    c.r = r / 256.0f;
+    c.g = g / 256.0f;
+    c.b = b / 256.0f;
+    c.a = alpha / 256.0f;
+
+    return c;
+  }
 };
 
 class BaseWindow {
-
-    public:
-    BaseWindow(int width, int height, const char* title, Color clear_color, Audio* audio_handler)
-;
-    ~BaseWindow();
-    int run();
-
-    private:
-    Color m_clear_color = {0};
-    int m_window_width;
-    int m_window_height;
-    const char* m_window_title;
-    GLFWwindow* m_window{};
-    Audio* m_audio_handler;
+ public:
+  BaseWindow(int width, int height, const char *title, Color clear_color,
+             Audio *audio_handler);
+  ~BaseWindow();
+  int run();
+
+ private:
+  Color m_clear_color = {0};
+  int m_window_width;
+  int m_window_height;
+  const char *m_window_title;
+  GLFWwindow *m_window{};
+  Audio *m_audio_handler;
 };