2012-04-17 81 views
-2

我正在寫一個程序,打開並打開一個圖像的GL窗口,並連接到我的Android設備,用戶使用該設備作爲一種觸控板來平移和放大和縮小。所有工作正常,但程序卡在glutMainLoop中,不會繼續接收設備中的數據。顯然glutIdleFunc是我的問題的解決方案,但我不能看到如何在我的代碼中實現這個沒有得到內存錯誤?有人能告訴我如何將函數放入我的代碼中,以便運行連接代碼以及opengl的東西嗎?使用glutIdleFunc擺脫glutMainLoop?

#include <stdlib.h> 
#include <stdio.h> 
#include <signal.h> 
#include <string.h> 
#include <strings.h> 
#include <vrpn_Shared.h> 
#include <vrpn_Analog.h> 
#include <vector> 
#include <GL/freeglut.h> 
#include <imageviewer.h> 

using namespace std; 

int done = 0; 
int accepted = 0;   // Signals that the program should exit 
unsigned tracker_stride = 1; // Every nth report will be printed 


//------------------------------------- 
// This section contains the data structure that holds information on 
// the devices that are created. For each named device, a remote of each 
// type analog is created. 

class device_info { 
    public: 
    char   *name; 

    vrpn_Analog_Remote *ana; 

}; 
const unsigned MAX_DEVICES = 2; 


//------------------------------------- 
// This section contains the data structure that is used to determine how 
// often to print a report for each sensor of each tracker. Each element 
// contains a counter that is used by the callback routine to keep track 
// of how many it has skipped. There is an element for each possible sensor. 
// A new array of elements is created for each new tracker object, and a 
// pointer to it is passed as the userdata pointer to the callback handlers. 


class t_user_callback { 
    public: 
    char   t_name[vrpn_MAX_TEXT_LEN]; 
     vector<unsigned> t_counts ; 
}; 

//Callback handlers 

void VRPN_CALLBACK handle_analog (void *userdata, const vrpn_ANALOGCB a) 
{ 
    int i; 
    const char *name = (const char *)userdata; 

    printf("Input from %s:\n \n  %5.0f", name, a.channel[0]); 
    for (i = 1; i < a.num_channel; i++) { 
    printf(" %5.0f \n", a.channel[1]); 
    } 
    printf(" \n"); 
} 




int main (int argc, char * argv []) 
{ 

    int print_for_tracker = 1; // Print tracker reports? 
    int print_for_button = 1; // Print button reports? 
    int print_for_analog = 1; // Print analog reports? 
    int print_for_dial = 1; // Print dial reports? 
    int print_for_text = 1; // Print warning/error messages? 


    device_info device_list[MAX_DEVICES]; 
    unsigned num_devices = 0; 


    int i; 

    // Parse arguments, creating objects 

    for (i = 1; i < argc; i++) { 
    if (!strcmp(argv[i], "-notracker")) { 
     print_for_tracker = 0; 
    } else if (!strcmp(argv[i], "-nobutton")) { 
     print_for_button = 0; 
    } else if (!strcmp(argv[i], "-noanalog")) { 
     print_for_analog = 0; 
    } else if (!strcmp(argv[i], "-nodial")) { 
     print_for_dial = 0; 
    } else if (!strcmp(argv[i], "-notext")) { 
     print_for_text = 0; 

    } else if (!strcmp(argv[i], "-trackerstride")) { 
     if (tracker_stride <= 0) { 
     fprintf(stderr, "-trackerstride argument must be 1 or greater\n"); 
     return -1; 
     } 
    } else { // Create a device and connect to it. 
    device_info *dev; 



    // Name the device and open it as everything 
    dev = &device_list[num_devices]; 
    dev->name = argv[i]; 

    dev->ana = new vrpn_Analog_Remote(dev->name); 


    if (print_for_analog) { 
     printf(" Analog"); 
     dev->ana->register_change_handler(dev->name, handle_analog); 
    } 
    printf(".\n"); 
    num_devices++; 
    } 

    } 

// main interactive loop 

    printf("Press ^C to exit.\n"); 
    while (! done) { 
     unsigned i; 

     // Let all the devices do their things 
     for (i = 0; i < num_devices; i++) { 

     device_list[i].ana->mainloop(); 

     glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 
    glutInitWindowSize(400,300); 
    glutInitWindowPosition(200,100); 
    glutCreateWindow("ImageViewer"); 


    init(); 
    glutDisplayFunc(display); 
    glutMotionFunc(drag); 
    glutMouseFunc(mouse); 
// glutIdleFunc(IdleFunc); 
    glutMainLoop(); 

     } 
    } 
return 0; 
} 
+0

GLUT是否適用於Android和OpenGL ES? – 2012-04-17 18:54:40

+0

圖像數據不在設備上呈現,全部在PC上呈現。林不知道你問尼科爾? – 2012-04-17 19:12:37

+0

也許他正在寫關於android的遊戲,並解釋標籤? – 2012-04-17 19:24:58

回答

1

如果它可以管理所有的輸入設備,並且所有設備都由它管理的輸入事件驅動,那麼過剩就是好事。一旦你有非託管輸入設備或非基於事件的處理,你可能想要使用除了過剩以外的東西。你的另一種選擇是在單獨的進程(或線程)中分叉和運行異步的東西。