2020-01-31 23:18:11 -05:00
|
|
|
package d2game
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2scene"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2term"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
var loadingSprite *d2ui.Sprite // The sprite shown when loading stuff
|
|
|
|
var lastTime float64 // Last time we updated the scene
|
2020-01-31 23:18:11 -05:00
|
|
|
var showFPS bool
|
|
|
|
var timeScale float64
|
|
|
|
|
|
|
|
type bsForInputHanding struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var bsHandler *bsForInputHanding
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
func Initialize(loadingSpr *d2ui.Sprite) error {
|
2020-01-31 23:18:11 -05:00
|
|
|
bsHandler = &bsForInputHanding{}
|
|
|
|
loadingSprite = loadingSpr
|
|
|
|
timeScale = 1.0
|
2020-02-01 21:06:22 -05:00
|
|
|
lastTime = d2common.Now()
|
2020-01-31 23:18:11 -05:00
|
|
|
d2input.BindHandler(bsHandler)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(gitBranch string) error {
|
|
|
|
if err := d2render.Run(update, 800, 600, "OpenDiablo 2 ("+gitBranch+")"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetTimeScale(scale float64) {
|
|
|
|
timeScale = scale
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTimeScale() float64 {
|
|
|
|
return timeScale
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *bsForInputHanding) OnKeyDown(event d2input.KeyEvent) bool {
|
|
|
|
if event.Key == d2input.KeyEnter && event.KeyMod == d2input.KeyModAlt {
|
|
|
|
isFullScreen, _ := d2render.IsFullScreen()
|
|
|
|
d2render.SetFullScreen(!isFullScreen)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Key == d2input.KeyF6 {
|
|
|
|
showFPS = !showFPS
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Key == d2input.KeyF8 {
|
|
|
|
enabled, _ := d2render.GetVSyncEnabled()
|
|
|
|
d2render.SetVSyncEnabled(!enabled)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance updates the internal state of the engine
|
|
|
|
func Advance() {
|
2020-02-01 18:55:56 -05:00
|
|
|
d2scene.UpdateScene()
|
|
|
|
if d2scene.GetCurrentScene() == nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
log.Fatal("no scene loaded")
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
if d2scene.IsLoading() {
|
2020-01-31 23:18:11 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
currentTime := d2common.Now()
|
2020-01-31 23:18:11 -05:00
|
|
|
deltaTime := (currentTime - lastTime) * timeScale
|
|
|
|
lastTime = currentTime
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
d2scene.Advance(deltaTime)
|
2020-01-31 23:18:11 -05:00
|
|
|
d2ui.Advance(deltaTime)
|
|
|
|
d2term.Advance(deltaTime)
|
|
|
|
d2input.Advance(deltaTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws the game
|
2020-02-01 20:39:28 -05:00
|
|
|
func render(target d2render.Surface) {
|
2020-02-01 18:55:56 -05:00
|
|
|
if d2scene.GetLoadingProgress() < 1.0 {
|
2020-02-01 21:06:22 -05:00
|
|
|
loadingSprite.SetCurrentFrame(int(d2common.Max(0,
|
|
|
|
d2common.Min(uint32(loadingSprite.GetFrameCount()-1),
|
2020-02-01 18:55:56 -05:00
|
|
|
uint32(float64(loadingSprite.GetFrameCount()-1)*d2scene.GetLoadingProgress())))))
|
2020-01-31 23:18:11 -05:00
|
|
|
loadingSprite.Render(target)
|
|
|
|
} else {
|
2020-02-01 18:55:56 -05:00
|
|
|
if d2scene.GetCurrentScene() == nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
log.Fatal("no scene loaded")
|
|
|
|
}
|
2020-02-01 18:55:56 -05:00
|
|
|
d2scene.Render(target)
|
2020-01-31 23:18:11 -05:00
|
|
|
d2ui.Render(target)
|
|
|
|
}
|
|
|
|
if showFPS {
|
|
|
|
target.PushTranslation(5, 565)
|
|
|
|
vsyncEnabled, _ := d2render.GetVSyncEnabled()
|
|
|
|
fps, _ := d2render.CurrentFPS()
|
|
|
|
target.DrawText("vsync:" + strconv.FormatBool(vsyncEnabled) + "\nFPS:" + strconv.Itoa(int(fps)))
|
|
|
|
target.Pop()
|
|
|
|
|
|
|
|
cx, cy, _ := d2render.GetCursorPos()
|
|
|
|
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
|
|
|
|
target.PushTranslation(680, 0)
|
|
|
|
target.DrawText("Alloc " + strconv.FormatInt(int64(m.Alloc)/1024/1024, 10))
|
|
|
|
target.PushTranslation(0, 16)
|
|
|
|
target.DrawText("Pause " + strconv.FormatInt(int64(m.PauseTotalNs/1024/1024), 10))
|
|
|
|
target.PushTranslation(0, 16)
|
|
|
|
target.DrawText("HeapSys " + strconv.FormatInt(int64(m.HeapSys/1024/1024), 10))
|
|
|
|
target.PushTranslation(0, 16)
|
|
|
|
target.DrawText("NumGC " + strconv.FormatInt(int64(m.NumGC), 10))
|
|
|
|
target.PushTranslation(0, 16)
|
|
|
|
target.DrawText("Coords " + strconv.FormatInt(int64(cx), 10) + "," + strconv.FormatInt(int64(cy), 10))
|
|
|
|
target.PopN(5)
|
|
|
|
}
|
|
|
|
|
|
|
|
d2term.Render(target)
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func update(screen d2render.Surface) error {
|
2020-01-31 23:18:11 -05:00
|
|
|
Advance()
|
|
|
|
err, drawingSkipped := d2render.IsDrawingSkipped()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !drawingSkipped {
|
|
|
|
_, surface := d2render.CreateSurface(screen)
|
|
|
|
render(surface)
|
|
|
|
if surface.GetDepth() > 0 {
|
|
|
|
panic("detected surface stack leak")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|