issue in application did enter background - c++

I am having a application in cocos2d-x. I want my application should run always until manually stops. But while enters into the background it got paused and when return back it resumes. I removed the codes
CCDirector::sharedDirector()->pause();
CCDirector::sharedDirector()->resume();
from Appdelegate.cpp functions (applicationDidEnterBackground() and applicationDidEnterForeground()). But still my application got pause while enter into background.I want my application should pause only when i got a phone call or manually i pause my application, until or unless it should run in the background. Please me to solve this.

Here is what happens when your app goes to background
class name >>> Cocos2dxActivity.java
#Override
protected void onPause() {
super.onPause();
Cocos2dxHelper.onPause();
this.mGLSurfaceView.onPause();
}
I think the code is self explanatory,i.e, it is pausing your app when it enters background (the whole purpose of onPause) and the ApplicationDelegate methods you mentioned are just JNI functions called when the activity goes to background/foreground so that you can do game related stuff there. As you may know that CCDirector's pause only pauses the running scene (scheduled timers) and changes the draw rate to 4 FPS to reduce CPU consumption.
So, removing pause/resume calls to CCDirector in Appdelegate will not help achieve those goals. For that you have to override the method and related methods I mentioned above and stop the activity from pausing.

Related

Making a music player in C++/GTK

I would like to create a music player, but I'm working with robots.
My robot should play a series of action (speech, move, etc) and I need to be able to stop it at anytime (for security).
I'm working with C++ and GTK.
I have a PLAY button linked to the function play_playlist:
void play_playlist ()
{
std::deque<history_record>::iterator it = list_to_play_.begin();
while (!g_stop_ && it != list_to_play_.end())
{
play_action(it); // take time to execute (simulate using sleep 3sec)
it++;
}
}
And a STOP button linked to the function set_stop_to_false:
void set_stop_to_true()
{
g_stop_ = true;
}
When I click PLAY the GUI freezes and I'm not able to click on STOP.
How can I have my playlist running and be able to click on the GUI? (i.e. GUI should be responsive)
My best hope is a thread, but I'm not sure how to use it appropriately.
When I click PLAY the GUI freezes and I'm not able to click on STOP.
You need to build a multi-threaded application. Your interface freezes because your application is busy doing something else, so not only that events raised from GUI are not being processed but they are not even raised.
Your application should start a worker thread that will do its job in the background, the main thread can then communicate with this worker thread for example via shared memory.
Once you enter a callback like play_playlist, the GTK main loop can't process events until you exit that function, which freezes the user interface. As play_action takes seconds to run, you only have 2 choices:
split play_playlist execution in several smaller steps, and use a
state machine to execute each step one after the other using a
callback triggered by g_idle_add. Here's an example of this
technique for lazy loading.
the other solution is to run your blocking play_action in a thread. Give a look at GThread. You start your thread when you press PLAY and stop it when you press STOP. However, you can't manipulate the user interface from a thread, GTK is not thread safe. Every GTK action should be processed from the main thread.

pause/ play a game in cocos2d android?

How to pause a game in cocos2d android? I searched many tutorials but i did not found anywhere.I tried below code, it is working when paused from game scene.But it not working while resuming the game from pause scene
in gameScene :
CCDirector.sharedDirector().onPause();
in pause Scene :
CCDirector.sharedDirector().onResume();
For pausing game scene I use:
CCDirector.sharedDirector().pause();
And for resuming:
CCDirector.sharedDirector().resume();
Try it with these functions. :)
Thanks for your reply. I used the same thing.But it wont stop schedulers and actions.
But finally i got the way we have to pause.
1.To pause we have to use pushScene() method as follows:
CCScene pauseScene = CCScene.node();
pauseScene.addChild(new PauseLayer);
CCDirector.sharedDirector().pushScene(pauseScene);
When you have done this, gamescene will get stored in the stack and now running scene will be pause scene.
2.To resume we have to use popScene() as follows:
CCDirector.sharedDirector().popScene();
3.If you are moving to LevelSelection form pause then along with above lines use these lines also:
CCDirector.sharedDirector().getRunningScene().removeAllChildren(true);
CCDirector.sharedDirector().replaceScene(levelSelectScene);
Declare a BOOL variable, then when you detect user pauses game, make this variable to NO and in your update method put this as a condition above to the code you want to pause on your game paused.
-(void)update:(ccTime)dt
{
if(isGameNotPaused)
{
// your code
}
}

How to make a loading/intro animation popup while waiting for program to start up clutter/GTK+

My program takes a few seconds to start up. I am using clutter for the GUI, and I decided to try and make something pop up to indicate that the program is starting up. I wanted to just have a logo pop up and rotate, then disappear when the program starts.
So in clutter, I figured I could just make a new stage (window) add an actor to it, make the and actor spin, in the first section of the main function. The window will pop up right away, but with no content, but the content wont show until you launch the clutter main loop.
So I was just wondering how I might be able to achieve this using clutter or GTK+.
If you are familar with reaper 4, the audio recording program, this program does something similar to what I want to mine to do.
What you want is called a splash screen. I'm unfamiliar with clutter, but I found this GTK splash screen example.
However, I think you're taking the problem the wrong way. Splash screens are a bad idea because you just add overhead. What you need is improving your startup performance, by doing some CPU and/or IO profiling. Loading stuff on-demand, and not all at once will help.
Unfortunately I'm unfamiliar with Clutter. But I'm pretty sure it will be difficult to render an animation without a main loop running in any high level library.
I'd try to put the code that causes the delay into a separate thread and inform the main loop when the startup is done.
Something like this is what i use:
string splashfile = path_templ + "/splashimg.png";
GtkWidget *image=gtk_image_new_from_file(splashfile.c_str());
gtk_container_add(GTK_CONTAINER(SplashWindow), image);
gtk_widget_show_all(SplashWindow);
//Cycle through all iterations (refresh everything in the GUI)
while (gtk_events_pending()){
gtk_main_iteration();
}
sleep(1);
(... rest of code ...)
gtk_main ();
gdk_threads_leave ();
Especially that last part of while events pending is the key

how to reset game in cocos2d

My question in the previous post was-
I have a cocos2d game, and after I
exit the game and start it again, it
starts at the exact same point it was
before closing.why this is
happening??and not only this, my game
is landscapemode but when I start it
again from simulator it comes in
portrait mode,not it landscapemode like
first time.
Is there any way to prevent this?
Anyone will come to rescue me???
I found a solution.Is it good??
when I exit the game(by pressing the back button in iPhone simulator) this problem occurred. Now I found that if I press back button the following function is called-
(void)applicationWillResignActive:(UIApplication *)application
So I set (divide by zero) or something like this in -(void)applicationWillResignActive function so that when this function called the application crashes. Then if I restart the application everything comes from beginning(reset) which I want. Is it good solution or anything better?
*my applicationDidFinishLaunching was not called again when application is restart from simulator.
You should configure the Info.plist to indicate that you does not want your application run in Background.
The key is
UIApplicationExitsOnSuspend
If you need more detail you should look at this tutorial
If you have iOS 4.0 or higher, multitasking is causing your problem.

QT Repaint/Redraw/Update/Do Something

I'm New to QT. I understand that you can force a display refresh, but I've pulled all my hair out trying to figure out how. Here is what I'm specifically trying to do.
I press a button (onClick signal event), which runs code that changes an image (QLabel) on the display, waits for input, and then proceeds by changing a new image (different QLabel). I've tried everything and the display doesn't refresh until the onclick signal event code is complete. Right now, I'm not waiting for user input, I'm using usleep(~500 ms) for testing purposes.
From what I read, QT is event driven meaning that I'm basically creating a bunch of events, that get put in a que, and executed when the (onClick signal event) returns to the (main loop)/(event handler). I don't want to wait until the function is complete, it's going to make programming extremely painful if I have to accomplish this routine entirely based on events.
How can I force the QLabel pixmap to refresh. I've tried everything. Below is all the code I have tried in my onClick signal event handler. (upButton is the name of the QLabel which is a pixmap)
update();
repaint();
ui->upButton->setUpdatesEnabled(TRUE);
update();
repaint();
QPaintEvent paintevent(ui->upButton->childrenRegion());
QPaintEvent * test = &paintevent;
paintEvent(test);
this->changeEvent(test);
ui->upButton->update();
ui->upButton->repaint();
ui->upButton->repaint(ui->upButton->childrenRegion());
repaint();
QApplication::sendPostedEvents();
this->parentWidget()->update();
usleep(100000);
As you can see, I'm just shooting in the dark at this point. I've tried to look at sample code and do all my homework, but I'm lost. Appreciate any help, advice, and or sample code.
I was using sleep to emulate a brief amount of time the computer was waiting for something to happen.
As I stated in my question, I didn't want to use events because it's a whole lot of unnecessary work to accomplish something extremely simply.
Also, the 'event' that needs to take place for the program to continue, is a USB event. Since I'm using an HID class device, there is no way to set an event to happen without a wait loop. USB HID classes don't permit setting interrupts, the OS claims the device.
I managed to get the above to work. I walked through the debugger and noticed the display would refresh before the sleep function. Running the program independently, I got random results with the display refreshing 1% of the time. I got rid of the sleep function, and added some other code in it's place to emulate a delay, and it was fine.
Just for everyone's knowledge, this is possible, it's not forbidden, and it's easy to do with the following:
qApp->processEvents();
qApp is a global external variable in the QApplication header.
Because this USB event is making my flow tricky, I stumbled upon the QWaitCondition Class. I was going to start a process waiting for the USB event. I would wait until the process releases the wait condition for my routine to continue.
But if anyone thinks this is a bad idea, please, speak out. I really do appreciate your feedback PiedPiper and Hostile Fork.
Thank you.
I noticed sometimes when you have multiple layered widgets, or widgets inside of widgets it helps to call their repaint() events.
For example
this->repaint();
this->parentWidget()->repaint();
this->parentWidget()->parentWidget()->repaint();
This is far easier then pushing out any processing to another Thread, or creating additional event handlers.
You shouldn't be waiting for input in your event handler. You need to rethink the logic of your program to use events the way they were intended. All the update() and repaint() calls in your code are unnecessary if you return to the event loop.
If i understood correctly, you have a slot and in this slot, you update the image shown in a QLabel. But you want this change to be displayed before the slot finishes.
If that is the case, issue an update() event, and call qApp->processEvents(). This method processes events that are waiting in the event queue and then returns, therefore this may be what you are after.
PS: an update() may not be necessary at all, i am not sure.