Strict
#HTML5_APP_FILENAME="index.html" 'Set the default name for the html outfile.
#HTML5_APP_TITLE="Cerberus Game - Example textured cube" 'Set the title inside the browser tab.
#GLFW_WINDOW_TITLE="Cerberus Game - Example textured cube"
Import minib3d
Function Main:Int()
New Game
Return 0
End
Class Game Extends App
Field cam:TCamera
Field light:TLight
Field cube:TMesh
Field txt:TText
' used by fps code
Field old_ms:Int
Field renders:Int
Field fps:Int
Field a:Float=0, dir:Int=0, oldTouchX:Int, oldTouchY:Int, touchBegin:Int, lr#, ud#
'Field anim_time:Int
Field redbrush:TBrush
Field init_gl:Bool = False
'--------------------------------------------------------
Method OnCreate:Int()
SetRender()
SetSwapInterval (0)
SetUpdateRate (0)
Return 0
End
'--------------------------------------------------------
Method Init:Int()
If init_gl Then Return 0
If Not TPixmap.PreLoadPixmap(["cx.png","mojo_font.png"])
Return 0
Endif
init_gl = True
cam = CreateCamera()
cam.CameraClsColor(0,0,80)
cam.PositionEntity 0,4,-7
light=CreateLight(1)
light.PositionEntity 0,3,-3
cube=CreateCube()
'cube.ScaleEntity(0.5,0.5,0.5)
cube.name = "cube"
PositionEntity cube,0,2,2
redbrush = New TBrush
redbrush.BrushColor(255,255,255)
redbrush.LoadTexture("cx.png") '<--------
cube.PaintEntity( redbrush)
txt = TText.CreateText2D()
'txt.NoSmooth()
old_ms=Millisecs()
'Wireframe(True)
Print "main: init done"
Return 0
End
'--------------------------------------------------------
Method OnUpdate:Int()
If KeyHit(KEY_CLOSE) Or KeyHit(KEY_ESCAPE) Then Error ""
If Not init_gl Then Init(); Return 0
' control camera
Local cr:Float = KeyDown(KEY_LEFT)-KeyDown(KEY_RIGHT)
Local cu:Float = KeyDown(KEY_DOWN)-KeyDown(KEY_UP)
Local camin:Float = KeyDown(KEY_W)-KeyDown(KEY_S)
Local camup:Float = KeyDown(KEY_D)-KeyDown(KEY_A)
Local turnzx:Float = KeyDown(KEY_Z)-KeyDown(KEY_X)
If TouchDown(0) And Not TouchDown(1)
If Not touchBegin
oldTouchX = TouchX()
oldTouchY = TouchY()
touchBegin = 1
Endif
lr = (TouchX() - oldTouchX) * 0.5
ud = (-TouchY() + oldTouchY) *0.5
oldTouchX = TouchX()
oldTouchY = TouchY()
Elseif TouchDown(1)
If Not touchBegin
oldTouchX = TouchX()
oldTouchY = TouchY()
touchBegin = 1
Endif
camup = (-TouchX() + oldTouchX) * 0.1
camin = (-TouchY() + oldTouchY) *0.1
oldTouchX = TouchX()
oldTouchY = TouchY()
Else
touchBegin = 0
Endif
TurnEntity cube,turnzx*2,0,0
MoveEntity cube,(lr)*0.2,0,ud*0.1'camup,0,camin
cam.MoveEntity camup,0,camin
cam.TurnEntity cu,cr,0
txt.SetText(fps+" fps")
txt.HideEntity()
txt.Draw(0,0)
' calculate fps
If Millisecs()-old_ms >= 1000
old_ms=Millisecs()
fps=renders
renders=0
Endif
UpdateWorld()
Return 0
End
'--------------------------------------------------------
Method OnRender:Int()
RenderWorld()
renders=renders+1
Return 0
End
End