glutCloseFunc没有终止应用程序

时间:2016-04-14 09:31:37

标签: c++ opengl glut freeglut glutcreatewindow

我创建了一个import re string = 'Less/RBR than/IN 1/2/CD of/IN all/DT US/NNP businesses/NNS are/VBP sole/JJ proprietorships/NNS ?/.' def create_relationship(pos_sent): # Get all the words individually words = re.findall(r'\b([0-9A-z\/]*)\/\w*?\b', pos_sent) # ['Less', 'than', '1/2', 'of', 'all', 'US', 'businesses', 'are', 'sole', 'proprietorships'] # Get all the tags individually penn_tag = re.findall(r'\b[0-9A-z\/]*\/(\w*)?\b', pos_sent) # ['RBR', 'IN', 'CD', 'IN', 'DT', 'NNP', 'NNS', 'VBP', 'JJ', 'NNS'] # Create a relationship between the words and penn tag: relationship = [] for i in range(0,len(words)): relationship.append([words[i],penn_tag[i]]) # [['Less', 'RBR'], ['than', 'IN'], ['1/2', 'CD'], ['of', 'IN'], ['all', 'DT'], # ['US', 'NNP'], ['businesses', 'NNS'], ['are', 'VBP'], ['sole', 'JJ'], ['proprietorships', 'NNS']] return relationship def get_words(pos_sent): # Pass string into relationship engine array = create_relationship(pos_sent) # Start with empty string s = '' # Conduct loop to combine string for i in range(0, len(array)): # index 0 has the words s = s + array[i][0] + ' ' # Return the sentence return s def get_noun_phrase(pos_sent): # Penn Tagset # Adjetive can be JJ,JJR,JJS # Noun can be NN,NNS,NNP,NNPS # Noun Phrase must be made of: DT+RB+JJ+NN+PR (http://www.clips.ua.ac.be/pages/mbsp-tags) # Pass string into relationship engine array = create_relationship(pos_sent) bucket = array output = [] # Find the last instance of NN where the next word is not "NN" # For example, NNP VBP qualifies. In the case of NN NNP VBP, then # the correct instance is NNP. To do this, we need to loop and use # a bucket to capture what we need. The bucket will shirnk as we # shrink the array to capture what we want noun = True # Keep doing this until there is no instances of Nouns while noun: # Would be ideal to have an if condition to see if there's a noun # in the first place to stop this form working (and avoiding errors) for i in range(0, len(bucket)): if re.match(r'(NN.*)',bucket[i][1]): # Set position of last noun last_noun = i noun_phrase = [] # If we don't have noun, it'll stop the while loop if last_noun < 0: noun = False else: # go backwards from the point where you found the last noun for x in range(last_noun, -1, -1): # The penn tag must match any of these conditions if re.match(r'(NN.*|DT.*|JJ.*|RB.*|PR.*)',bucket[x][1]): # if there is a match, then let's build the word noun_phrase.append(bucket[x][0]) bucket.pop(x) else: last_noun = -1 break # Make sure noun phrase isn't empty if noun_phrase: # Collect the noun phrase output.append(" ".join(reversed(noun_phrase))) # Fix the reverse issue return [i for i in reversed(output)] print get_noun_phrase(string) # ['all US businesses', 'sole proprietorships'] 的窗口,并使用glutCreateWindow开始循环。我想结束该循环并关闭窗口,因此我使用glutMainLoopglutLeaveMainLoop来销毁它。我的应用程序自动终止。

我希望应用程序在窗口被销毁后继续存在。有可能吗?

根据这个link,我能做到,但我不知道如何做。我正在使用 freeglut

1 个答案:

答案 0 :(得分:5)

the doc for glutCloseFunc()中:

  

希望在窗口关闭时阻止FreeGLUT退出的用户应该使用glutSetOption来设置GLUT_ACTION_ON_WINDOW_CLOSE

导致the glutSetOption() docs

  

GLUT_ACTION_ON_WINDOW_CLOSE - 控制用户或系统关闭窗口时发生的情况:

     
      
  • GLUT_ACTION_EXIT将立即退出应用程序(默认情况下,GLUT的行为)。
  •   
  • GLUT_ACTION_GLUTMAINLOOP_RETURNS将立即从主循环返回。
  •   
  • GLUT_ACTION_CONTINUE_EXECUTION将继续执行剩余的窗口。
  •   

来自glutLeaveMainLoop()

  

glutLeaveMainLoop函数导致freeglut停止事件循环。如果GLUT_ACTION_ON_WINDOW_CLOSE选项已设置为GLUT_ACTION_GLUTMAINLOOP_RETURNSGLUT_ACTION_CONTINUE_EXECUTION,则控件将返回到名为glutMainLoop的函数;否则申请将退出。

把各个部分放在一起:

#include <GL/freeglut.h>
#include <iostream>

void display()
{
    glClear( GL_COLOR_BUFFER_BIT );
    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutSetOption( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS ); 
    std::cout << "Before glutMainLoop()!" << std::endl;
    glutMainLoop();
    std::cout << "Back in main()!" << std::endl;
    return 0;
}