Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
105 user(s) are online (87 user(s) are browsing Forums)

Members: 0
Guests: 105

more...

Support us!

Headlines

 
  Register To Post  

exit from a function defined for glutMainLoop() possible ?
Quite a regular
Quite a regular


See User information
Hi All,

in fact, I'd like to know if I can do something like this :


My display function :
void MyDisplayFunctionvoid ){
  If ( 
MyChecking == TRUE ){
    Exit(
0);
   }
 }


Define the display function from setup :
glutDisplayFuncMyDisplayFunction );


And the glut start :
...
... (
some code);
...
glutMainLoop();
...
... (
some code part 2);
...


In fact, I'd like to replace Exit(0) with something that will quit the program from glutMainLoop() and continue just after (at some code part 2 place)...
is it possible ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Just can't stay away
Just can't stay away


See User information
@freddix

This is from a program of mine:
#include <setjmp.h>

static jmp_buf jmpbuf;
static 
int jmp_set FALSE;

void enter_main_loop () {
    if (!
setjmp(jmpbuf)) {
        
jmp_set TRUE;
        
glutMainLoop();
    }
    
jmp_set FALSE;
}

void leave_main_loop () {
    if (
jmp_setlongjmp(jmpbuf1);
}

Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Quite a regular
Quite a regular


See User information
@Salass00:
If I'm not wrong, I should do something like this :

At the beginning of my source code :
#include <setjmp.h>
static jmp_buf jmpbuf;


Display function :
void MyDisplayFunctionvoid ){
  ... 
all my display function here
  
if (jmp_setlongjmp(jmpbuf1);
   }
 }


Define the display function from setup :
glutDisplayFuncMyDisplayFunction );


And the glut start :
...
... (
some code);
...
if (!
setjmp(jmpbuf)){
  
jmp_set TRUE;
  
glutMainLoop();
 }
jmp_set FALSE;
... (
some code part 2);
...


Is that correct ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Just can't stay away
Just can't stay away


See User information
@freddix

If I were you I would just use the functions I posted as is, i.e. replace glutMainLoop() in your code with enter_main_loop() and add leave_main_loop() where you want to leave the loop. If you don't like the function names you can always rename them.

Some GLUT implementations have a glutLeaveMainLoop() function but apparently this function isn't implemented in MiniGL.

Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Quite a regular
Quite a regular


See User information
@Salass00:
Thank you :)
Your system work perfectly.
It allow me to use glut and to control the sync sequencially without having it to loop with the glutDisplayFunc() defined :p

All we have to decide is what to do with the time that is given to us.
Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Home away from home
Home away from home


See User information
@freddix

No need to make things so complicated. The C std lib allows you to define an exit hook using atexit(). For example, my code usually looks something like this:

Quote:

void cleanup();

int main(int argc, const char* argv[])
{
// ----- Insert code to Parse args, etc. -----

atexit(cleanup);

// ----- Code to init GLUT, create objects, etc. ----

exit(0);
}

void cleanup()
{
// ----- Code to cleanup ----
}



This allows you to call exit() anywhere, and still have it execute your cleanup code.

MiniGL's glut also has the ability to set a hook for when someone clicks on the close window button:
glCloseFunc();

Or, you could tell GLUT to exit the main loop when someone clicks on the close button by using glutSet() to set:
GLUT_ACTION_ON_WINDOW_CLOSE
to:
GLUT_ACTION_GLUTMAINLOOP_RETURNS

Unfortunately there is no glutLeaveMainLoop() though, although it could be added in a future version, if people really needed it.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Quite a regular
Quite a regular


See User information
@Hans
In fact, my problem is not to quit the app, but simply quit the glutMainLoop...
And after quitting it it can recall it again ...
it's because I want glutMainLoop to act as single step function and not as a loop.
With Salass00 informations, I can do that now :)

Thank you all.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Home away from home
Home away from home


See User information
@freddix

That kind of defeats the purpose of a mainloop now doesn't it? I would have simply had a static flag that indicates when to do the next step, e.g., :

Quote:
static BOOL nextStep = FALSE;

// -----------

if ( MyChecking == TRUE ){
nextStep = TRUE;
}

// -----------

if(nextStep)
{
doNextStep();
nextStep = FALSE;
}



IMHO, this is simpler than trying to get GLUT to do something that it's not designed for.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: exit from a function defined for glutMainLoop() possible ?
Quite a regular
Quite a regular


See User information
@Hans
The principle is quite simple. Having something act like an engine/ or a basic language (engine is only the first step ... language will be the second one).

For exemple, here is my demo source code for stars in its actual state :

#include "AmiDARKEngine.c"

void DarkLoopvoid ){
  
int XLoop 0;
  
int SColor 0;
  
float XSpeed 0.0;

  for( 
XLoop 0XLoop<=(StaticAmount -1); XLoop++){
    
StarsXLoop ].XPos = (float)DERnd640 );
    
StarsXLoop ].YPos = (float)DERnd479 );
   }

  
DESetDisplayMode64048032 );
  
DESyncOff();
  
DEInkDERgb255255255 ), );

  while( !
DELoop() ){
    
DECls();
    for( 
XLoop 0XLoop<=(StaticAmount -1); XLoop++){
      
XSpeed = (float)(XLoop+1) * 0.0025 ;
      
StarsXLoop ].XPos StarsXLoop ].XPos XSpeed;
      if( 
StarsXLoop ].XPos 0.0 ){
        
StarsXLoop ].XPos StarsXLoop ].XPos 640.0;
       }
      
SColor XLoop 2.5 ;
      
DEDotEx( (int)StarsXLoop ].XPos, (int)StarsXLoop ].YPosDERgbSColorSColorSColor ) );
     }
    
DESync();
   }
 }

This is what developer makes to use my engine and doing star scroll.
in background, my system uses GLUT.

The functions-sets constructors are defined like this :
void CORE_Constructorvoid ){
  
IsSyncOn FALSE;
  
glutDisplayFuncMyDisplayFunc );
  
glutIdleFuncMyDisplayFunc );
 }

For example, in this one I set the display one ... it's what interest us ..

and now, MyDisplayFunc :
void MyDisplayFunc(){
  
// Automatically clear the backdrop is active
  
if( BasicCamera.Backdrop == ){
    
glClearGL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT );
   }
  
UpdateLights(); // Update all true 3D lighting.
  
UpdateWorld(); // Update all Basic 3D
  
UpdateCamera(); // Update all camera 3D
  
glutSwapBuffers();
  if (
jmp_setlongjmpjmpbuf1);
  
// Until the function glutLeaveMainLoop() is added to MiniGL.
 
}

The end of the function forces to quit glutMainLoop...

Now, if you look the 1st code, you'll see 2 interesting function : DELoop() ...
It work like a QUIT function detecting specific entries that can lead to quit the application.
For example escape key.

Look for example, the keyboard, mouse and specials functions are defined as follow :
MyMouseFuncint MButtonsint Stateint XPint YP ){
  
DBMouse.LeftButton FALSE;
  
DBMouse.RightButton FALSE;
  
DBMouse.MiddleButton FALSE;
  
DBMouse.OldXPos DBMouse.XPos;
  
DBMouse.OldYPos DBMouse.YPos;
  
DBMouse.XPos XP;
  
DBMouse.YPos YP;
  if (
State == GLUT_DOWN ){
    switch( 
MButtons ){
      case 
GLUT_LEFT_BUTTON:
        
DBMouse.LeftButton TRUE;
        break;
      case 
GLUT_RIGHT_BUTTON:
        
DBMouse.RightButton TRUE;
        break;
     }
   }
 }

MyKeyboardFuncunsigned char kint xint y ){
  
// Reset Special Keys
  
DBKey.Escape FALSE;
  switch (
k){
    
// Get Escape Key
    
case 27:
      
DBKey.Escape TRUE;
      break;
    default:
      return;
   }
 }

MySpecialFuncvoid ){

 }
MyEntryFuncvoid ){
 }


INPUT_Constructorvoid ){
  
glutKeyboardFuncMyKeyboardFunc );
  
glutMouseFuncMyMouseFunc );
  
glutEntryFuncMyEntryFunc );
  
glutSpecialFuncMySpecialFunc );
  
DBMouse.XPos 0;
  
DBMouse.YPos 0;
  
DBMouse.OldXPos 0;
  
DBMouse.OldYPos 0;
  
DBMouse.LeftButton 0;
  
DBMouse.RightButton 0;
  
DBMouse.MiddleButton 0;
 }

I can then store all infos and make them available in my entire program ..
With that, my DELoop() function is simple :
BOOL DELoop(){    
  
int done DBKey.Escape;
  return 
done;
 }

I will add support for window close gadget caption.

Do you know understand the principle ?

At the end of this,
When developer call DESync() function it do this :
/* Rafraichir l'affichage */
void DESync(){
  if ( !
setjmpjmpbuf)){
    
jmp_set TRUE;
    
glutMainLoop();
   }
  
Basic2D.IsOrthoActive FALSE;
 }

It setup the longjmp, call glutMainLoop() that'll execute all GLUT defined functions I've made (key, mouse, special, and at the end display or idle and then quit glutMainLoop() .. glutMainLoop() do what it is supposed to ... provide a full set of functionalities and then it quit it ... I get these informaitons and then can use them in my full program ...

Here is the explanation on why I needed this ...

All we have to decide is what to do with the time that is given to us.
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project