温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

opengl 第一个小游戏

发布时间:2020-07-10 00:23:08 来源:网络 阅读:791 作者:wzdouban 栏目:编程语言
#include <windows.h>    /* Windows的头文件 */
#include <gl/gl.h>      /* OpenGL32库的头文件 */
#include <gl/glu.h>     /* GLu32库的头文件 */
/* #include <gl/glaux.h>    // GLaux库的头文件 */
#include <gl/glut.h>    /* Glut库头文件 */
#include <time.h>
#include <iostream>
using namespace std;
int	aa[20][20]; int xx = 0; int yy = 0;
int	screenWidth	= 640;
int	screenHeight	= 480;
int	cc		= 0;
/* 全局变量 */
int winWidth( 500 ), winHeight( 500 );

#define MAX_CHAR 128

void Initialization()
{
	glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); /* 设置背景色为白色 */
}


void OnReshape( int w, int h )
{
	winWidth	= w;
	winHeight	= h;

	glViewport( 0, 0, w, h );       /* 设置视区大小 */

	glMatrixMode( GL_PROJECTION );  /* 投影模式 */
	glLoadIdentity();               /* 载入单位矩阵以初始化当前矩阵 */

	gluOrtho2D( 0, w, 0, h );       /* 将当前绘图区域设置为二维正交投影 */

	glMatrixMode( GL_MODELVIEW );   /* 模型视图模式 */
	glLoadIdentity();               /* 初始化当前矩阵 */
}


void drawString( const char* str )      /* 屏幕显示字体 */
{
	static int	isFirstCall = 1;
	static GLuint	lists;

	if ( isFirstCall )
	{
		isFirstCall = 0;
		/* 申请MAX_CHAR个连续的显示列表编号 */
		lists = glGenLists( MAX_CHAR );
		/* 把每个字符的绘制命令都装到对应的显示列表中 */
		wglUseFontBitmaps( wglGetCurrentDC(), 0, MAX_CHAR, lists );
	}
	/* 调用每个字符对应的显示列表,绘制每个字符 */
	for (; *str != '\0'; ++str )
	{
		glCallList( lists + *str );
	}
}


void OnDisplay()
{
	glClear( GL_COLOR_BUFFER_BIT );

	glColor3f( 0.0f, 0.0f, 0.0f );          /* 设置字体颜色 */
	glRasterPos2i( 0, winHeight - 15 );     /* 起始位置 */
	drawString( "  Hello,OpenGL." );        /* 输出的字符串 */

	glutSwapBuffers();                      /* 交换前后缓存区 */
}


void myInit()
{
	glClearColor( 1.0, 1.0, 1.0, 0.0 );
/* 设置背景颜色为亮白 */
	glColor3f( 0.0f, 0.0f, 0.0f );
/* 设置绘图颜色为黑色 */
	glPointSize( 4.0 );
/* 设置点的大小为4*4像素 */
	glMatrixMode( GL_PROJECTION );
/* 设置合适的矩阵 */
	glLoadIdentity();
	gluOrtho2D( 0.0, screenWidth, 0.0, screenHeight );
}


void myMouse2( int button, int state, int x, int y )
{
	if ( state == GLUT_DOWN )
	{
		if ( button == GLUT_RIGHT_BUTTON )
		{
			glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
			glClear( GL_COLOR_BUFFER_BIT );
			glFlush();
		}
	}
	return;
}


void drawDot( int x, int y )
{
	xx = (x - 30) / 20; yy = y / 20;
	glBegin( GL_POINTS );
	glVertex2i( x, y );
	/* 画一些点 */

	if ( 30 < x && x < 430 && 0 < y && y < 400 )
		cc++;
	/* aa[yy][xx] == 0;  思路1 */

	cout << cc << endl;


	if ( cc >= 4 )
	{
		glVertex2i( 450, 450 );
		/* 思路3 */
		myMouse2( GLUT_RIGHT_BUTTON, GLUT_DOWN, 300, 400 );
	}
	glEnd();
}


void myMouse( int button, int state, int x, int y )
{
	if ( state == GLUT_DOWN )
	{
		if ( button == GLUT_LEFT_BUTTON )
		{
			drawDot( x, screenHeight - y );
			glFlush();
		}else if ( button == GLUT_RIGHT_BUTTON )
		{
			glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
			glClear( GL_COLOR_BUFFER_BIT );
			glFlush();
		}
	}
	return;
}


void myDisplay()
{
	glClear( GL_COLOR_BUFFER_BIT );
	/* 清屏 */
	glBegin( GL_POINTS );

	/* 画一些点 */
	for ( int i = 0; i < 20; i++ )
	{
		for ( int j = 0; j < 20; j++ )
		{
			if ( aa[i][j] == 1 )
				glVertex2i( i * 20 + 33, j * 20 + 5 );
		}
	}
	glEnd();

	GLfloat curSizeLine = 2;
	glLineWidth( curSizeLine );
	glBegin( GL_LINES );
	for ( int i = 0; i <= 400; )
	{
		glVertex3f( 30, 0.0 + i, 0.0f ); glVertex3f( 430, 0.0 + i, 0.0f );
		glVertex3f( 30.0 + i, 0, 0.0f ); glVertex3f( 30.0 + i, 400, 0.0f );
		i += 20;
	}


	glEnd();
	glFlush();
	/* 送所有输出到显示设备 */
}


void funrand()
{
	int s = 4; int x = 0; int y = 0;
	srand( time( 0 ) );
	while ( s-- )
	{
		x		= rand() % 20; y = rand() % 20;
		aa[x][y]	= 1;
	}
}


int check()
{
	for ( int i = 0; i < 20; i++ )
	{
		for ( int j = 0; j < 20; j++ )
		{
			if ( aa[i][j] == 1 )
				return(1);
		}
	}
	return(0);
}


void main( int argc, char **argv )
{
	funrand();
	glutInit( &argc, argv );                        /* 初始化工具包 */
	glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );  /* 设置显示模式 */
	glutInitWindowSize( 640, 480 );                 /* 设置窗口大小 */
	glutInitWindowPosition( 100, 150 );             /* 设置窗口在屏幕上的位置 */
	glutCreateWindow( "大家来找茬,找找点" );                /* 打开屏幕窗口    //注册回调函数 */
	Initialization();
	glutDisplayFunc( myDisplay );    glutMouseFunc( myMouse );
	myInit();
	glutReshapeFunc( OnReshape );                   /* 重绘函数     打印提示信息好难 */

	glutMainLoop();                                 /* 进入循环 */
}


/****************
*
*  实现鼠标监听
*  运行效果为面板上加点
**BUG1  打点越界
*有的点能返回1  有的点确不能
**模糊判断
****************/


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI