第一种方式更简单些,我花的时间也非常少:
/*
file:gl_game_window.c
*/
#include<stdlib.h>
#include "GL.c"
int width,height,ul_x,ul_y;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectd(-0.5,0.2,0.5,0.8);
glText(-0.5,0.1, "e -- full screen");
glText(-0.5, 0, "d -- normal window");
glText(-0.5, -0.1,"right click -- menu");
glText(-0.5,-0.2,"ESC -- quit");
glutSwapBuffers();
}
void keyboard(unsigned char key ,int x,int y)
{
if(key == 27)exit(0);
else if(key == 'e')
glutFullScreen();
else if(key == 'd')
{
glutReshapeWindow(width,height);
glutPositionWindow(ul_x,ul_y);
}
}
void menu(int value)
{
printf("menu %d/n",value);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(width=400,height=400);
glutInitWindowPosition(ul_x=50,ul_y=100);
glutCreateWindow("e--全屏,d--窗口");
int m = glutCreateMenu(menu);
glutAddMenuEntry("Red",1);
glutAddMenuEntry("BLUE",2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
第二种方式我花了不少时间,不断试错之后才知必须处理特别键的按下和弹起,否则出现莫名其妙的错误。
/*
file:xx.c
*/
#include <stdlib.h> //exit
#include "GL.c"
char FPS[] = "FPS:0";
void init();
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(0.0,1.0,1.0);
get_fps(FPS);
glText(-1,0.8,FPS);
glText(-1,0,"e -- Game Mode ");
glText(-1,-0.2,"d -- Window Mode");
glText(-1,-0.4,"Esc -- exit");
glutSwapBuffers();
frame++;
}
void normal_key(unsigned char key, int x, int y) {
if(27 == key)
{
exit(0);
}
else if('e' == key)
{
glutGameModeString("640x480");
if(glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
{
glutEnterGameMode();
init();
}
}
else if('d' == key)
{
if(glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
glutLeaveGameMode();
}
}
void special_key(int key, int x, int y){}
void special_key_up(int key, int x, int y) {}
void init() {
glutDisplayFunc(display);
glutIdleFunc(display);
glutIgnoreKeyRepeat(1);
glutKeyboardFunc(normal_key);
glutSpecialFunc(special_key);
glutSpecialUpFunc(special_key_up);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,360);
glutCreateWindow(argv[0]);
init();
glutMainLoop();
return 0;
}
为了完整起见,附加用到的文件:
/*
file:GL.c
-------Utility Library-------*/
#include <GL/glut.h>
#include<time.h>
#include <stdio.h>
static int frame,current_time,last_time;
int get_fps(char *str);
void get_time(char *str);
void glText(double x, double y, char *str);
int get_fps(char *str)
{/*return nonzero if updated */
current_time = glutGet(GLUT_ELAPSED_TIME);
if(current_time -last_time > 1000)
{
sprintf(str,"FPS:%.2f",
(frame*1000.0/(current_time - last_time)));
last_time = current_time;
frame = 0;
}
else return 0;
return 1;
}
void get_time(char *str)
{
time_t raw;
struct tm *info;
time(&raw);
info = localtime(&raw);
sprintf(str,"%s",asctime(info));
}
void glText(double x, double y, char *str)
{
glRasterPos2f(x,y);
while(*str)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,*str);
str++;
}
}