Mintoris Forum

Author Topic: GEN004 Star Sprite  (Read 1266 times)

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
GEN004 Star Sprite
« on: May 23, 2017, 10:13 PM »
This demo shows how to initialize a sprite using drawing commands instead of a bitmap file.

Code: [Select]
' Game Engine Tutorial 4
' First Sprite
' Written By: Charles Charbonneau
' 07-20-16

include "Game Engine Constants.inc"

' Set the orientation and turn on the Game Engine
orientation 2
GameEngine On

init()

Global endTime = 0
Global startTime = time()

' Give the sprite a 45 degree per second spin
GenSpinSprite sprite, 45

' GenFadeSprite sprite, 100, 100, 100, 0, 10000
SetOnGenSpriteModifierSub sprite, onSpriteMod()

' Override the home and back buttons so the
' program will exit without a message box
' when the back button is pressed
SetOnBackButtonSub onBack()
SetOnPauseSub onPause()

' Loop until the spinDone() sub is triggered
do until endTime > 0
  wait 1000
loop

GameEngine off
print endTime - startTime


Sub onSpriteMod(sprite, type)

    endTime = time()
    GenSetSpriteColor sprite, rnd(100),rnd(100),rnd(100),100
    
End Sub


Sub init()

  ' Create a tile atlas
  atlas = GenTileAtlas(512, 512)

  ' Define a five pointed star
  starX() = [Cos(1/10*Pi), Cos(9/10*Pi), Cos(17/10*Pi), Cos(5/10*Pi), Cos(13/10*Pi), Cos(1/10*Pi)] * 50
  starY() = [Sin(1/10*Pi), Sin(9/10*Pi), Sin(17/10*Pi), Sin(5/10*Pi), Sin(13/10*Pi), Sin(1/10*Pi)] * 50

  ' Create the star bitmap
  bmStar = CreateBitmap(100, 100)

  ' Set the drawing surface to the star bitmap
  SetDrawingSurface bmStar

  ' Draw the star on the bitmap
  Color 100,100,100,100
  Poly starX(), starY(), 50, 50, true
  SetDrawingSurface off

  ' Create the tileset from the star bitmap
  Global tileset = GenTileSet(bmStar)

  ' Put the font into the atlas
  GenMapToAtlas atlas, tileset, 0, 0

  ' Load the atlas into the GPU
  GenLoadAtlas atlas

  Global sprite = GenSprite(tileset)

  GenAddSprite sprite, 0

End Sub


' The Back Button event sub override
' Just exit
Sub onBack()
  exit
End Sub

' The Home Button event sub override
' Home Button causes a pause
' Just exit
Sub onPause()
  exit
End Sub


This program automatically ends after the sprite does one revolution. The GenSpinSprite command triggers an onSpriteMod() sub call. Once the endTime is set, the program ends.

Code: [Select]
' Give the sprite a 45 degree per second spin
GenSpinSprite sprite, 45

Code: [Select]
Sub onSpriteMod(sprite, type)

    endTime = time()
    GenSetSpriteColor sprite, rnd(100),rnd(100),rnd(100),100
    
End Sub

« Last Edit: May 23, 2017, 10:24 PM by Chuck »