Snippet Firepaint Mojo2

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,134
Converted demo Firepaint into Mojo2 (complete 10x increase of performance in HTML5)

Cerberus:
' Firepaint redux!
'
' Really a multitouch test...

Import mojo2

Function Main()
    New Firepaint
End

Global BigList:=New List<Spark>

Class Firepaint Extends App

    Field canvas:Canvas
    Field sparkImage:Image
    Field sparks:=New List<Spark>
    Field prim
 
    Method OnCreate()
        For Local i = 0 Until 65536*16
'            BigList.AddLast New Spark
        Next
        sparkImage=Image.Load("bluspark.png",0.5,0.5,0)
        canvas = New Canvas()
        SetSwapInterval 1 ; SetUpdateRate 0
    End
 
    Method OnUpdate()
        Local out:=New List<Spark>
        For Local spark:=Eachin sparks
            spark.a=Max(spark.a-.01,0.0)
            If spark.a <= 0 Continue
            spark.yv += 0.05
            spark.x += spark.xv
            spark.y += spark.yv
            out.AddLast spark
        Next

        sparks=out
     
        For Local i = 0 Until 32
            If TouchDown(i)
                For Local j = 1 To 10
                    sparks.AddLast New Spark( TouchX(i),TouchY(i) )
                Next
            Endif
        Next
     
        If KeyHit(KEY_SPACE) Then prim = (prim + 1) Mod 4
     
    End
 
    Method OnRender()
        Local w = DeviceWidth,h = DeviceHeight
        canvas.SetBlendMode BlendMode.Additive ' LightenBlend in Mojo1
        canvas.Clear
        canvas.DrawText "This way UP!",0,0
        canvas.SetColor 1,0,0
        canvas.DrawRect 0,0,w,32
        canvas.SetColor 0,1,0
        canvas.DrawRect w-32,0,32,h
        canvas.SetColor 0,0,1
        canvas.DrawRect 0,h-32,w,32
        canvas.SetColor 0.5,0.5,0.5
        canvas.DrawRect 0,0,32,h
     
        For Local spark:=Eachin sparks
            canvas.SetAlpha spark.a
            Select prim
            Case 0
                canvas.DrawImage sparkImage,spark.x,spark.y
            Case 1
                canvas.DrawRect spark.x-15,spark.y-15,30,30
            Case 2
                canvas.DrawOval spark.x-15,spark.y-15,30,30
            Case 3
                canvas.DrawLine spark.x-15,spark.y-15,spark.x+15,spark.y+15
                canvas.DrawLine spark.x+15,spark.y-15,spark.x-15,spark.y+15
            End
        Next
        canvas.Flush
    End

End

Class Spark

    Field x#,y#,xv#,yv#,a#
 
    Method New( x#,y# )
        Self.x = x
        Self.y = y
        Local an# = Rnd(360),v# = Rnd(3,4)
        Self.xv = Cos(an) * v
        Self.yv = Sin(an) * v
        Self.a = 1
    End
 
End
 

Attachments

  • firepaint.zip
    5.9 KB · Views: 7
  • bluspark.png
    bluspark.png
    4.6 KB · Views: 23
Last edited:

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,307
yes, it is great to see how much the performance improves with mojo2.
 
Top Bottom