Strict
Import mojo2
#GLFW_WINDOW_WIDTH=1536
#GLFW_WINDOW_HEIGHT=1024
Global BubbleMax:int = 65536
Global A_Bubbles:TBubble[]
Global L_Bubbles:= New List < TBubble>
Global BubblesCount:int
Global BubbleSprite:int
Global ScrW:int = 1536
Global ScrH:int = 1024
Function Main:Int()
New MyApp()
Return 0
End
Function UpdateBubble:Void(Abub:TBubble)
Local LhalfSize:= int(TBubble.imagesize * Abub.size / 2) + 2
If Abub.pos.x > ScrW - LhalfSize or Abub.pos.x < LhalfSize
Abub.speed.x = 0 - Abub.speed.x
Abub.life = Abub.life - 1
EndIf
If Abub.pos.y > ScrH - LhalfSize or Abub.pos.y < 1 + LhalfSize
Abub.speed.y = 0 - Abub.speed.y
Abub.life = Abub.life - 1
EndIf
Abub.pos.x = Abub.pos.x + Abub.speed.x
Abub.pos.y = Abub.pos.y + Abub.speed.y
Abub.iridescent = Abub.iridescent + 0.03
If Abub.iridescent > 0.9 Then Abub.iridescent = 1 - Abub.iridescent' ???
Abub.animData.x = Abub.animData.x + Abub.animSpeed
Abub.animData.y = Abub.animData.y + Abub.animSpeed
End
Class MyApp Extends App
Field BubbleImage:Image
Field BubbleSprite:Image
Field myCanvas:Canvas
Field effect:ShaderEffect
Field level:Float = 0.5
Field ticks:float
Field frames:int
Field fps:int
Field time:int
Field ShaderEnable:bool
Method OnCreate:Int()
BubbleImage = Image.Load("bubble.png")
TBubble.imagesize = BubbleImage.Width()
BubbleSprite = New Image(BubbleImage.Width(), BubbleImage.Height())
effect = New ShaderEffect()
effect.InitTexture(BubbleImage)
myCanvas = New Canvas()
Return 0
End
Method OnUpdate:Int()
Local bub:TBubble
If MouseDown(MOUSE_LEFT)
For Local i:= 0 To 9
bub = New TBubble
bub.pos.x = MouseX()
bub.pos.y = MouseY()
L_Bubbles.AddLast(bub)
Next
EndIf
If KeyHit(KEY_SPACE)
ShaderEnable = not (ShaderEnable)
EndIf
Return 0
End
Method OnRender:Int()
Local delta1:int
Local le:int
Local Ss:string
frames += 1
le = Millisecs() -time
If le >= 1000
fps = frames
frames = 0
time += le
EndIf
myCanvas.Clear(0.2, 0.2, 0.2)
le = Millisecs()
effect.Render(BubbleSprite)
For Local Bubble:= EachIn L_Bubbles
If ShaderEnable
effect.SetLevel(Bubble.iridescent, Bubble.cr, Bubble.cg, Bubble.cb)
myCanvas.DrawImage(BubbleSprite, Bubble.pos.x, Bubble.pos.y,
Bubble.animSpeed * Sin(Bubble.animData.x),
Bubble.size - (Sin(Bubble.animData.x) / 20),
Bubble.size - (Cos(Bubble.animData.y) / 20))
' myCanvas.Flush()
Ss = "enable."
Else
myCanvas.DrawImage(BubbleImage, Bubble.pos.x, Bubble.pos.y,
Bubble.animSpeed * Sin(Bubble.animData.x),
Bubble.size - (Sin(Bubble.animData.x) / 20),
Bubble.size - (Cos(Bubble.animData.y) / 20))
Ss = "disable."
EndIf
UpdateBubble(Bubble)
If Bubble.life < 1
L_Bubbles.Remove(Bubble)
EndIf
Next
delta1 = Millisecs() -le
myCanvas.DrawText("Clic LMB to generate bubbles, press SPACE to enable/disable shader ", 10, 10)
myCanvas.DrawText("Bubbles: " + String(L_Bubbles.Count()), 10, 26)
myCanvas.DrawText("Shader: " + Ss + " FPS: " + String(fps), 10, 42)
myCanvas.DrawText("Draw ms: " + String(delta1), 10, 58)
myCanvas.Flush()
Return 0
End
End'MyApp
Class float2d
Field x:float
Field y:float
End'float2d
Class TBubble
Global imagesize:int
Field pos:float2d
Field speed:float2d
Field animData:float2d
Field animSpeed:float
Field life:int
Field iridescent:float
Field size:float
Field cr:float
Field cg:float
Field cb:float
Method New()
pos = New float2d
speed = New float2d
Local angle:= Rnd(1, 360)
Local spd:= Rnd(0.5, 4)
speed.x = Cos(angle) * spd
speed.y = Sin(angle) * spd
animData = New float2d
animData.x = Rnd(-2, 2)
animData.y = Rnd(-2, 2)
animSpeed = Rnd(2, 6)
size = Rnd(0.2, 1.0)
life = int(Rnd(10, 50))
iridescent = Rnd(0.0, 1.0)
Select int(Rnd(1, 3.4))
Case 1
cr = 0.0
cg = 0.1
cb = 0.5
Case 2
cr = 0.0
cg = 0.5
cb = 0.0
Case 3
cr = 0.3
cg = 0.4
cb = 0.0
End
End
End'TBubble
Class BWShader Extends Shader
Private
Global _instance:BWShader
Method New()
Build(LoadString("shader.glsl"))
End
Method OnInitMaterial:Void( myMaterial:Material )
myMaterial.SetTexture("ColorTexture", Texture.White())
myMaterial.SetScalar("offset", 0)
myMaterial.SetScalar("cr", 0.0)
myMaterial.SetScalar("cg", 0.0)
myMaterial.SetScalar("cb", 0.0)
End
Function Instance:BWShader()
If Not _instance _instance = New BWShader()
Return _instance
End
End'BWShader
'========================================================
Class ShaderEffect
Private
Global _canvas:Canvas
Field _material:Material
Method New()
' ensure there is a single instance of the canvas
If Not _canvas _canvas = New Canvas()
_material = New Material(BWShader.Instance())
End
Method SetLevel:Void(level:Float, c1:float, c2:float, c3:float)
' set the level of effect to the supplied value
_material.SetScalar("offset", level)
_material.SetScalar("cr", c1)
_material.SetScalar("cg", c2)
_material.SetScalar("cb", c3)
End
Method InitTexture:Void(source:Image)
_material.SetTexture("ColorTexture", source.Material.ColorTexture)
End
Method Render:Void(target:Image)
_canvas.SetRenderTarget(target)
_canvas.SetViewport(0, 0, target.Width(), target.Height())
_canvas.SetProjection2d(0, target.Width(), 0, target.Height())
_canvas.Clear(0.0, 0.0, 0.0, 0.0)
_canvas.DrawRect(0, 0, target.Width(), target.Height(), _material)
_canvas.Flush()
End
End'ShaderEffect