#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 = 0
  LAYER_BACKGROUND
  MAX_LAYER
end enum


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
var spriteset = TLN_LoadSpriteset("smw_sprite")
TLN_SetSpriteSet(0, spriteset)
TLN_SetSpritePicture(0, 0)
TLN_SetSpritePosition(0, player_x, player_y)

'  setup animations
var sp = TLN_LoadSequencePack("sequences.sqx")
var 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()
