Porting BlitzBasic code to BlitzMax

BlitzMax Forums/BlitzMax Programming/Porting BlitzBasic code to BlitzMax

DannyD(Posted 2006) [#1]
Hi,
Wondering if anyone can give me a hand converting some old BlitzBasic version 1.48 code to BlitzMax.

So far I've converted:
-All comments from ; to '
-AppTitle "title" to Apptitle = "Title"
-Color 255,0,0 to Setcolor 255,0,0
-Dim to Global
-Dim tailX(100) to Dim tailX[100] ........ This is an array right?
-.draw to #draw a Goto Label loop.


Stuck on
- Rect tailx(a),taily(a),10,12,1
I assume Rect is BlitBasic's version of Drawrect. I am however having a problem as Drawrect complains about too many paramaters being passed.

Any help appreciated...


Eric(Posted 2006) [#2]
Drawrect tailx(a),taily(a),10,12

If I remember correctly the last parameter was filled or unfilled.


Dreamora(Posted 2006) [#3]
Goto: nope definitely not. As you better use strict, goto is out as it does not work.
Not programming with strict is no option ... you will find out quite fast why.

Another thing you have to change:

- Image references need :TImage
- Bank references :TBank
etc


tonyg(Posted 2006) [#4]
Have you tried using File / Import BB Project?
<edit> Anyway, take the '1' or '0' off the rect parms.


kfprimm(Posted 2006) [#5]
Function Rect(x,y,width,height,filled=True)
	If Not filled
		DrawLine x,y,x+width-1,y
		DrawLine x+width-1,y,x+width-1,y+height-1
		DrawLine x,y+height-1,x+width-1,y+height-1
		DrawLine x,y,x,y+height-1
	Else
		DrawRect x,y,width,height
	EndIf
End Function


In BlitzMax, there is no filled/unfilled parameter so just insert this function into your code.


Yan(Posted 2006) [#6]
[edit]
...Obviously of no use then... ;o)
[/edit]


Azathoth(Posted 2006) [#7]
-Dim tailX(100) to Dim tailX[100] ........ This is an array right?


In Blitzbasic was it 0 to 100? Blitzmax is like C with its arrays 0 to 99 in this case


DannyD(Posted 2006) [#8]
Thanks to all above. I'm working on this project again on Saturday and I'm sure I'll have more queries :)

BTW: I tried the import BB option. I think all it does is update the comments from ; to ' and include two libraries
Import "bbtype.bmx"
Import "bbvkey.bmx"

The rest of the code appears unchanged.


skidracer(Posted 2006) [#9]
It's been a long time since I've looked at the conversion program and there are several things that could be done a lot better now BlitzMax has matured. Here is the history file from the source that lists some of the things you may need to address yourself in converting code:
' AND OR operators -> bitwise if not followed by conditional operators =<>  
' Min and Max -> _Min and _Max
' HandleFromObject and HandleToObject added to bbtype.bmx 
' link and list fields in bbtype -> _link and _list
' IncrementArrays$(code$) adds 1 to all array dimensions of array declarations
' ReadString(TStream) function added and FreeSound(sound)->Release(sound)
' hidepointer -> hidemouse showpointer -> showmouse
' delete obj -> obj.remove
' readpixel->dev.blitz3d.readpixel writepixel->dev.blitz3d.writepixel
' seekfile -> seekstream  filepos -> streampos
' channelvolume->SetChannelVolume soundvolume->
' readbytes -> ReadBank writebytes-> WriteBankq
' xor->~ getkey->waitkey mousewait->waitmouse
' handle -> HandleFromObject object -> HandleToObject
' stop -> DebugStop
' .label -> #label
' param[n]->param[] in function declarations
' added TypePrefix$ to help avoid type name / variable name clashes
' processes include files
' output bbtype.bmx and bbvkey.bmx files (thanks to Terabit) 
' moved to bmk_bb2bmx
' inline comments replace ; with '
' command separators replace : with ;
' type specifiers replace . with :
' field separators replace \ with .
' boolean operators replace and or with & |
' array declarations replace Dim x(100) with Global x[100+1]
' graphics commands Line,Rect,Oval,Text -> DrawLine,DrawRect,DrawOval,DrawText
' implement old style Type handling for Each First Last Before After Insert
' Str becomes String
' Delete Each list -> list.Clear()
' Delete -> Release
' Data->DefData Read->ReadData
' KeyDown->VKeyDown KeyHit->VKeyHit
' native Color and ClsColor commands added to header



DannyD(Posted 2006) [#10]
Thanks for the info Skid, very useful.

I'm stuck on these now:
-SetBuffer BackBuffer()' Start dobbelbuffering
Am I safe in assuming this is redundant in BlitzMax ?

-Text 0,0, "Length: "+length ' Show length
Tried Drawtext but paramaters are wrong.


tonyg(Posted 2006) [#11]
No setbuffer in Bmax so omit it.
Drawtext is

Function DrawText( t$,x#,y# )


from the command reference
e.g.
graphics 640,480
drawtext "Length: "+length, 0,0
flip
waitkey()



DannyD(Posted 2006) [#12]
Many thanks to all you assisted, the ported code is below.
' Ported to BlitzMax by scsicork@... August 2006
' Thanks to all at the BlitzMax forums for their help
' Version 2.0
' Februar 2001
' Made by Einar Wedoe
' Programtype: Novell server screensaver look-a-like
' Use arrow-keys to change speed and length of tail !

Import "bbtype.bmx"
Import "bbvkey.bmx"
AppTitle ="Novell screensaver"

Graphics 800,600,32															' Sett Grphics mode
SetColor 255,0,0																	' Set Color
HideMouse()
Global x=400																	' Leader-dot-x-pos
Global y=300																	' Leader-dot-y-pos
Global direction=1																' Direction variable
Global directiontmp=0															' Temporary direction variable
Global round=10																	' How long between every change of direction ?
Global roundcount=0																' Counter for the rounds
Global speed#=3																	' Speed
Global speedcount=1																' Counter for speed
Global length=35																' Initial tail-length
Global show=0																	' Variable for showing speed and length on screen

Global tailX[100+1]																	' Tail x can be up to 1000 blocks long
Global tailY[100+1]																	' Tail y can be up to 1000 blocks long

For a=1 To 100																	' Set the current tail off the screen
 tailx(a)=-20
 taily(a)=-20
Next

SeedRnd MilliSecs()																' Make random even more random 
Global frametimer=CreateTimer(75)												' Set up timer to make everything run nice on all PC's



'-----------------------------------------	MAIN -----------------------------
#start																			' Mainloop

While Not VKeyHit(1)																' Loop until ESC is pressed
	WaitTimer(frametimer)															' Wait....
	Cls																				' Clean up
	novell()																			' Do stuff
	changespeedortail()																' Wanna change speed or/and tail ?
	Flip																			' Change buffer
Wend																			' Next round please
End	


'-----------------------------------------	NOVELL ----------------------------
Function novell()																' Start the function to actually put something on the screen
speedcount=speedcount+1
 If speed > speedcount Then Goto draw

Select direction																' Moving blocks about
		Case 1 
		x=x-11
		y=y-13
		
		Case 2
		y=y-13
		
		Case 3
		y=y-13
		x=x+11
		
		Case 4
		x=x+11
	
		Case 5
		x=x+11
		y=y+13
		
		Case 6
		y=y+13
		
		Case 7
		x=x-11
		y=y+13
		
		Case 8
		x=x-11	
End Select

speedcount=0
border																			' Check if we hit the border
	If direction=100 Then  roundcount=round+1									' If we did, do something about it !
directiontest																	' Check if time for change of direction

For a=length To 1 Step-1														' Move the x and y down the line
	tailx(a+1)=tailx(a)
	taily(a+1)=taily(a)
Next
tailx(1)=x																		' Put x in the end of the line
taily(1)=y																		' Put y in the end of the line
			
			
#draw


colordrop=255/length															' Find how much to drop color pr. rect
colortmp=0																		' Set color to 0
For a=length To 1 Step -1														' Draw tail											
SetColor colortmp,0,0															' Set color for current rect
  DrawRect tailx(a),taily(a),10,12											' Draw current rect
  colortmp=colortmp+colordrop													' Count up color
Next 


End Function


'-----------------------------------------	BORDER ----------------------
Function border()																' Set direction to 100 if border is reached
Select direction
		Case 1 
		If 	x < 15 Or y < 5 Then direction=100
		Case 2
		If y < 5 Then direction=100
		Case 3
		If x > 780 Or y < 5 Then direction=100
		Case 4
		If x > 780 Then direction=100
		Case 5
		If x > 780 Or y > 585 Then direction=100
		Case 6
		If y > 585 Then direction=100
		Case 7
		If x < 15 Or y > 585 Then direction=100
		Case 8
		If x < 15 Then direction=100
End Select
End Function
'-----------------------------------------
Function directiontest()														' Find new direction if it's time
roundcount=roundcount+1				
	If roundcount > round Then													' Time for a change
						  directiontmp=direction								' Temporary store old direction
#findnewdirection		  direction=Rand(8)										' Find new direction
					      If direction=directiontmp Then Goto findnewdirection	' If it's the same then try again
						  border												' Check for border
						  If direction=100 Then Goto findnewdirection			' Bounce border
						  roundcount=0											' Reset counter
						  round=Rand(20)										' Set new point for change of direction again
						  EndIf		
						
						
End Function
'-----------------------------------------
Function changespeedortail()													' Change things	
	If VKeyDown(200) And length < 99 Then 										' Add length of tail if arrow-up is pressed
									length=length+1								' All length by 1
									tailx(length)=-20							' Set new x-part of tail offscreen
									taily(length)=-20							' Set new x-part of tail offscreen
									show=255									' Set show variable to print to scren for 200 rasters
									EndIf
	If VKeyDown(208) And length > 5 Then 										' Decrease length of tail if arrow down is pressed
									length=length-1								' Dec by 1
									show=255									' Set show variable to print to scren for 200 rasters
									EndIf
	If VKeyDown(203) And speed < 10 Then 										' Add speed if arrow-right is pressed
									speed=speed+0.05							' Add speed by 0.05
									show=255									' Set show variable to print to scren for 200 rasters
									EndIf							
	If VKeyDown(205) And speed > 1 Then 											' Dec speed if arrow-left is pressed		
									speed=speed-0.05							' Dec speed by 0.05
									show=255									' Set show variable to print to scren for 200 rasters
									EndIf
show=show-1																		' Decrease show-variable
If show < 0 Then show=0															' If show drops below zero keep it on zero
If show > 0 Then 																' If show-variable is activated
			SetColor show,0,0														' Fade away text
			DrawText "Length: "+length,0,0											' Show length
			DrawText "Speed:  "+Int(10-speed+1),0,12							' Show speed but since speed is oposite of normal recalculate it
			EndIf								
									
End Function
'-----------------------------------------



DannyD(Posted 2006) [#13]
Skid or anyone else.. Is it possible to update the import BB routine? Is the source code available for me to edit myself?


skidracer(Posted 2006) [#14]
Yes, make a copy of the file blitzmax/src/bmk/bmk_bb2bmx.

Add a call to main() at the top of the file to use it as a standalone converter.


Hotcakes(Posted 2006) [#15]
Don't mean to nag too much here, but using the help files for your questions about simple things like the DrawRect and DrawText changes would make a lot of sense.


DannyD(Posted 2006) [#16]
Anyone suggest a replacement for the copyrect code in the code below ?
Import "bbtype.bmx"
Import "bbvkey.bmx"

Graphics 640,480,32,2
temp = LoadImage( "picture.jpg" )

'SetBuffer BackBuffer()

melt( temp, 640, 480, 2 )

WaitKey
End

'---------------------------------------------------------------------
' NAME : melt()
' PURPOSE : draws an image onto the screen in a melting like effect
' INPUTS : image handle, width, height, update frequency
' RETURNS : nothing
'---------------------------------------------------------------------
Function melt( PICTURE, WIDTH, HEIGHT, UPDATEFREQ )
HEIGHT = HEIGHT-1
Local lastupdate = MilliSecs()
For y1 = 0 To HEIGHT
While MilliSecs() < ( lastupdate + UPDATEFREQ )
Wend
lastupdate = MilliSecs()
For y2 = 0 To HEIGHT-y
CopyRect 0, HEIGHT-y1, WIDTH, 1, 0, y2, ImageBuffer( PICTURE ), BackBuffer()
Next
CopyRect 0, HEIGHT-y1, WIDTH, y1+1, 0, HEIGHT-y1, ImageBuffer( PICTURE ), BackBuffer()
Flip
Next
End Function



tonyg(Posted 2006) [#17]
Indiepath's RTT module and use the SetUV method to write to it?
The (slow) alternative is drawimage, grabpixmap, loadimage, drawimage cycle.
I think it's a shame that many of these useful commands haven't been added to native Bmax.