glutMouseFunc()でマウスボタンの操作を知る
#include <stdio.h> #include <GL/glut.h> void display(void){ glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void resize(int w, int h){ glViewport(0, 0, w, h); glLoadIdentity(); } void mouse(int button, int state, int x, int y){ switch(button){ case GLUT_LEFT_BUTTON: printf("left"); break; case GLUT_MIDDLE_BUTTON: printf("middle"); break; case GLUT_RIGHT_BUTTON: printf("right"); break; default: break; } printf(" button is "); switch(state){ case GLUT_UP: printf("up"); break; case GLUT_DOWN: printf("down"); break; default: break; } printf(" at (%d, %d)\n", x, y); } void init(void){ glClearColor(1.0, 1.0, 1.0, 1.0); } int main(int argc, char *argv[]){ glutInitWindowPosition(100, 100); glutInitWindowSize(320, 240); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); init(); glutMainLoop(); return 0; }
$ ./app
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
left button is down at (172, 123)
left button is up at (172, 123)
right button is down at (188, 65)
right button is up at (188, 65)
right button is down at (188, 65)
right button is up at (186, 123)
left button is down at (186, 123)
left button is up at (109, 116)
left button is down at (207, 222)
left button is up at (207, 222)
left button is down at (103, 74)
left button is up at (102, 170)
お、大分シューティングゲームのイメージが出来てきました。
### ボタンを押した位置から離した位置まで線を引く
#include <stdio.h> #include <GL/glut.h> void display(void){ glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void resize(int w, int h){ glViewport(0, 0, w, h); glLoadIdentity(); glOrtho(-0.5, (GLdouble)w - 0.5, (GLdouble)h - 0.5, -0.5, -1.0, 1.0); } void mouse(int button, int state, int x, int y){ static int x0, y0; switch(button){ case GLUT_LEFT_BUTTON: if(state == GLUT_UP){ glColor3d(0.0, 0.0, 0.0); glBegin(GL_LINES); glVertex2i(x0, y0); glVertex2i(x, y); glEnd(); glFlush(); } else { x0 = x; y0 = y; } break; case GLUT_MIDDLE_BUTTON: break; case GLUT_RIGHT_BUTTON: break; default: break; } } void init(void){ glClearColor(1.0, 1.0, 1.0, 1.0); } int main(int argc, char *argv[]){ glutInitWindowPosition(100, 100); glutInitWindowSize(320, 240); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); init(); glutMainLoop(); return 0; }
すげえええええええええええええええ
マウス処理そのものやんか
### 配列に位置を記憶する
#include#include #define MAXPOINTS 100 /* 記憶する点の数 */ GLint point[MAXPOINTS][2]; /* 座標を記憶する配列 */ int pointnum = 0; /* 記憶した座標の数 */ void display(void){ int i; glClear(GL_COLOR_BUFFER_BIT); if(pointnum > 1){ glColor3d(0.0, 0.0, 0.0); glBegin(GL_LINES); for (i = 0; i < pointnum; ++i){ glVertex2iv(point[i]); } glEnd(); } glFlush(); } void resize(int w, int h){ glViewport(0, 0, w, h); glLoadIdentity(); glOrtho(-0.5, (GLdouble)w - 0.5, (GLdouble)h - 0.5, -0.5, -1.0, 1.0); } void mouse(int button, int state, int x, int y){ switch(button){ case GLUT_LEFT_BUTTON: point[pointnum][0] = x; point[pointnum][1] = y; if(state == GLUT_UP){ glColor3d(0.0, 0.0, 0.0); glBegin(GL_LINES); glVertex2iv(point[pointnum - 1]); glVertex2iv(point[pointnum]); glEnd(); glFlush(); } else { } if(pointnum < MAXPOINTS - 1) ++pointnum; break; case GLUT_MIDDLE_BUTTON: break; case GLUT_RIGHT_BUTTON: break; default: break; } } void init(void){ glClearColor(1.0, 1.0, 1.0, 1.0); } int main(int argc, char *argv[]){ glutInitWindowPosition(100, 100); glutInitWindowSize(320, 240); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(resize); glutMouseFunc(mouse); init(); glutMainLoop(); return 0; }
画面のマウス操作そのもの。
なるほど、OSはこういうGLエンジンも使ってるってことか。