twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

hooks.go (1648B)


      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()
     48 	onResumeAudio  func()
     49 )
     50 
     51 func OnSuspendAudio(f func()) {
     52 	m.Lock()
     53 	onSuspendAudio = f
     54 	m.Unlock()
     55 }
     56 
     57 func OnResumeAudio(f func()) {
     58 	m.Lock()
     59 	onResumeAudio = f
     60 	m.Unlock()
     61 }
     62 
     63 func SuspendAudio() {
     64 	m.Lock()
     65 	defer m.Unlock()
     66 	if audioSuspended {
     67 		return
     68 	}
     69 	audioSuspended = true
     70 	if onSuspendAudio != nil {
     71 		onSuspendAudio()
     72 	}
     73 }
     74 
     75 func ResumeAudio() {
     76 	m.Lock()
     77 	defer m.Unlock()
     78 	if !audioSuspended {
     79 		return
     80 	}
     81 	audioSuspended = false
     82 	if onResumeAudio != nil {
     83 		onResumeAudio()
     84 	}
     85 }