⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myhouse.cpp

📁 鼠标键盘操作 b画线
💻 CPP
字号:
#include <GL/glut.h>
#include <gl/Gl.h>
#include"house.h"

const int screenWidth = 660;
const int screenHeight = 660;

polygon list[80];
int st;
int indexi, indexj;
int p = 0;


//<<<<<<<<<<<<<<<<<<<<  myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void){
	glClearColor(1.0, 1.0, 1.0, 0.0);    //the background is white
	glColor3f(0.0f,0.0f,0.0f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
}


//<<<<<<<<<<<<<<<<<<<  someFunc >>>>>>>>>>>>>>>>>>>
   void drawPolyLine(int x,int y){
		   list[p].ver[++list[p].last].x = x;
		   list[p].ver[list[p].last].y = screenHeight -y;
		   
		   glClear(GL_COLOR_BUFFER_BIT);
		   glFlush();
   }



  void searchDot(int x, int y){
            x = x;
	        y = screenHeight-y;
		   for(int i=0; i<=p;i++){
			   for(int j=0; j<=list[i].last; j++){
				   if (x<list[i].ver[j].x+5&&x>list[i].ver[j].x-5
					   &&y<list[i].ver[j].y+5&&y>list[i].ver[j].y-5){
					   indexi=i;indexj=j;
					   }		  
			   }
		   }
   }



  void delDot(){
	  for(int m=0; m<=list[indexi].last; m++){
		  list[indexi].ver[indexj] = list[indexi].ver[indexj+1];} 
  }
  void delDotLast(){ 
	  list[indexi].last-=1;
  }



  void movDot(int x, int y){ 
	   x = x;
	   y = screenHeight- y;
	  list[indexi].ver[indexj].x =x;
	  list[indexi].ver[indexj].y=y;
  }


//<<<<<<<<<<<<<<<<<<  myDisplay >>>>>>>>>>>>>>>>>>>
void myDisplay(void){
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(0.5, 0.0, 1.0);
   glLineWidth(3.0);

   for(int j = 0; j<=p; j++){//draw all the points
	   glBegin(GL_LINE_STRIP);
	   for(int i =0; i<=list[j].last;i++)
		   glVertex2i(list[j].ver[i].x, list[j].ver[i].y);
	   glEnd();
   }   

   glFlush();
}


//<<<<<<<<<<<<<<<<<<  myReshape >>>>>>>>>>>>>>>>>>
void myReshape(GLsizei w, GLsizei h){
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
}



//<<<<<<<<<<<<<<<<<  myKeyboard >>>>>>>>>>>>>>>>>>
void myKeyboard(unsigned char key, int mousex, int mousey){ 
   GLint x = mousex;
   GLint y = screenHeight-mousey;
 
   switch(key){
case 'b':
	st=1;
	p++;
	break;
case 'd':
	st=2;
	break;
case 'm':                          
	st=3;
	break;
case 'r':
	glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0, 1.0, 1.0, 0.0);  
	glFlush();
	break;
case 'q':
	exit(-1);
default:
	break;
   }
}



//<<<<<<<<<<<<<<<<<<  myMouse &myMoveMouse>>>>>>>>>>>>>>>>>>>>
   void myMouse(int button, int state,int x,int y){
	   if(button == GLUT_LEFT_BUTTON && state ==GLUT_DOWN&&st==1){	   
			   drawPolyLine(x, y);
		       glutPostRedisplay();
	       }
		   else if(button == GLUT_LEFT_BUTTON && state ==GLUT_DOWN&&st==2){
		       searchDot(x, y);
			   if(indexj!=list[indexi].last)
				   delDot();
			   else
				   delDotLast();

		       glutPostRedisplay();
		   }
   }

   void myMovedMouse(int x,int y){
	   if (st==3){
		   searchDot(x,y);
		   movDot(x,y);
		   glutPostRedisplay();
	   }
   }


//<<<<<<<<<<<<<<<<<<<<<  main >>>>>>>>>>>>>>>>>>>>>
   void main(int argc, char** argv){
	   glutInit(&argc, argv);
	   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	   glutInitWindowSize(screenWidth,screenHeight);
	   glutInitWindowPosition(300, 300);
	   glutCreateWindow("Happy Tree");

	   glutDisplayFunc(myDisplay);
	   glutReshapeFunc(myReshape);
	   glutMotionFunc(myMovedMouse);
	   glutMouseFunc(myMouse);	
	   glutKeyboardFunc(myKeyboard);
	   
	   myInit();
	   glutMainLoop();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -