1 registered members (AndrewAMD),
827
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Latency of value assignment in loop with wait
#462391
09/26/16 17:45
09/26/16 17:45
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
Budapest
|
I tried to make a code that is able to move a panel with mouse drag. I was surprised when turned out there is a latency of value assignment in loop with wait. Here is a sample code:
function drag_panel(PANEL* inPanel) {
int old_x;
int old_y;
int dif_x, dif_y;
diag("\ndrag_panel");
old_x = mouse_cursor.x;
old_y = mouse_cursor.y;
while(mouse_left) {
diag_var("\nold_x:%4.0f", old_x);
diag_var("\nold_y:%4.0f", old_y);
dif_x = mouse_cursor.x - old_x;
dif_y = mouse_cursor.y - old_y;
if(0 != dif_x || 0 != dif_y) {
old_x = mouse_cursor.x;
old_y = mouse_cursor.y;
diag_var("\nmouse_pos.x:%4.0f", mouse_cursor.x);
diag_var("\nmouse_pos.y:%4.0f", mouse_cursor.y);
inPanel.pos_x += dif_x;
inPanel.pos_y += dif_y;
diag_var("\nnew old_x:%4.0f", old_x);
diag_var("\nnew old_y:%4.0f", old_y);
}
wait(1);
}
}
The content of the old_x and the old_y variables will be changed after 15-30 frames. It is strange. I read the wait documentation, but i can't find out why the heck is working this way.. What is the reason? What should be the solution? What a pity, lite-c has no option to declare volatile variables.
|
|
|
Re: Latency of value assignment in loop with wait
[Re: Superku]
#462419
09/28/16 18:53
09/28/16 18:53
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
Budapest
|
For the record, here is the well working code:
function drag_panel(PANEL* inPanel) {
int old_x;
int old_y;
int dif_x, dif_y;
diag("\ndrag_panel");
old_x = inPanel.pos_x - mouse_cursor.x;
old_y = inPanel.pos_y - mouse_cursor.y;
while(mouse_left) {
diag_var("\nold_x:%4.0f", old_x);
diag_var("\nold_y:%4.0f", old_y);
dif_x = inPanel.pos_x - mouse_cursor.x - old_x;
dif_y = inPanel.pos_y - mouse_cursor.y - old_y;
if(0 != dif_x || 0 != dif_y) {
diag_var("\nmouse_pos.x:%4.0f", mouse_cursor.x);
diag_var("\nmouse_pos.y:%4.0f", mouse_cursor.y);
inPanel.pos_x += dif_x;
inPanel.pos_y += dif_y;
diag_var("\ndif_x:%4.0f", dif_x);
diag_var("\ndif_y:%4.0f", dif_y);
}
wait(1);
}
}
You can see old_x is computed only one time, before the start of the loop. So, it can be reused again and again because it wouldn't changed while the cycle runs.
|
|
|
Re: Latency of value assignment in loop with wait
[Re: Aku_Aku]
#462438
09/29/16 19:09
09/29/16 19:09
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
To be honest, I don't really believe that there's any latency when assigning values. If you write "var x = y;" then after this line of code has been executed x has the same value as y. I don't see how there can be any delay.. programming like that just wouldn't make any sense.
If I had to guess it's diag_var() causing some delay.
Last edited by Kartoffel; 09/29/16 19:11.
POTATO-MAN saves the day! - Random
|
|
|
Re: Latency of value assignment in loop with wait
[Re: Kartoffel]
#462439
09/29/16 19:18
09/29/16 19:18
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
Budapest
|
Quote from the Online Manual. A remark in the description of the wait statement: While a function is waiting, all other functions - including the calling function - continue to run in parallel, so all global variables and all pointers except my and you can possibly change during the wait time. !! While the content of local variables is preserved, their addresses change after every wait() because the function runs every time in a different stack frame. Please concentrate on the part that deals with local variables. I think that could be the reason. Or a glitch in the Acknex engine. Who knows? Finally, you have the option to try out, in your own code. Or try with my code that i attached in my opening post.
Last edited by Aku_Aku; 09/29/16 19:20.
|
|
|
Re: Latency of value assignment in loop with wait
[Re: Aku_Aku]
#462440
09/29/16 20:06
09/29/16 20:06
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Well as long as I'm not misinterpreting that quote, this only applies to local pointers. However, in your code you're not using any pointers Have you tried it with something other than diag_var (like DEBUG_VAR, for example)?
Last edited by Kartoffel; 09/29/16 20:07.
POTATO-MAN saves the day! - Random
|
|
|
|