Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by D.J.Peters »

Full working open source Tile engine for Win32/64, Linux32/64, Raspberry Pi and Beaglebone black.

Tilengine - The 2D retro graphics engine

Homepage: http://www.tilengine.org/

github: Tilengine

download: FBTilengine.zip

online help:
first steps
reference of the various modules

I created import libs for Win32/64
FBTilengine/lib/win32/libSDL2.dll.a
FBTilengine/lib/win32/libTilengine.dll.a

FBTilengine/lib/win64/libSDL2.dll.a
FBTilengine/lib/win64/libTilengine.dll.a

putted all runtime in the bin folder:
FBTilengine/bin/arm/libTilengine.so

FBTilengine/bin/lin32/libTilengine.so
FBTilengine/bin/lin64/libTilengine.so

FBTilengine/bin/win32/SDL2.dll
FBTilengine/bin/win32/Tilengine.dll

FBTilengine/bin/win64/SDL2.dll
FBTilengine/bin/win64/Tilengine.dll

I translated:
Tilengine.h Tutorial.c ColorCycle.c SuperMarioClone.c Mode7.c
FBTilengine/Tilengine.bi
FBTilengine/Tutorial.bas
FBTilengine/ColorCycle.bas
FBTilengine/SuperMarioClone.bas
FBTilengine/Mode7.bas

I compiled 32/64-bit Tutorial.bas ColorCycle.bas SuperMarioClone.bas Mode7.bas
FBTilengine/bin/win32/Tutorial.exe
FBTilengine/bin/win32/ColorCycle.exe
FBTilengine/bin/win32/SuperMarioClone.exe
FBTilengine/bin/win32/Mode7.exe

FBTilengine/bin/win64/Tutorial.exe
FBTilengine/bin/win64/ColorCycle.exe
FBTilengine/bin/win64/SuperMarioClone.exe
FBTilengine/bin/win64/Mode7.exe

Note: if you move your compiled binary to another folder be sure the assets can be loaded !

You have to set the relative path to the assets folder

For example to run binaries from bin/win32 or bin/win64 folder
I used TLN_SetLoadPath("../assets")

Joshy

file: Tutorial.bas

Code: Select all

#include "Tilengine.bi"

#ifndef NULL
#define NULL cptr(any ptr,0)
#endif

#define SRC_WIDTH	400
#define SRC_HEIGHT	240

dim as TLN_Tilemap tilemap
dim as long frame

' setup engine
TLN_Init(SRC_WIDTH, SRC_HEIGHT, 1,0,0)

' load layer
TLN_SetLoadPath ("../assets/sonic")
tilemap = TLN_LoadTilemap ("Sonic_md_fg1.tmx", NULL)
	
' setup the layer
TLN_SetLayer(0, NULL, tilemap)
TLN_SetBGColor(32,32,128)

' main loop
TLN_CreateWindow(NULL, 0)
while TLN_ProcessWindow()
  ' scroll the layer, one pixel per frame */
  TLN_SetLayerPosition (0, frame, 0)
  ' render to the window 
  TLN_DrawFrame(frame)
  frame+=1
wend

' release resources
TLN_DeleteTilemap(tilemap)
TLN_Deinit()
file: ColorCycle.bas

Code: Select all

#include once "Tilengine.bi"

#ifndef NULL
#define NULL cptr(any ptr,0)
#endif

dim as TLN_Bitmap bkg
dim as TLN_SequencePack sp
dim as TLN_Sequence sq
dim as TLN_Palette pal
dim as long frame

' engine init
TLN_Init(640,480,0,0,1)

' load resources
TLN_SetLoadPath("../assets/color")
bkg = TLN_LoadBitmap("beach.png")
pal = TLN_GetBitmapPalette(bkg)
sp  = TLN_LoadSequencePack("beach.sqx")
sq  = TLN_FindSequence(sp, "beach")

' setup
TLN_SetBGBitmap(bkg)
TLN_SetPaletteAnimation(0, pal, sq, true)

' main loop
TLN_CreateWindow(NULL, 0)
while TLN_ProcessWindow()
  TLN_DrawFrame(frame)
  frame+=1
wend  

TLN_DeleteBitmap(bkg)
TLN_DeleteSequencePack(sp)
TLN_Deinit()
Last edited by D.J.Peters on Oct 12, 2022 18:23, edited 6 times in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by D.J.Peters »

I think the most important part of a Sprite engine are scaling, rotating and collision detection.
I don't know why but TLN_SetSpriteRotation() isn't a part of the runtime libraries and is out commented in the *.h file also ?

Joshy
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by D.J.Peters »

translated SuperMarioClone.c

added in the zip file.
FBTilengine/SuperMarioClone.bas

compiled and tested under Win32/64
FBTilengine/bin/win32/SuperMarioClone.exe
FBTilengine/bin/win64/SuperMarioClone.exe

file: SuperMarioClone.bas

Code: Select all

#include once "Tilengine.bi"

#ifndef NULL
#define NULL cptr(any ptr,0)
#endif

#define SCR_WIDTH  400
#define SCR_HEIGHT 240

' layers
enum
  LAYER_FOREGROUND
  LAYER_BACKGROUND
  MAX_LAYER
end enum

dim as TLN_SequencePack sp
dim as TLN_Sequence seq_walking
dim as TLN_Spriteset spriteset
dim as TLN_Tilemap tilemaps(MAX_LAYER-1)
dim as long frame = 0
dim as long player_x = -16
dim as long player_y = 160

' basic setup
TLN_Init(SCR_WIDTH, SCR_HEIGHT, MAX_LAYER,1,3)
TLN_SetBGColor(0, 96, 184)

' load resources
TLN_SetLoadPath ("../assets/smw")
tilemaps(LAYER_FOREGROUND) = TLN_LoadTilemap ("smw_foreground.tmx", NULL)
tilemaps(LAYER_BACKGROUND) = TLN_LoadTilemap ("smw_background.tmx", NULL)
TLN_SetLayer(LAYER_FOREGROUND, NULL, tilemaps(LAYER_FOREGROUND))
TLN_SetLayer(LAYER_BACKGROUND, NULL, tilemaps(LAYER_BACKGROUND))
TLN_SetLayerPosition(LAYER_FOREGROUND, 0,48)
TLN_SetLayerPosition(LAYER_BACKGROUND, 0,80)

' setup sprite
spriteset = TLN_LoadSpriteset ("smw_sprite")
TLN_SetSpriteSet(0, spriteset)
TLN_SetSpritePicture(0, 0)
TLN_SetSpritePosition(0, player_x, player_y)

'  setup animations
sp = TLN_LoadSequencePack("sequences.sqx")
seq_walking = TLN_FindSequence (sp, "seq_walking")
TLN_SetSpriteAnimation(2, 0, seq_walking, 0)

' main loop
TLN_CreateWindow("overlay.bmp", CWF_VSYNC)
while TLN_ProcessWindow()
  player_x += 1
  if (player_x >= SCR_WIDTH) then player_x = -16
  TLN_SetSpritePosition(0, player_x, player_y)
  TLN_DrawFrame(frame)
  frame+=1
wend

' deinit
TLN_DeleteTilemap(tilemaps(LAYER_FOREGROUND))
TLN_DeleteTilemap(tilemaps(LAYER_BACKGROUND))
TLN_DeleteSequencePack(sp)
TLN_Deinit()
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by D.J.Peters »

I translated Mode7.c

added in the zip file.
FBTilengine/Mode7.bas

compiled and tested under Win32/64
FBTilengine/bin/win32/Mode7.exe
FBTilengine/bin/win64/Mode7.exe

file: Mode7.bas

Code: Select all

/'*****************************************************************************
*
* Tilengine sample
* 2015 Marc Palacios
* http://www.tilengine.org
*
* This example show a classic Mode 7 perspective projection plane like the 
* one seen in SNES games like Super Mario Kart. It uses a single transformed
* layer with a raster effect setting the scaling factor for each line
*
*****************************************************************************'/

#include once "Tilengine.bi"

#ifndef NULL
#define NULL cptr(any ptr,0)
#endif

#ifndef M_PI
#define M_PI 3.14159265f
#endif

dim shared as long sintable(360-1)
dim shared as long costable(360-1)

sub BuildSinTable
  for c as integer = 0 to 359
	  sintable(c) = sin(c*M_PI/180)*256
		costable(c) = cos(c*M_PI/180)*256
	next
end sub

function CalcSin(angle as long, factor as long) as long
	if (angle > 359) then angle = angle mod 360
	return (sintable(angle)*factor) shr 8
end function

function CalcCos(angle as long, factor as long) as long
	if (angle > 359) then angle = angle mod 360
  return (costable(angle)*factor) shr 8
end function


#define SCR_WIDTH  400
#define SCR_HEIGHT 240

' linear interploation
#define lerp(x, x0,x1, fx0,fx1) (fx0) + ((fx1) - (fx0))*((x) - (x0))/((x1) - (x0))


' layers
enum
	LAYER_FOREGROUND
	LAYER_BACKGROUND
	MAX_LAYER
end enum

enum
	MAP_HORIZON
	MAP_TRACK
	MAX_MAP
end enum

dim as long pos_foreground
dim as long pos_background(6-1)
dim as long inc_background(6-1)
dim shared as TLN_Tileset tilesets(MAX_MAP-1)
dim shared as TLN_Tilemap tilemaps(MAX_MAP-1)
dim as ulong frame
dim as ulong _time
dim shared as fix_t x,y,s,a

dim shared as TLN_Affine affine
dim as long angle

' raster callback (virtual HBLANK)
sub raster_callback cdecl(byval _line as long)
	if (_line = 24) then
	  TLN_SetLayer (LAYER_BACKGROUND, tilesets(MAP_TRACK), tilemaps(MAP_TRACK))
		TLN_SetLayerPosition(LAYER_BACKGROUND, fix2int(x), fix2int(y))
		TLN_DisableLayer(LAYER_FOREGROUND)
	end if

	if (_line >= 24) then
		dim as fix_t s0 = float2fix(0.2f)
		dim as fix_t s1 = float2fix(5.0f)
		dim as fix_t s = lerp (_line, 24,SCR_HEIGHT, s0,s1)
		dim as single scale = fix2float(s)

		affine.sx = scale
		affine.sy = scale		
		TLN_SetLayerAffineTransform (LAYER_BACKGROUND, @affine)
	end if
end sub



' setup engine
TLN_Init (SCR_WIDTH,SCR_HEIGHT, MAX_LAYER, 0, 5)
TLN_SetRasterCallback(@raster_callback)
TLN_SetBGColor(0,0,0)

' load resources
TLN_SetLoadPath ("../assets/smk")
tilesets(MAP_HORIZON) = TLN_LoadTileset("track1_bg.tsx")
tilemaps(MAP_HORIZON) = TLN_LoadTilemap("track1_bg.tmx", NULL)
tilesets(MAP_TRACK  ) = TLN_LoadTileset("track1.tsx")
tilemaps(MAP_TRACK  ) = TLN_LoadTilemap("track1.tmx", NULL)

' startup display
TLN_CreateWindow(NULL, 0)

x = int2fix(-136)
y = int2fix(336)
s = 0
a = float2fix(0.2f)
angle = 0

BuildSinTable()

affine.dx = SCR_WIDTH/2
affine.dy = SCR_HEIGHT
affine.sx = 1.0f
affine.sy = 1.0f
affine.angle = angle

' main loop
while TLN_ProcessWindow()
' timekeeper
  _time = frame

  TLN_SetLayer(LAYER_FOREGROUND, tilesets(MAP_HORIZON), tilemaps(MAP_HORIZON))
  TLN_SetLayer(LAYER_BACKGROUND, tilesets(MAP_HORIZON), tilemaps(MAP_HORIZON))
  TLN_SetLayerPosition(LAYER_FOREGROUND, lerp(angle*2, 0,360, 0,256), 24)
  TLN_SetLayerPosition(LAYER_BACKGROUND, lerp(angle, 0,360, 0,256), 0)
  TLN_SetLayerAffineTransform(LAYER_BACKGROUND, NULL)

  ' input  
  if TLN_GetInput(INPUT_LEFT) then
    angle-=2
  elseif TLN_GetInput(INPUT_RIGHT) then
    angle+=2
  end if  

  if TLN_GetInput(INPUT_UP) then
    s += a
    if (s > int2fix(2)) then s = int2fix(2)
  elseif (s >= a) then
    s -= a
  end if

  if (TLN_GetInput (INPUT_DOWN)) then
    s -= a
    if (s < -int2fix(2)) then  s = -int2fix(2)
  elseif (s <= -a) then
    s += a
  end if
  
  if (s<>0) then
    angle = angle mod 360
    if (angle < 0) then  angle += 360
    x += CalcSin(angle, s)
    y -= CalcCos(angle, s)
  end if

  affine.angle = angle
  ' render to window
  TLN_DrawFrame(_time)
  frame+=1
wend

' deinit
TLN_DeleteTileset(tilesets(MAP_HORIZON))
TLN_DeleteTilemap(tilemaps(MAP_HORIZON))
TLN_DeleteTileset(tilesets(MAP_TRACK  ))
TLN_DeleteTilemap(tilemaps(MAP_TRACK  ))
TLN_DeleteWindow()
TLN_Deinit()
c-sanchez
Posts: 145
Joined: Dec 06, 2012 0:38

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by c-sanchez »

Thanks for this D.J.Peters! although I'm happy with Windows only, is nice to have option for all :)
btw, I wonder if can you make a port/wrapper from these libraries:

Nuclear: https://github.com/vurtun/nuklear
Single-header, minimal state immediate mode graphical user interface toolkit written in ANSI C.
This looks simple and very nice.

Raylib: https://www.raylib.com
Simple and easy-to-use library to enjoy videogames programming.
Tilengine is nice indeed, but raylib is "full of features", some notable are:
NO external dependencies, Written in plain C code (C99), Hardware accelerated with OpenGL (1.1, 2.1, 3.3 or ES 2.0), Full 3d support, Audio loading and playing with streaming support (WAV, OGG, MP3, FLAC, XM, MOD)
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by lizard »

Thanks Joshy!

Previous version worked on Win 10, now we have it for Linux, too. Then Tiled is important:

https://doc.mapeditor.org/en/stable/

These both make a good environment for programming oldschool games with tilemaps, palettes and such.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by D.J.Peters »

lizard wrote:Then Tiled is important
Yes tiled is my first choice and the tmx map format it's easy to use and very well documented.

Joshy
Image
Last edited by D.J.Peters on Oct 12, 2022 18:24, edited 1 time in total.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by lizard »

Looks promising!
c-sanchez
Posts: 145
Joined: Dec 06, 2012 0:38

Re: Tilengine for Win32/64 Linux x86/x86_64 Raspberry PI and Beglebone black.

Post by c-sanchez »

Hi D.J.Peters, can you update to current tilengine version if not problem?
Thanks.
Post Reply