OpenGL/ How can I stop glutTimerFunc? - animation

I want to make my human figure walk when I press '0', and stop when I press '0' again.
I managed to make it walk as I want, but I do not know the way to stop glutTimerFunc.
Is there any OpenGL function that makes glutTimerFunc stop?
And here is my code.
if (key == '0') {
if (!walk_working_flag) {//when walk does not working
walk();
walk_working_flag = true;
}
else {
//stop walking
glutTimerFunc(100, stop_animation , 0);
}
}
in stop_animation function, no rotation occurs.
Thank you.

As stated in the API docs:
There is no support for canceling a registered callback. Instead,
ignore a callback based on its value parameter when it is triggered.

Related

Check of checkmate in chess

I am using this object oriented design of chess. I have implemented generating of valid moves for all pieces. Now I am trying implement check of checkmate.
I tried to make a method, which if player have moves, which cancel the checkmate. But the program end with StackOverflowError.
I delete the method. But the pseudoalgorithm of the method was something like that
boolean isGameOver(arg){
if(playerIsInCheck){
if(!hasValidMoves){
print("checkmate");
return true;
}
else{
return false;
}
}
else{
if(!hasValidMoves){
print("stalemate");
return true;
}
else{
return false;
}
}
}
I don't know how to check if the move cancel the checkmate. Can anyone advise me? I do not need all the code written in any programming language. Pseudoalgorithm will be sufficient.
The algorithm for checking for checkmate is as follows:
public boolean checkmated(Player player) {
if (!player.getKing().inCheck() || player.isStalemated()) {
return false; //not checkmate if we are not
//in check at all or we are stalemated.
}
//therefore if we get here on out, we are currently in check...
Pieces myPieces = player.getPieces();
for (Piece each : myPieces) {
each.doMove(); //modify the state of the board
if (!player.getKing().inCheck()) { //now we can check the modified board
each.undoMove(); //undo, we dont want to change the board
return false;
//not checkmate, we can make a move,
//that results in our escape from checkmate.
}
each.undoMove();
}
return true;
//all pieces have been examined and none can make a move and we have
//confimred earlier that we have been previously checked by the opponent
//and that we are not in stalemate.
}
I can't tell you why you are getting a stack overflow without seeing your method definitions, but I can explain how you check for mate-cancelling moves ( no pseudocode, sorry).
Basically, you generate a List of all possible Moves (Pseudolegals) and you let your programm try each of them. If the players king is no longer hittable in the resulting position (in your case you use the IsInCheck method), the current move is cancelling the mate.
If you do need the pseudocode, write a comment and I'll see what I can do.

How handle directions with interrupt in Proteus

I want to implement Snake game with Atmega16 I following this tutorial. My problem is I couldnt handle the KeyPad ! I found another tutorial (here) about one Button with interrupt But in Snake we need 4 button for our directions And I dont know how to handle this in Proteus ?
we have 3 external interrupt and 4 button I dont know what to do :(
Any help really appreciated
EDITED :
This is my last try but now it's not detect directions and always get into first condition of if-statement in my Interrupt and not check other conditions
Main :
void main()
{
TCCR0=0X01;
DDRC=0XFF;
DDRB=0XFF;
DDRD|=(1<<PD0)|(1<<PD1)|(1<<PD7);
DDRD&=~((1<<PD2)|(1<<PD3)|(1<<PD4)|(1<<PD5)|(1<<PD6));
DDRA=0xFF;
pos=1;
position();
right();
while(1)
{
no_inp();
init_interrupts();
}
}
And here my interrupt :
ISR (INT0_vect){
sss=0;
if((PIND&(1<<PIND3))&& status!=3)
{
right();
status=1;
}
else if((PIND&(1<<PIND4))&& status!=4)
{
up();
status=2;
}
else if((PIND&(1<<PIND5))&& status!=1)
{
left();
status=3;
}
else if((PIND&(1<<PIND6))&& status!=2)
{
down();
status=4;
}
else
{
no_inp();
}
}
You always end up in the first condition because you use "active low" logic for your buttons, but you check if the bit is HIGH. However, when not pressed, your button inputs are pulled HIGH. So just invert your if-condition and check if the respective pin is LOW (actually pressed) instead.

alternating arduino outputs based on if statement

This is my first post on stackoverflow and one of my first arduino projects. Thanks for your help.
I am building a relay controller that alternates which battery is being charged based on an LED indicator light. I'm getting close, but I am having difficulty with the control logic.
I am able to read the LED and determine when the battery should be switched (my boolean variable says switch or don't). It also seems that a for statement is the best way to proceed to the next output. I found this example but it doesn't quite fit: http://arduino.cc/en/Tutorial/ForLoop
Looking for these states:
https://www.dropbox.com/s/ue192ebrhng3xcw/relaystates.jpg?dl=0
This is my first attempt:
for (int thisPin = 9; relayswitch = true; thisPin++){
// turn the last pin off:
digitalWrite(thisPin-1, LOW);
delay(2);
// turn the next pin on:
digitalWrite(thisPin, HIGH);
}
//resets relay switch indicator
boolean relayswitch = false;
What is your question?
One answer might be: you have implemented an endless loop.
for (...; relayswitch = true; ...){
// code that will not alter relayswitch
}
Notice that you do NOT check for a condition with the assignment operator "=". A condition would be checked with "==". Thus the "while condition" of the for loop will be true no matter what you do in the body of the loop. However even if you would check with "==" it would not matter as you do not alter this variable inside the loop.

XEvent Handling in animation

I'm having some understanding problem of event-handling with Xlib functions.
My question would be - how do i press a key during an animation, without disturbing the animation.
My setup so far, is that i have some animation in a while loop and want to achieve a KeyPress event which modifies a parameter.
It looks something like this
while(1){
XNextEvent(dis, &report);
switch (report.type) {
case KeyPress:
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//}}
//...Some animation where parameter a is used to modify animation...//}
Now, the problem is that i have to press the key consistently to get the animation on my screen, otherwise nothing appears. I've tried some multiple code-modifications, with KeyRelease etc. but i don't have clou, really.
Trivially said - i need to hook a key during animation without the XNextEvent process, waiting for any event. But without the XNextEvent statement in my code, conditional statements for KeyPress event checking aren't working.
I guess formally this would mean:
while(1){
if(report.type==KeyPress) {
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//}}
//...Some animation where parameter a is used to modify animation...//}
Use XPending() to check for XEvents before getting them with XNextEvent().
XPending() returns then number of events in the event queue so modify your loop:
while(1){
if (XPending(dis) > 0) {
XNextEvent(dis, &report);
switch (report.type) {
case KeyPress:
if (XLookupKeysym(&report.xkey, 0) == XK_space){
//...modify parameter a..//
}
}
}
//...Some animation where parameter a is used to modify animation...//
}

GTK+ - How to listen to an event from within a method?

I'm writing an application that runs an algorithm, but allows you to 'step through' the algorithm by pressing a button - displaying what's happening at each step.
How do I listen for events while within a method?
eg, look at the code I've got.
static int proceed;
button1Event(GtkWidget *widget)
{
proceed = 0;
int i = 0;
for (i=0; i<15; i++) //this is our example 'algorithm'
{
while (proceed ==0) continue;
printf("the nunmber is %d\n", i);
proceed = 0;
}
}
button2Event(GtkWidget *widget)
{
proceed = 1;
}
This doesn't work because it's required to exit out of the button1 method before it can listen for button2 (or any other events).
I'm thinking something like in that while loop.
while(proceed == 0)
{
listen_for_button_click();
}
What method is that?
The "real" answer here (the one any experienced GTK+ programmer will give you) isn't one you will like perhaps: don't do this, your code is structured the wrong way.
The options include:
recommended: restructure the app to be event-driven instead; probably you need to keep track of your state (either a state machine or just a boolean flag) and ignore whichever button is not currently applicable.
you can run a recursive main loop, as in the other answer with gtk_main_iteration(); however this is quite dangerous because any UI event can happen in that loop, such as windows closing or other totally unrelated stuff. Not workable in most real apps of any size.
move the blocking logic to another thread and communicate via a GAsyncQueue or something along those lines (caution, this is hard-ish to get right and likely to be overkill).
I think you are going wrong here:
while(proceed == 0)
{
listen_for_button_click();
}
You don't want while loops like this; you just want the GTK+ main loop doing your blocking. When you get the button click, in the callback for it, then write whatever the code after this while loop would have been.
You could check for pending events & handle the events in while loop in the clicked callback. Something on these lines:
button1Event(GtkWidget *widget)
{
proceed = 0;
int i = 0;
for (i=0; i<15; i++) //this is our example 'algorithm'
{
while (proceed ==0)
{
/* Check for all pending events */
while(gtk_events_pending())
{
gtk_main_iteration(); /* Handle the events */
}
continue;
}
printf("the nunmber is %d\n", i);
proceed = 0;
}
}
This way when the events related click on the second button is added to the event queue to be handled, the check will see the events as pending and handle them & then proceed. This way your global value changes can be reflected & stepping should be possible.
Hope this helps!
If you want to do it like this, the only way that comes to my mind is to create a separate thread for your algorithm and use some synchronization methods to notify that thread from within button click handlers.
GTK+ (glib, to be more specific) has its own API for threads and synchronization. As far as I know Condition variables are a standard way to implement wait-notify logic.

Resources