我输入了glutPostRedisplay();但是它没有刷新窗口和/或输出?

时间:2015-06-18 20:49:54

标签: c opengl cross-platform glut

当我尝试运行此代码时,它只是在退出函数之后发布输出我不知道它为什么它不起作用。我知道代码很长,因为我在学习OpenGL的同时制作游戏,并且我试图让它尽可能地跨平台。第80行直到窗口关闭才执行。 有人可以找出并解决问题,还是因为我奇怪的游戏设计?

如果这里的兼容性问题是规格:

ArchLinux(x86_64)和xfce4 编译器是gcc

#include <GL/glut.h>
#include <stdio.h>
#ifdef WIN32 // Will need this later
#include <io.h>
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h>   // for nanosleep
#include <unistd.h>
#else
#include <unistd.h> // for usleep
#endif

/**
    By default it should be 
    UP - move on negative z axis
    DOWN - move on positive z axis
    LEFT - move left relative to the camera (mostly negative x axis)
    RIGHT - move right relative to the camera (mostly positive x axis)
    SPACE - jump (fxp. decreasing the Z value)
**/

unsigned char controls[5],areyougoingtoexit;
unsigned char configurationdone,ingame = 0;
FILE *configuration;
unsigned short displayx,displayy,displaymode;
int winIDMain;

void mainmenurender(void);

void mainmenukeyboard(unsigned char c, int x, int y);

void mainmenumouse(int button, int state, int x, int y);

idle(void){ //To prevent compilation errors ,thats why I puted it infront of the code
    glutSetWindow(winIDMain);
    glutPostRedisplay();

}

int main(int argc, char* argv) {
    while(1){
        if(configurationdone){
        glutInit(&argc, argv);
            glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
            glutInitWindowPosition(100, 100);
            glutInitWindowSize(displayx, displayy);
        winIDMain = glutCreateWindow("GL Game");

            if(ingame == 0){
                glutSetWindow(winIDMain);
                glutDisplayFunc(mainmenurender);       
                glutKeyboardFunc(mainmenukeyboard);
                glutMouseFunc(mainmenumouse);
                glutIdleFunc (idle);
            }
            if(displaymode == 1){
                glutFullScreen();
            }

            glutMainLoop(); 
        }else{
            loadconfiguration();
        }
    }
}

void mainmenukeyboard(unsigned char c, int x, int y) {
        if (c == 27) {
        if(displaymode == 1)
                    displaymode == 0;
        else
            displaymode == 1;
        }
}


void mainmenumouse(int button, int state, int x, int y) {
    int perpartpixels = glutGet(GLUT_WINDOW_HEIGHT) / 40; //Aproxx 5% of the resolution 
    if (button == GLUT_LEFT_BUTTON){
        printf("X:%d Y:%d",x,y);
        if((y >= perpartpixels * 3 && y <= perpartpixels * 3 + 15) && (x >= 0 && x <= 81)){
            printf("Play");
        }else if((y >= perpartpixels * 4 && y <= perpartpixels * 4 + 15) && (x >= 0 && x <= 72)){
            printf("Settings");
        }else if((y >= perpartpixels * 5 && y <= perpartpixels * 5 + 15) && (x >= 0 && x <= 36)){
            exit(0);
        }
    }        
    if (button == GLUT_RIGHT_BUTTON) {
                exit(0);
        }
}

void mainmenurender(void) {
    glutSetWindow(winIDMain);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity ();

    char mainmenustring[9] = "Main Menu";
    glColor3f(1, 1, 1);
    glRasterPos2f(-1, 0.9);
    int mainmenulen, mainmenui;
    mainmenulen = (int)strlen(mainmenustring);
    for (mainmenui = 0; mainmenui < mainmenulen; mainmenui++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, mainmenustring[mainmenui]);
    }
    char startstring[5] = "Start";
    glColor3f(1, 1, 1);
    glRasterPos2f(-1, 0.8);
    int startlen, starti;
    startlen = (int)strlen(startstring);
    for (starti = 0; starti < startlen; starti++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, startstring[starti]);
    }
    char settingsstring[8] = "Settings";
    glColor3f(1, 1, 1);
    glRasterPos2f(-1, 0.75);
    int settingslen, settingsi;
    settingslen = (int)strlen(settingsstring);
    for (settingsi = 0; settingsi < settingslen; settingsi++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, settingsstring[settingsi]);
    }
    char exitstring[9] = "Exit";
    glColor3f(1, 1, 1);
    glRasterPos2f(-1, 0.7);
    int exitlen, exiti;
    exitlen = (int)strlen(exitstring);
    for (exiti = 0; exiti < exitlen; exiti++) {
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, exitstring[exiti]);
    }
        glutSwapBuffers();
}






// Load config functions
int loadconfiguration(void) {
    //Default variables
    char defaultcontrolconfig[5] = { 0x18, 0x19, 0x1A, 0x1B, 0x20 }; //UP,DOWN,LEFT,RIGHT,SPACE(JUMP)
    unsigned short defaultx = 640,defaulty = 480,defaultmode = 0; //X RES,Y RES, FULLSCREENMODE (0 NO ,1 YES)
    //Check if config folder is present otherwise create it 
    int result = mkdir("configuration", 0777);
    if(result == -1){
        printf("Ignore creating folder:\nError -1 Directory already exists\n");
    }else if(result != 0){
        printf("Error: %d while creating configuration folder\n", result);
    }
    //Check if control configuration is present otherwise create it
    if (access("configuration/controls.conf",F_OK)!= -1)
    {
        printf ("Found controls configuration file\n");
        configuration = fopen("configuration/controls.conf", "rb");
    }
    else
    {
        configuration = fopen("configuration/controls.conf", "wb");
        fwrite(defaultcontrolconfig,1,5,configuration);
    }
    fread(controls,1,5,configuration);
    printf("Finished loading controls configuration\n");
    fclose(configuration);

    //Check if display configuration is present otherwise create it
    if (access("configuration/display.conf",F_OK)!= -1)
    {
        printf ("Found display configuration file\n");
        configuration = fopen("configuration/display.conf", "rb");
    }
    else
    {
        configuration = fopen("configuration/display.conf", "wb");
        fprintf(configuration,"%hd\n%hd\n%hd",defaultx,defaulty,defaultmode);
    }
    rewind(configuration);
    fscanf(configuration,"%hd\n%hd\n%hd",&displayx,&displayy,&displaymode);
    printf("Finished loading display configuration\n");
    fclose(configuration);


    printf("Finished loading configurations\n");
    configurationdone = 1;
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我自己想通了。程序只能输出过剩显示功能(我实际上在谈论的是glutDisplayFunc();)。