sdlplayer refactoring

This commit is contained in:
doitux
2012-11-27 00:53:26 +01:00
parent e29b0de6f2
commit e76ecc8fc6
13 changed files with 117 additions and 125 deletions
+3 -2
View File
@@ -20,6 +20,7 @@
#include "configfile.h" #include "configfile.h"
#include "gametablestylereader.h" #include "gametablestylereader.h"
#include "gamelobbydialogimpl.h" #include "gamelobbydialogimpl.h"
#include "soundevents.h"
#include <iostream> #include <iostream>
@@ -82,7 +83,7 @@ void ChatTools::receiveMessage(QString playerName, QString message, bool pm)
tempMsg = QString("<span style=\"font-weight:bold; color:red;\">"+message+"</span>"); tempMsg = QString("<span style=\"font-weight:bold; color:red;\">"+message+"</span>");
//play beep sound only in INET-lobby-chat //play beep sound only in INET-lobby-chat
if(myLobby->isVisible() && myConfig->readConfigInt("PlayLobbyChatNotification")) { if(myLobby->isVisible() && myConfig->readConfigInt("PlayLobbyChatNotification")) {
myLobby->getMyW()->getMySDLPlayer()->playSound("lobbychatnotify",0); myLobby->getMyW()->getMySoundEventHandler()->playSound("lobbychatnotify",0);
} }
} else if(message.contains(myNick, Qt::CaseInsensitive)) { } else if(message.contains(myNick, Qt::CaseInsensitive)) {
@@ -92,7 +93,7 @@ void ChatTools::receiveMessage(QString playerName, QString message, bool pm)
//play beep sound only in INET-lobby-chat //play beep sound only in INET-lobby-chat
// TODO dont play when message is from yourself // TODO dont play when message is from yourself
if(myLobby->isVisible() && myConfig->readConfigInt("PlayLobbyChatNotification")) { if(myLobby->isVisible() && myConfig->readConfigInt("PlayLobbyChatNotification")) {
myLobby->getMyW()->getMySDLPlayer()->playSound("lobbychatnotify",0); myLobby->getMyW()->getMySoundEventHandler()->playSound("lobbychatnotify",0);
} }
} }
break; break;
@@ -27,6 +27,7 @@
#include "game_defs.h" #include "game_defs.h"
#include <net/socket_msg.h> #include <net/socket_msg.h>
#include "mymessagedialogimpl.h" #include "mymessagedialogimpl.h"
#include "soundevents.h"
using namespace std; using namespace std;
@@ -969,9 +970,9 @@ void gameLobbyDialogImpl::addConnectedPlayer(unsigned playerId, QString playerNa
if(this->isVisible() && inGame && myConfig->readConfigInt("PlayNetworkGameNotification")) { if(this->isVisible() && inGame && myConfig->readConfigInt("PlayNetworkGameNotification")) {
if(treeWidget_connectedPlayers->topLevelItemCount() < treeWidget_connectedPlayers->headerItem()->data(0, Qt::UserRole).toInt()) { if(treeWidget_connectedPlayers->topLevelItemCount() < treeWidget_connectedPlayers->headerItem()->data(0, Qt::UserRole).toInt()) {
myW->getMySDLPlayer()->playSound("playerconnected", 0); myW->getMySoundEventHandler()->playSound("playerconnected", 0);
} else { } else {
myW->getMySDLPlayer()->playSound("onlinegameready", 0); myW->getMySoundEventHandler()->playSound("onlinegameready", 0);
showAutoStartTimer(); showAutoStartTimer();
} }
} }
@@ -29,7 +29,6 @@
#include <gamedata.h> #include <gamedata.h>
#include "createinternetgamedialogimpl.h" #include "createinternetgamedialogimpl.h"
#include "sdlplayer.h"
#include "gametableimpl.h" #include "gametableimpl.h"
#include <map> #include <map>
+2 -7
View File
@@ -75,8 +75,7 @@ gameTableImpl::gameTableImpl(ConfigFile *c, QMainWindow *parent)
setupUi(this); setupUi(this);
//Sound //Sound
mySDLPlayer = new SDLPlayer(myConfig);
mySoundEventHandler = new SoundEvents(myConfig); mySoundEventHandler = new SoundEvents(myConfig);
// Init game table style // Init game table style
@@ -839,10 +838,6 @@ void gameTableImpl::applySettings(settingsDialogImpl* mySettingsDialog)
refreshGroupbox(); refreshGroupbox();
provideMyActions(); provideMyActions();
} }
// Re-init audio.
mySDLPlayer->initAudio();
mySDLPlayer->closeAudio();
} }
void gameTableImpl::initGui(int speed) void gameTableImpl::initGui(int speed)
@@ -1169,7 +1164,7 @@ void gameTableImpl::refreshAction(int playerID, int playerAction)
//play sounds if exist //play sounds if exist
if(myConfig->readConfigInt("PlayGameActions")) if(myConfig->readConfigInt("PlayGameActions"))
mySDLPlayer->playSound(actionArray[playerAction].toStdString(), playerID); mySoundEventHandler->playSound(actionArray[playerAction].toStdString(), playerID);
} }
if (playerAction == 1) { // FOLD if (playerAction == 1) { // FOLD
-6
View File
@@ -53,8 +53,6 @@ class myMessageDialogImpl;
class GameTableStyleReader; class GameTableStyleReader;
class CardDeckStyleReader; class CardDeckStyleReader;
class SDLPlayer;
class SoundEvents; class SoundEvents;
enum SeatState { SEAT_UNDEFINED, SEAT_ACTIVE, SEAT_AUTOFOLD, SEAT_STAYONTABLE, SEAT_CLEAR }; enum SeatState { SEAT_UNDEFINED, SEAT_ACTIVE, SEAT_AUTOFOLD, SEAT_STAYONTABLE, SEAT_CLEAR };
@@ -70,9 +68,6 @@ public:
boost::shared_ptr<Session> getSession(); boost::shared_ptr<Session> getSession();
SDLPlayer* getMySDLPlayer() const {
return mySDLPlayer;
}
SoundEvents* getMySoundEventHandler() const { SoundEvents* getMySoundEventHandler() const {
return mySoundEventHandler; return mySoundEventHandler;
} }
@@ -427,7 +422,6 @@ private:
myMessageDialogImpl *myUniversalMessageDialog; myMessageDialogImpl *myUniversalMessageDialog;
//Sound //Sound
SDLPlayer *mySDLPlayer;
SoundEvents *mySoundEventHandler; SoundEvents *mySoundEventHandler;
QString myAppDataPath; QString myAppDataPath;
+2 -1
View File
@@ -17,6 +17,7 @@
*****************************************************************************/ *****************************************************************************/
#include "mytimeoutlabel.h" #include "mytimeoutlabel.h"
#include "gametableimpl.h" #include "gametableimpl.h"
#include "soundevents.h"
using namespace std; using namespace std;
@@ -95,7 +96,7 @@ void MyTimeoutLabel::nextTimeOutAnimationFrame()
if(timeOutFrame > waitFrames) { if(timeOutFrame > waitFrames) {
//play beep after waitFrames one time //play beep after waitFrames one time
if(isBeep && !isBeepPlayed) { if(isBeep && !isBeepPlayed) {
myW->getMySDLPlayer()->playSound("yourturn",0); myW->getMySoundEventHandler()->playSound("yourturn",0);
isBeepPlayed = TRUE; isBeepPlayed = TRUE;
} }
//save gfx ressources and never play more the 10 pps //save gfx ressources and never play more the 10 pps
-1
View File
@@ -23,7 +23,6 @@
#include <QtGui> #include <QtGui>
#include <QtCore> #include <QtCore>
#include <third_party/boost/timers.hpp> #include <third_party/boost/timers.hpp>
#include "sdlplayer.h"
class gameTableImpl; class gameTableImpl;
+91 -97
View File
@@ -29,11 +29,13 @@ using namespace std;
SDLPlayer::SDLPlayer(ConfigFile *c) SDLPlayer::SDLPlayer(ConfigFile *c)
#ifndef ANDROID #ifndef ANDROID
//: sound(NULL), soundData(NULL), currentChannel(0) , audioEnabled(0), myConfig(c) : sound(NULL), soundData(NULL), currentChannel(0), audioEnabled(FALSE), myConfig(c)
#endif #endif
{ {
#ifndef ANDROID #ifndef ANDROID
// myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str()); myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
SDL_Init(SDL_INIT_AUDIO);
initAudio();
#endif #endif
} }
@@ -42,128 +44,120 @@ SDLPlayer::~SDLPlayer()
{ {
#ifndef ANDROID #ifndef ANDROID
closeAudio(); closeAudio();
SDL_Quit();
#endif #endif
} }
void SDLPlayer::initAudio() void SDLPlayer::initAudio()
{ {
#ifndef ANDROID #ifndef ANDROID
SDL_Init(SDL_INIT_AUDIO); if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
qDebug() << "SDL_Init()"; int audio_rate = 44100;
// if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
// int audio_rate = 44100; int audio_channels = 2;
// Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ int audio_buffers = 4096;
// int audio_channels = 2; sound = NULL;
// int audio_buffers = 4096;
// sound = NULL;
// if( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) { if( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) {
// Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
// audioEnabled = 1; audioEnabled = 1;
// } qDebug() << "Mix_OpenAudio()";
// else { }
// qDebug() << "Mix_OpenAudio() was not successfull, no sound possible :("; else {
// } qDebug() << "Mix_OpenAudio() was not successfull, no sound possible :(";
// } }
}
#endif #endif
} }
void SDLPlayer::playSound(string audioString, int playerID) void SDLPlayer::playSound(string audioString, int playerID)
{ {
#ifndef ANDROID #ifndef ANDROID
initAudio(); if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
// if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
// QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav"); QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav");
// if(myFile.open(QIODevice::ReadOnly)) { if(myFile.open(QIODevice::ReadOnly)) {
// //set 3d position for player //set 3d position for player
// int position = 0; int position = 0;
// int distance = 0; int distance = 0;
// switch (playerID) { switch (playerID) {
// case 0: { case 0: {
// position = 180; position = 180;
// distance = 10; distance = 10;
// } }
// break; break;
// case 1: { case 1: {
// position = 281; position = 281;
// distance = 50; distance = 50;
// } }
// break; break;
// case 2: { case 2: {
// position = 315; position = 315;
// distance = 120; distance = 120;
// } }
// break; break;
// case 3: { case 3: {
// position = 338; position = 338;
// distance = 160; distance = 160;
// } }
// break; break;
// case 4: { case 4: {
// position = 23; position = 23;
// distance = 160; distance = 160;
// } }
// break; break;
// case 5: { case 5: {
// position = 45; position = 45;
// distance = 120; distance = 120;
// } }
// break; break;
// case 6: { case 6: {
// position = 79; position = 79;
// distance = 50; distance = 50;
// } }
// break; break;
// default: { default: {
// position = 0; position = 0;
// distance = 0; distance = 0;
// } }
// break; break;
// } }
// QDataStream in(&myFile); QDataStream in(&myFile);
// soundData = new Uint8[(int)myFile.size()]; soundData = new Uint8[(int)myFile.size()];
// in.readRawData( (char*)soundData, (int)myFile.size() ); in.readRawData( (char*)soundData, (int)myFile.size() );
// sound = Mix_QuickLoad_WAV(soundData); sound = Mix_QuickLoad_WAV(soundData);
// // set channel 0 to settings volume // set channel 0 to settings volume
// Mix_Volume(-1,myConfig->readConfigInt("SoundVolume")*10); Mix_Volume(-1,myConfig->readConfigInt("SoundVolume")*10);
// // set 3d effect // set 3d effect
// if(!Mix_SetPosition(0, position, distance)) { if(!Mix_SetPosition(0, position, distance)) {
// printf("Mix_SetPosition: %s\n", Mix_GetError()); printf("Mix_SetPosition: %s\n", Mix_GetError());
// // no position effect, is it ok? // no position effect, is it ok?
// } }
// currentChannel = Mix_PlayChannel(-1, sound,0); currentChannel = Mix_PlayChannel(-1, sound,0);
// }
// //TESTING: release audio after every sound when there is no other sound currently played
if(SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) {
closeAudio();
} }
// } }
#endif #endif
} }
void SDLPlayer::closeAudio() void SDLPlayer::closeAudio()
{ {
#ifndef ANDROID #ifndef ANDROID
// if(audioEnabled) { if(audioEnabled) {
// Mix_HaltChannel(currentChannel); Mix_HaltChannel(currentChannel);
// Mix_FreeChunk(sound); Mix_FreeChunk(sound);
// sound = NULL; sound = NULL;
// delete[] soundData; delete[] soundData;
// soundData = NULL; soundData = NULL;
// Mix_CloseAudio(); Mix_CloseAudio();
// audioEnabled = false; qDebug() << "Mix_CloseAudio()";
// } audioEnabled = false;
}
SDL_Quit();
qDebug() << "SDL_Quit()";
#endif #endif
} }
+4 -5
View File
@@ -31,7 +31,7 @@
#endif #endif
#endif #endif
// struct Mix_Chunk; struct Mix_Chunk;
/** /**
@author FThauer FHammer <webmaster@pokerth.net> @author FThauer FHammer <webmaster@pokerth.net>
@@ -52,14 +52,13 @@ public:
private: private:
#ifndef ANDROID #ifndef ANDROID
// Mix_Chunk *sound; Mix_Chunk *sound;
unsigned char *soundData; unsigned char *soundData;
int currentChannel; int currentChannel;
bool audioEnabled;
bool audioEnabled;
ConfigFile *myConfig; ConfigFile *myConfig;
QString myAppDataPath; QString myAppDataPath;
#endif #endif
}; };
+6
View File
@@ -45,3 +45,9 @@ void SoundEvents::newGameStarts()
{ {
newGameNow = true; newGameNow = true;
} }
//HACK until playSound is done by events
void SoundEvents::playSound(std::string audioString, int playerID)
{
mySDLPlayer->playSound(audioString, playerID);
}
+3
View File
@@ -1,6 +1,8 @@
#ifndef SOUNDEVENTS_H #ifndef SOUNDEVENTS_H
#define SOUNDEVENTS_H #define SOUNDEVENTS_H
#include <string>
class SDLPlayer; class SDLPlayer;
class ConfigFile; class ConfigFile;
class SoundEvents class SoundEvents
@@ -11,6 +13,7 @@ public:
void blindsWereSet(int sB); void blindsWereSet(int sB);
void newGameStarts(); void newGameStarts();
void playSound(std::string audioString, int playerID);
private: private:
SDLPlayer *mySDLPlayer; SDLPlayer *mySDLPlayer;
@@ -20,6 +20,7 @@
#include "session.h" #include "session.h"
#include "configfile.h" #include "configfile.h"
#include "chattools.h" #include "chattools.h"
#include "soundevents.h"
#include <net/socket_msg.h> #include <net/socket_msg.h>
startNetworkGameDialogImpl::startNetworkGameDialogImpl(startWindowImpl *parent, ConfigFile *config) startNetworkGameDialogImpl::startNetworkGameDialogImpl(startWindowImpl *parent, ConfigFile *config)
@@ -93,9 +94,9 @@ void startNetworkGameDialogImpl::addConnectedPlayer(unsigned playerId, QString p
if(this->isVisible() && myConfig->readConfigInt("PlayNetworkGameNotification")) { if(this->isVisible() && myConfig->readConfigInt("PlayNetworkGameNotification")) {
if(treeWidget->topLevelItemCount() < info.data.maxNumberOfPlayers) { if(treeWidget->topLevelItemCount() < info.data.maxNumberOfPlayers) {
myW->getMySDLPlayer()->playSound("playerconnected", 0); myW->getMySoundEventHandler()->playSound("playerconnected", 0);
} else { } else {
myW->getMySDLPlayer()->playSound("onlinegameready", 0); myW->getMySoundEventHandler()->playSound("onlinegameready", 0);
} }
} }
@@ -26,7 +26,6 @@
#include <QtGui> #include <QtGui>
#include <QtCore> #include <QtCore>
#include "sdlplayer.h"
#include "gametableimpl.h" #include "gametableimpl.h"
class Session; class Session;