zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

hooks.go (1730B)


      1 // Copyright 2018 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package hooks
     16 
     17 import (
     18 	"sync"
     19 )
     20 
     21 var m sync.Mutex
     22 
     23 var onBeforeUpdateHooks = []func() error{}
     24 
     25 // AppendHookOnBeforeUpdate appends a hook function that is run before the main update function
     26 // every frame.
     27 func AppendHookOnBeforeUpdate(f func() error) {
     28 	m.Lock()
     29 	onBeforeUpdateHooks = append(onBeforeUpdateHooks, f)
     30 	m.Unlock()
     31 }
     32 
     33 func RunBeforeUpdateHooks() error {
     34 	m.Lock()
     35 	defer m.Unlock()
     36 
     37 	for _, f := range onBeforeUpdateHooks {
     38 		if err := f(); err != nil {
     39 			return err
     40 		}
     41 	}
     42 	return nil
     43 }
     44 
     45 var (
     46 	audioSuspended bool
     47 	onSuspendAudio func() error
     48 	onResumeAudio  func() error
     49 )
     50 
     51 func OnSuspendAudio(f func() error) {
     52 	m.Lock()
     53 	onSuspendAudio = f
     54 	m.Unlock()
     55 }
     56 
     57 func OnResumeAudio(f func() error) {
     58 	m.Lock()
     59 	onResumeAudio = f
     60 	m.Unlock()
     61 }
     62 
     63 func SuspendAudio() error {
     64 	m.Lock()
     65 	defer m.Unlock()
     66 	if audioSuspended {
     67 		return nil
     68 	}
     69 	audioSuspended = true
     70 	if onSuspendAudio != nil {
     71 		return onSuspendAudio()
     72 	}
     73 	return nil
     74 }
     75 
     76 func ResumeAudio() error {
     77 	m.Lock()
     78 	defer m.Unlock()
     79 	if !audioSuspended {
     80 		return nil
     81 	}
     82 	audioSuspended = false
     83 	if onResumeAudio != nil {
     84 		return onResumeAudio()
     85 	}
     86 	return nil
     87 }