Smooth scroll (at same spped on every pc's..)

BlitzPlus Forums/BlitzPlus Programming/Smooth scroll (at same spped on every pc's..)

Valgar(Posted 2003) [#1]
Hi all.
I've this piece of code (among many other's similar..)and i'm wondering how to improve the scrolling so it runs on every pc's at the same speed (60 hz )of 60 fps.
I have tried to implement the tutorial in the archives but the scrolling...seem to "scatter"....
Here's the code without the time routine...
(Sorry i don't know how to highligts the code -_-''' )

Graphics 640,480,32,1
SetBuffer BackBuffer() 
Global bit=1 
Global intro=LoadImage("sfondo1.bmp")	;a simple logo....
Global aereo=LoadImage("jet1.bmp") ;a simple sprite 
Global terreno=LoadImage("terreno.bmp") ;a simple texture of 256*256 
Global missile=LoadImage("bullet1.bmp") ;a simple sprite 
Global alien=LoadImage("enemyjet1.bmp") ;a simple sprite
Global alien2=LoadImage("enemyjet2.bmp")
Global explosion=LoadAnimImage("explode.bmp",64,64,1,10);an animstrip of 10 frames
Global explosion2=LoadAnimImage("explode2.bmp",64,64,1,7);an animstrip of 7 frame
Global explosion3=LoadAnimImage("explode3.bmp",32,32,1,9);an animstrip of 9 frame
Global explosion4=LoadAnimImage("explode4.bmp",64,64,1,15)
Global animation
Global timer_proiettili1
MaskImage aereo,0,67,171
MaskImage missile,0,67,171
MaskImage alien,0,67,171
MaskImage alien2,0,67,171
MidHandle aereo 
MidHandle missile 
MidHandle alien 
MidHandle alien2
MidHandle explosion
MidHandle explosion2
MidHandle explosion3
MidHandle explosion4
HidePointer  
Global b.alieno 
Type missile ;an object called missile 
	Field posx Field posy Field avanzamento Field contatore 
	Field image
End Type 

Type alieno 
	Field limite_dx,limite_sx,movx,movy,movimento,energia,image,percorso,strada,sinistra,destra,dimensione
End Type
 
Type esplosione
	Field posizionex,posizioney,frame,timer,grandezza
End Type


;MAIN
;introduzione()
Repeat
	Cls
	or_x=or_x
	or_y=or_y+bit ;this start the up scrolling adding "bit" to origin
	Origin or_x,or_y
	If or_y=2560 Then End ;if origin reach 2560 the program end
 	maptiles()
	Origin 0,0
	If or_y=200
		create_enemy(150,0,alien,5,1,2,2)
		create_enemy(200,0,alien,5,1,2,2)
		create_enemy(300,0,alien,5,1,2,2)
		create_enemy(400,0,alien,5,1,2,2) 
	EndIf
	
	If or_y=500
		create_enemy(20,0,alien2,1,2,1,4)
		create_enemy(80,0,alien2,1,2,2,4)
		create_enemy(140,0,alien2,1,2,1,4)
		create_enemy(200,0,alien2,1,2,2,4)
		create_enemy(260,0,alien2,1,2,1,4)
		
	EndIf
	 
	move_enemy()
	
	DrawImage aereo,MouseX(),MouseY() 
	
	If MouseDown(1)=1 ;if i hit the mouse button i create a missile 
	
		If MilliSecs() > timer_proiettili1 + 150 ;i use a "slowdown"of 100 millisecs to each fired bullet
		timer_proiettili1=MilliSecs() 
			create_missile() 
		EndIf 
	EndIf
	
	play_missile() ;this start the missil anim
	esplosione()
	
	Flip
	
Until KeyDown(1)=1 
End 
;END MAIN

Function create_missile() 
	a.missile=New missile
	a\image=missile
	a\posx=MouseX() ;the x position of missile creation is the mouse coords(the same coords of my jet of course) 
	a\posy=a\posy+MouseY()-30 ;the missile creation start a bit up to the jet(for simulating the launch-bay hehe) 
	a\avanzamento=4 ;the speed of the missile 
	a\contatore=0 ;the starting value of the animation 
End Function 

Function play_missile() 
	For a.missile=Each missile  
		DrawImage a\image,a\posx,a\posy 
		a\posy=a\posy-a\avanzamento ;this move the missile up 
		a\contatore=a\contatore+a\avanzamento ;this start the counter of the missile course 
		For b.alieno=Each alieno
			If ImagesCollide(a\image,a\posx,a\posy,0,b\image,b\movx,b\movy,0) ;if the alien collide with the missile the energy go down of one unit
					b\energia=b\energia-1
					If b\energia=<0 Then ;Delete b
					crea_esplosione()
					Delete b
					EndIf
					Delete a ;if the missile collide with the alien the missile stop it's existance :P
					Exit ;exit loop if missile is dead. will cause error as they's no missile to check against the aliens
			EndIf
		Next
	Next 
End Function 

Function create_enemy(x,y,immagine,energia,movimento,velocita,tipo) 
	b.alieno=New alieno
	b\image=immagine
	b\movx=x
	b\movy=y 
	b\movimento=velocita 
	b\energia=energia ;the "teoric" energy of the alien,from when reach 0 the alien explode 
	b\percorso=y
	b\strada=movimento
	b\sinistra= - velocita
	b\destra= + velocita
	b\limite_dx=x+100
	b\limite_sx=x-1
	b\dimensione=tipo
End Function 

Function move_enemy() 
	For b.alieno =Each alieno 
		DrawImage b\image,b\movx,b\movy 
		Text b\movx,b\movy-20,b\energia
		If b\strada=1 
			b\movx=b\movx+b\movimento	;start going right
			b\movy=b\movy+1	;start go down
			If b\movx => b\limite_dx	;if reach a certain value reverse its course
				b\movx=b\movx-b\movimento 
				b\movimento=b\sinistra
			EndIf 
		
			If b\movx =< b\limite_sx	;if reach a cerain value reverse its course and so on...
				b\movx=b\movx+b\movimento 
				b\movimento=b\destra
			EndIf 

			If b\movy=b\percorso+500	;continue its course until the movement down are equal to 400
				Delete b
				Exit
			EndIf
		EndIf
		If b\strada=2 
			b\movy=b\movy+(b\movimento+1)	;start go down
			If b\movy=b\percorso+500
				Delete b
				Exit
			EndIf
		EndIf
	Next
End Function

Function MapTiles()
tilex=0
tiley=0
For tiley=256 To -2560 Step -256
For tilex=0 To 640 Step 256
DrawBlock terreno,tilex,tiley
Next
Next
End Function

Function crea_esplosione()
c.esplosione=New esplosione
c\posizionex=b\movx	;the explosion position is the same as the alien position(considering mid-handle)
c\posizioney=b\movy	;see up :P
c\grandezza=b\dimensione	;IMPORTANTISSIMO...questo e il puntatore al tipo di esplosione da mostrare
End Function

Function esplosione()
For c.esplosione=Each esplosione
	Select c\grandezza
		Case 1	;small
			If MilliSecs() > c\timer + 50 Then	;i use a slowdown of 50 millisecs
				c\timer=MilliSecs()
			c\frame=(c\frame+1) Mod 9	;i loop trough the 7 frames of animation:this is a smaller explosion
			EndIf
			DrawImage explosion3,c\posizionex,c\posizioney,c\frame
			If c\frame => 8 Then Delete c
		Case 2	;medium
			If MilliSecs() > c\timer + 50 Then	;i use a slowdown of 50 millisecs
				c\timer=MilliSecs()
				c\frame=(c\frame+1) Mod 7	;i loop trough the 7 frames of animation:this is a smaller explosion
			EndIf
			DrawImage explosion2,c\posizionex,c\posizioney,c\frame
			If c\frame => 6 Then Delete c
		Case 3	;big
			If MilliSecs() > c\timer + 50 Then	;i use a "slowdown"of 50 millisecs
				c\timer=MilliSecs() 
				c\frame=( c\frame + 1 ) Mod 10	;i loop trough the 10 frames of the explosion
			EndIf
			DrawImage explosion,c\posizionex,c\posizioney,c\frame
			If c\frame=>9 Then Delete c	;when the frame 10 is displayed the explosion delete itself
		Case 4	; medium2
			If MilliSecs() > c\timer + 30 Then	;i use a "slowdown"of 30 millisecs
				c\timer=MilliSecs() 
				c\frame=( c\frame + 1 ) Mod 15	;i loop trough the 10 frames of the explosion
			EndIf
			DrawImage explosion4,c\posizionex,c\posizioney,c\frame
			If c\frame =>14 Then Delete c
	End Select
Next
End Function

Function introduzione()
Repeat
;SetBuffer BackBuffer()
Cls
DrawImage intro,0,0
Flip
Until KeyHit(57)
End Function


thanks in advance.


xlsior(Posted 2003) [#2]
Just one thing: You can't expect all systems to run at 60Hz...

For instance, my computer runs all screen modes in 100Hz by default, both windowed and fullscreen

(Nice monitor driver hack, that tells my computer that the monitor won't support anything less than that... the lower Hz used to make these awful squealing noises on my system)


Valgar(Posted 2003) [#3]
Yes i know it.
It's the thing i have asked (sorry for the strange english...im' italian).
How can i make scrolling and animation go at the same speed on every pc (say about 60 fps).
At the moment i have readed the routine of krylar (i think) and implemented it in my code (it's the routine that have the "goto" instead of functions because he say that goto's are faster than function..)but the scrolling runs strange...every few seconds it go jerky and make a step.


Paradox7(Posted 2003) [#4]
use the [ and ] around your code
so first one is [ . code . ]
and at the very end put [ . /code . ]
take out the spaces and take out the Peroids .


One of the better ways to break up your code is to have 2 main Functions, a UpdateLogic, and a Render

In the updatelogic all you doing in there is updating any game logic, like x = x + 1, ect ect, No Drawing

and in the render function, all you do is draw everything to the screen.


Mr Brine(Posted 2003) [#5]
use the create/wait timer commands