Skip to content

Commit

Permalink
Scene and GUI rework (OpenDiablo2#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
FooSoft authored Feb 9, 2020
1 parent b957661 commit 5e958b9
Show file tree
Hide file tree
Showing 18 changed files with 1,298 additions and 1,143 deletions.
18 changes: 13 additions & 5 deletions d2common/d2fileformats/d2mpq/crypto_buff.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package d2mpq

// CryptoBuffer contains the crypto bytes for filename hashing
var CryptoBuffer [0x500]uint32
var cryptoBuffer [0x500]uint32
var cryptoBufferReady bool

// InitializeCryptoBuffer initializes the crypto buffer
func InitializeCryptoBuffer() {
func cryptoLookup(index uint32) uint32 {
if !cryptoBufferReady {
cryptoInitialize()
cryptoBufferReady = true
}

return cryptoBuffer[index]
}

func cryptoInitialize() {
seed := uint32(0x00100001)
for index1 := 0; index1 < 0x100; index1++ {
index2 := index1
Expand All @@ -13,7 +21,7 @@ func InitializeCryptoBuffer() {
temp1 := (seed & 0xFFFF) << 0x10
seed = (seed*125 + 3) % 0x2AAAAB
temp2 := seed & 0xFFFF
CryptoBuffer[index2] = temp1 | temp2
cryptoBuffer[index2] = temp1 | temp2
index2 += 0x100
}
}
Expand Down
6 changes: 3 additions & 3 deletions d2common/d2fileformats/d2mpq/mpq.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func decrypt(data []uint32, seed uint32) {
seed2 := uint32(0xeeeeeeee)

for i := 0; i < len(data); i++ {
seed2 += CryptoBuffer[0x400+(seed&0xff)]
seed2 += cryptoLookup(0x400 + (seed & 0xff))
result := data[i]
result ^= seed + seed2

Expand All @@ -215,7 +215,7 @@ func decrypt(data []uint32, seed uint32) {
func decryptBytes(data []byte, seed uint32) {
seed2 := uint32(0xEEEEEEEE)
for i := 0; i < len(data)-3; i += 4 {
seed2 += CryptoBuffer[0x400+(seed&0xFF)]
seed2 += cryptoLookup(0x400 + (seed & 0xFF))
result := binary.LittleEndian.Uint32(data[i : i+4])
result ^= seed + seed2
seed = ((^seed << 21) + 0x11111111) | (seed >> 11)
Expand All @@ -235,7 +235,7 @@ func hashString(key string, hashType uint32) uint32 {

/* prepare seeds. */
for _, char := range strings.ToUpper(key) {
seed1 = CryptoBuffer[(hashType*0x100)+uint32(char)] ^ (seed1 + seed2)
seed1 = cryptoLookup((hashType*0x100)+uint32(char)) ^ (seed1 + seed2)
seed2 = uint32(char) + seed1 + seed2 + (seed2 << 5) + 3
}
return seed1
Expand Down
57 changes: 28 additions & 29 deletions d2core/d2audio/ebiten/ebiten_audio_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,36 @@ func (eap *AudioProvider) PlayBGM(song string) {
_ = eap.bgmAudio.Pause()
return
}
go func() {
if eap.bgmAudio != nil {
err := eap.bgmAudio.Close()
if err != nil {
log.Panic(err)
}
}
audioData, err := d2asset.LoadFile(song)
if err != nil {
panic(err)
}
d, err := wav.Decode(eap.audioContext, audio.BytesReadSeekCloser(audioData))
if err != nil {
log.Fatal(err)
}
s := audio.NewInfiniteLoop(d, d.Length())
eap.bgmAudio, err = audio.NewPlayer(eap.audioContext, s)
if err != nil {
log.Fatal(err)
}
eap.bgmAudio.SetVolume(eap.bgmVolume)
// Play the infinite-length stream. This never ends.
err = eap.bgmAudio.Rewind()
if err != nil {
panic(err)
}
err = eap.bgmAudio.Play()

if eap.bgmAudio != nil {
err := eap.bgmAudio.Close()
if err != nil {
panic(err)
log.Panic(err)
}
}()
}
audioData, err := d2asset.LoadFile(song)
if err != nil {
panic(err)
}
d, err := wav.Decode(eap.audioContext, audio.BytesReadSeekCloser(audioData))
if err != nil {
log.Fatal(err)
}
s := audio.NewInfiniteLoop(d, d.Length())
eap.bgmAudio, err = audio.NewPlayer(eap.audioContext, s)
if err != nil {
log.Fatal(err)
}
eap.bgmAudio.SetVolume(eap.bgmVolume)
// Play the infinite-length stream. This never ends.
err = eap.bgmAudio.Rewind()
if err != nil {
panic(err)
}
err = eap.bgmAudio.Play()
if err != nil {
panic(err)
}
}

func (eap *AudioProvider) LoadSoundEffect(sfx string) (d2audio.SoundEffect, error) {
Expand Down
76 changes: 76 additions & 0 deletions d2core/d2gui/d2gui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package d2gui

import (
"errors"

"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)

var (
ErrWasInit = errors.New("gui system is already initialized")
ErrNotInit = errors.New("gui system is not initialized")
)

var singleton *guiManager

func Initialize() error {
assertNotInit()

var err error
if singleton, err = createGuiManager(); err != nil {
return err
}

return nil
}

func Shutdown() {
singleton = nil
}

func Render(target d2render.Surface) error {
assertWasInit()
return singleton.render(target)
}

func Advance(elapsed float64) error {
assertWasInit()
return singleton.advance(elapsed)
}

func Clear() {
assertWasInit()
singleton.clear()
}

func ShowLoadScreen(progress float64) {
assertWasInit()
singleton.showLoadScreen(progress)
}

func HideLoadScreen() {
assertWasInit()
singleton.hideLoadScreen()
}

func ShowCursor() {
assertWasInit()
singleton.showCursor()
}

func HideCursor() {
assertWasInit()
singleton.hideCursor()
}

func assertWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}

func assertNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}
125 changes: 125 additions & 0 deletions d2core/d2gui/gui_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package d2gui

import (
"image/color"
"math"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)

type guiWidget interface {
getLayer() int
render(target d2render.Surface) error
advance(elapsed float64) error
}

type guiManager struct {
cursorSprite *Sprite
loadSprite *Sprite
widgets []guiWidget
loading bool
}

func createGuiManager() (*guiManager, error) {
cursorSprite, err := CreateSprite(d2resource.CursorDefault, d2resource.PaletteUnits)
if err != nil {
return nil, err
}

loadSprite, err := CreateSprite(d2resource.LoadingScreen, d2resource.PaletteLoading)
if err != nil {
return nil, err
}

width, height := loadSprite.getSize()
loadSprite.SetPosition(400-width/2, 300+height/2)

manager := &guiManager{
cursorSprite: cursorSprite,
loadSprite: loadSprite,
}

if err := d2input.BindHandler(manager); err != nil {
return nil, err
}

return manager, nil
}

func (gui *guiManager) OnMouseMove(event d2input.MouseMoveEvent) bool {
gui.cursorSprite.SetPosition(event.X, event.Y)
return false
}

func (gui *guiManager) render(target d2render.Surface) error {
if gui.loading {
target.Clear(color.Black)
if err := gui.loadSprite.render(target); err != nil {
return err
}
} else {
for _, widget := range gui.widgets {
if err := widget.render(target); err != nil {
return err
}
}
}

if err := gui.cursorSprite.render(target); err != nil {
return err
}

return nil
}

func (gui *guiManager) advance(elapsed float64) error {
if gui.loading {
gui.loadSprite.Show()
if err := gui.loadSprite.advance(elapsed); err != nil {
return err
}
} else {
gui.loadSprite.Hide()
for _, widget := range gui.widgets {
if err := widget.advance(elapsed); err != nil {
return err
}
}
}

if err := gui.loadSprite.advance(elapsed); err != nil {
return err
}

return nil
}

func (gui *guiManager) showLoadScreen(progress float64) {
progress = math.Min(progress, 1.0)
progress = math.Max(progress, 0.0)

animation := gui.loadSprite.animation
frameCount := animation.GetFrameCount()
animation.SetCurrentFrame(int(float64(frameCount-1.0) * progress))

gui.loading = true
}

func (gui *guiManager) hideLoadScreen() {
gui.loading = false
}

func (gui *guiManager) showCursor() {
gui.cursorSprite.Show()
}

func (gui *guiManager) hideCursor() {
gui.cursorSprite.Hide()
}

func (gui *guiManager) clear() {
gui.widgets = nil
gui.hideLoadScreen()
}
Loading

0 comments on commit 5e958b9

Please sign in to comment.