So I saw other game examples and saw how the games have this feature that the sound paused when pause button was pressed, and when resume, the sound resume exactly where it was.
Found the answer myself.
So I did some research and found this awesome fix. Well not a fix, but an addition to the code. More importantly, it does work!
In CocosDenshion that include SimpleAudioEngine to ease out the audio problem of your games you need to edit the file to be able to PAUSE your sound effects.
This is the code you need to insert in CDSoundEngine class which is inside SimpleAudioEngine class inside CocosDenshion class. Copy it Exactly like below:
- (void)pauseAllSounds
{
for (int i=0; i < sourceTotal_; i++)
{
ALint state;
alGetSourcei(_sources[i].sourceId, AL_SOURCE_STATE, &state);
if(state == AL_PLAYING)
{
alSourcePause(_sources[i].sourceId);
}
}
alGetError();
}
- (void)resumeAllSounds
{
for (int i=0; i < sourceTotal_; i++)
{
ALint state;
alGetSourcei(_sources[i].sourceId, AL_SOURCE_STATE, &state);
if(state == AL_PAUSED)
{
alSourcePlay(_sources[i].sourceId);
}
}
alGetError();
}
The following code needs to be added in SimpleAudioEngine class to be able to use the method directly in your cocos2d game code.
- (void)pauseAllEffects
{
[soundEngine pauseAllSounds];
}
- (void)resumeAllEffects
{
[soundEngine resumeAllSounds];
}
I used this code in my game and works perfectly.
DISCLAIMER: I DON'T CLAIM AT ANY TIME CREDIT FOR THIS FIX. THIS WAS FOUND BY ME IN THIS SITE: http://nial.me/2012/06/pausing-and-resuming-sound-effects-in-cocos2d/ HOWEVER, SINCE I DIDN'T SEE ANY SIMILAR SOLUTION TO THIS PROBLEM IN STACKOVERFLOW I PROVIDE IT.
Related
I have developed an OpenGL ES 2.0 win32 application, that works fine in a single thread. But I also understand that UI thread and a rendering thread should be separate.
Currently my game loop looks something like that:
done = 0;
while(!done)
{
msg = GetMessage(..); // getting messages from OS
if(msg == QUIT) // the window has been closed
{
done = 1;
}
DispatchMessage(msg,..); //Calling KeyDown, KeyUp events to handle user input;
DrawCall(...); //Render a frame
Update(..); // Update
}
Please view it as a pseudo code, cause i don't want to bother you with details at this point.
So my next step was to turn done into an std::atomic_int and create a function
RenderThreadMain()
{
while(!done.load())
{
Draw(...);
}
}
and create a std::unique_ptr<std::thread> m_renderThread variable. As you can guess nothing has worked for me so far, so i made my code as stupid and simple as possible in order to make sure i don't break anything with the order i call methods in. So right now my game loop works like this.
done.store(0);
bool created = false;
while(!done)
{
msg = GetMessage(..); // getting messages from OS
if(msg == QUIT) // the window has been closed
{
done.store(1);
}
DispatchMessage(msg,..); //Calling KeyDown, KeyUp events to handle user input;
// to make sure, that my problem is not related to the fact, that i'm rendering too early.
if(!created)
{
m_renderThread = std::make_unique<std::thread>(RenderThreadMain, ...);
created = true;
}
Update(..); // Update
}
But this doesn't work. On every draw call, when i try to somehow access or use my buffers \ textures anything else, i get the GL_INVALID_OPERATION error code.
So my guess would be, that the problem is in me calling glGenBuffers(mk_bufferNumber, m_bufferIds); in the main thread during initialization and then calling glBindBuffer(GL_ARRAY_BUFFER, m_bufferIds[0]); in a render thread during the draw call. (the same applies to every openGL object i have)
But I don't now if i'm right or wrong.
I am programming for one of my project "Auction". I put all the conditions for login at both server and client side. My program runs better till some statements but in meanwhile server stops responding.
I research over that by putting print statements and over internet and I found that it is sync problem rather than coding. May be the reason would be different i am not sure.
I also want to mention that when I put "read()" on while condition, things are running as expected. Can anyone please tell me that how to remove/avoid this sync problem (may be something else rather than sync)?
Server.cpp (only some part which is running well)
while(((n = read(sockfd,buffer,4096))!=0)&&(l==false)) {
if(i==0) cout<<"Username:"<<buffer;
if(i==1) { l = true; cout<<"Password:"<<buffer; }
i++;
}
Server.cpp (part before modification that was stucked at i=1)
for(i=0; i<2; i++) { cout<<endl<<"i="<<i<<endl;
bzero(&buffer, sizeof(buffer));
if((n = read(sockfd,buffer,4096))==0) { cout<<"problem";
close(sockfd);
FD_CLR(sockfd, &allset);
client[i] = -1;
} else { cout<<buffer<<i<<endl; if(i==1) {cout<<"copied password"; }
if(i == 0) { cout<<"copied user";} }
I'm doing dialog based, MFC game.
During the game there is a battle phase , and I want to display each text on the screen with a small delay,
I tried using sleep and also tried CTime, but both display only the last text at each battle...
here is an example of the code...
void CDNDPprojectDlg::OnBnClickedAction()
{
CString Damaged;
damage=me->Attack(enemy,WeID);
SDialog.Format(_T("You done %d damage, using %s,\r\n %s has %d HP left"), damage,Damaged,monName,enemy->getHP());
Output.SetWindowTextW(SDialog);
if(enemy->getHP() <1)
{
Won();
return;
}
NextAttack();
if(me->getHP() <1)
{
Died();
return;
}
}
at the beginning of all 3 functions there is Sleep(700). it does a delay but display only the last
Output.SetWindowTextW(SDialog);
that I put...I guess its something of threading, please help me.
thanks
There is a hard way, an easy way and a shameful way.
The hard way is to do multi threading - check AfxBeginThread, you pass a pointer to your CDNDPprojectDlg, and do the job there after each of your sleep. It's hard because at one point you will need to synchronise your main thread and your new thread when they access common resource... a lot to learn there but before that a lot of pain
The other way is to either use a timer SetTimer and override OnTimer
The third way is to use something like the DoEvents in Visual Basic (ha the shame!). Just call it after you have done something and your screen will refresh. I will deny any knowledge of having passed that code to you.
BOOL DoEvents()
{
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
return FALSE;
}
if (!AfxGetApp()->PreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
return TRUE;
}
Good luck!
I'm writing a server for an online game based on IOCP, and the core codes handling game message is something like below:
CMessage ret;
int now_roomnum = recv_msg->para1;
int now_playernum = recv_msg->para2;
/*if(true)
{
cout<<"Received Game Message: "<<endl;
cout<<"type2 = "<<recv_msg->type2;
cout<<" player_num = "<<now_playernum<<" msg= "<<recv_msg->msg<<endl;
cout<<endl;
}*/
if(recv_msg->type2 == MSG_GAME_OPERATION)
{
ret.type1 = MSG_GAME;
ret.type2 = MSG_GAME_OPERATION;
while(game_host[now_roomnum].Ready(now_playernum) == true)
{
;
}
//cout<<"Entered from "<<now_playernum<<endl;
game_host[now_roomnum].SetMessage(now_playernum, recv_msg->msg);
game_host[now_roomnum].SetReady(now_playernum, true);
game_host[now_roomnum].SetUsed(now_playernum, false);
while(true)
{
bool tmp = game_host[now_roomnum].AllReady();
if(tmp == true)
break;
}
//cout<<"AllReady from"<<now_playernum<<endl;
string all_msg = game_host[now_roomnum].GetAllMessage();
game_host[now_roomnum].SetUsed(now_playernum, true);
while(!game_host[now_roomnum].AllUsed())
{
;
}
//cout<<"AllUsed from "<<now_playernum<<endl;
EnterCriticalSection(&cs);
game_host[now_roomnum].ClearReady();
LeaveCriticalSection(&cs);
strcpy_s(ret.msg, all_msg.c_str());
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;
}
return ret;
Now, the problem is: on a PC, when all cout are commented like above, the game freezes at once; but when I cancel the comments, the server works well.
What's more, when I run the server on my laptop, everything goes fine, no matter whether I comment the cout or not. The main difference between my laptop and PC is that my laptop's OS is Windows 8.1, while the PC is Windows 7.
I'm totally confused. It will be of great help if someone can tell me what to do. Thank you!
Looks like a multithreading issue.
By the way I see you use a Critical section around ClearReady but not when testing for AllReady. That call should be wrapped as well (or, better, write a LockedAllReady that makes use of the lock).
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;
What you mean by ret.msg? if msg is method you must do ret.msg(); , is it a field?
If you have this good then like they say above probably a timing problem, try to do cout without ret.msg and see what will happen, and then you know from where the problem is.
I am making a game using SDL and my SoundHandler class is not working and I cannot figure out why. The file paths are definatly correct and I have SDL_Mixer set up properly as I have had sound work correctly before, I also get no errors or warnings the game runs fine there is just no music.
SoundHandler.h:
enum Sounds
{
BACKGROUND_MUSIC, STICK_COLLECT
};
class SoundHandler
{
public:
SoundHandler();
void PlaySound(Sounds sound);
private:
Mix_Music *backMusic;
Mix_Music *stickCollect;
};
SoundHandler.cpp:
SoundHandler::SoundHandler()
{
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
this->backMusic = Mix_LoadMUS("Data//Music//Background.mp3");
this->stickCollect = Mix_LoadMUS("Data//Sounds//StickCollect.mp3");
Mix_VolumeMusic(128);
}
void SoundHandler::PlaySound(Sounds sound)
{
if(sound == BACKGROUND_MUSIC)
{
Mix_PlayMusic(this->backMusic, -1);
}
if(sound == STICK_COLLECT)
{
Mix_PlayMusic(this->stickCollect, 1);
}
}
Relevant Lines in main.cpp:
// Initialise Sound
SoundHandler soundHandler;
// Play Background Music
soundHandler.PlaySound(BACKGROUND_MUSIC);
// Play Sound
soundHandler.PlaySound(STICK_COLLECT);
I think that problem is in double slashes in file path, try to use single slash.
You will have long time of debugging without error checking.