A very simple tile engine

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
angros47
Posts: 1633
Joined: Jun 21, 2005 19:04

A very simple tile engine

Postby angros47 » Jun 25, 2010 22:01

I tried to build a tile engine: it supports smooth scrolling and animate tiles.

Tiles are simple FB images (so, they can be used to build very complex scenarios): the TileMap matrix is a bi-dimensional array of fb image pointers, and it's used to store the map.

Every tile must have the same size. You can modify a tile while the program is running, and this allow animated tiles (useful for water effect, for example)

Since this program use direct access to screen buffer, it should be fast enough.

Code: Select all

#include "fbgfx.bi"

Dim shared TileMap(100,100) as fb.Image ptr


Sub DrawTiles(ox as integer, oy as integer)
  dim as integer w,h,w2,h2
  ScreenInfo w,h
  w+=ox-1:h+=oy-1
  dim As integer Ptr tile
  dim as integer ptr target=screenptr

  w2=TileMap(0,0)->width
  h2=TileMap(0,0)->height

  screenlock
  dim tline as integer
  for y as integer=oy to h
    tline=(y mod h2)*(TileMap(0,0)->pitch Shr 2)

    for x as integer=ox to w
      target+=1
      tile=cptr(integer Ptr,TileMap (x\w2,y\h2))+8
      tile+=tline+x mod w2

      *target=*tile
     
    next
  next
  screenunlock
end sub





Screenres 800,600,32

dim As fb.Image Ptr Img,img2

  img=ImageCreate(32,32)
line img,(0,0)-(100,100),rgb(255,0,0),bf
Draw string img,(1,1),"abc"

circle img,(30,30),28

  img2=ImageCreate(32,32)
line img2,(0,0)-(100,100),rgb(0,0,255),bf
Draw string img2,(1,1),"xxx"



dim as integer a,b

for a=0 to 100
for b=0 to 100

tilemap (a,b)=img

next:next
tilemap (1,1)=img2
tilemap (2,1)=img2
tilemap (20,5)=img2

for a=0 to 1000
DrawTiles a,100+100*sin(a/57)
if a mod 10>5 then
line img2,(0,0)-(100,100),rgb(0,0,255),bf
else
Draw string img2,(1,1),"xxx"
end if
screensync
next

sleep
end
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Postby rolliebollocks » Jun 25, 2010 22:43

For being as small as it is, it's really impressive. That's gotta be the smallest tile engine I've ever seen.

This one is cool too:

http://www.freebasic.net/forum/viewtopic.php?t=10867&highlight=map+walking

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 5 guests