OpenGL 满屏的两种方式:借助glutFullScreen ,借助glutEnterGameMode

 

 第一种方式更简单些,我花的时间也非常少:

/*
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++;
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值