Skip to content

Commit 7621116

Browse files
authored
Merge pull request #2 from Hugo-RM/jaime's-updates
addition of audio the project
2 parents bf3d48e + 04fde3d commit 7621116

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ set(SOURCES
5252
src/meowstro.cpp
5353
src/RenderWindow.cpp
5454
src/Entity.cpp
55-
)
55+
src/Audio.cpp
5656

5757
# Define Header Files
5858
set(HEADERS
5959
include/RenderWindow.hpp
6060
include/Entity.hpp
61+
include/Audio.hpp
6162
)
6263

6364
# Ensure at least one source file exists

assets/audio/mymarch.mp3

3.21 MB
Binary file not shown.

include/Audio.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <SDL.h>
4+
#include <SDL_mixer.h>
5+
6+
class Audio {
7+
private:
8+
std::string myaudio_name;
9+
public:
10+
Audio();
11+
Audio(std::string myaudio_name);
12+
int AudioPlayer(std::string path_of_audio);
13+
};

src/Audio.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "Audio.hpp"
2+
#include <iostream>
3+
Audio::Audio() {
4+
myaudio_name = "";
5+
}
6+
Audio::Audio(std::string myaudio_name) {
7+
this->myaudio_name = myaudio_name;
8+
}
9+
int Audio::AudioPlayer(std::string path_of_audio) {
10+
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
11+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL initialization failed: %s\n", SDL_GetError());
12+
return 1;
13+
}
14+
15+
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
16+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_mixer initialization failed: %s\n", Mix_GetError());
17+
//SDL_Quit();
18+
return 1;
19+
}
20+
21+
Mix_Music* music = Mix_LoadMUS(path_of_audio.c_str()); // Replace with your music file
22+
if (music == nullptr) {
23+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load music: %s\n", Mix_GetError());
24+
Mix_Quit();
25+
//SDL_Quit();
26+
return 1;
27+
}
28+
29+
std::cout << "Playing music..." << std::endl;
30+
if (Mix_PlayMusic(music, -1) == -1) {
31+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play music: %s\n", Mix_GetError());
32+
Mix_FreeMusic(music);
33+
Mix_Quit();
34+
//SDL_Quit();
35+
return 1;
36+
}
37+
38+
// Keep the program running for a while (replace with your game loop or main program logic)
39+
SDL_Delay(210000); // Play for 10 seconds
40+
41+
//std::cout << "Fading out music..." << std::endl;
42+
//Mix_FadeOutMusic(2000);
43+
//SDL_Delay(2000); // Wait for the fade out
44+
45+
Mix_FreeMusic(music);
46+
Mix_Quit();
47+
//SDL_Quit();
48+
return 0;
49+
}

0 commit comments

Comments
 (0)