예제 #1
0
func StartEngine() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	runtime.LockOSThread()
	fmt.Println("Enginge started!")
	var err error
	if err = glfw.Init(); err != nil {
		panic(err)
	}
	fmt.Println("GLFW Initialized!")

	glfw.OpenWindowHint(glfw.Accelerated, 1)

	if err = glfw.OpenWindow(Width, Height, 8, 8, 8, 8, 8, 8, glfw.Windowed); err != nil {
		panic(err)
	}

	glfw.SetSwapInterval(1) //0 to disable vsync, 1 to enable it
	glfw.SetWindowTitle(windowTitle)
	glfw.SetWindowSizeCallback(onResize)
	glfw.SetKeyCallback(input.OnKey)
	glfw.SetCharCallback(input.OnChar)
	glfw.SetMouseButtonCallback(input.ButtonPress)
	glfw.SetMouseWheel(0)
	glfw.SetMouseWheelCallback(input.MouseWheelCallback)

	input.MouseWheelPosition = glfw.MouseWheel
	input.MousePosition = glfw.MousePos

	if err = initGL(); err != nil {
		panic(err)
	}
	fmt.Println("Opengl Initialized!")

	TextureMaterial = NewBasicMaterial(spriteVertexShader, spriteFragmentShader)
	err = TextureMaterial.Load()
	if err != nil {
		fmt.Println(err)
	}

	SDFMaterial = NewBasicMaterial(sdfVertexShader, sdfFragmentShader)
	err = SDFMaterial.Load()
	if err != nil {
		fmt.Println(err)
	}

	internalMaterial = NewBasicMaterial(spriteVertexShader, spriteFragmentShader)
	err = internalMaterial.Load()
	if err != nil {
		fmt.Println(err)
	}

	initDefaultPlane()
	glfw.SwapBuffers()

	gameTime = time.Time{}
	lastTime = time.Now()
	dl = glfw.Time()
}
예제 #2
0
func Run() {
	timeNow := time.Now()
	gameTime = gameTime.Add(timeNow.Sub(lastTime))
	//deltaTime = float64(timeNow.Sub(lastTime).Nanoseconds()) / float64(time.Second)
	lastTime = timeNow
	before := timeNow

	dn := glfw.Time()
	dd = dn - dl
	dl = dn
	deltaTime = dd

	timer := NewTimer()
	timer.Start()

	CurrentCamera().Clear()

	var destroyDelta,
		startDelta,
		fixedUpdateDelta,
		physicsDelta,
		updateDelta,
		lateUpdateDelta,
		drawDelta,
		coroutinesDelta,
		stepDelta,
		behaviorDelta,
		startPhysicsDelta,
		endPhysicsDelta time.Duration

	if mainScene != nil {
		d := deltaTime
		if d > maxPhysicsTime {
			d = maxPhysicsTime
		}
		fixedTime += d
		sd := mainScene.SceneBase()

		arr := &sd.gameObjects

		timer.StartCustom("Destory routines")
		iter(arr, destoyGameObject)
		destroyDelta = timer.StopCustom("Destory routines")

		//Better to not do it every frame.
		mainScene.SceneBase().cleanNil()

		timer.StartCustom("Start routines")
		iter(arr, startGameObject)
		startDelta = timer.StopCustom("Start routines")

		//

		timer.StartCustom("Physics time")
		if EnablePhysics {
			timer.StartCustom("Physics step time")

			for fixedTime >= stepTime {
				timer.StartCustom("FixedUpdate routines")
				iter(arr, fixedUdpateGameObject)
				fixedUpdateDelta = timer.StopCustom("FixedUpdate routines")

				timer.StartCustom("PreStep Physics Delta")
				iter(arr, preStepGameObject)
				startPhysicsDelta = timer.StopCustom("PreStep Physics Delta")

				Space.Step(vect.Float(stepTime))
				fixedTime -= stepTime

				_ = timer.StopCustom("Physics step time")

				timer.StartCustom("PostStep Physics Delta")
				iter(arr, postStepGameObject)
				endPhysicsDelta = timer.StopCustom("PostStep Physics Delta")
			}
			if fixedTime > 0 && fixedTime < stepTime {
				iter(arr, interpolateGameObject)
			}
		}
		physicsDelta = timer.StopCustom("Physics time")

		timer.StartCustom("Update routines")
		internalFPSObject.Update()
		iter(arr, udpateGameObject)
		updateDelta = timer.StopCustom("Update routines")

		timer.StartCustom("LateUpdate routines")
		iter(arr, lateudpateGameObject)
		lateUpdateDelta = timer.StopCustom("LateUpdate routines")

		timer.StartCustom("Draw routines")
		depthMap.Iter(drawGameObject)
		drawDelta = timer.StopCustom("Draw routines")

		timer.StartCustom("coroutines")
		cr.Run()
		coroutinesDelta = timer.StopCustom("coroutines")

		timer.StartCustom("BehaviorTree")
		bt.Run(BehaviorTicks)
		behaviorDelta = timer.StopCustom("BehaviorTree")

		input.UpdateInput()

		stepDelta = timer.Stop()
	}
	timer.StartCustom("SwapBuffers")
	glfw.SwapBuffers()
	swapBuffersDelta := timer.StopCustom("SwapBuffers")

	now := time.Now()
	deltaDur := now.Sub(before)

	if Debug {
		fmt.Println()
		fmt.Println("##################")
		if InternalFPS < 20 {
			fmt.Println("FPS is lower than 20. FPS:", InternalFPS)
		} else if InternalFPS < 30 {
			fmt.Println("FPS is lower than 30. FPS:", InternalFPS)
		} else if InternalFPS < 40 {
			fmt.Println("FPS is lower than 40. FPS:", InternalFPS)
		}
		if stepDelta > 17*time.Millisecond {
			fmt.Println("StepDelta time is lower than normal")
		}
		fmt.Println("Debugging Times:")
		if (deltaDur.Nanoseconds() / int64(time.Millisecond)) != 0 {
			fmt.Println("Expected FPS", 1000/(deltaDur.Nanoseconds()/int64(time.Millisecond)))
		}
		fmt.Println("Step time", stepDelta)
		fmt.Println("Destroy time", destroyDelta)
		fmt.Println("Start time", startDelta)
		fmt.Println("FixedUpdate time", fixedUpdateDelta)
		fmt.Println("Update time", updateDelta)
		fmt.Println("LateUpdate time", lateUpdateDelta)
		fmt.Println("Draw time", drawDelta)
		fmt.Println("Delta time", deltaDur, deltaTime)
		fmt.Println("SwapBuffers time", swapBuffersDelta)
		fmt.Println("Coroutines time", coroutinesDelta)
		fmt.Println("BehaviorTree time", behaviorDelta)
		fmt.Println("------------------")
		fmt.Println("Physics time:", physicsDelta)
		fmt.Println("PreStepDelta time", startPhysicsDelta)
		fmt.Println("PostStepDelta time", endPhysicsDelta)
		fmt.Println("StepTime time", Space.StepTime)
		fmt.Println("ApplyImpulse time", Space.ApplyImpulsesTime)
		fmt.Println("ReindexQueryTime time", Space.ReindexQueryTime)
		fmt.Println("Arbiters", len(Space.Arbiters))
		fmt.Println("##################")
		fmt.Println()
	}
}