Beispiel für "relative Koordinaten"

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

 

walski

Ehemaliger Admin

Betreff: Beispiel für "relative Koordinaten"

BeitragSo, März 21, 2004 16:02
Antworten mit Zitat
Benutzer-Profile anzeigen
Moin,
ein Beispiel für, von mir sog., "relative Koordinaten".
Das heißt: Egal welche Auflösung, die Bilder sind immer an der gleichen Stelle und werden entsprechend vergrößert/verkleinert.
Dazu habe ich den Screen in ein Raster von 1000*1000 Punkten eingeteilt, die dann auf die jeweilige Auflösung umgerechnet werden.
Nebenbei ist da noch n wirklich sehr simpler Config-Parser bei.

BlitzBasic: [AUSKLAPPEN]

; just a tiny MenuSample
; (c) 2004 by walski

; Screen Mode Constants -> SM_XXX
Const SM_FullScreen = 1
Const SM_Windowed = 2
Const SM_Resize = 3

; Screen Data Field
Type Screen
Field Width,Height
Field Mode
Field Depth
End Type
Global Screen.Screen = New Screen

;Config Parameter
Type confParams
Field Parameter$
End Type

;Global Image-Vars
Global IMG_Switch640
Global IMG_Switch800
Global IMG_Cursor

;Local Vars
;Mouse Position
Local mX,mY
;Stream Data
Local Stream

;Writing a test Config
Stream = WriteFile("m00.dat")
WriteLine Stream,"screendata=640,480,32,1"
CloseFile(Stream)

;Mainloop
Init()
While Not KeyHit(1)
Cls
;Draw the Switch-Images
;Relative-Matrix = 1000*1000 "points"
;Center of the Screen: 500,500
;Relative Image-Sizes: 200*100
;Half of each Image: 100,50
;Drawing at: CenterOfScreen - AHalfImage
;This is for both, x and y
DrawImage IMG_Switch640,Rel2Abs(500,Screen\Width)-Rel2Abs(100,Screen\Width),Rel2Abs(250,Screen\Height)-Rel2Abs(50,Screen\Height)
DrawImage IMG_Switch800,Rel2Abs(500,Screen\Width)-Rel2Abs(100,Screen\Width),Rel2Abs(750,Screen\Height)-Rel2Abs(50,Screen\Height)
;Drawing the Mouse
mX = MouseX()
mY = MouseY()
DrawImage IMG_Cursor,mX,mY

;Switching the Modes
If MouseHit(1) Then
;If Mouse is "inside" the 640er Switch
If mX > Rel2Abs(500,Screen\Width)-Rel2Abs(100,Screen\Width) And mX < Rel2Abs(500,Screen\Width)+Rel2Abs(100,Screen\Width) Then
If mY > Rel2Abs(250,Screen\Height)-Rel2Abs(50,Screen\Height) And mY < Rel2Abs(250,Screen\Height)+Rel2Abs(50,Screen\Height) Then
;Rewrite the config-file and reinitialise
Stream = WriteFile("m00.dat")
WriteLine Stream,"screendata=640,480,32,1"
CloseFile(Stream)
Init()
EndIf
EndIf
;If Mouse is "inside" the 800er Switch
If mX > Rel2Abs(500,Screen\Width)-Rel2Abs(100,Screen\Width) And mX < Rel2Abs(500,Screen\Width)+Rel2Abs(100,Screen\Width) Then
If mY > Rel2Abs(750,Screen\Height)-Rel2Abs(50,Screen\Height) And mY < Rel2Abs(750,Screen\Height)+Rel2Abs(50,Screen\Height) Then
;Rewrite the config-file and reinitialise
Stream = WriteFile("m00.dat")
WriteLine Stream,"screendata=800,600,32,1"
CloseFile(Stream)
Init()
EndIf
EndIf
EndIf
;Debug Output. ABSOLUTE Coordinates... I'm too lazy to do this Relative, too... hm this comment took more time then coding this relative....dooo!
Text 1,1,Str(Screen\Width) + " (" + Str(GraphicsWidth()) + ")"
Text 1,20,(Screen\Height) + " (" + Str(GraphicsHeight()) + ")"
Text 1,40,Str(Screen\Depth) + " (" + Str(GraphicsDepth()) + ")"
Text 1,60,Screen\Mode
Flip
Wend

;Reads a config file
Function LoadSettings(file$)
;Uses Globals:
;- Screen

;The Config-File Stream
Local Stream
;The current Line
Local cLine$
;The current Command-Part of cLine$
Local cCommand$
;The current Parameter-Part of cLine$
Local cParams.confParams
;Loop Var
Local i
;Currently checked Character
Local cChar$
;Open Quotation Indicator
Local QuoteOpen
;Last Position of the Parameter Seperator
Local lastSeperator

;Open Config File
Stream = ReadFile(file$)
While Not Eof(Stream)
;Read the current Line
cLine$ = ReadLine(Stream)
;Go through every single character of the current line and skip spaces
For i=1 To Len(cLine$)
cChar$ = Mid(cLine$,i,1)
Select cChar$
;If the current character is a " then change the Quotation-Mode
Case Chr(34)
If QuoteOpen = 1 Then
QuoteOpen = 0
Else
QuoteOpen = 1
EndIf
;If there is a space and it is not inside of two " " then delete it
Case " "
If QuoteOpen = 0 Then
cLine$ = Mid(cLine$,1,i-1) + Mid(cLine$,i+1)
i = i - 1
EndIf
End Select
Next
;A config line is assembled like this:
;Command = Param1,Param2,"Param 3",Param4
;This reads the current Command. The part in front of the =
Command$ = Mid(cLine$,1,Instr(cLine$,"=")-1)


;Cut the current Commandout of the current Line
cLine$ = Mid(cLine$,Len(Command$) + 2)
lastSeperator = 1
;Deletes every old Parameter
For cParams = Each confParams
Delete cParams
Next
;Goes through every single character of the remaining line and looks for ,
For i=1 To Len(cLine$)
cChar$ = Mid(cLine$,i,1)
Select cChar$
;If the current character is a " then change the Quotation-Mode and delete the "
Case Chr(34)
If QuoteOpen = 1 Then
cLine$ = Mid(cLine$,1,i-1) + Mid(cLine$,i+1)
i = i - 1
QuoteOpen = 0
Else
cLine$ = Mid(cLine$,1,i-1) + Mid(cLine$,i+1)
i = i - 1
QuoteOpen = 1
EndIf
;If there is a , then add a new Parameter which lasts in the current line from the last found , to the current position
Case ","
If QuoteOpen = 0 Then
cParams = New confParams
cParams\Parameter$ = Mid(cLine$,lastSeperator,(i)-lastSeperator)
cLine$ = Mid(cLine$,1,i-1) + Mid(cLine$,i+1)
i = i - 1
lastSeperator = i + 1
EndIf
End Select
Next
;Adds the part from the last found , to the end of line as a new Parameter
cParams = New confParams
cParams\Parameter$ = Mid(cLine$,lastSeperator)

;Select LowerCase Command - Not Case-Sensitive
Select Lower(Command$)
Case "screendata"
cParams = First confParams
;Set Screen Width
Screen\Width = Int(cParams\Parameter$)
cParams = After cParams
;Set Screen Height
Screen\Height = Int(cParams\Parameter$)
cParams = After cParams
;Set Screen Depth
Screen\Depth = Int(cParams\Parameter$)
cParams = After cParams
;Set Screen Mode
Screen\Mode = Int(cParams\Parameter$)
End Select
Wend
End Function

;Translates the Relative "Pos" to it's absolute Value at range "size"
Function Rel2Abs(Pos,Size)
Return Int((Size/1000.0)*Pos)
End Function

;Creates Sample Images
Function CreateSampleIMGS()
;Uses Globals:
;- IMG_Switch640
;- IMG_Switch800
;- IMG_Cursor

;Creates the Image of the Switch to 640*480 resolution
IMG_Switch640 = CreateImage(100,50)
SetBuffer ImageBuffer(IMG_Switch640)
Color 255,0,0
Rect 0,0,100,50
Color 255,255,255
Text 50,25,"640*480",1,1

;Creates the Image of the Switch to 800*600 resolution
IMG_Switch800 = CreateImage(100,50)
SetBuffer ImageBuffer(IMG_Switch800)
Color 255,0,0
Rect 0,0,100,50
Color 255,255,255
Text 50,25,"800*600",1,1

;Creates the Mouse-Cursor Image
IMG_Cursor = CreateImage(16,16)
SetBuffer ImageBuffer(IMG_Cursor)
Rect 0,0,16,1
Rect 0,0,1,16
End Function


;Initilaises the Program
Function Init()
;Uses Globals:
;- Screen

;Load Settings
LoadSettings("m00.dat")
;Set Graphcs Mode
Graphics Screen\Width,Screen\Height,Screen\Depth,Screen\Mode
;Create Images
CreateSampleIMGS()
;Resizes the Images
FitImages()
;Initalises the DoubleBuffering
SetBuffer BackBuffer()
End Function

;Resizes the Images to the current resolution-fiting values
Function FitImages()
;Uses Globals:
;- IMG_Switch640
;- IMG_Switch800
;- IMG_Cursor
;- Screen

TFormFilter 0
ResizeImage IMG_Switch640,Rel2Abs(200,Screen\Width),Rel2Abs(100,Screen\Height)
ResizeImage IMG_Switch800,Rel2Abs(200,Screen\Width),Rel2Abs(100,Screen\Height)
ResizeImage IMG_Cursor,Rel2Abs(50,Screen\Width),Rel2Abs(50,Screen\Height)
TFormFilter 1
End Function


walski
buh!

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group