c++ - Keyboard input doesn't work, works when clicking the mouse -
i have code:
#include <iostream> #include <gl/glut.h> using namespace std; bool* keys = new bool[256]; void keyboarddown(unsigned char key, int x, int y) { keys[key] = true; } void keyboardup(unsigned char key, int x, int y) { keys[key] = false; } void reshape(int width, int height) { glfloat fieldofview = 90.0f; glviewport(0, 0, (glsizei)width, (glsizei)height); glmatrixmode(gl_projection); glloadidentity(); gluperspective(fieldofview, (glfloat)width / (glfloat)height, 0.1, 500.0); glmatrixmode(gl_modelview); glloadidentity(); } void draw() { if (keys['e']) cout << "e" << endl; glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glmatrixmode(gl_modelview); glloadidentity(); glbegin(gl_quads); glcolor3f(0.1, 0.1, 0.1); glvertex3f(1.0, 1.0, -1.0); glcolor3f(0.1, 0.9, 0.1); glvertex3f(1.0, -1.0, -1.0); glcolor3f(0.9, 0.1, 0.1); glvertex3f(-1.0, -1.0, -1.0); glcolor3f(0.1, 0.1, 0.9); glvertex3f(-1.0, 1.0, -1.0); glend(); glflush(); glutswapbuffers(); } void initgl(int width, int height) { reshape(width, height); glclearcolor(0.1f, 0.5f, 0.7f, 1.0f); glcleardepth(1.0f); glortho(0, 1, 0, 1, 1, 10); glenable(gl_depth_test); gldepthfunc(gl_lequal); } /* initialize glut settings, register callbacks, enter main loop */ int main(int argc, char** argv) { glutinit(&argc, argv); glutinitdisplaymode(glut_double | glut_rgba | glut_depth); glutinitwindowsize(800, 800); glutinitwindowposition(100, 100); glutcreatewindow("perspective's glut template"); glutkeyboardfunc(keyboarddown); glutkeyboardupfunc(keyboardup); glutspecialfunc(keyboardspecialdown); glutspecialupfunc(keyboardspecialup); glutreshapefunc(reshape); glutdisplayfunc(draw); glutignorekeyrepeat(true); // ignore keys held down initgl(800, 600); glutmainloop(); return 0; }
when start program, writes 'e' when press on mouse. can't find problem. why work when mouse held down?
you dont have mouse functions in code, thats why not working. insert here : http://www.zeuscmd.com/tutorials/glut/03-mouseinput.php
#include <iostream> #include <gl/glut.h> using namespace std; bool lbuttondown = false; bool init() { return true; } void display() { } void mouse(int button, int state, int x, int y) { if (button == glut_right_button) { if (state == glut_down) cout << "right button pressed" << endl; else cout << "right button lifted " << "at (" << x << "," << y << ")" << endl; } else if (button == glut_left_button) { if (state == glut_down) lbuttondown = true; else lbuttondown = false; } } void motion(int x, int y) { if (lbuttondown) cout << "mouse dragged left button @ " << "(" << x << "," << y << ")" << endl; } void motionpassive(int x, int y) { cout << "mouse moved @ " << "(" << x << "," << y << ")" << endl; } void entry(int state) { if (state == glut_entered) cout << "mouse entered" << endl; else cout << "mouse left" << endl; } int main(int argc, char *argv[]) { glutinit(&argc, argv); glutinitwindowposition(200, 200); glutinitwindowsize(200, 200); glutcreatewindow("03 - mouse input"); glutdisplayfunc(display); glutmousefunc(mouse); glutmotionfunc(motion); glutpassivemotionfunc(motionpassive); glutentryfunc(entry); if (!init()) return 1; glutmainloop(); return 0; }
Comments
Post a Comment