LoadFont "arial",1,16,0,8 // ----------------------------------------------------------- // Size the Map blocks we'll use. // ----------------------------------------------------------- Constant TileWidth =32 Constant TileHeight =32 // ----------------------------------------------------------- // Make the Backdrop image. This buffer is also used for the 'FX surface' // ----------------------------------------------------------- BackDropFX =MakeBackdropImage() // ----------------------------------------------------------- // Make a blob image to represent the sprites in the scene // ----------------------------------------------------------- Blob= MakeBlobImage(64,64) // ----------------------------------------------------------- // Define our RECT structure to hold the bounds of each sprite // ----------------------------------------------------------- Type tRect x1,y1 x2,y2 EndType // ----------------------------------------------------------- // Define a type to simulate some characters in the scene. // ----------------------------------------------------------- Type tAlien Sprite speed# direction# Rect As tRect EndType // ------------------------------------------------------ // Dim a list called Character to hold the objects // ------------------------------------------------------ Dim Character As TAlien List // ------------------------------------------------------ // Set the clip zone of the object // ------------------------------------------------------ clipSize=64 ViewportX1=-clipSize ViewportX2=GetScreenWidth()+clipSize ViewportY1=-clipSize ViewportY2=GetScreenHeight()+clipSize // ------------------------------------------------------ // MAKE MAP / LEVEL / BLOCKS from our backdrop picture // ------------------------------------------------------ Map,OriginalLevel,RefreshLevel=Make_Map_FRom_Image(BackDropFX) ;*=-----------------------------------------------------------------------------=* ; >> Main Loop << ;*=-----------------------------------------------------------------------------=* Screen=NewImage(GetScreenWidth(),GetScreenHeight(),2) Do // Add new charcter to scene if the space key is being pressed If SpaceKey() Or LastTime<Timer() LastTime=Timer()+50 Add_Random_Character(Blob) EndIf // Process and render to the objects in the character list RenderToImage Screen // Draw the refresh level to our FX image screen DrawMap Map,RefreshLevel,0,0 // reset the refresh level ClearLevel Map,RefreshLevel,0 // Process our list of characters For Each Character() ThisSprite=Character.Sprite Speed# =Character.speed# Angle# =Character.direction# MoveSprite ThisSprite,CosRadius(angle#,speed#),SinRadius(angle#,speed#) ; Check if the character is inside the screen ? If RectHitSprite(ViewportX1,ViewportY1,ViewportX2,ViewportY2,ThisSprite)=false DeleteSprite Sprite Character=null Continue EndIf // Get the rect around this sprite If GetSpriteRect(ThisSprite,Character.Rect) // Convert Pixels coordinates to Map Tile positions x1=Character.Rect.x1/TileWidth y1=Character.Rect.y1/TileHeight x2=Character.Rect.x2/TileWidth y2=Character.Rect.y2/TileHeight CopyLevel Map,OriginalLevel,x1,y1,x2+1,y2+1,Map,RefreshLevel,TilePad+x1,TilePad+y1 EndIf Next // Draw the Sprite List to the FX buffer image DrawAllSprites // Direct rendering to the PB display RenderToScreen // Draw the current state of the Backdrop to the SCREEN DrawImage Screen,0,0,false // Draw the info to the screen for the viewer Text 0, 0,"Refresh %:"+CalcRefreshPercentage(Map,RefreshLevel) Text 0,20,"Fps:"+Str$(FPS()) Sync Loop ;*=-----------------------------------------------------------------------------=* ; >> Add Random Character << ;*=-----------------------------------------------------------------------------=* ; ; This function adds a new character to our liost and fills it with random ; position and speed data. ;*=-----------------------------------------------------------------------------=* Psub Add_Random_Character(ThisImage) Character = New tAlien x =Rnd(GetScreenWidth()) y =Rnd(GetScreenHeight()) Character.Sprite =NewSprite(X,y,ThisIMage) Character.Speed =RndRange(1,5) Character.direction# =Rnd(360) EndPsub ;*=-----------------------------------------------------------------------------=* ; >> Calc ReFresh Percentage << ;*=-----------------------------------------------------------------------------=* ; ; This function converts an image into the array of tiles representing this ; image. Each cell in the array contains the image handle of that 'sector' ; of the original image. This is used to refresh the image during refresh. ; ;*=-----------------------------------------------------------------------------=* Psub CalcRefreshPercentage(Map,Level) Count=0 Width=GetLevelWidth(Map,Level) Height=GetLevelHeight(Map,Level) Total = (Width+1)*(Height+1) For ylp=0 To Height For xlp=0 To Width Tile=PeekLevelTile(Map,Level,xlp,ylp) Count+=Tile<>0 Next Next p#=Float(Count)/Total s$=Str$(p#*100 ) EndPsub s$ ;*=-----------------------------------------------------------------------------=* ; >> Make Blob image << ;*=-----------------------------------------------------------------------------=* ; ; This function makes a Blob (circle/ellipse) image with alpha channel, which ; is used to represent a hand drawn sprite in your game. ; ;*=-----------------------------------------------------------------------------=* Function MakeBlobImage(width,height) oldsurface=GetSurface() Thisimage=GetFreeImage() CreateFXImageEx ThisImage,Width,Height,32 RenderToImage Thisimage radiusX=Width/2 radiusY=Height/2 LockBuffer EllipseC (width/2),(height/2),radiusX,radiusY,true,RGB(255,255,255) nullpixel=Point(0,0) For ylp=0 To height-1 A=Float(ylp)/height*255 A=LSL32(a,24) For xlp=0 To width-1 ThisPixel=FastPoint(xlp,ylp) And $00ffffff If ThisPixel FastDot xlp,ylp,ThisPixel Or A EndIf Next Next UnLockBuffer PrepareAFXImage ThisImage RenderToImage oldsurface EndFunction ThisImage ;*=-----------------------------------------------------------------------------=* ; >> Make Backdrop image << ;*=-----------------------------------------------------------------------------=* ; ; This function create a backdrop image. It's a bit of mess, but it'll do for ; the sake of the demo. In a real game you'd obviously load your own backdrop. ;*=-----------------------------------------------------------------------------=* Function MakeBackdropImage() // Make the backdrop sw=GetScreenWidth() sh=GetScreenHeight() backdrop=NewImage(sw,sh,2) RenderToImage backdrop c1=ARGB(255,0,0,100) c2=ARGB(255,70,50,0) c3=ARGB(255,0,200,0) c4=ARGB(255,0,0,0) ShadeBox 0,0,sw,sh,c1,c2,c3,c4 LoadFont "Arial",2,64,0 SetFont 2 For lp=0 To 200 Ink RndRGB()| $ff000000 CenterText Rnd(sw),Rnd(sh),"<>" Next Ink ARGB(255,55,55,55) x=sw/2 y=sh*0.4 CenterText x-2,y-2,"Game Backdrop Image" Ink ARGB(255,255,255,255) CenterText x,y,"Game Backdrop Image" SetFont 1 DeleteFont 2 RenderToScreen EndFunction BackDrop Function Make_Map_FRom_Image(ThisIMage) // ------------------------------------------------------ // MAKE MAP / LEVEL / BLOCKS from our backdrop picture // ------------------------------------------------------ Map=NewMap(10) IMageWidth =GetImageWidth(ThisIMage) IMageHeight =GetImageHeight(ThisIMage) TilesWide =IMageWidth/TileWidth TilesHigh =IMageHeight/TileHeight If (TilesWide*TileWidth)>ImageWidth Then TilesWide++ If (TilesHigh*TileHeight)>ImageHieght Then TilesHigh++ BlockCount=(TilesWide*TilesHigh)+1 CreateMapGFX Map,TileWidth,TileHeight,BlockCount,RGB(0,0,0),2 OriginalLevel=NewLevel(Map,TilesWide-1,TilesHigh-1) RenderToImage ThisImage // ---------------------------------- // Build map representation of this image // ---------------------------------- Index=1 For ylp=0 To TilesHigh-1 For xlp=0 To TilesWide-1 Xpos=xlp*TileWidth Ypos=ylp*TileHeight GetMapBlk Map,Index,Xpos,Ypos PokeLevelTile Map,OriginalLevel, Xlp,ylp,Index Index++ Next Next RefreshLevel =NewLevel(Map,TilesWide-1,TilesHigh-1) PasteLevel map,OriginalLevel,Map,RefreshLevel,0,0,0 LevelTransparent Map,RefreshLevel,0 EndFunction Map,OriginalLevel,RefreshLevel |