Page 1 of 1

Multiple game Loops.

Posted: Sun Jan 10, 2010 6:53 pm
by AlkaSoft
Hi everyone!

Im having a small problem that i couldn't solve, so your help is appricieted. Here is a pseudo-code to understand what is happenning in my application:

Code: Select all

...
int X = 0;
{
While (device->run())
{     
     driver->beginScene(true, true,SColor(0,255,255,255));

      while (X <= 1000)
      {
      device->run();
       driver->beginScene(true, true,SColor(0,255,255,255));
      
       X++;
       printf( X ); ///Show X value

      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
       }
      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
}
    device->drop();
    return false;
}
The second loop simply increment X until it gets to 1001, but stills draw the scene. if i close the application (alt-F4) when the second loop is not finished, the application closes and get off the screen, but it still activated in my task manager and uses alot of my CPU. Its still open but i couldn't close it until i end the process (in task manager).

If I close the application when im in the first loop (when X >= 1001), the application ends sucessfully.

I dont know if its a real problem or simply a lack of C++ thecniques, but i tried a lot of things, and i searched everywhere and i couldn't find any solution.

Thx for helping.

Posted: Sun Jan 10, 2010 9:07 pm
by Bate
I'm not sure but maybe it has something to do with the fact that you call beginScene twice and then endScene twice. When the inner loop ends it ends with endScene and then you wanna draw again in the outer loop without calling beginScene.

Code: Select all

API says "Applications must call beginScene before performing any rendering."

Posted: Sun Jan 10, 2010 9:27 pm
by humbrol
if i understand it right, wouldnt this work a tad better for you? not sure why you would want a function that has the smgr,gui and driver end scene within the loop.

Code: Select all

int X = 0;
{
While (device->run())
{     
      while (X <= 1000)
        {
            driver->beginScene(true, true,SColor(0,255,255,255));
            X++;
           printf( X ); ///Show X value
           smgr->drawAll();
           gui->drawAll();
          driver->endScene();
         }
        
      
      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
}
    device->drop();
    return false;
} 

Posted: Sun Jan 10, 2010 10:54 pm
by AlkaSoft
@Bate :

I tried to change endScene and BeginScene, it gives the same result. Application closes, but its still running in the processes. I have tried to add a console with the application, and i observed that when the application closes (in second loop), the console is still open and the second loop stop, it continues with the first one until i close it.

Also, i need BeginScene in my second loop because i make some rendering in it.

@humbrol :

Code: Select all

#include <irrlicht.h>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main(int argc, char** argv)
{
    IrrlichtDevice *device =
        createDevice(EDT_OPENGL, dimension2d<u32>(640, 480), 16,
            false, false, false, 0);

    device->setWindowCaption(L"TEST-MULTILOOP");

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();

    int X = 0;
    ITexture* Tex = driver->getTexture("A.png");

    while(device->run())
    {
        driver->beginScene(true, true, SColor(0,0,0,0));

        while (X<=1000)
        {
            device->run();
            driver->beginScene(true, true,SColor(0,255,255,255));

            driver->draw2DImage(Tex,
            device->getCursorControl()->getPosition()
            ,core::rect<s32>(0,0,100,100),
            NULL,video::SColor(255,255,255,255),true);

            X++;
            printf("%d\n", X);

            smgr->drawAll();
            guienv->drawAll();
            driver->endScene();
        }
        printf("FirstLoop\n");
        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }
    device->drop();

    return 0;
}
if I remove device->run(), the application dont want to close even when i press (alt-f4), until the second lopp end.
If device->run() is uncommented, the application closes and the console is still open (hiden in processes).

The only thing that i need is to sucessfully close the app while in the second loop.
Thx for trying to help.

Posted: Sun Jan 10, 2010 11:00 pm
by Bate
what about this?

Code: Select all

int X = 0;
{
While (device->run())
{     
      while ( (X <= 1000) && device->run() )
      {
       driver->beginScene(true, true,SColor(0,255,255,255));
     
       X++;
       printf( X ); ///Show X value

      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
       }
 
      driver->beginScene(true, true,SColor(0,255,255,255));
      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
}
    device->drop();
    return false;
}

Posted: Sun Jan 10, 2010 11:07 pm
by AlkaSoft
Yes i tried it before. Same results.
WHY!?!?!

Posted: Sun Jan 10, 2010 11:21 pm
by humbrol
what is your end goal, you could accomplish the text output without going through the whole 2nd loop

Code: Select all

int X = 0;
{
While (device->run())
{     
      while (X <= 1000)
        {
            X++;
           printf( X ); ///Show X value
        }
       
     
      smgr->drawAll();
      gui->drawAll();
      driver->endScene();
}
    device->drop();
    return false;
} 

Posted: Mon Jan 11, 2010 12:38 am
by AlkaSoft
No you could not do that, because the app would output text (x variable) and the engine would stop rendering and running until X is not 1001.

What i want is to draw some images in a different loop.

Posted: Mon Jan 11, 2010 12:55 am
by humbrol
change the while to a if statement and then it will just autoincrement as long as x is below your value

Posted: Mon Jan 11, 2010 1:16 am
by AlkaSoft
This was initially a pseudo-code, i dont need to increment X, its an example. I need to draw and affect some images in another loop.

The simple problem is when i implement a second loop. When i close the program, and my second loop is running, the application closes but the console is hidden and still run in my processes. I simply want it to close totally, is there a way or its simply impossible?

Posted: Mon Jan 11, 2010 1:29 am
by DavidJE13
a loop inside the game loop is quite bad form; much better is to use states;

Code: Select all

while( run ) {
 switch( state ) {
  case STATE1:
   do something;
   break;
  case STATE2:
   do something else;
   break;
 }
 draw stuff;
}
although this may not be what you want, so this may help;

Code: Select all

while( true ) {
 running = true;
 while( x < 1000 ) {
  if( !run ) {
   running = false;
   break;
  }
  x ++;
  draw stuff;
 }
 if( !running )
  break;
 if( !run )
  break;
 draw stuff;
}
AFAIK, it's not a good idea to rely on run returning false more than once - it returns false on first exit, not sure what it does after that. So anything you do should make sure that as soon as run() fails the program knows to exit without calling it again.

Posted: Mon Jan 11, 2010 1:44 am
by hybrid
run should keep the false value until it's deleted - at least since Irrlicht 1.6