跳轉到內容

BlitzMax/模組/BASIC/BlitzMax 執行時

來自華夏公益教科書

Blitz 執行時模組提供 BlitzMax 應用程式執行時所需的低階功能。 這包括記憶體管理、異常處理以及字串和陣列操作等。

此模組提供的許多功能對應用程式程式設計師隱藏,而是由編譯器在“幕後”使用。 但是,有一些非常有用的命令用於除錯、記憶體管理和簡單的標準 IO。

全域性變數

[編輯 | 編輯原始碼]

Global AppDir$="bbAppDir"

描述: 應用程式目錄

資訊: AppDir 全域性變數包含當前正在執行的應用程式的完全限定目錄。 應用程式啟動時,應用程式的初始當前目錄也設定為 AppDir

示例:

' appdir.bmx
' requests the user to select a file from the application's directory

Print "Application Directory="+AppDir$

file$=RequestFile("Select File to Open","",False,AppDir$)

Print "file selected was :"+file

Global AppFile$="bbAppFile"

描述: 應用程式檔名

資訊: AppFile 全域性變數包含當前正在執行的應用程式的完全限定檔名。

示例:

' appfile.bmx

Print "This program's executable is located at "+AppFile$

Global AppTitle$="bbAppTitle"

描述: 應用程式標題

資訊: AppTitle 全域性變數在需要預設應用程式標題時由各種命令使用 - 例如,在開啟簡單的視窗或請求器時。 最初,AppTitle 設定為值“BlitzMax Application”。 但是,您可以隨時使用簡單的賦值更改 AppTitle

Global AppArgs$[]="bbAppArgs"

描述: 啟動時傳遞給應用程式的引數

資訊: AppArgs 全域性陣列包含應用程式啟動時傳送給應用程式的命令列引數。 AppArgs 的第一個元素始終包含應用程式的名稱。 但是,名稱的格式可能會根據應用程式的啟動方式而改變。 使用 AppDirAppFile 獲取有關應用程式名稱或目錄的一致資訊。

示例:

' appargs.bmx
' print the command line arguments passed to the program at runtime

Print "Number of arguments = "+AppArgs.length

For a$=EachIn AppArgs
Print a$
Next

LaunchDir

[編輯 | 編輯原始碼]

Global LaunchDir$="bbLaunchDir"

描述: 啟動應用程式的目錄

資訊: LaunchDir 全域性變數包含啟動應用程式時的當前目錄。 這主要用於可能需要訪問“shell”當前目錄而不是應用程式目錄的命令列工具。

示例:

' launchdir.bmx

Print "This program was launched from "+LaunchDir$

RuntimeError

[編輯 | 編輯原始碼]

Function RuntimeError( message$ )

描述: 產生執行時錯誤

資訊: 丟擲 TRuntimeException。

示例:

' runtimeerror.bmx

If a=0 RuntimeError "This program has failed badly."

DebugStop

[edit | edit source]

Function DebugStop()

描述: 停止程式執行並進入偵錯程式

資訊: 如果沒有偵錯程式,則忽略此命令。

DebugLog

[edit | edit source]

Function DebugLog( message$ )

描述: 將字串寫入除錯日誌

資訊: 如果沒有偵錯程式,則忽略此命令。

註釋: 請注意,與 Assert 不同,DebugLog 將在無論編譯模式如何都會被編譯 - 只是在釋出模式下不列印任何內容。

OnEnd

[edit | edit source]

Function OnEnd( fun() )

描述: 新增一個在程式結束時呼叫的函式

資訊: OnEnd 允許您指定一個在程式結束時呼叫的函式。OnEnd 函式以新增它們的相反順序呼叫。

示例:

' onend.bmx

Function cleanup()
Print "cleaning up"
End Function

OnEnd cleanup
Print "program running"
End	'the cleanup function will be called at this time

ReadStdin

[edit | edit source]

Function ReadStdin$()

描述: 從 stdin 讀取字串

返回值: 從 stdin 讀取的字串。如果存在換行符,則將其包含在返回的字串中。

WriteStdout

[edit | edit source]

Function WriteStdout( str$ )

描述: 將字串寫入 stdout

資訊: 將 str 寫入 stdout 並重新整理 stdout。

WriteStderr

[edit | edit source]

Function WriteStderr( str$ )

描述: 將字串寫入 stderr

資訊: 將 str 寫入 stderr 並重新整理 stderr。

Delay

[edit | edit source]

Function Delay( millis )

描述: 等待給定的毫秒數

資訊: Delay 暫停程式執行至少 millis 毫秒。毫秒是千分之一秒。

MilliSecs

[edit | edit source]

Function MilliSecs())

描述: 獲取毫秒計數器

返回值: 計算機開機以來的毫秒數。

資訊: MilliSecs 返回計算機開機以來的毫秒數。毫秒是千分之一秒。

MemAlloc

[edit | edit source]

Function MemAlloc:Byte Ptr( size )

描述: 分配記憶體

返回值: 一個新的記憶體塊,長度為 size 位元組

MemFree

[edit | edit source]

Function MemFree( mem:Byte Ptr )

描述: 釋放分配的記憶體

資訊: 由 mem 指定的記憶體必須先前由 MemAllocMemExtend 分配。

MemExtend

[edit | edit source]

Function MemExtend:Byte Ptr( mem:Byte Ptr,size,new_size )

描述: 擴充套件記憶體塊

返回值: 一個新的記憶體塊,長度為 new_size 位元組

資訊: 由 memsize 指定的現有記憶體塊被複制到一個新的長度為 new_size 位元組的記憶體塊中。釋放現有塊並返回新塊。

MemClear

[edit | edit source]

Function MemClear( mem:Byte Ptr,size )

描述: 將記憶體塊清除為 0

MemCopy

[edit | edit source]

Function MemCopy( dst:Byte Ptr,src:Byte Ptr,size )

描述: 複製一個非重疊的記憶體塊

MemMove

[edit | edit source]

Function MemMove( dst:Byte Ptr,src:Byte Ptr,size )

描述: 複製一個可能重疊的記憶體塊

GCSetMode

[edit | edit source]

Function GCSetMode( mode )

描述: 設定垃圾收集器模式

資訊: mode 可以是以下之一
1 : 自動 GC - 記憶體將自動被垃圾回收
2 : 手動 GC - 在呼叫 GCCollect 之前不會回收任何記憶體
預設的 GC 模式是自動 GC。

GCSuspend

[edit | edit source]

Function GCSuspend()

描述: 暫停垃圾收集器

資訊: GCSuspend 臨時暫停垃圾收集器。在呼叫 GCSuspend 之後,將不會執行垃圾回收。
使用 GCResume 恢復垃圾收集器。請注意,GCSuspendGCResume '巢狀',這意味著對 GCSuspend 的每次呼叫都必須與對 GCResume 的呼叫匹配。

GCResume

[edit | edit source]

Function GCResume()

描述: 恢復垃圾收集器

資訊: GCResume 在呼叫 GCSuspend 之後恢復垃圾回收。
有關更多詳細資訊,請參閱 GCSuspend

GCCollect

[edit | edit source]

Function GCCollect()

描述: 執行垃圾收集器

返回值: 收集的記憶體量(以位元組為單位)。

資訊: 如果垃圾收集器因 GCSuspend 而被暫停,則此函式將無效。

GCMemAlloced

[edit | edit source]

Function GCMemAlloced()

描述: 應用程式分配的記憶體

返回值: 應用程式當前分配的記憶體量(以位元組為單位)。

資訊: 此函式僅返回 '託管記憶體'。這包括應用程式使用的所有物件、字串和陣列。

GCEnter

[edit | edit source]

Function GCEnter()

描述: 私有:請勿使用

Function GCLeave()

描述: 私有:請勿使用

HandleFromObject

[編輯 | 編輯原始碼]

Function HandleFromObject( obj:Object )

描述: 將物件轉換為整數控制代碼

返回值: 一個整數物件控制代碼

資訊: 將物件轉換為整數控制代碼後,您必須使用 Release 命令釋放它。

HandleToObject

[編輯 | 編輯原始碼]

Function HandleToObject:Object( handle )

描述: 將整數控制代碼轉換為物件

返回值: 與整數控制代碼關聯的物件

示例:

' handlefromobject.bmx

Type MyType
	Field x:Int=100
End Type

Local obj:MyType=New MyType
Local handle:Int=HandleFromObject(obj)
Print "handle = "+handle

Local obj2:MyType=MyType(HandleToObject(handle))
If obj=obj2 Then Print "obj = obj2"
Print "x = "+obj2.x

Release handle

關鍵詞

[編輯 | 編輯原始碼]

描述: 設定嚴格模式

資訊: 有關嚴格模式程式設計的更多資訊,請參閱 BASIC 相容性

註釋: 在嚴格模式下,允許自動資料型別。預設資料型別為 Int。

示例:

Rem
Strict advises the BlitzMax compiler to report as errors all auto defined variables.
End Rem

Strict

local a=10

b=20	'compiler error, strict mode means variables must be declared b4 use

SuperStrict

[編輯 | 編輯原始碼]

描述: 設定 SuperStrict 模式

註釋: 在 SuperStrict 模式下,必須始終指定資料型別。

描述: 結束程式執行

示例:

Rem
The End command causes a program to exit immediately.
End Rem

Print "Hello!"
End
Print "This line will never be printed as the program has already been ended."

描述: 開始一個註釋塊

示例:

Rem

My Rem Example
First created 9th August, 2004

(C)2050 Blitz Intergalactic Software Corporation

End Rem

Print "This program has no useful function"

Rem

Remarks are useful for making your program easily readable.
You can leave details explaining the function of your program
in a remarks section so that you and any other programmers
that work with your code can more easily understand the workings
of your program

End Rem

Print "Sorry."
End

描述: 結束一個註釋塊

示例:

Rem
All code between Rem and EndRem is ignored by the BlitzMax compiler.
Print "hello"
End Rem

Print "goodbye"

描述: 值為 1 的常量整數

示例:

Rem
True is a Constant Integer assigned the value 1.
End Rem

Print "True="+True

If True
	Print "This line will always be executed"
EndIf

If Not True
	Print "This line will never be executed"
EndIf

描述: 值為 0 的常量整數

示例:

Rem
False is a Constant Integer assigned the value 0.
End Rem

Print "False="+False

If False
	Print "This code will never be executed"
Endif

描述: 常量 Pi 值: 3.1415926535897932384626433832795

示例:

Rem
Pi is a Constant Double assigned the value 3.1415926535897932384626433832795
End Rem

Print "Pi="+Pi

描述: 獲取預設 Null 值

示例:

Rem
Null is a BlitzMax Constant representing an empty Object reference.
End Rem

Type mytype
	Field	atypevariable
End Type

Global a:mytype

if a=null Print "a is uninitialized"
a=new mytype
if a<>null Print "a is initialized"

描述: 無符號 8 位整數型別

示例:

Rem
Byte is an unsigned 8 bit integer BlitzMax primitive type.
End Rem

Local a:Byte

a=512;Print "a="+a	'prints 0
a=-1;Print "a="+a	'prints 255

描述: 無符號 16 位整數型別

示例:

Rem
Short is an unsigned 16 bit integer BlitzMax primitive type.
End Rem

Local a:Short

a=65536;Print "a="+a	'prints 0
a=-1;Print "a="+a	'prints 65535

描述: 有符號 32 位整數型別

示例:

Rem
Int is a signed 32 bit integer BlitzMax primitive type.
End Rem

Local a:Int

' the following values all print 0 as BlitzMax "rounds to zero"
' when converting from floating point to integer

a=0.1;Print a
a=0.9;Print a
a=-0.1;Print a
a=-0.9;Print a

描述: 有符號 64 位整數型別

示例:

Rem
Long is a signed 64 bit integer BlitzMax primitive type.
End Rem

Const MAXLONG:Long=$7fffffffffffffff:Long
Const MINLONG:Long=$8000000000000000:Long

Print "A long can have the maximum value of:"+MAXLONG
Print "A long can have the minimum value of:"+MINLONG

描述: 32 位浮點型別

示例:

Rem
Float is a 32 bit floating point BlitzMax primitive type.
End Rem

Local a:Float

a=1

For i=1 To 8
	Print a
	a=a*0.1
Next

For i=1 To 8
	a=a*10
	Print a
Next

描述: 64 位浮點型別

示例:

Rem
Double is a 64 bit floating point BlitzMax primitive type.
End Rem

Local speedoflight:Double
Local distance:Double
Local seconds:Double

speedoflight=299792458:Double	'meters per second
distance=149597890000:Double	'average distance in meters from earth sun

seconds=distance/speedoflight

Print "Number of seconds for light to travel from earth to sun="+seconds

描述: 字串型別

示例:

Rem
String is a BlitzMax container type containing a sequence of unicode characters.
End Rem

quote:String=Chr(34)
Print quote+"Hello World!"+quote

描述: 物件型別

示例:

Rem
Object is the base class of all BlitzMax types.

The following function attempts to cast from any object to
the custom type TImage and returns true if the given object
is an instance of TImage or an instance of a &Type derived
from TImage.
End Rem

Function IsImage(obj:Object)
	If TImage(obj) return True
	Return False
End Function

描述: 用於“按引用”型別的複合型別說明符

示例:

Rem
Var is a composite type containing a reference to a variable of the 
specified Type.
End Rem

' the following illustrates parsing function parameters by reference

Function ReturnMultiplevalues(a Var,b Var,c Var)
	a=10
	b=20
	c=30
	Return
End Function

Local x,y,z

ReturnMultipleValues(x,y,z)

Print "x="+x	'10
Print "y="+y	'20
Print "z="+z	'30

描述: 用於指標型別的複合型別說明符

示例:

Rem
Ptr is a composite type containing a pointer to a variable of the specified Type.
End Rem

' the following illustrates the use of traditional c style pointers

Local c[]=[1,2,3,4]
Local p:Int Ptr

p=c
Print "pointer 'p' points to:"+p[0]	'1

p:+1
Print "pointer 'p' points to:"+p[0]	'2

p:+1
Print "pointer 'p' points to:"+p[0]	'3

p:+1
Print "pointer 'p' points to:"+p[0]	'4

描述: 開始一個條件塊。

示例:

Rem
If begins a conditional block.
End Rem

If 3<5 Print "3<5"	'single line if

If 5<7			'if block
	Print "5<7"
EndIf

描述: If 語句中條件和關聯程式碼之間的可選分隔符。

示例:

Rem
Then is an optional separator between the condition and the block of code following an If statement.
End Rem

If 3<5 Then Print "3<5"

描述: Else 提供了 If Then 結構在 If 條件為 False 時執行第二塊程式碼的功能。

示例:

Rem
Else provides the ability for an If Then construct to execute a second block of code when the If condition is false.
End Rem

i=3

If i<5 Print "i<5" Else Print "i>=5"	'single line If Else

If i<5					'block style If Else
	Print "i<5"
Else
	Print "i>=5"
EndIf

描述: ElseIf 提供了在初始條件失敗時測試和執行程式碼段的功能。

示例:

Rem
ElseIf provides the ability to test and execute a section of code if the initial condition failed.
End Rem

age=Int( Input("How old Are You?") )

If age<13
	Print "You are young"
ElseIf age<20
	Print "You are a teen!"
Else
	Print "You are neither young nor a teen"
EndIf

EndIf

[edit | edit source]

描述: 標記 If Then 塊的結束。

示例:

Rem
EndIf marks the end of an If Then block.
End Rem

i=5

If i<10
	Print "i<10"
EndIf

描述: 標記使用迭代器重複執行程式碼段的迴圈的開始。

示例:

Rem
For marks the start of a loop that uses an iterator to execute a section of code repeatedly.
End Rem

' print 5 times table

For i=1 to 12
	Print "5*"+i+"="+5*i
Next

描述: 後面跟著一個常量,用於計算何時退出 For..Next 迴圈。

示例:

Rem
Followed by a constant which is used to calculate when to exit a For..Next loop.
End Rem

For i=1 To 5
	Print i
Next

描述: 指定一個可選常量,用於遞增 For 迭代器。

示例:

Rem
Specifies an optional constant that is used to increment the For iterator.
End Rem

' count backwards from 10 to 0

For i=10 to 0 step -1
	Print i
Next

描述: 結束 For 塊

示例:

Rem
Marks the end of a For section.
End Rem

For i=1 To 5;Print i;Next

EachIn

[edit | edit source]

描述: 遍歷陣列或集合

示例:

Rem
Specifies a BlitzMax collection type whose values are assigned sequentially to the For iterator.
End Rem

Local a[]=[0,5,12,13,20]

For b=EachIn a
	Print b
Next

While

[edit | edit source]

描述: 在條件為真時執行程式碼塊

示例:

Rem
While executes the following section of code repeatedly while a given condition is true.
End Rem

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)	'loop until escape key is pressed
	Cls
	For i=1 to 200
		DrawLine rnd(640),rnd(480),rnd(640),rnd(480)
	Next
	Flip
Wend

描述: 結束 While 塊

示例:

Rem
Wend marks the end of a While section.
End Rem

While i<5
	Print i
	i:+1
Wend

EndWhile

[edit | edit source]

描述: 結束 While 塊

Repeat

[edit | edit source]

描述: 直到滿足終止條件或永遠執行程式碼塊

示例:

Rem
Repeat executes the following section of code until a terminating condition is true.
End Rem

Repeat
	Print i
	i:+1
Until i=5

Until

[edit | edit source]

描述: 有條件地繼續 Repeat 塊

示例:

Rem
Until marks the end of a Repeat block and is followed by a terminating condition.
End Rem

i=2
Repeat
	Print i
	i:*2
Until i>1000000

Forever

[edit | edit source]

描述: 無限地繼續 Repeat 塊

示例:

Rem
Forever is an alternate ending to a Repeat block that will cause the loop to always repeat.
End Rem

Repeat
	Print i+" Ctrl-C to End!" 
	i:+1
Forever

Select

[edit | edit source]

描述: 開始 Select 塊

示例:

Rem
Select begins a block featuring a sequence of multiple comparisons with a single value.
End Rem

a=Int( Input("Enter Your Country Code ") )

Select a
	Case 1
		Print "You are from America"
	Case 44
		Print "You are from the United Kingdom"
	Case 62
		Print "You are from Australia"
	Case 64
		Print "You are from New Zealand"
	Default
		Print "I cannot tell which country you are from"
End Select

EndSelect

[edit | edit source]

描述: 結束 Select 塊

示例:

Rem
EndSelect marks the end of a Select block.
End Rem

SeedRnd MilliSecs()

a=Rand(5)

Select a
	Case 1 Print "one"
	Case 2 Print "two"
	Case 3 Print "three"
	Case 4 Print "four"
	Case 5 Print "five"
	Default Print "Program Error"
End Select

描述: Select 塊中的條件程式碼

示例:

' case.bmx

' Case performs a comparison with the preceeding value(s) and that
' listed in the enclosing Select statement:

a=Int( Input("Enter a number between 1 and 5 ") )

Select a
	Case 1 Print "You think small"
	Case 2 Print "You are even tempered"
	Case 3,4 Print "You are middle of the road"	
	Case 5 Print "You think big"
	Default Print "You are unable to follow instructions"
End Select		

Default

[edit | edit source]

描述: Select 塊中的預設程式碼

示例:

Rem
Default is used in a Select block to mark a code section that is executed if all prior Case statements fail.
End Rem

a$=Input("What is your favorite color?")
a$=Lower(a$)	'make sure the answer is lower case

Select a$
	Case "yellow" Print "You a bright and breezy"
	Case "blue" Print "You are a typical boy"
	Case "pink" Print "You are a typical girl"
	Default Print "You are quite unique!"
End Select

描述: 退出封閉迴圈

示例:

Rem
Exit causes program flow to exit the enclosing While, Repeat or For loop.
End Rem

Repeat
	Print n
	n:+1
	If n=5 Exit
Forever

Continue

[edit | edit source]

描述: 繼續執行封閉迴圈

示例:

Rem
Continue causes program flow to return to the start of the enclosing While, Repeat or For program loop
End Rem

For i=1 To 20
	If i Mod 2 Continue
	Print i
Next

Const

[edit | edit source]

描述: 宣告一個常量

示例:

Rem
Const defines the preceeding variable declaration as constant.
End Rem

Const ON=True
Const OFF=False

Const TWOPI#=2*Pi

Print TWOPI

Local

[edit | edit source]

描述: 宣告一個區域性變數

示例:

Rem
Local defines a variable as local to the Method or Function it is defined meaning it is automatically released when the function returns.
End Rem

Function TestLocal()
	Local	a
	a=20
	Print "a="+a
	Return
End Function

TestLocal
Print "a="+a	'prints 0 or if in Strict mode is an error as a is only local to the TestLocal function

Global

[edit | edit source]

描述: 宣告一個全域性變數

示例:

Rem
Global defines a variable as Global allowing it be accessed from within Methods and Functions.
End Rem

Global a=20

Function TestGlobal()
	Print "a="+a
End Function

TestGlobal
Print "a="+a

Field

[edit | edit source]

描述: 宣告一個 Field 變數

示例:

Rem
Field is used to declare the member variable(s) of a type.
End Rem

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

Function

[edit | edit source]

描述: 開始一個函式宣告

示例:

Rem
Function marks the beginning of a BlitzMax function declaration.

When a function does not return a value the use of brackets when
calling the function is optional.
End Rem

Function NextArg(a$)
	Local	p
	p=Instr(a$,",")
	If p 
		NextArg a$[p..]
		Print a$[..p-1]
	Else
		Print a$
	EndIf
End Function

NextArg("one,two,three,four")

NextArg "22,25,20"	'look ma, no brackets

EndFunction

[edit | edit source]

描述: 結束一個函式宣告

示例:

Rem
Function marks the end of a BlitzMax function declaration.
End Rem

Function RandomName$()
	local a$[]=["Bob","Joe","Bill"]
	Return a[Rnd(Len a)]
End Function

For i=1 To 5
	Print RandomName$()
Next

Return

[edit | edit source]

描述: 從函式返回

示例:

Rem
Return exits a BlitzMax function or method with an optional value.
The type of return value is dictated by the type of the function.
End Rem

Function CrossProduct#(x0#,y0#,z0#,x1#,y1#,z1#)
	Return x0*x1+y0*y1+z0*z1
End Function

Print "(0,1,2)x(2,3,4)="+CrossProduct(0,1,2,2,3,4)

Function LongRand:Long()
	Return (Rand($80000000,$7fffffff) Shl 32)|(Rand($80000000,$7fffffff))
End Function

Print "LongRand()="+LongRand()
Print "LongRand()="+LongRand()

描述: 開始一個使用者定義的型別宣告

示例:

Rem
Type marks the beginning of a BlitzMax custom type.

Standard BlitzMax types use a preceeding "T" naming
convention to differentiate themselves from standard
BlitzMax variable names.
End Rem

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

EndType

[edit | edit source]

描述: 結束一個使用者定義的型別宣告

示例:

Rem
EndType marks the end of a BlitzMax custom type.
End Rem

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

Extends

[edit | edit source]

描述: 指定使用者定義型別超型別

註釋: 型別繼承了給定的基本型別。

示例:

Rem
Extends is used in a BlitzMax Type declaration to derive the Type from a specified base class.
End Rem

Type TShape
	Field	xpos,ypos
	Method Draw() Abstract
End Type

Type TCircle Extends TShape
	Field	radius
	
	Function Create:TCircle(x,y,r)
		Local c:TCircle=New TCircle
		c.xpos=x;c.ypos=y;c.radius=r
		Return c
	End Function
	
	Method Draw()
		DrawOval xpos,ypos,radius,radius
	End Method
End Type

Type TRect Extends TShape
	Field	width,height
	
	Function Create:TRect(x,y,w,h)
		Local r:TRect=New TRect
		r.xpos=x;r.ypos=y;r.width=w;r.height=h
		Return r
	End Function
	
	Method Draw()
		DrawRect xpos,ypos,width,height
	End Method
End Type

Local 	shapelist:TShape[4]
Local	shape:TShape

shapelist[0]=TCircle.Create(200,50,50)
shapelist[1]=TRect.Create(300,50,40,40)
shapelist[2]=TCircle.Create(400,50,50)
shapelist[3]=TRect.Create(200,180,250,20)

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)
	Cls
	For shape=EachIn shapelist
		shape.draw
	Next
	Flip
Wend
End

方法

[edit | edit source]

描述: 開始方法宣告

註釋: 方法是特定於例項的函式。

示例:

Rem
Method marks the beginning of a BlitzMax custom type member function.
End Rem

Type TPoint
	Field	x,y

	Method ToString$()
		Return x+","+y
	End Method
End Type

a:TPoint=New TPoint
Print a.ToString()

EndMethod

[edit | edit source]

描述: 結束方法宣告

示例:

Rem
EndMethod marks the end of a BlitzMax Method declaration.
End Rem

Type TPoint
	Field	x,y

	Method ToString$()
		Return x+","+y
	End Method
End Type

a:TPoint=New TPoint
Print a.ToString()

抽象

[edit | edit source]

描述: 將型別或方法標記為抽象

註釋: 型別或方法必須擴充套件(如果未擴充套件,編譯將失敗)。

示例:

Rem
A BlitzMax type that contains Abstract methods becomes abstract itself.
Abstract types are used to define interfaces that extending types must 
implement before they can be used to create new instances.

In the following code TShape is an abstract type in that you can not
create a TShape but anything extending a TShape must implement a Draw()
method.
End Rem

Type TShape
	Field	xpos,ypos
	Method Draw() Abstract
End Type

Type TCircle Extends TShape
	Field	radius
	
	Function Create:TCircle(x,y,r)
		Local c:TCircle=New TCircle
		c.xpos=x;c.ypos=y;c.radius=r
		Return c
	End Function
	
	Method Draw()
		DrawOval xpos,ypos,radius,radius
	End Method
End Type

Type TRect Extends TShape
	Field	width,height
	
	Function Create:TRect(x,y,w,h)
		Local r:TRect=New TRect
		r.xpos=x;r.ypos=y;r.width=w;r.height=h
		Return r
	End Function
	
	Method Draw()
		DrawRect xpos,ypos,width,height
	End Method
End Type

Local 	shapelist:TShape[4]
Local	shape:TShape

shapelist[0]=TCircle.Create(200,50,50)
shapelist[1]=TRect.Create(300,50,40,40)
shapelist[2]=TCircle.Create(400,50,50)
shapelist[3]=TRect.Create(200,180,250,20)

Graphics 640,480
While Not KeyHit(KEY_ESCAPE)
	Cls
	For shape=EachIn shapelist
		shape.draw
	Next
	Flip
Wend
End

最終

[edit | edit source]

描述: 將型別或方法標記為最終

註釋: 型別或方法不可擴充套件。

示例:

Rem
Final stops methods from being redefined in super classes.
End Rem

Type T1
	Method ToString$() Final
		Return "T1"
	End Method
End Type

Type T2 Extends T1
	Method ToString$()	'compile time error "Final methods cannot be overridden"
		Return "T2"
	End Method
End Type

描述: 建立使用者定義型別的例項

示例:

Rem
New creates a BlitzMax variable of the Type specified.
End Rem

Type MyType
	Field	a,b,c
End Type

Local t:MyType
t=New MyType
t.a=20

Print t.a

' if a new method is defined for the type it will also be called

Type MyClass
	Field	a,b,c
	Method New()
		Print "Constructor invoked!"
		a=10
	End Method
End Type

Local c:MyClass
c=New MyClass
Print c.a

自我

[edit | edit source]

描述: 對此方法的物件例項的引用

註釋: 允許物件訪問其自身的例項(在大多數情況下不需要)。

示例:

Rem
Self is used in BlitzMax Methods to reference the invoking variable.
End Rem

Type MyClass
	Global	count	
	Field	id
	
	Method New()
		id=count
		count:+1
		ClassList.AddLast(Self)	'adds this new instance to a global list		
	End Method
End Type

Global ClassList:TList

classlist=New TList

Local c:MyClass

c=New MyClass
c=New MyClass
c=New MyClass

For c=EachIn ClassList
	Print c.id
Next

超級

[edit | edit source]

描述: 對超型別物件例項的引用

註釋: 允許物件訪問其父型別(它擴充套件的型別)。

示例:

Rem
Super evaluates to Self cast to the method's immediate base class.
End Rem

Type TypeA
	Method Report()
		Print "TypeA reporting"
	End Method
End Type

Type TypeB Extends TypeA
	Method Report()
		Print "TypeB Reporting"
		Super.Report()
	End Method
End Type

b:TypeB=New TypeB
b.Report()

刪除

[edit | edit source]

描述: 為將來擴充套件保留

示例:

Rem
Reserved for future expansions.
End Rem

釋放

[edit | edit source]

描述: 釋放整數物件控制代碼

示例:

Rem
Release removes the internal reference caused by creating an integer handle to a type.
End Rem

Type MyType
	Field bigmap[1024*1024]
End Type

GCCollect
Print GCMemAlloced()

a=New MyType
GCCollect
Print GCMemAlloced()

Release a
GCCollect
Print GCMemAlloced()

公開

[edit | edit source]

描述: Public 使常量、全域性變數或函式從當前原始檔外部訪問(預設)

示例:

Rem
Public makes a variable, function or method accessible from outside the current source file (default).
End Rem

Public

Global	Score,Lives,Health

Private

Global	posx,posy,posz

私人

[edit | edit source]

描述: Private 使常量、全域性變數或函式只能從當前原始檔內部訪問

示例:

Rem
Private makes a variable, function or method only accessible from within the 
current source file.
End Rem

Public

Global	Score,Lives,Health

Private

Global	posx,posy,posz

外部

[edit | edit source]

描述: Extern 標記外部函式宣告列表的開頭

示例:

Rem
Extern marks the beginning of an external list of function declarations.
End Rem

Extern 
	Function puts( str$z )
	Function my_puts( str$z )="puts"
End Extern

puts "Using clib's put string!"
my_puts "Also using clib's put string!"

EndExtern

[edit | edit source]

描述: EndExtern 標記 Extern 部分的結尾

示例:

Rem
EndExtern marks the end of an Extern section.
End Rem

Extern 
	Function puts( str$z )
End Extern

puts "Using clib's put string!"

模組

[edit | edit source]

描述: 宣告模組範圍和識別符號

資訊: 有關更多資訊,請參見 模組

示例:

Rem
The Module keyword advises the BlitzMax program maker BMK to create a code 
module from the source file.
End Rem

Module PUB.Sequencer

ModuleInfo "Framework: Audio Sequencer for use with FreeAudio"
ModuleInfo "Copyright: Blitz Research Ltd"
ModuleInfo "Author: Simon Armstrong"
ModuleInfo "Version: 1.00"

ModuleInfo

[edit | edit source]

描述: 定義模組屬性

示例:

Rem
ModuleInfo allows properties such as Author and Copyright to be included in a module file.
End Rem

Module PUB.Sequencer

ModuleInfo "Framework: Audio Sequencer for use with FreeAudio"
ModuleInfo "Copyright: Blitz Research Ltd"
ModuleInfo "Author: Simon Armstrong"
ModuleInfo "Version: 1.00"

Incbin

[edit | edit source]

描述: 嵌入資料檔案

示例:

Rem
IncBin embeds an external data file in a BlitzMax program that can 
then be read using the "incbin::" device name.
End Rem

' code snippet from demos/firepaint/firepaint.bmx

Incbin "stars.png"

Local stars=LoadImage( "incbin::stars.png" )

IncbinPtr

[edit | edit source]

描述: 獲取嵌入資料檔案的起始地址

示例:

Rem
IncBinPtr returns a byte pointer to the specified embedded binary file.
End Rem

Incbin "incbinptr.bmx"

Local p:Byte Ptr=IncbinPtr("incbinptr.bmx")
Local bytes=IncbinLen("incbinptr.bmx")

Local s$=String.FromBytes(p,bytes)

Print "StringFromBytes(p,bytes)="+s$

IncbinLen

[edit | edit source]

描述: 獲取嵌入資料檔案的長度

示例:

Rem
IncBinLen returns the size in bytes of the specified embedded binary file.
End Rem

Incbin "incbinlen.bmx"

Local p:Byte Ptr=IncbinPtr("incbinlen.bmx")
Local bytes=IncbinLen("incbinlen.bmx")

Local s$=StringFromBytes(p,bytes)

Print "StringFromBytes(p,bytes)="+s$

包括

[edit | edit source]

描述: Include 有效地將指定檔案“插入”正在編譯的檔案中。

框架

[edit | edit source]

描述: Framework 使用指定的模組構建 BlitzMax 應用程式,而不是構建所有已安裝的模組。

註釋: 如果使用 Framework 指定了某個模組,則僅該模組及其匯入的模組會與程式碼一起編譯。然後,您可以使用 Import 匯入其他模組。常見的框架模組是 _Brl.Basic_ 和 Brl.Max2D,分別用於控制檯和 2D 應用程式。

進口

[edit | edit source]

描述: 從模組或原始檔匯入宣告

示例:

Rem
:Import specifies the external BlitzMax modules and source files used by the program.
End Rem

Framework BRL.GlMax2D

Import BRL.System

Graphics 640,480,32

While Not KeyHit(KEY_ESCAPE)
	Cls
	DrawText "Minimal 2D App!",0,0
	Flip
Wend

斷言

[edit | edit source]

描述: 如果條件為 False,則丟擲 RuntimeError

註釋: Assert 在除錯模式下丟擲執行時錯誤。

示例:

Rem
Assert generates a BlitzMax runtime error if the specified condition is false.
End Rem

a=LoadImage("nonexistant image file")
Assert a,"Image Failed to Load"

描述: 將程式流程轉移到指定的標籤

註釋: Goto 在 SuperStrict 或 Strict 模式下不起作用。 此外,標籤僅限於 DefData.

示例:

Rem
Causes program execution to jump to the #label specified.
End Rem

Print "one"
Goto here
Print "two"
#here
Print "three"

描述: 開始宣告 Try 塊

註釋: Try 塊嘗試捕獲可能在 Try 和 Catch 之間丟擲的異常,並嘗試處理該異常(而不是終止程式)。

示例:

Rem
Begin declaration of a Try block.
End Rem

Try
	Repeat
		a:+1
		Print a
		If a>20 Throw "chunks"
	Forever
Catch a$
	Print "caught exception "+a$
EndTry

描述: 結束宣告 Try 塊

示例:

Rem
EndTry
EndTry marks the end of a Try block.
End Rem

描述: 在 Try 塊中捕獲異常物件

示例:

Rem
Catch defines an exception handler following a Try..EndTry Block.
End Rem

Try
	Repeat
		a:+1
		Print a
		If a>20 Throw "chunks"
	Forever
Catch a$
	Print "caught exception "+a$
EndTry

描述: 將異常物件丟擲到包含的 Try 塊

示例:

Rem
Throw generates a BlitzMax exception.
End Rem

Try
	Repeat
		a:+1
		Print a
		If a>20 Throw "chunks"
	Forever
Catch a$
	Print "caught exception "+a$
EndTry

描述: 定義類 BASIC 樣式資料

示例:

' defdata.bmx

ReadData name$
ReadData age,skill

Print "name="+name+" age="+age+" skill="+skill

DefData "Simon",37,5000

描述: 讀取經典 BASIC 樣式資料

RestoreData

[編輯 | 編輯原始碼]

描述: 恢復經典 BASIC 樣式資料

示例:

' restoredata.bmx

For i=1 To 5
	RestoreData mydata	'reset the data pointer everly loop so we don't read past the end
	ReadData name$,age,skill
	Print "name="+name+" age="+age+" skill="+skill
Next

#mydata	'program label that can be used with the RestoreData command

DefData "Simon",37,5000

描述: 條件“And”二元運算子

示例:

Rem
And is a boolean operator that performs the AND function.
End Rem

For i=1 To 10
	If i>3 And i<6 Print "!" Else Print i
Next

描述: 條件“Or”二元運算子

示例:

Rem
Or is a boolean operator that performs the OR function.
End Rem

For i=1 To 5
	If i=2 Or i=4 Print "!" Else Print i
Next

描述: 條件“Not”二元運算子

示例:

Rem
Not is a boolean unary operator that performs the NOT function.
End Rem

Print Not 0		'prints 1 (TRUE)
Print Not 20		'prints 0 (FALSE)

描述: 位“左移”二元運算子

示例:

Rem
Shl is a binary operator that performs the shift to left function.
End Rem

b=1
For i=1 To 32
	Print Bin(b)
	b=b Shl 1
Next

描述: 位“右移”二元運算子

示例:

Rem
Shr is a binary operator that performs the shift to right function.
End Rem

b=-1
For i=1 To 32
	Print Bin(b)
	b=b Shr 1
Next

描述: 位“算術右移”二元運算子

示例:

Rem
Sar is a binary operator that performs the arithmetic shift to right function.
End Rem

b=$f0f0f0f0
For i=1 To 32
	Print Bin(b)
	b=b Sar 1
Next

描述: 字串中字元數量或陣列中元素數量

註釋: 您也可以使用陣列或字串的 length 欄位,因為它們是物件。

示例:

Rem
Len is a BlitzMax operator that returns the number of elements in a container Type.
End Rem

a$="BlitzMax Rocks"
Print Len a$	'prints 14

Local b[]
Print Len b		'prints 0

b=New Int[20]
Print Len b		'prints 20

描述: 數值“絕對值”一元運算子

示例:

Rem
Abs is a mathematical operator that performs the Absolute function.
End Rem

For f#=-1 To 1 Step 0.125
	Print "Abs "+f+"="+Abs f
Next 

描述: 數值“模數”或“餘數”二元運算子

示例:

Rem
Mod is a mathematical operator that performs the Modulo function.
End Rem

For i=6 to -6 Step -1
	Print i+" Mod 3="+(i Mod 3)
Next 

描述: 數值“符號”一元運算子

示例:

Rem
Sgn is a mathematical operator that returns the sign of a value.
End Rem

Print Sgn 50	'1
Print Sgn 0	'0
Print Sgn -50	'-1

描述: 數值“最小值”內建函式

返回: 兩個數值引數中較小的那個

描述: 數值“最大值”內建函式

返回: 兩個數值引數中較大的那個

描述: 查詢變數的地址

示例:

Rem
Varptr returns the address of a variable in system memory.
End Rem

Local a:Int
Local p:Int Ptr

a=20
p=Varptr a
Print p[0]

描述: 變數、字串、陣列或物件佔用的位元組大小

示例:

Rem
SizeOf returns the number of bytes of system memory used to store the variable.
End Rem

Type MyType
	Field a,b,c
End Type

Local t:MyType
Print SizeOf t	'prints 12

Local f!
Print SizeOf f	'prints 8

Local i
Print SizeOf i	'prints 4

Local b:Byte
Print SizeOf b	'prints 1

a$="Hello World"
Print SizeOf a	'prints 22 (unicode characters take 2 bytes each)

描述: 獲取字串第一個字元的字元值

示例:

Rem
Asc returns the unicode value of the first character of a string.
End Rem

Print Asc("A")	'65
Print "A"[0]	'65 - equivalent index style implementation

描述: 建立一個長度為 1 的字串,其中包含字元程式碼

示例:

Rem
Chr returns a String of length 1 containing the unicode character of the value.
End Rem

Print Chr(65)	'A
華夏公益教科書