PDA

View Full Version : draw/show function using glutkeyboardfunc c++



Pedrosanchez87
03-17-2017, 05:48 PM
I have a switch statement which checks for user input using glutkeyboardfunc, but I am wanting to have something drawn when a certain key is pressed, i was thinking something along the lines of this should work, but it doesnt seem to be

main.cpp



void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a': lander.movement(-0.1, 0);
break;
case 'd': lander.movement(0.1, 0);
break;
case 'w': lander.movement(0.0, 0.1);
thruster.show();
break;
case 's': lander.movement(0.0, -0.1);
break;
default:
lander.movement(0, 0);
break;
}
}


thruster.cpp



void Thruster::show()
{
draw();
}

void Thruster::draw()
{
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f( 200,13);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(500, 3);
glColor3f(1.0, 0.0, 0.0);
glVertex2f( 900, 3);
glEnd();
}


I have also tried calling it using



void booster(char press) {
cin >> press;
if (press == 'w')
thruster.show();

};


but none of these ideas seem to be working, is there a way to do this?

GClements
03-17-2017, 08:08 PM
You shouldn't be issuing drawing commands other than from within the display callback. The display callback needs to call glutSwapBuffers() after everything has been drawn.

The keyboard and mouse callbacks should just be setting variables which affect what the display callback draws. If you aren't redrawing continuously, then call glutPostRedisplay() from the mouse/keyboard callbacks to schedule a redraw.

Pedrosanchez87
03-18-2017, 12:15 PM
I am running a timer, which calls on the display function, would i be able to put an if statement in the display function then that would be say something like if key==65 thruster.draw

GClements
03-18-2017, 01:56 PM
I am running a timer, which calls on the display function, would i be able to put an if statement in the display function then that would be say something like if key==65 thruster.draw
Create a global variable, set it to 1 in the key-press callback, set it to 0 in the key-release callback, have the display callback draw the thruster only if the variable is non-zero.

Pedrosanchez87
03-18-2017, 02:22 PM
I am not using a key realise etc only



void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a': lander.movement(-0.1, 0);

break;
case 'd': lander.movement(0.1, 0);
break;
case 'w': lander.movement(0.0, 0.1);
G_ThrustShow == 1;
break;
case 's': lander.movement(0.0, -0.1);
break;
default:
lander.movement(0, 0);
break;
}
}


but i have declared a global var G_ThrustShow set to 0

You can see in the code above w should set it to 1 then in the display i have



if (G_ThrustShow ==1)
{
thruster.show();
}


is this what you sort of meant, should this work, as it doesnt seem to be, maybe i have something wrong?

GClements
03-18-2017, 02:54 PM
I am not using a key realise etc
Then how are you going to know when to stop displaying the thruster? You need to use glutKeyboardUpFunc() to register a key-release callback.





G_ThrustShow == 1;


The above should use "=" (assignment) rather than "==" (test for equality). If you compile with warnings enabled, the compiler should warn you about that (e.g. "statement with no effect" with gcc -Wall).

Pedrosanchez87
03-18-2017, 02:59 PM
Then how are you going to know when to stop displaying the thruster? You need to use glutKeyboardUpFunc() to register a key-release callback.


That i was planning on working out when i had gotten it to work :D

and i havent actually went over any key release callbacks etc , im just new to programming really, so will need to look it up now

Pedrosanchez87
03-18-2017, 04:03 PM
Do you think this should work



void keyboard(unsigned char key, int x, int y)
{
if (key == (77));
{
lander.movement(0.0, 0.1);
G_ThrustShow = 1;
}

if (key == (!77));
{

G_ThrustShow = 0;
}
}

GClements
03-18-2017, 07:03 PM
Do you think this should work


if (key == (!77));


No.

That's the same as


if (key == 0);

Also, the trailing semicolons shouldn't be there (they'll terminate the "if" statement", making the following statement a separate statement rather than part of the "if" statement).

To be honest, I think that you need to spend some more time learning C++ before trying to learn OpenGL or GLUT.

Pedrosanchez87
03-18-2017, 07:24 PM
I know i really need to learn more c++ but im learning in uni and they sort of only taught the basics of c++ then moved on to opengl, no idea why.

But I did get it to work, its not perfect. I have the timer function resetting the variable, so it sort of flashes, but it is meant to represent flames so it sort of suits.