Problems with precalculated arrays.

Blitz3D Forums/Blitz3D Beginners Area/Problems with precalculated arrays.

Hallo to all.
I've a problem related to arrays.
Actually i have a program that move object based on some points that i paint on the screen (something similar to a pathfinder...);after i click the start button the object follow those points.
All goes ok if i use abs,sin,cos,atan "on the fly"....
But i have noticed that after a wile or after more object on screen the speed of the program fall down......
So i have think that if i use precalculated tables of sin and cos may i speed up the program a bit.
But here's the problem.....
i use two array to store the sin and cos in this manner.
Dim Sin2#(360)
Dim Cos2#(360)
For loop=0 To 359
	Sin2#(loop)=Sin(loop)		
	Cos2#(loop)=Cos(loop)
Next

So i know every valor of the 360 possible angolation.
I have inserted this piece of code into my mani in this manner:
a\angolo=ATan2(a\pathy[a\current_path+1]-a\pathy[a\current_path],a\pathx[a\current_path+1]-a\pathx[a\current_path])
a\cx=a\cx+Cos2#(a\angolo)*a\velocita

The angle depends on a previous "atan2" so if i have a decimal angle......the approximation of the angle is calculated by blitz on the fly or i have to precalculate it too?
I tell this because if i start the movement of the object,when he reach some point (depends on the path that i have drawn...)i have a "array index out of bounds" error!
What can i do to resolve this problem?
I can't precalculate ALL the movement calculations because they depends on the path drawn....and the possibility are something infinite!
Many thanks.

'Array index out of bounds' means that we are trying to access and array subscript that does not exist...

Dim NumberArray(1000)
Print NumberArray(1001)

Will produce the same error.

So somewhere there is an attempt to access a subscript beyond 360 (or possibly, I suppose, less than zero).

With the speed of modern computers now. I wouldn't even bother with pre computed cos tables.

Get the computer to do em on the fly.

Atan2 returns angles between -180 and 180, so you need to add 180 to a\angolo...Ie:
a\angolo=ATan2(a\pathy[a\current_path+1]-a\pathy[a\current_path],a\pathx[a\current_path+1]-a\pathx[a\current_path])
a\angolo = a\angolo+180.0
a\cx=a\cx+Cos2#(a\angolo)*a\velocita


I have tried to add 180.0 at every angles but the object go out of the path.....mmm....don't figured out why i have an out of bounds.....
Another thing...is possible that my speed-down of the program is related to many lines and points drawn at the same time on the screen???

Angles are unchanged when you add 360.

So if ATan2 is negative you add 360, e.g. -10 becomes +350.

So what can i do is add 360 at every angles?
And make an array of 720 instead of only 360 right?

YESSSSSSSSS!
It works!!!
I didn't know that adding 360 the angle did not change so that i didn't have negative numbers!
A simple question....where do you read those information?
Because i'm not much greath at maths,i want to know such things.....some maths books maybe?
Or some internet site?
Many thanks for the solution,the problem it was only negative numbers so if my array start at 0.....negative aren't in.

The only problem is that after a while the speed of the movement come down,lookup tables yes or not...
So the problem isn't math....something screw up in the calculation...argh

Post more of your code. We might be able to help.

Why not add 360 and mod 360

So you end up with a = (a + 360) mod 360

which would return a value from 0 - 359

However, I would concur with the why bother?

Ok,i have tested under another machine and that effect apparently don't happen...(i try to correct it...if i don't succeed i post the code[it's big....])
For the speed of the program...i think that if i store the xcoords and ycoords in an array i spped up a lot the whole procedure.
But it seems that something go wrong.
I have two function:one that create the object and assign it's starting value,and another that calculate the xcoords and ycoords and draw the object.
For storing the x and y coordinates i have dimensioned 2 arrays and i have inserted in the function that draw the object this line:
array(position)=xcoord
array2(position)=ycoord
position=position+1
So i think that the info are coorectly stored because every frame "position"isupgraded to store the new data.
After the animation stop i have a function that write to file the data in this manner
for x=0 to max_array_position
writestring (file,array(position))
writestring (file,array2(position))
position=position+1
next
closefile

But when i open the file in a text editor....i see that only the first x&y coords are written!the other value i see is a bunch of "0,0",something like the array position are empty(i think).
I try to write an example to show the procedure of storing data that i use:

createobject()

function move_object()
*the comand for calculating the coords*
drawimage image,posx,posy
array(array_position)=posx
array2(array_position)=posy
array_position=array_position+1
if animationstop=1 then stop
end function

*main*
create object
if i click button 1 move_object()
if i click button 2 save_coords()
if i click button 3 exit_program
*end main*

function save_coords()
*write file*
for a=0 to array_max_position
writestring (file,array(array_position))
writestring (file,array2(array_position))
array_position=array_position+1
next
closefile file


Someone know where's the error??

(the strange thing is that i use the SAME saving function for storing other coordinates and in this case works well...)

function save_coords()
*write file*
for a=0 to array_max_position
writestring (file,array(array_position))
writestring (file,array2(array_position))
array_position=array_position+1
next
closefile file


surely you should be using the loop in the writestring function otherwise. Something like this.


function save_coords()
*write file*
for a=0 to array_max_position
    writestring (file,array(a))
    writestring (file,array2(a))
next
closefile file


I have changed the function as you suggested but the same effect.....only the first arrays value are writed....the others "0,0"....
Maybe the error is in the moveobject function?
I increment the position of the two array inside the function move.....

I think i have discovered the error...correct me if i'm wrong(i have read the output file and it seems to be ok.....this time the file is full of x&y values[don't know if they are correct :P].
The string that increment the array position MUST BE into the main loop!
otherwise only the first position is stored (don't know why.....the function move_object is always active!).

Did you declare array_position global? A new instance is made every time you run the function, unless it's a global.

Yes is global.
But i think i drop the idea of precalculated arrays of x&y coords.
What can i do when the object on-screen are many-many more then one?
I can't make precalculated table for every object...it's a lot of work!
Another question(sorry if i bother you...):with the modern pc (from p2 350 to athlon 3000 ecc ecc)is better make calculation on the fly or storing info in memory and read after the calculation???
Example:i make 30 alien for a shoot-em-up....is better that every alien calculate the route when is onscreen or is better that every alien read the data stored in array when onscreen??
(It's simplier to make my code work on the fly rather than pracalculate everything....).
One last question (alleluja!):i have read many times that is better divide the game logic in 2 trunks (or more)
load time
calculation time
display time
Actually i made only 3 trunks but in this manner
1)load time
2)object creation (types and arrays)
3)display and route calculation

Maybe my speed-down of the program is related to this method??

Example:i make 30 alien for a shoot-em-up....is better that every alien calculate the route when is onscreen or is better that every alien read the data stored in array when onscreen??
(It's simplier to make my code work on the fly rather than pre calculate everything....).


The maths ability of modern cpu are much faster then reading pre stored data in memory. I promise you that the delay in your program is not caused by this aspect.

If you should precalculate or not depends on lots of stuff. But in general, you should only precalculate if it runs too slow on the fly. Your trunk method is not a problem, but statistics show that when a game gets slower with time, it's very likely a memory leak, search trough everything you load, create or copy in the main loop, watch out for handle overwrites.

Edit: By the way, try running your program while monitoring the memory use with taskmanager.

oldTime#=MilliSecs() 

For loop = 1 To 5000000
	a = Cos(loop)
Next
newtime# = MilliSecs()
dif# = newtime - oldtime 

Print "Still waiting!" + dif


Run this on your system.

My system can do around 5 million calcs a second or 83,000
calcs every frame assumming your game is running at 60 frames a sec.

I have 1052.0 as a result with the above code.
I have tried to see the memory usege with task manager....nothing wrong at all!
But still if i wait some seconds (about 1 minute or 2) the speed fall down a lot.....
In the main loop i have many line,point,rect commands.
I run always in debug mode,windowed (not compiled it runs in the editor).
MMM...i try to implement a simple frame limiter (now i don't have one because this program isn't finished yet).

I have tried the "createtimer=60 ..... waittimer(60)" but the speed loss is always here....
Another thing:i have noticed that EVERY graphical action slows down,every the mouse movements......if i cancel everything (using the cancel button)types,array,ecc ecc and start again....all go well until the same amount of time.....another thing.
I have two valuse for storing the speed of the object (a\speed for a types,b\speed for b types)and...if i load a path and when the animation of the path is started....if i create a path *BOING* the 2 speed sums itself!!!
But i NEVER make an addiction of the two valor,and they are stored in different variables!!!
Argh......

Valgar,

You can send the code to me and I'll have a look at it if you want.
I'm no blitz expert buy two sets of eyes are better then one.

Check my profile for my email addy.

Maybe it's about time that you post the source code, unless anybody else have a qualified guess on the slowdown. For the two speeds summing, you shouldn't happen to do something like:
pos=pos+aspeed
pos=pos+bspeed
?

Or wasn't that what happens?

I can post the code but it's commented in italian,and not very well written i think....
If the italian commentatios isn't a problem i post the source.
Only make sure to follow those steps.
The program is for creating path nodes (for the follow routine,or for routes..).
So the first time you run it don't load a file...create a new one and save it.
And load a bmp also,because if you don't the animation don't start.
After that you can load the file and if you can see the problem.
(I hope that you have the xlntii include...because i use them and i think that don't work without...)
here's the code:
Include "XSTART.BB"
AppTitle "Resolution selector"
;un semplice requester per decidere la risoluzione dello schermo
Print "What screen resolution do you want to use?"
Print "1] 640*480"
Print "2] 800*600"
Print "3] 1024*768"
Print "4] 1280*1024"
scelta=Input("Choose a resolution:")
Select scelta
	Case 1
	screen_height=640
	screen_width=480
	Case 2
	screen_height=800
	screen_width=600
	Case 3
	screen_height=1024
	screen_width=768
	Case 4
	screen_height=1280
	screen_width=1024
End Select

Graphics screen_height,screen_width,32,2
fps_timer=CreateTimer(60)
GUI_GFXSETUP()
;Window
Window = GUI_WINDOW(0,0,160,400,"Path-points Creator","",62,0,$00DADADA,$007682BC,$00FFFFFF,$00C8C8C8,$006470AA)
;Gadgets
Button1 = GUI_BUTTON(Window,8,32,104,"New path","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button2 = GUI_BUTTON(Window,8,56,104,"Load path","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button3 = GUI_BUTTON(Window,8,80,104,"Save path","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button4 = GUI_BUTTON(Window,8,104,104,"Try loaded p.","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button5 = GUI_BUTTON(Window,8,128,104,"Try new p.","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button6 = GUI_BUTTON(Window,8,152,104,"N° nodes","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button7 = GUI_BUTTON(Window,8,176,104,"Speed","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button8 = GUI_BUTTON(Window,8,200,104,"Precision","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button9 = GUI_BUTTON(Window,8,224,104,"Smoothness","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button10 = GUI_BUTTON(Window,8,248,104,"Objects","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button11 = GUI_BUTTON(Window,8,272,104,"Background","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button12 = GUI_BUTTON(Window,8,320,104,"Clear all path","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
Button13 = GUI_BUTTON(Window,8,368,104,"Exit","",0,1,"",$00EEEEEE,$00D2D2D2,$00000000)
GUI_OPENWIN(Window)	;abbiamo finito di creare la finestra perciò la apriamo

AppTitle "Path Generator                                  Matteo M. 2004"	;titolo applicazione

;Caricamento immagini con relativo handle
;******************************************************************************

Global puntatore=LoadImage("puntatore.bmp")
Global enemy$;=LoadImage("enemy.bmp")
;******************************************************************************

;caricamento fonts
;******************************************************************************
Global caratteri=LoadFont("Comic Sans MS",20,0,0,0)
;******************************************************************************

;dichiarazione variabili globali,dimensionamento arrays,maniglie degli sprites,variabili e costanti varie
;******************************************************************************
Global uscita
Global percorso
Global salvapath
Global provapath
Global clearpath
Global npath
Global num
Global caricapath
Global creapath
Global testcurrent
Global mouse1	;questa e la variabile assegnata al pulsante del mouse...
Global mouse2	;idem per il tasto destro.....
MidHandle puntatore	;la maniglia del puntatore è il suo centro esatto
;MidHandle enemy	;maniglia dell'oggetto che si muove,qui chiamato "alieno"
HidePointer	;nasconde il puntatore del mouse
Global try$
Dim pathx#(max_path_points)	;array che serve per leggere i dati del percorso corrente
Dim pathy#(max_path_points)	;idem sopra...
Global path_counter=-1	;contatore dei nodi
Global prova
Global puntatori
Global a.alieno	;il type a 
Global b.alieno	;il type b
Const max_path_points=10	;il numero massimo di nodi da utilizzare nell'array
Global startdrawing		;mi serve per far sapere che ho cliccato il pulsante per disegnare il percorso
Global alieno_creato		;mi serve per dire che ho caricato un immagine dell'oggetto
;******************************************************************************

;creazione "type"
;******************************************************************************
Type alieno
	Field posizionex#
	Field posizioney#
	Field velocita#
	Field angolo#
	Field angolo2#
	Field pathx#[max_path_points]
	Field pathy#[max_path_points]
	Field smoothness
	Field precisione
	Field current_path
	Field path_counter
	Field distanza
	Field cx#
	Field cy#
	;Field percorso
End Type
;******************************************************************************

;funzioni varie:creazione oggetti,interfaccia,movimenti,disegno nodi ecc. ecc.
;******************************************************************************
Function crea_alieno_drawn(speed,smooth,precis)
	If creapath=0 Then Goto uscita5
	b.alieno=New alieno
	read_drawn_path()
	b\velocita#=speed
	b\angolo#=0
	b\angolo2#=0
	b\smoothness=smooth
	b\precisione=precis
	b\path_counter=-1
	b\distanza=0
	b\current_path=0; resetta il nodo corrente a zero
	b\cx=b\pathx[0]; setta le coordinate del rettangolo al primo nodo (cioe il nodo che e memorizzato nell'array)
	b\cy=b\pathy[0]
	movement_started=1; e il "flag" della partenza del movimento
	b\posizionex=b\pathx#[0]	;questa e la partenza dell'oggetto,cioe il primo nodo!
	b\posizioney=b\pathy#[0]	;idem come sopra per l'asse y
	.uscita5
End Function

Function crea_alieno(speed,smooth,precis)
		a.alieno=New alieno
		read_path()
		a\velocita#=speed
		a\angolo#=0
		a\angolo2#=0
		a\smoothness=smooth
		a\precisione=precis
		a\path_counter=-1
		a\distanza=0
		a\current_path=0; resetta il nodo corrente a zero
		a\cx=a\pathx[0]; setta le coordinate del rettangolo al primo nodo (cioe il nodo che e memorizzato nell'array)
		a\cy=a\pathy[0]
		;movement_started=1; e il "flag" della partenza del movimento
		a\posizionex=a\pathx#[0]	;questa e la partenza dell'oggetto,cioe il primo nodo!
		a\posizioney=a\pathy#[0]	;idem come sopra per l'asse y
		percorso=0
End Function

Function muovi_alieno_drawn()
	If creapath=0 Then Goto uscita3
	For b.alieno=Each alieno
	draw_current_path()
	b\angolo#=0			;setto l'angolo a zero
	If b\current_path < b\path_counter Then;cosi facendo siamo sicuri che il nodo corrente sia minore di quello massimo consentito
	 								 ;nell'array
		b\angolo=ATan2(b\pathy[b\current_path+1]-b\pathy[b\current_path],b\pathx[b\current_path+1]-b\pathx[b\current_path])
		;calcola l'angolo tra il nodo corrente e il prossimo nodo
		b\cx=b\cx+Cos(b\angolo)*b\velocita;aumenta le coordinate x del rettangolo formato dal nodo e dall'origine,cioe quel rettangolo
							   ;che si viene a formare se tracciassimo i lati dal punto di origine delle coordinate
							   ;dello schermo a quelle del punto dove risiede il nodo
							
		b\cy=b\cy+Sin(b\angolo)*b\velocita;aumenta le coordinate y del rettangolo formato dal nodo e dall'origine,cioe quel rettangolo
							   ;che si viene a formare se tracceremo i lati dal punto di origine delle coordinate
						   ;dello schermo a quelle del punto dove risiede il nodo

		Color 100,200,100; cambia il colore di disegno
		Rect b\cx-3,b\cy-3,6,6; disegna il rettangolino centrale alle sue coordinate
		If Abs(b\cx-b\pathx[b\current_path+1]) < b\precisione And Abs(b\cy-b\pathy[b\current_path+1]) < b\precisione Then
			;se le coordinate del famoso rettangolo (costruito tramite i punti di origine e del nodo...)sono entro TOT pixel
			;di distanza dal PROSSIMO nodo (questa e una sorta di approssimazione,cioe e come una campo di forza che attrae
			;l'oggetto...se il campo di forza e piccolo l'oggetto prosegue con l'angolatura che aveva prima e se ne
			;infischiera dei nodi obbligatori...)
			b\current_path=b\current_path+1; setta il nodo corrente come il prossimo nodo contenuto nell'array
			b\cx=b\pathx[b\current_path]; setta le coordinate del famoso rettangolo (formato da BLA BLA BLA...)a quelle del nodo corrente
			b\cy=b\pathy[b\current_path];idem come sopra con le coordinate y
			;tutto questo viene calcolato con la variabile "precisione"
		End If
	End If
	;If current_path => path_counter Then current_path=0
	b\angolo2=ATan2( b\cy - b\posizioney, b\cx - b\posizionex ); calcola l'angolo tra le coordinate del nodo e quelle dell'oggetto che si vuole muovere
	b\distanza=Sqr( (b\posizionex-b\cx)*(b\posizionex-b\cx)+(b\posizioney-b\cy)*(b\posizioney-b\cy) ); calcola la distanza tra loro due
	DebugLog dist	
	DebugLog b\velocita		;apre la finestra di debug con i valori della distanza in real-time
	b\posizionex=b\posizionex+(Cos(b\angolo2)/1)*(b\distanza / b\smoothness); muovi l'oggetto ad una velocita relativa alla distanza...il parametro "smoothness" e la
										  ; "morbidezza" della curva che fara l'oggetto da muovere...piu e morbida la curva
										  ;piu sara lenta
	b\posizioney=b\posizioney+(Sin(b\angolo2)/1)*(b\distanza / b\smoothness);idem come sopra per la coordinata y
	
	DrawImage enemy,b\posizionex,b\posizioney; disegna l'oggetto
	b\path_counter=b\path_counter+1; incrementa il contatore dei nodi
;precedente funzione set_path() ora inglobata all'interno di muovi_alieno()    
	If b\path_counter>max_path_points-1 Then ; se il contatore dei nodi e piu grande del numero massimo di nodi resettalo a 1
    	b\path_counter=max_path_points-1
    End If
;precedente funzione di loop tra i nodi,ora inglobata all'interno di muovi_alieno()
If b\current_path=max_path_points-1 Then; se il nodo corrente e 9 (per simulare un percorso ciclico...)
	b\pathx[max_path_points-1]=b\pathx[0]
	b\pathy[max_path_points-1]=b\pathy[0]
	b\cx=b\pathx[0]	  ; setta le coordinate del rettangolo al primo path-point
	b\cy=b\pathy[0]
	b\current_path=0; setta il percorso corrente al primo che e stato dichiarato
End If							

Next
.uscita3
End Function

Function muovi_alieno()
For a.alieno=Each alieno
	draw_path_points()
	a\angolo#=0			;setto l'angolo a zero
	If a\current_path < a\path_counter Then;cosi facendo siamo sicuri che il nodo corrente sia minore di quello massimo consentito
	 								 ;nell'array
		a\angolo=ATan2(a\pathy[a\current_path+1]-a\pathy[a\current_path],a\pathx[a\current_path+1]-a\pathx[a\current_path])
		;calcola l'angolo tra il nodo corrente e il prossimo nodo
		a\cx=a\cx+Cos(a\angolo)*a\velocita;aumenta le coordinate x del rettangolo formato dal nodo e dall'origine,cioe quel rettangolo
							   ;che si viene a formare se tracciassimo i lati dal punto di origine delle coordinate
							   ;dello schermo a quelle del punto dove risiede il nodo
							
		a\cy=a\cy+Sin(a\angolo)*a\velocita;aumenta le coordinate y del rettangolo formato dal nodo e dall'origine,cioe quel rettangolo
							   ;che si viene a formare se tracceremo i lati dal punto di origine delle coordinate
						   ;dello schermo a quelle del punto dove risiede il nodo

		Color 100,200,100; cambia il colore di disegno
		Rect a\cx-3,a\cy-3,6,6; disegna il rettangolino centrale alle sue coordinate
		If Abs(a\cx-a\pathx[a\current_path+1]) < a\precisione And Abs(a\cy-a\pathy[a\current_path+1]) < a\precisione Then
			;se le coordinate del famoso rettangolo (costruito tramite i punti di origine e del nodo...)sono entro TOT pixel
			;di distanza dal PROSSIMO nodo (questa e una sorta di approssimazione,cioe e come una campo di forza che attrae
			;l'oggetto...se il campo di forza e piccolo l'oggetto prosegue con l'angolatura che aveva prima e se ne
			;infischiera dei nodi obbligatori...)
			a\current_path=a\current_path+1; setta il nodo corrente come il prossimo nodo contenuto nell'array
			a\cx=a\pathx[a\current_path]; setta le coordinate del famoso rettangolo (formato da BLA BLA BLA...)a quelle del nodo corrente
			a\cy=a\pathy[a\current_path];idem come sopra con le coordinate y
			;tutto questo viene calcolato con la variabile "precisione"
		End If
	End If
	;If current_path => path_counter Then current_path=0
	a\angolo2=ATan2( a\cy - a\posizioney, a\cx - a\posizionex ); calcola l'angolo tra le coordinate del nodo e quelle dell'oggetto che si vuole muovere
	a\distanza=Sqr( (a\posizionex-a\cx)*(a\posizionex-a\cx)+(a\posizioney-a\cy)*(a\posizioney-a\cy) ); calcola la distanza tra loro due
	DebugLog dist	
	DebugLog a\velocita		;apre la finestra di debug con i valori della distanza in real-time
	a\posizionex=a\posizionex+(Cos(a\angolo2)/1)*(a\distanza / a\smoothness); muovi l'oggetto ad una velocita relativa alla distanza...il parametro "smoothness" e la
										  ; "morbidezza" della curva che fara l'oggetto da muovere...piu e morbida la curva
										  ;piu sara lenta
	a\posizioney=a\posizioney+(Sin(a\angolo2)/1)*(a\distanza / a\smoothness);idem come sopra per la coordinata y
	
	DrawImage enemy,a\posizionex,a\posizioney; disegna l'oggetto
	a\path_counter=a\path_counter+1; incrementa il contatore dei nodi
;precedente funzione set_path() ora inglobata all'interno di muovi_alieno()    
	If a\path_counter>max_path_points-1 Then ; se il contatore dei nodi e piu grande del numero massimo di nodi resettalo a 1
    	a\path_counter=max_path_points-1
    End If
;precedente funzione di loop tra i nodi,ora inglobata all'interno di muovi_alieno()
If a\current_path=max_path_points-1 Then; se il nodo corrente e 9 (per simulare un percorso ciclico...)
	a\pathx[max_path_points-1]=a\pathx[0]
	a\pathy[max_path_points-1]=a\pathy[0]
	a\cx=a\pathx[0]	  ; setta le coordinate del rettangolo al primo path-point
	a\cy=a\pathy[0]
	a\current_path=0; setta il percorso corrente al primo che e stato dichiarato
End If							

	
Next
End Function

Function draw_path_points()
	For loop=0 To a\path_counter; fai un loop tra tutti i nodi
		Color 100,100,200
		Rect a\pathx[loop],a\pathy[loop],2,2
		If loop<a\path_counter Then;fai questo loop solo se il nodo corrente e piu basso del numero massimo di nodi
			Rect a\pathx[loop+1]-1,a\pathy[loop+1]-1,2,2; disegna un rettangolo sul nodo
			Color 50,50,150; cambia il colore di disegno
			Line a\pathx[loop],a\pathy[loop],a\pathx[loop+1],a\pathy[loop+1]; disegna una linea tra il nodo corente e il prossimo
		End If
	Next
End Function

Function draw_current_path()
	For loop=0 To b\path_counter; fai un loop tra tutti i nodi
		Color 100,100,200
		Rect b\pathx[loop],b\pathy[loop],2,2
		If loop<b\path_counter Then;fai questo loop solo se il nodo corrente e piu basso del numero massimo di nodi
			Rect b\pathx[loop+1]-1,b\pathy[loop+1]-1,2,2; disegna un rettangolo sul nodo
			Color 50,50,150; cambia il colore di disegno
			Line b\pathx[loop],b\pathy[loop],b\pathx[loop+1],b\pathy[loop+1]; disegna una linea tra il nodo corente e il prossimo
		End If
	Next
End Function

Function read_path()				;funzione che carica da file i dati dei nodi
	file=ReadFile(try$)					;apro il file creato precedentemente per la lettura
	If file=0 Then Goto uscita			;se non ho aperto nessun file o se il file non esiste esci dalla funzione
	pointer=0							;puntatore al movimento nel file...aumenta di 4 in 4 siccome per un valore nei
									;file vengono allocati automaticamente 4 byte di dati (numeri,stringhe ecc ecc)
	pointer2=0							;puntatore al valore che,incrementando,fa si che leggo i valori nell'array ciclicamente
	For x=1 To 10						;scorre tutti e 10 i valori
		SeekFile (file,pointer)				;mi muovo all'interno del file cosi
		a\pathx#[pointer2]=ReadFloat (file)	;leggo il valore x ,un float-number dentro il primo valore nell'array
		pointer=pointer+4					;mi sposto al prossimo dato dentro il file
		a\pathy#[pointer2]=ReadFloat (file)	;leggo il valore y
		pointer2=pointer2+1					;passo ai prossimi due valori nell'array
		pointer=pointer+4					;mi sposto ancora al prossimo dato nel file
	Next
	CloseFile (file)					;chiudo il file
	.uscita
End Function

Function write_path_nodes()
Locate 0,570		;mi sposto quasi alla fine dello schermo con il cursore
nomefile$=Input("Digita il nome del file da salvare:")		;digito il nome del file da salvare,ricordarsi di mettere .txt

fileout=WriteFile (nomefile$)		;scrivo il file con il nome che ho digitato sopra,se esiste gia con quel nome lo sovrascrive
try2=0										;questo mi serve per scorrere tra il numero di nodi in modo da usare un for--next
											;per leggerli tutti
											
For a.alieno=Each alieno		;questo va fatto per tutti i type "alieno",cioe gli oggetti che muoverò									
For x=1 To 10								;scrivi solo da x=1 a x=10,cioe scrivi 10 volte
WriteFloat (fileout,a\pathx#[try2])									;il try tra parentesi e la variabile che contiene
WriteFloat (fileout,a\pathy#[try2])									;il numero del nodo corrente dell'array!!!
try2=try2+1					;incremento di uno...cioe dico che il nodo e il prossimo dell'array
Next
Next
CloseFile (fileout)			;chiudo il file
End Function

Function setpath(x,y)
	path_counter=path_counter+1; incrementa il contatore dei nodi
    If path_counter>max_path_points-1 Then; se il contatore dei nodi e piu grande del numero massimo di nodi resettalo a 1
    	path_counter=0	;percorso ancora da iniziare
    	pathx(path_counter)=MouseX()	;resetto anche le coordinate x e y del primo nodo
    	pathy(path_counter)=MouseY()
    Else; altrimenti 
		pathx(path_counter)=x;memorizza le coordinate x e y del nodo
		pathy(path_counter)=y
    End If
End Function

Function draw_current_path2()
	For loop=0 To path_counter; fai un loop tra tutti i nodi
		Color 100,100,200
		Rect pathx(loop),pathy(loop),2,2
		If loop<path_counter Then;fai questo loop solo se il nodo corrente e piu basso del numero massimo di nodi
			Rect pathx(loop+1)-1,pathy(loop+1)-1,2,2; disegna un rettangolo sul nodo
			Color 50,50,150; cambia il colore di disegno
			Line pathx(loop),pathy(loop),pathx(loop+1),pathy(loop+1); disegna una linea tra il nodo corente e il prossimo
		End If
	Next
End Function

Function read_drawn_path()
	If creapath=0 Then Goto uscita2	;se non e stato premuto precedentemente il pulsante crea esci dalla funzione
	punt=0
	For trigger=1 To 10
		b\pathx#[punt]=pathx(punt)
		b\pathy#[punt]=pathy(punt)
		punt=punt+1
	Next
	.uscita2
End Function
;******************************************************************************

;M A I N
;codice delle finestre

While Not uscita=1	;fino a che uscita non e uguale a 1
SetBuffer BackBuffer():Cls
WaitTimer(fps_timer)
;DrawImage puntatore,MouseX(),MouseY()
provapath=0	;devo dichiararlo sempre all'inizio altrimenti memorizza il valore anche se l'ho premuto precedentemente...
mouse1=MouseHit(1)
mouse2=MouseHit(2)
If startdrawing=1
	If mouse2=True
		pathx(current_path)=MouseX()	;resetto anche le coordinate x e y del primo nodo
    	pathy(current_path)=MouseY()
		setpath(MouseX(),MouseY())		;disegno i path-points premendo il tasto "esc"
	EndIf
	If pathx(9)<>pathx(0)
		pathx(9)=pathx(0)		;queste due routine sono una specie di autocorrezione dell'ultimo nodo
	EndIf						;facendolo diventare uguale al primo
	If pathy(9)<>pathy(0)
		pathy(9)=pathy(0)
	EndIf
	draw_current_path2()
	creapath=1
EndIf

GUI()
    Select EV_RELEASE()
        Case Button1		;disegno un percorso nuovo
        startdrawing=1
		
			
        Case Button2		;carico un percorso
        	try$=IO_FileRequest$("Load path:",".txt|")
			percorso=1
			caricapath=0
			
        Case Button3		;salvo il percorso appena disegnato,non quello caricato!
        	write_path_nodes()	;lo salva  nella directory di esecuzione del programma
			salvapath=0
			
        Case Button4		;provo il percorso caricato
        	If percorso=1
				crea_alieno(5,15,10)
				provapath=0
				percorso=0
			EndIf
			
        Case Button5		;provo il percorso appena disegnato
        	crea_alieno_drawn(5,15,10)
			testcurrent=0
			creapath=0
			startdrawing=0
			
        Case Button6		;decido il numero di nodi del percorso che voglio creare
        	Locate 0,570
			num=Input("How many path nodes?:")
			npath=0
			
        Case Button7
			;qui inseriremo il codice che gestisce la velocita
        Case Button8
			;qui quello della precisione
        Case Button9
			;quello della morbidezza della curva
        Case Button10
        	enemy=LoadImage (IO_FileRequest$("Load object image:",".bmp|"))
        	MidHandle enemy
        	alieno_creato=1
			;quello che sceglie l'oggetto da disegnare
        Case Button11
			;quello che sceglie lo sfondo
			
        Case Button12
			For a.alieno=Each alieno
				Delete a
			Next
			For b.alieno=Each alieno
				Delete b
			Next
		puntator=0
		clearpath=0
		path_counter=0
		creapath=0
		current_path=0
		movement_started=0
		percorso=0
		provapath=0
		startdrawing=0
		
        Case Button13
			uscita=1
			
        Default

    End Select




SetFont caratteri
If alieno_creato=1
muovi_alieno()
muovi_alieno_drawn()
EndIf
Color 200,200,200; il colore di disegno
Flip
Wend
GUI_FREEWIN(Window)

End
;E N D    M A I N 


I hope you don't have problem with the include files....
ps:i have run the debugger a while...but i have don't see anything strange....the speed valor is always correct....ArGh

Argh..made an error..sorry.
Make sure that where you run the code you have a file called "puntatore.bmp";it's the mouse pointer and i think that you have a "MAV" if you don't have this in the same directory....

Valgar, Have you tried running with debug mode off? (You say above you run with debug mode always on.)

Yes,i have tried to erase all text,rect,line command:also turned debug mode off.
It seems that the situation is a bit different.
I also have noticed that if i run in windowed mode,and when the speed come down..if i scroll the program window a bit over the monitor...the speed return to normal!

valgar,

I managed to get it to work but don't experience any slowdown when I use the app for a couple of minutes.

Can you describe exactly the problem.

Yes.
I create some object (i use a ball sprite of about 60*60 pixel).
After a wile i see that the balls...start skipping frames!
The effect is similar to the "slow motion" effect in max payne.
And if i load a path,animate it and AFTER i create a new path.....don't you see the speed of the object that grow up(but only when you are placing the nodes...).

I think I saw your problem.

Delete all occurances of debuglog. I think it's slowing your program down.

Yes....i have removed those occurance and it seems that don't slow down....
But i haven't see any relation about the 2 speed.
Strange that if you don't see any slowdown even with debuglog...about the small test that you send me...the valor that i have i correct or do you think is too low?
I have an athlon 2100+ (toroughbed i think....)512 ddr400 ram (cas 2.5) radeon 9700pro 128 meg.

Not sure about the summing of the 2 speeds. I had to hack your code somewhat because the IO_FileRequest$ caused a error on my machine.

The strange thing is that i have traked every variable trough the code with the debugger and the speed variable are always correct! -_-'
(thanks for the help)

I got this to work but still haven't seen the speed up issue.
Please explain exactly how you created this issue.

Ok.(excuse but i'm watching a film on the tv :P ).
Start the program.
Load an image.
Start a new path (a max of 10 points you stop at the 9 because the 10 is the first[for a ciclic route]),then try the new path....and while the object follow the path...try to start another new path......immediately after you hit the start button...you notice that the speed of the object that are onscreen is doubled!
This bug applyies with loaded path also.....after you hit the button for starting the new path......the speed return to normal.

(I think that the speed down problem is related to the math formula of the movement.....i have noticed that with some speed value the movement is not affected while with other value is affected.....mmm....complex solution :P )