SmoothScrolling

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Farbfinsternis

Betreff: SmoothScrolling

BeitragDo, Dez 28, 2006 18:53
Antworten mit Zitat
Benutzer-Profile anzeigen
Folgender Source erstellt eine beliebig große Tilemap welche mit einstellbarem ScrollSpeed gescrollt werden kann (Pfeiltasten). Dabei wird immer nur der tatsächlich sichtbare Bereich gezeichnet.

Code: [AUSKLAPPEN]

SuperStrict

Graphics 800,600,16,0
SetBlend ALPHABLEND

Const TILE_WIDTH:Byte         = 32
Const TILE_HEIGHT:Byte         = 32


Type TLevel
   Field disp_w:Int
   Field disp_h:Int
   Field scr_w:Int
   Field scr_h:Int
   
   Field width:Int
   Field height:Int
   
   Field background:Int[,]
   
   Field as:Int
   Field az:Int
   Field cx:Int
   Field cy:Int
   
   Field scrollspeed:Int
   
   Function Init:TLevel(width:Int, height:Int)
      Local level:TLevel = New TLevel
      
      level.width            = width
      level.height         = height
      level.as               = 0
      level.az               = 0
      level.cx               = 0
      level.cy               = 0
      level.scrollspeed   = 4
      
      level.background   = New Int[width, height]
      
      level.scr_w            = GraphicsWidth()
      level.scr_h            = GraphicsHeight()
      
      level.disp_w         = (level.scr_w/TILE_WIDTH)+2
      level.disp_h         = (level.scr_h/TILE_HEIGHT)+2
      
      If level.disp_w > width - 1 Then level.disp_w = width - 1
      If level.disp_h > height - 1 Then level.disp_h = height - 1
      
      Return level
   End Function
   
   
   Method Draw()
      Local s:Int = 0
      Local z:Int = 0
      Local x:Int = cx
      Local y:Int = cy
      
      Repeat
         Local tile:Int = background[as+s, az+z]
         
         If tile = 0 Then DrawRect x,y,TILE_WIDTH-1, TILE_HEIGHT-1
         
         s:+ 1
         x:+ TILE_WIDTH
         
         If s = disp_w
            s = 0
            x = cx
            z:+ 1
            y:+ TILE_HEIGHT
         EndIf
      Until z = disp_h
   End Method
   
   
   Method ScrollUp()
      cy:+scrollspeed
      If cy > 0
         az:-1
         cy = (0-TILE_HEIGHT)+scrollspeed
      EndIf
      
      If az <= 0
         cy = 0
         az = 0
      EndIf
   End Method
   
   
   Method ScrollRight()
      cx:-scrollspeed
      If cx < 0-TILE_WIDTH
         as:+ 1
         cx = -scrollspeed
      EndIf
      
      If as+disp_w >= width-1
         as = width - disp_w
         cx = 0
      EndIf
   End Method
   
   
   Method ScrollDown()
      cy:-scrollspeed
      If cy < 0-TILE_HEIGHT
         az:+ 1
         cy = -scrollspeed
      EndIf
      
      If az+disp_h >= height-1
         az = height - disp_h
         cy = 0
      EndIf

   End Method
   
   
   Method ScrollLeft()
      cx:+scrollspeed
      If cx > 0
         as:-1
         cx = (0-TILE_WIDTH)+scrollspeed
      EndIf
      
      If as <= 0
         cx = 0
         as = 0
      EndIf
   End Method
End Type




Local level:TLevel = TLevel.Init(50,50)




Repeat
   Cls
   
   If KeyHit(KEY_ESCAPE) Then Exit
   If KeyDown(KEY_UP) Then level.ScrollUp()
   If KeyDown(KEY_RIGHT) Then level.ScrollRight()
   If KeyDown(KEY_DOWN) Then level.ScrollDown()
   If KeyDown(KEY_LEFT) Then level.ScrollLeft()
   
   level.Draw()
   
   SetColor 255,0,0
   DrawText String(level.as)+" x "+String(level.az),2,2
   DrawText "FPS: "+String(GetFPS()),2,20
   
   SetColor 0,50,0
   
   Flip
   
Forever

End


Function GetFPS:Int()
   Global fps:Int
   Global fps_time:Int
   Global fps_temp:Int
   
   fps_temp:+ 1
   Local time:Int = MilliSecs()
   
   If time - fps_time > 1000
      fps            = fps_temp
      fps_temp   = 0
      fps_time   = time
   EndIf
   
   Return fps
End Function
Farbfinsternis.tv

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group