Latency of value assignment in loop with wait

Posted By: Aku_Aku

Latency of value assignment in loop with wait - 09/26/16 17:45

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:
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.
Posted By: Superku

Re: Latency of value assignment in loop with wait - 09/27/16 16:03

What do you mean with 15-30 frames delay? How are you measuring that delay?
Or do you mean the visible delay when you drag a panel - assuming you use the Windows mouse pointer and not mouse_map?
Posted By: Aku_Aku

Re: Latency of value assignment in loop with wait - 09/28/16 18:38

I opened the acklog.txt file.
I saw in there, if the mouse_cursor position was changed,
my loop 15-30 times run while the new position value appeared as the value in variable, where that should displayed at the first run.
So i ment the delay is this: the variable should show the new position value at the first run of the cycle, not after 15-30.
Please assume that, i used the mouse_cursor as it can be see in the code. By the way this thing impacts visually not on the pointer, that was ok, but on the panel that i tried to move, that moved like a fool.
Posted By: Aku_Aku

Re: Latency of value assignment in loop with wait - 09/28/16 18:53

For the record, here is the well working code:
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.
Posted By: Superku

Re: Latency of value assignment in loop with wait - 09/29/16 10:55

I was asking for the mouse pointer/ mouse_map because there is a visible delay between the Windows (!) mouse pointer, the white arrow or "hand", and the in-game mouse_cursor/_map positions.
When you only use the former and don't set mouse_map instead, this can lead to a visible delay between the mouse cursor and the window that's being dragged, especially on low framerates.
Posted By: Aku_Aku

Re: Latency of value assignment in loop with wait - 09/29/16 18:56

OK i understand you. Thank for your helping will.
But the latency was manifested in the value assignment.
Posted By: Kartoffel

Re: Latency of value assignment in loop with wait - 09/29/16 19:09

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.
Posted By: Aku_Aku

Re: Latency of value assignment in loop with wait - 09/29/16 19:18

Quote from the Online Manual.
A remark in the description of the wait statement:
Quote:
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.
Posted By: Kartoffel

Re: Latency of value assignment in loop with wait - 09/29/16 20:06

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 confused
Have you tried it with something other than diag_var (like DEBUG_VAR, for example)?
Posted By: Aku_Aku

Re: Latency of value assignment in loop with wait - 09/29/16 22:57

I think the "local variables" are not only pointers...
Posted By: Kartoffel

Re: Latency of value assignment in loop with wait - 09/30/16 11:21

confused I don't know but it says that local variables are preserved, which means they don't change inbetween wait() calls. So if you assign a value to a variable, wait() isn't going to change it.
It's address isn't preserved after each wait() but in your code you're working with the values of your variables (which stay the same), not their addresses.
Posted By: Aku_Aku

Re: Latency of value assignment in loop with wait - 09/30/16 19:48

But, they are changing.
But only after a few frames, as i observed.
© 2024 lite-C Forums