-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic.c
More file actions
62 lines (45 loc) · 1.25 KB
/
music.c
File metadata and controls
62 lines (45 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <SDL.h>
#include <SDL_mixer.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
/*
makefile example:
- gcc music.c -o music `sdl2-config --cflags --libs` -lSDL2_mixer
安裝 SDL2 和 SDL_mixer
1.Ubuntu / Debian:
sudo apt install libsdl2-dev libsdl2-mixer-dev
2.macOS (使用 Homebrew):
brew install sdl2 sdl2_mixer
*/
void handle_sigint(int sig) {
printf("\n收到 Ctrl+C,中止程式...\n");
exit(0);
}
int main(int argc, char* argv[]) {
signal(SIGINT, handle_sigint);
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError());
return 1;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
fprintf(stderr, "Mix_OpenAudio error: %s\n", Mix_GetError());
return 1;
}
Mix_Music* bgm = Mix_LoadMUS("music.mp3");
if (!bgm) {
fprintf(stderr, "Load BGM error: %s\n", Mix_GetError());
return 1;
}
// 主邏輯:模擬遊戲主迴圈
for (int i = 0; i < 10; i++) {
printf("遊戲進行中 %d...\n", i);
SDL_Delay(1000); // 假裝每秒進行一次遊戲邏輯
}
// 結束時釋放音樂
Mix_HaltMusic();
Mix_FreeMusic(bgm);
Mix_CloseAudio();
SDL_Quit();
return 0;
}