For those not familiar with BlitzMax the way it handles sprites (images) is rather different, in a nutshell you have to exclusively call a DrawImage function for each image you want to render to the screen each loop. AppGameKit does this very differently drawing any sprite you've created so it's thrown me for a loop, to help I created a set of function to make me feel more at home. The code I think is self explanatory. Assuming I'll be making exclusively 2D can anyone see an issues with going forward as-is? is manipulating the depth order of sprites each loop expensive for example? How do others handle sprite rendering is there a best practice?
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "test project" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
//=========================================================================================
// = SETUP ================================================================================
//=========================================================================================
// Init Sprite manager - used to control drawing order
global sprite_manager as TSpriteManager
// Load image #1
global img_1 as integer
img_1= ImageLoad("test1.png",0)
ImageSetMidHandle(img_1)
// Load image #2
global img_2 as integer
img_2= ImageLoad("test.png",0)
ImageSetScale(img_2,2,2)
// used to rotation images
global rot as float
//=========================================================================================
// = MAIN APP =============================================================================
//=========================================================================================
// Main Game Loop
do
SpriteManager_init()
UpdateGame()
RenderGame()
SpriteManager_sync()
Sync()
loop
// Main Game Render function
Function RenderGame()
ImageDraw(img_2,100,100)
ImageDraw(img_1,GetPointerX(),GetPointerY())
Endfunction
// Main Game Update function
Function UpdateGame()
if rot >= 360 then rot=0 else rot=rot+1
ImageSetRotate(img_2,rot)
Endfunction
//=========================================================================================
// = SPRITE MANAGER =======================================================================
//=========================================================================================
Type TSpriteManager
draw_called_sprite_ids as integer[] // Sprite id's called from ImageDraw()
draw_order as integer
EndType
Function SpriteManager_init()
// Set Sprite as NOT Visable - sprite that had been called via ImageDraw() on the last loop
for a=1 To sprite_manager.draw_called_sprite_ids.length
SetSpriteVisible(sprite_manager.draw_called_sprite_ids[a],0)
next
// Reset values
sprite_manager.draw_called_sprite_ids =[0]
sprite_manager.draw_order=10001
Endfunction
Function SpriteManager_sync()
// Set Sprite as Visable - sprite that have been called via ImageDraw() this loop
for a=1 To sprite_manager.draw_called_sprite_ids.length
SetSpriteVisible(sprite_manager.draw_called_sprite_ids[a],1)
next
Endfunction
//=========================================================================================
// = IMAGE FUNCTIONS ======================================================================
//=========================================================================================
Function ImageLoad(filename as String,linear_filter as integer)
local image as integer
image = LoadImage(filename)
SetImageMagFilter(image,linear_filter)
local sprite as integer
sprite = CreateSprite(image)
SetSpriteOffset(sprite,0,0)
SetSpriteVisible(sprite,0)
Endfunction sprite
Function ImageDraw(image_id as integer, x as float, y as float)
SetSpritePosition(image_id,x-GetSpriteOffsetX(image_id),y-GetSpriteOffsetY(image_id))
sprite_manager.draw_order = sprite_manager.draw_order-1
SetSpriteDepth(image_id,sprite_manager.draw_order)
sprite_manager.draw_called_sprite_ids.insert(image_id)
Endfunction
Function ImageSetMidHandle(image_id as integer)
SetSpriteOffset(image_id,GetSpriteWidth(image_id)/2,GetSpriteHeight(image_id)/2)
Endfunction
Function ImageSetRotate(image_id as integer,rot as float)
SetSpriteAngle(image_id,rot)
Endfunction
Function ImageSetScale(image_id as integer,x as float, y as float)
SetSpriteScale(image_id,x,y)
Endfunction