PLAYBASIC SOURCE CODES


Object Echoes - Object Motion Blur

Object Echoes / Object Motion Blur

Today we'll take a look a one approach for creation echo or motion blur styled effect using nothing more than some variable addition alpha blending.

PlayBASIC Code:
; PROJECT : Echo or Motion Effect
; AUTHOR : Kev Picone - http://PlayBASIC.com
; CREATED : 14/11/2021
; EDITED : 14/11/2021
; ---------------------------------------------------------------------



// -------------------------------------------------------
// ---------------------- MAIN LOOP ----------------------
// -------------------------------------------------------



type tobject
X#(10)
Y#(10)

SpeedX#
SpeedY#
Colour
endtype

Dim Objects(35) as tobject

for lp=1 to getarrayelements(Objects())
Objects(lp)= new tobject
x#=rnd(800)
y#=rnd(600)


for poslp=0 to 10
Objects(lp).x#(poslp) = x#
Objects(lp).y#(poslp)= y#
next

Speed#=rndrange#(10,20)
Angle#=Rnd(360)
Objects(lp).speedx#=cos(Angle#)*Speed#
Objects(lp).speedy#=sin(Angle#)*Speed#
Objects(lp).Colour =rndrgb()
next




Screen=Newfximage(GetScreenWidth(),GetScreenheight())


// -------------------------------------------------------
do// -------------------- MAIN LOOP ---------------------
// -------------------------------------------------------



for lp=1 to getarrayelements(Objects())
x#=Objects(lp).x#(0)
y#=Objects(lp).y#(0)

x#+=Objects(lp).speedx#
y#+=Objects(lp).speedy#

// scroll old positions down
for oldpos=10 to 1 step -1
Objects(lp).x#(oldpos)=Objects(lp).x#(oldpos-1)
Objects(lp).y#(oldpos)=Objects(lp).y#(oldpos-1)
next

if x#<0 then x#=0 : Objects(lp).speedx#*= -1
if x#>800 then x#=800 : Objects(lp).speedx#*= -1
if y#<0 then y#=0 : Objects(lp).speedy#*= -1
if y#>600 then y#=600 : Objects(lp).speedy#*= -1

Objects(lp).x#(0)=x#
Objects(lp).y#(0)=y#

next



rendertoimage Screen
cls 0
// render 
lockbuffer
; set ink pen drawing mode to Alpha Addition / Alpha ADD
inkmode 1+64

; step through the objects and the draw the oldest ones first
; looping to the last down to first.
for pass=10 to 0 step -1
blendlevel#=cliprange#(pass/10.0,0,1)

BlendColour =255-(255*BlendLevel#)
BlendColour=Rgb(BlendColour,BlendColour,BlendColour)


// Draw all the objects from this pass in one group
for lp=1 to getarrayelements(Objects())
x#=Objects(lp).x#(pass)
y#=Objects(lp).y#(pass)

// Compute the objects colour with the fade level
ThisRGB = rgbAlphamult (Objects(lp).Colour, BlendColour)

// draw it as a circle
circlec x#,y#,32,true,ThisRGB
next
next

unlockbuffer

; set inkmode back to normal
inkmode 1
rendertoscreen
drawimage screen,0,0,false


sync
wait 10
loop spacekey()



PlayBASIC LIVE - Overview of Object Echo / Motion Blur Example - (2021-11-16)



Point On Line - Line Hit Point - Point Intersect Line

Point On Line / Line Hit Point / Point Intersect Line

    This code defines a tLines data type that consists of four floating point numbers (x1#, y1#, x2#, y2#). It then creates an array of 100 elements of this data type called Lines.

    It then initializes each element of the Lines array by assigning random values to its x1#, y1#, x2#, and y2# fields.

    Afterwards, the code enters an infinite loop that does the following:

     Clears the screen

     Gets the current mouse position and assigns it to variables mx# and my#

     Iterates through each element lp in the Lines array and does the following:

     Assigns the x1#, y1#, x2#, and y2# fields of the current element to local variables x1#, y1#, x2#, and y2#, respectively

     Calls the Point_On_Line function, passing it mx#, my#, x1#, y1#, x2#, and y2# as arguments. The return value of this function is a boolean that indicates whether the point (mx#, my#) is on the line defined by the points (x1#, y1#) and (x2#, y2#).

     If the return value of Point_On_Line is True, it sets the current drawing color to a random RGB value.

     It then draws a line between the points (x1#, y1#) and (x2#, y2#) using the current drawing color.

     Waits for the vertical blanking interval (to prevent tearing)

     Loops back to the beginning if the space key is not pressed

    The code then defines the Point_On_Line function, which takes six floating point arguments: pointx#, pointy#, x1#, y1#, x2#, and y2#. It returns a boolean value indicating whether the point (pointx#, pointy#) is on the line defined by the points (x1#, y1#) and (x2#, y2#).

    The function first checks if the point (pointx#, pointy#) is at least parallel to the line defined by the points (x1#, y1#) and (x2#, y2#). If it is, it calculates the nearest point on the line to (pointx#, pointy#) and checks if the distance between the two points is within a certain tolerance (1 in this case). If the distance is within the tolerance, the function returns True, otherwise it returns False. If the point (pointx#, pointy#) was not parallel to the line, the function immediately returns False.

PlayBASIC Code:
; PROJECT : POint On Line
; AUTHOR : PlayBASIC TUTOR
; CREATED : 4/10/2021
; EDITED : 5/10/2021
; ---------------------------------------------------------------------

type tlines
x1#,y1#,x2#,y2#
endtype

Dim Lines(100) as tLines

for lp =0 to 100

Lines(lp).x1 = rnd(800)
Lines(lp).y1 = rnd(600)
Lines(lp).x2 = rnd(800)
Lines(lp).y2 = rnd(600)


next

do
cls

mx#=mousex()
my#=mousey()

for lp =0 to 100

x1#=lines(lp).x1
y1#=lines(lp).y1
x2#=lines(lp).x2
y2#=lines(lp).y2
ink -1
if Point_On_line(mx#,my#,x1#,y1#,x2#,y2#)
ink rndrgb()
endif
line x1#,y1#,x2#,y2#
next

sync
loop spacekey()

end




function Point_On_line(pointx#,pointy#,x1#,y1#,x2#,y2#)

// compute nearest point along the line
  dx31#=PointX#-x1#
  dx21#=x2#-x1#
  dy31#=PointY#-y1#
  dy21#=y2#-y1#

l#=((dx31#*dx21#)+(dy31#*dy21#)) / ((dx21#*dx21#)+(dy21#*dy21#))

// see if our point at least lies parallel with our line
status = l#>=0 and L#<=1
if Status

// if so, we compute the nearest point on the line
x#=x1#+(dx21#*l#)
y#=y1#+(dy21#*l#)

// then get the distance from the line to our point. 
Dist# = getdistance2d(x#,y#,pointx#,pointy#)

// check if it's within a tolerance of 1, might need
// to be tweaked 
Status = Dist#<=1

endif

EndFunction status
COMMANDS USED: DIM | RND | CLS | MOUSEX | MOUSEY | INK | RNDRGB | LINE | SYNC | SPACEKEY | AND | GETDISTANCE2D |

Sin Scroller X and Y axis

     This code is rendering text to the screen in a sine wave pattern. The text will fade in and out, and the vertical position of the text will wobble. The text will also have a gradient color effect, with the color changing from blue to purple. The text is displayed in the center of the screen, and will continue to animate until the space key or escape key is pressed.

PlayBASIC Code:
; PROJECT : Xand Y sine wave text renering test
; AUTHOR  : Kev Picone - http://PlayBASIC.com
; CREATED : 30/12/2021
; EDITED  : 30/12/2021
; ---------------------------------------------------------------------


         if GetFontStatus(50)=false
            LoadFont   "Agency FB",50,148,1,8
            FontDrawmode 50,1
         endif
         
            
setfps 20
      screen=newfximage(getscreenwidth(),getscreenheight())
      
      Message$="Sine Wave X&Y Axis"
      FadeLevel#=1
   Do
      ;cls 
      rendertoimage screen
      cls $112233
         setfont 50
      ShadedText(Screen,100,Message$, FadeLevel#)
   rendertoscreen
   
      fadeangle#=wrapangle(fadeangle#,0.5)
   FadeLevel#= 0.5+sin(fadeangle#)   
      drawimage screen,0,0,false

      sync
   loop spacekey()=true or esckey()=true

   
   end




Function ShadedText(Screen,Ypos,Message$, FadeLevel#)
   oldSurface   =getSurface()

  Static WobbleAngle#
  Static WobbleAngle2#


    Static VertialWobbleAngle#
 
      rendertoimage screen
      
      
      //FadeLevel#=cliprange(FadeLevel#,0,1)
      
      screenwidth   =GetSurfaceWidth()
      screenHeight=GetSurfaceHeight()
      
      
      imageviewport Screen,0,0,screenWidth,ScreenHeight

      TW=GetTextWidth(Message$)

      Xpos=(ScreenWidth/2)-(TW/2)

      ThisRGB=rgb(50,50,50)


      
      Level = Cliprange( ($90*FadeLevel#) , 0,255)
      if level>0
         for lp =0 to 2      
            ink rgbfade(ThisRgB,50) or Argb(Level,0,0,0)
            text Xpos,Ypos,Message$
            Level+=$16
            Xpos+=2
            Ypos+=1
         next
      endif   
      Rgb1=rgb(20,70,170)
      Rgb2=rgb(220,230,250)

      Rows=GetTextHeight("|")   
      
      Alpha = Cliprange( ($ff*FadeLevel#) , 0,255)
      
      if Alpha>0
      TextWidthInPixels = GetTextWidth(Message$)
      VerticalWaveChunkSize = 4   
      
      VerticalRows=Rows+16
      For lp =0 to VerticalRows   ;Rows+16
         scaler#=float(lp)/VerticalRows
         
         scaler#*= cos(WobbleAngle#)+0.5
         
         ThisRGB=RgbAlphaBlend(Rgb1,RGB2,Scaler#*100) and $00ffffff
         ink lsl32(Alpha,24) or ThisRGB
         
         
         Ypos2=Ypos+lp
         
         OffsetX   =Cos(WobbleAngle#+(lp*1))*5
         OffsetX   +=Sin(WobbleAngle2#-(lp/5))*3
         
         
         //  draw this strip with 
         
      for xwave=0 to TextWidthInPixels step VerticalWaveChunkSize
         
         
            VerticalOffset =sin(VertialWobbleAngle#+(Xwave*0.5))*20
         ;   VerticalOffset+=cos(VertialWobbleAngle#+(Xwave*0.3))*4
            
            ;cosradius(WobbleAngle#,12)

            ScreenX1 = xwave+xpos+OffsetX               
            ScreenX2 = ScreenX1+VerticalWaveChunkSize
            FinalYpos =Ypos2+VerticalOffset   
            imageviewport Screen,ScreenX1,ypos2,ScreenX2,Ypos2+1

            text Xpos+OFFSETX,ypos+VerticalOffset,Message$
         next   
   next
   
   endif   

      imageviewport Screen,0,0,screenWidth,ScreenHeight
      
      WobbleAngle#=wrapangle(WobbleAngle#,2.5)
      WobbleAngle2#=wrapangle(WobbleAngle2#,1.5)
      
      
      VertialWobbleAngle#= wrapangle(VertialWobbleAngle#,0.5)
      
   rendertoimage oldSurface

   
endFunction




PlayBASIC - Code Snippet - Sine Wave Scroller - 18th June 2006


This is classic source code example from way back in 2006. The demo shows a simple right to left scrolling message with sine wave distortions applied. First we start with the basic scroller, next we add sine distortion on the X axis, then the Y axis finishing off by combining them..


VIDEO:




music by: https://BenSound.com


Code:


PlayBASIC Code:
; PROJECT : Sine_Scrollers
; AUTHOR : Kevin Picone
; CREATED : 18/06/2006
; EDITED : 18/06/2006
; ---------------------------------------------------------------------

 ; load the Courier New font
Loadfont "Courier New",1,32,0
MakeBitmapFont 1,$ff0000

 ; load the Courier New font
Loadfont "Courier New",2,64,0
MakeBitmapFont 2,$8f70a0
setfont 2

; Make a type to hold the various bits of info ab

SineWaveWidth=32
ScrollHeight=GetTextHeight("Y")


; CReate an array to hold the Scroll text in charater form
Dim ScrollText(0)
  Read_Text_To_Scroller_Array(ScrollText())
 

; Original Scroll buffer
ScrollBuffer=NewImage(800+(SineWaveWidth*2)+GetFontWidth(2),ScrollHeight)

ScrollBuffer2=NewImage(800+(SineWaveWidth*2)+GetFontWidth(2),ScrollHeight)


setfps 60
RendertoScreen
Do
Cls 0

; Update the Scroll Buffer
setfont 2
UpdateScroller(ScrollText(),ScrollBuffer)


; select What effect render 
Select Effect
case 0
; draw the scroll messagae
Drawimage ScrollBuffer,0,280,true
EffectName$="Standard Scroller"

case 1
 Draw_XSine_Image(ScrollBuffer,0,280,32)
EffectName$="Scroller with sine X distortion"

case 2
Draw_YSine_Image(ScrollBuffer,0,280,270)
EffectName$="Scroller with sine Y distortion"

case 3
rendertoimage Scrollbuffer2
Draw_XSine_Image(ScrollBuffer,0,0,24)
rendertoscreen
Draw_YSine_Image(ScrollBuffer2,0,280,270)
EffectName$="Scroller with combined X&Y distortion"
endselect




if Spacekey() 
Inc Effect
If effect>3 then effect=0
flushkeys
endif

Setfont 1
x=getscreenwidth()/2
y=GetScreenheight()/2+200
centertext x,y,EffectName$ 
centertext x,y+50,"Press Space"


Sync
loop


; ---------------------------------------------------------------------
; Copy an Image width a X sine wave applied
; ---------------------------------------------------------------------

Function Draw_XSine_Image(ThisImage,Xpos,Ypos,XwaveRAdius)
Static BaseAngle# 

OutputBuffer=getsurface()

IW=GetImageWidth(ThisImage)
IH=GetImageheight(ThisImage)

For Angle#=BaseAngle# to Baseangle#+IH
X=SinRadius(Angle#,XwaveRadius)
CopyStripH ThisImage,y,0,iw,OutPutBuffer,Xpos-XwaveRAdius+X,ypos 
inc Y
inc Ypos
next

BaseAngle#=wrapangle(BaseAngle#,0.5)
rendertoscreen
EndFUnction




; ---------------------------------------------------------------------
; Copy an Image with a Y sine wave applied
; ---------------------------------------------------------------------

Function Draw_YSine_Image(ThisImage,Xpos,Ypos,WaveRAdius)
Static BaseAngle# 

OutputBuffer=getsurface()

IW=GetImageWidth(ThisImage)
IH=GetImageheight(ThisImage)

Angle#=baseangle#
if iw>GetImageWidth(Outputbuffer) then iw=GetImageWidth(Outputbuffer)
For x=0 to iw-1
Y=SinRadius(Angle#,waveRadius)
; to reduce the number of blits
CopyRect ThisImage,x,0,x+2,ih,OutPutBuffer,Xpos,ypos+Y 
inc xpos
inc xpos
Angle#=wrapangle(Angle#,0.5)
next

BaseAngle#=wrapangle(BaseAngle#,2)
rendertoscreen
EndFUnction







; ---------------------------------------------------------------------
; Refresh Scroll Text/ Buffer
; ---------------------------------------------------------------------

Function UpdateScroller(ChrBuffer(),ScrollBuffer)
; use static variables to keep track of the displacement
Static PixelShifts,CurrentChr
CurrentSurface=getSurface()

; get the current fonts + width & height
ThisFont=GetCurrentFOnt()
FontWidth=GetFontWidth(ThisFont)
FontHeight=GetFontHeight(ThisFont)

; Get the Scroll Buffers Width and Height
IW=GetImagewidth(ScrollBuffer)
IH=GetImageHeight(ScrollBuffer)


; Move the Scroll buffer 1 pixel to the left 
 CopyRect ScrollBuffer,1,0,IW,IH,ScrollBuffer,0,0

 Inc PixelShifts
 if PixelShifts=>GetFontWidth(GetCurrentFont())
 PixelShifts=0

 rendertoimage scrollbuffer
; clear any left over pixels 
boxc iw-fontwidth,0,iw,ih,true,0
; draw a new character on the end of the scroll image buffer
 text iw-FontWidth,0,chr$(ChrBuffer(CurrentChr))

; bump the current character index
inc CurrentChr
; check if the current character index is inside the buonds
; of the Scroll text array
if CurrentChr=>GetArrayElements(Chrbuffer(),1)
CurrentChr=0
endif
 endif

; restore old render surface
RenderToImage CurrentSurface

EndFunction



Function Read_Text_To_Scroller_Array(Buffer())
Dim buffer(0)
repeat
Size=GetArrayElements(Buffer(),1)
t$=readdata$()
if t$<>""
redim Buffer(Size+Len(t$))
For c=1 to len(t$)
buffer(size+c-1)=mid(t$,c)
next
endif
until t$=""
EndFUnction


 data "Hello World... Here's some old skool scroll text" 
 Data "......................."
 data "" ; end of scroll text

X and Y Sine Wave Code Snippet.png

Convert Map To Collision Worlds

     This code creates a game level with a series of randomly colored boxes on the screen, which are drawn as an image. The image is then imported into a map called "MyMap." The code then reads in a level from data statements, makes the level transparent, and draws it to the screen. The code also creates a collision world from the map, and sets up a sprite and camera.

    The sprite can be moved around the screen using arrow keys, and will automatically slide off walls it hits in the collision world. The camera is positioned over the sprite, and will draw what is in view. The game will continue to loop until the space key is pressed.

PlayBASIC Code:
  MyMap =NewMap(1)  
  
;  
  TileWidth=48
  TileHeight=48
  Number_of_tiles = 5
  
; Loop through and draw a series of randomly coloured Boxes to the screen
  For lp=1 To Number_of_tiles
     xpos=lp*tileWidth  
     BoxC Xpos,0,Xpos+TileWidth,TileHeight,1,RndRGB()     
  Next 

; Grab the box graphics that we've just drawn as image.
  TempImage=GetFreeImage()
  GetImage TempImage,0,0,xpos+TIleWidth,Tileheight
  
  
; Import this image into our previously defined Map.  
  MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0)



; =======================================================
; Read the Level in from the data statements bellow
; =======================================================
  
   Read_Level(1,1)

   Leveltransparent 1,1,0
    

; Draw the Level to the screen at Xpos 0 and Ypos 0
   Xpos=0
   Ypos=0


 ; Create the Collision world
   MyCollisionWorld=NewWorld()
   CaptureToWorld MyCollisionWorld
   MakeCollisionWorldFromMap(MyMap,1,xpos,ypos)
   PartitionWorld MyCollisionWorld,64

; restore drawing back to immediate mode
   drawgfximmediate
   
   
   PLayerSize = 32
   
; Create an image of a circle (for something to look at)
   Cls 0
   Size=PLayerSize/2
   Circlec Size,Size,Size,1,$00ff00
   GetImage 10,0,0,PLayerSize,PLayerSize
   preparefximage 10


; create a sprite 
   CreateSprite 1
   autocenterspritehandle 1,true
   SpriteImage 1,10
   spriteCollision 1,true
;  Set collision mode to World
   SpriteCollisionMode 1,4
   SpriteCollisionWorld 1,MyCollisionWorld

; Set the Sprites Collision Circle to just smaller than then players
; image.  In this example the players image is also a circle.
   SpriteCollisionRadius 1,(PlayerSize/2)-2
;   SpriteCollisionDebug 1,true
   
   PositionSprite 1,150,150

 ; Create a Camera
   CreateCamera 1 
  

   Do
        CaptureToScene
        ClsScene 
     
        Capturedepth 100          
        DrawMap MyMap,1,0,0

   
    ; MOve the Sprite around (8 way movement)
        Speed=5
     
        Xspeed=0
        Yspeed=0
        if LeftKey() then Xspeed=-Speed
        if RightKey() then Xspeed=Speed
        if UpKey() then Yspeed=-Speed
        if DownKey() then Yspeed=Speed
   
        if Xspeed<>0 or Yspeed<>0 
        ; MOve the sprite, this will auto slide the sprite
        ; off any wall it hits in the Collision World 
                MoveSprite 1,xspeed,yspeed
        endif
     
        DrawAllSprites

    ;POsition the camera over the sprite
        Xpos=GetSpriteX(1)-(GetScreenWidth()/2)
        Ypos=GetSpriteY(1)-(GetScreenHeight()/2)
        POsitionCamera 1,Xpos,Ypos

    ; Tell the camera to draw whats in viwe
        Drawcamera 1     

     Sync
     
   loop spacekey()
end





; =============================================================================
;  This Function converts a map Level into collision world, which is nothing
; more than drawing lines around the bounding area around of the tiles.  It
; considers tile zero to be transparent (which is soft). Everything else is
; think is hard  
;
;  once the map is a collision world you can run ray interest on it, sliding
; collision among other things..
; =============================================================================


   Acreset
   Constant EdgeState_Searching   =ac(1)
   Constant EdgeState_FoundStart   =ac(1)


Function MakeCollisionWorldFromMap(thisMap,Thislevel,xpos,ypos)

   LevelWidth   =GetLevelWidth(Thismap,thislevel)
   LevelHeight =GetLevelHeight(Thismap,thislevel)

   BlockWidth=GetMapBLockWidth(ThisMap)
   BlockHeight=GetMapBLockHeight(ThisMap)
  
 ; ====================================================================
; #1 first Pass over the level looking for blocks running Left to Right
 ; =====================================================================

   Dot 0,0

   Y=Ypos
   For Ylp=0 to LevelHeight
  Y2=Y+BlockHeight
  X=Xpos
  
  TopEdgeState=EdgeSTate_Searching
  TopStartXpos=Xpos

  BotEdgeState=EdgeSTate_Searching
  BotStartXpos=Xpos
  
  For Xlp=0 to LevelWidth

     CurrentTile   =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp)

 ; Check if this tile is not our transparent tile zero  
     If CurrentTile<>0

  ; Check the Y position to make sure we don't step off the Level array
    iF Ylp>0
   ; Grab the tile above our current tile
       AboveCurrentTile  =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp-1)
    
   ; Check if this tile is transparent
       if AboveCurrentTile=0  
    ; Since there's no tile above then this must be a top
    ; edge of the block
      if TopEdgeState=EdgeSTate_Searching
         inc TopEdgeState              
         TopStartXpos=X      
      endif
       else
      if TopEdgeState=EdgeSTate_FoundStart
        TopEdgeState=EdgeSTate_Searching
 ;'       Line TopStartXpos,y,X,y
        Line x,y,TopStartXpos,y
      endif
       endif      
    Endif

  ; Check the Y position to make sure we don't step off the Level array
    if Ylp<LevelHeight
   ; Grab the tile above our current tile
       BellowCurrentTile  =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp+1)

   ; Check if this tile is transparent
       if BellowCurrentTile=0  
    ; Since there's no tile above then this must be a top
    ; edge of the block
      if BotEdgeState=EdgeSTate_Searching
         inc BotEdgeState              
         BotStartXpos=X      
      endif
       else
      if BotEdgeState=EdgeSTate_FoundStart
        BotEdgeState=EdgeSTate_Searching
        Line BotStartXpos,y2,X,y2
      endif
       endif      
    endif

     else
  ; close edges
    if TopEdgeState=EdgeSTate_FoundStart
       TopEdgeState=EdgeSTate_Searching
       Line x,y,TopStartXpos,y
    endif
    if BotEdgeState=EdgeSTate_FoundStart
       BotEdgeState=EdgeSTate_Searching
       Line BotStartXpos,y2,X,y2
    endif
     endif

     X=X+BlockWidth
  next
  Y=Y+BlockHeight
   next


 ; ====================================================================
; #2 now we repeat this process, but scanning from Top to Bottom
; =====================================================================

   X=Xpos
   For Xlp=0 to LevelWidth
  X2=X+BlockWidth
  Y=Ypos
  
  LeftEdgeState=EdgeSTate_Searching
  LeftStartYpos=Y

  RightEdgeState=EdgeSTate_Searching
  RightStartYpos=Y
  
  For ylp=0 to LevelHeight
  
     CurrentTile   =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp)

 ; Check if this tile is not our transparent tile zero  
     If CurrentTile<>0

  ; Check the Y position to make sure we don't step off the Level array
    iF Xlp>0
       
   ; Grab the tile to the LEFT of our current tile
       PreviousTile  =PeekLevelTile(Thismap,Thislevel,Xlp-1,Ylp)
    
   ; Check if this tile is transparent
       if PreviousTile=0  
    ; Since there's no tile then this must be the LEFT
    ; edge of the block
      if LeftEdgeState=EdgeSTate_Searching
         inc LeftEdgeState              
         LeftStartYpos=y      
      endif
       else
      if LeftEdgeState=EdgeSTate_FoundStart
        LeftEdgeState=EdgeSTate_Searching
        Line x,LeftStartYpos,x,y
      endif
       endif      
    Endif

  ; Check the X position to make sure we don't step off the Level array
    if Xlp<LevelWidth

   ; Grab the tile above our current tile
       NextTile  =PeekLevelTile(Thismap,Thislevel,Xlp+1,Ylp)

   ; Check if this tile is transparent
       if NextTile=0  
    ; Since there's no tile on the RIGHT then this must be a right
    ; edge of the block
      if RightEdgeState=EdgeSTate_Searching
         inc RightEdgeState              
         RightStartYpos=Y      
      endif
       else
      if RightEdgeState=EdgeSTate_FoundStart
        RightEdgeState=EdgeSTate_Searching
;        Line x2,RightStartYpos,x2,y
        Line x2,y,x2,RightStartYpos
      endif
       endif      
    endif

     else

  ; close edges (if open)
    if LeftEdgeState=EdgeSTate_FoundStart
       LeftEdgeState=EdgeSTate_Searching
       Line x,LeftStartYpos,x,y
    endif

    if RightEdgeState=EdgeSTate_FoundStart
      RightEdgeState=EdgeSTate_Searching
;'      Line x2,RightStartYpos,x2,y
      Line x2,y,x2,RightStartYpos
    endif

     endif

     Y=Y+BlockHeight
  next
  X=X+BlockWidth
   next


EndFunction



` *=----------------------------------------------------------------=*
`                    -------- Read Map ---------
` *=----------------------------------------------------------------=*

Function Read_Level(ThisMap,ThisLevel)
   Map_Width=readdata()
   Map_Height=readdata()

; Init the Level array within this Map
   createlevel ThisMap,ThisLevel,Map_Width+1,Map_Height+1

   for zlp=0 to map_Height-1
  for xlp=0 to map_width-1
     pokeleveltile ThisMap,Thislevel,xlp,zlp,ReadData()
  next xlp
   next zlp

EndFunction Map_Width,Map_Depth




 data 32
 data 42
 data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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0,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1
 data 1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1
 data 1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,1
 data 1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,1
 data 1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
 data 1,0,1,0,0,1,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1
 data 1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

GetAbsolutePathToFile


GetAbsolutePathToFile


This function takes a base path and then computes the absolute path from this base from an assumed relative path in the file name





PlayBASIC Code:
BasePath$="D:\MyProjectFolder\Gfx\_examples"
ink $ff0000
print BasePath$
ink -1

print ""
print GetAbsolutePathToFile(BasePath$, "Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "subfolder/Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "../Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "../../Stuff.txt")
print GetAbsolutePathToFile(BasePath$, "subfolder/../../Stuff.txt")



Sync
waitkey


Function GetAbsolutePathToFile(BasePath$, Filename$)

BasePath$=replace$(BasePath$,"/","\")
Filename$=replace$(Filename$,"/","\")

Filename$=trim$(Filename$)
BasePath$=trimright$(BasePath$,"\")+"\"

local Device$=GetDevicename$(Filename$) 
if len(Device$)=0

// if the Filename is relative, we'll try and
// build an absolute path from BASE to this Filename
local Path$=GetFolderName$(Filename$) 

 FileName$=BasePath$+Filename$

// Check for "..\" in path
if instring(Path$,"..\")

Dim FolderChunks$(50)
local Count=splittoarray(filename$,"\",FolderChunks$())

local lp,s$
local Output=0


for lp =0 to count-1
s$=FolderChunks$(lp)
if s$<>".." 
if output>=0 then FolderChunks$(Output)=s$
Output++
else
output--
endif
next

// Put it back together
Filename$=""
for lp =0 to output-1
Filename$+=FolderChunks$(lp)
if lp<output-1 then Filename$+="\"
next

endif 

endif


EndFunction Filename$





Example output:

CODE:

D:\MyProjectFolder\Gfx\_examples
D:\MyProjectFolder\Gfx\_examples\Stuff.txt
D:\MyProjectFolder\Gfx\_examples\subfolder\Stuff.txt
D:\MyProjectFolder\Gfx\Stuff.txt
D:\MyProjectFolder\Stuff.txt
D:\MyProjectFolder\Gfx\Stuff.txt