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