- Música i só amb SDL_mixer
This commit is contained in:
67
mini.cpp
67
mini.cpp
@@ -3,8 +3,7 @@
|
||||
#include <string.h>
|
||||
#include "lua.h"
|
||||
#include "gif.c"
|
||||
|
||||
#include "chirp.h"
|
||||
#include "SDL2/SDL_mixer.h"
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
@@ -75,6 +74,10 @@ Uint8 key_just_pressed = 0;
|
||||
int mouse_x, mouse_y, mouse_wheel;
|
||||
Uint32 mouse_buttons;
|
||||
|
||||
#define MAX_SOUNDS 50
|
||||
Mix_Music *music = NULL;
|
||||
Mix_Chunk *sounds[MAX_SOUNDS];
|
||||
|
||||
char* get_value_from_line(char* line) {
|
||||
char* equal_character = strchr(line, '=');
|
||||
if (equal_character == NULL) return NULL;
|
||||
@@ -122,10 +125,13 @@ void reinit() {
|
||||
}
|
||||
|
||||
void initaudio() {
|
||||
chirp_init();
|
||||
Mix_OpenAudio(44100, AUDIO_S16, 1, 512);
|
||||
for (int i=0;i<MAX_SOUNDS;++i) sounds[i] = NULL;
|
||||
}
|
||||
void quitaudio() {
|
||||
|
||||
if (music != NULL) Mix_FreeMusic(music);
|
||||
for (int i=0;i<MAX_SOUNDS;++i) if (sounds[i]!=NULL) Mix_FreeChunk(sounds[i]);
|
||||
Mix_CloseAudio();
|
||||
}
|
||||
|
||||
int scrw() {
|
||||
@@ -202,6 +208,8 @@ int main(int argc,char*argv[]){
|
||||
SDL_ShowCursor(show_cursor);
|
||||
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height);
|
||||
|
||||
Mix_Init(MIX_INIT_OGG);
|
||||
|
||||
SDL_Event mini_eve;
|
||||
|
||||
reinit();
|
||||
@@ -219,7 +227,6 @@ int main(int argc,char*argv[]){
|
||||
if (mini_eve.type == SDL_KEYDOWN) {
|
||||
#ifdef DEBUG
|
||||
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F1) {
|
||||
chirp_stop();
|
||||
if (lua_is_playing()) {
|
||||
lua_quit();
|
||||
quitaudio();
|
||||
@@ -228,7 +235,6 @@ int main(int argc,char*argv[]){
|
||||
should_exit=true;
|
||||
}
|
||||
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
||||
chirp_stop();
|
||||
should_exit=true;
|
||||
} else {
|
||||
key_just_pressed = mini_eve.key.keysym.scancode;
|
||||
@@ -262,6 +268,9 @@ int main(int argc,char*argv[]){
|
||||
}
|
||||
lua_quit();
|
||||
quitaudio();
|
||||
|
||||
Mix_Quit();
|
||||
|
||||
for (int i=0;i<10;++i) freesurf(i);
|
||||
SDL_DestroyTexture(mini_bak);
|
||||
SDL_DestroyRenderer(mini_ren);
|
||||
@@ -987,12 +996,50 @@ bool freadb() {
|
||||
return strcmp(fstr, "true")==0?true:false;
|
||||
}
|
||||
|
||||
void playchirp(const char *song) {
|
||||
chirp_play(song);
|
||||
void playmusic(const char *filename, const int volume) {
|
||||
int size;
|
||||
char *buffer = file_getfilebuffer(filename, size);
|
||||
if (music != NULL) Mix_FreeMusic(music);
|
||||
music = Mix_LoadMUS_RW(SDL_RWFromMem(buffer, size), 1);
|
||||
Mix_PlayMusic(music, -1);
|
||||
if (volume!=-1) Mix_VolumeMusic(volume);
|
||||
}
|
||||
|
||||
void stopchirp() {
|
||||
chirp_stop();
|
||||
void pausemusic() {
|
||||
Mix_PauseMusic();
|
||||
}
|
||||
|
||||
void resumemusic() {
|
||||
Mix_ResumeMusic();
|
||||
}
|
||||
|
||||
void stopmusic() {
|
||||
Mix_HaltMusic();
|
||||
}
|
||||
|
||||
int loadsound(const char *filename) {
|
||||
int size;
|
||||
char *buffer = file_getfilebuffer(filename, size);
|
||||
int i=0;
|
||||
while (i<MAX_SOUNDS && sounds[i]!=NULL) i++;
|
||||
if (i==MAX_SOUNDS) { i=0; Mix_FreeChunk(sounds[i]); }
|
||||
sounds[i]=Mix_LoadWAV_RW(SDL_RWFromMem(buffer, size), 1);
|
||||
return i;
|
||||
}
|
||||
|
||||
void freesound(int soundfile) {
|
||||
Mix_FreeChunk(sounds[soundfile]);
|
||||
sounds[soundfile] = NULL;
|
||||
}
|
||||
|
||||
int playsound(int soundfile, const int volume) {
|
||||
const int channel = Mix_PlayChannel(-1, sounds[soundfile], 0);
|
||||
if (volume!=-1) Mix_Volume(channel, volume);
|
||||
return channel;
|
||||
}
|
||||
|
||||
void stopsound(int soundchannel) {
|
||||
Mix_HaltChannel(soundchannel);
|
||||
}
|
||||
|
||||
void exit() {
|
||||
|
||||
Reference in New Issue
Block a user