Skip to content

Commit

Permalink
add mode filtering
Browse files Browse the repository at this point in the history
get rid of the stupid "if true" stuff, support an environment
variable (at least in the tests) that controls selected modes.
  • Loading branch information
seebs committed Sep 8, 2019
1 parent af16efb commit 8fe05cb
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 21 deletions.
6 changes: 2 additions & 4 deletions modes/dotgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ func distanceCompute(s *dotGridScene, base [][]g.DotGridBase, prev [][]g.DotGrid
}

func init() {
if true {
for _, mode := range dotGridModes {
allModes = append(allModes, mode)
}
for _, mode := range dotGridModes {
defaultList.Add(mode)
}
}

Expand Down
6 changes: 2 additions & 4 deletions modes/hexpaint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ var hexPaintModes = []hexPaintMode{
}

func init() {
if true {
for _, mode := range hexPaintModes {
allModes = append(allModes, mode)
}
for _, mode := range hexPaintModes {
defaultList.Add(mode)
}
}

Expand Down
6 changes: 2 additions & 4 deletions modes/knights.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ var knightModes = []knightMode{
}

func init() {
if true {
for _, mode := range knightModes {
allModes = append(allModes, mode)
}
for _, mode := range knightModes {
defaultList.Add(mode)
}
}

Expand Down
6 changes: 2 additions & 4 deletions modes/match3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ var match3Modes = []match3Mode{
}

func init() {
if true {
for _, mode := range match3Modes {
allModes = append([]Mode{mode}, allModes...)
}
for _, mode := range match3Modes {
defaultList.Add(mode)
}
}

Expand Down
100 changes: 97 additions & 3 deletions modes/mode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package modes

import (
"strings"

"github.com/hajimehoshi/ebiten"
"seebs.net/modus/g"
"seebs.net/modus/keys"
Expand Down Expand Up @@ -35,9 +37,101 @@ type Scene interface {
Draw(screen *ebiten.Image) error
}

var allModes []Mode
// ModeFilter defines the way we're currently filtering modes. The set
// of modes is whitelist (or all modes if no whitelist is present) minus
// blacklist.
type ModeFilter struct {
whitelist map[string]struct{}
blacklist map[string]struct{}
}

// Whitelist whitelists the given mode.
func (mf *ModeFilter) Whitelist(add string) {
if mf.whitelist == nil {
mf.whitelist = map[string]struct{}{add: struct{}{}}
return
}
mf.whitelist[add] = struct{}{}
}

// Blacklist blacklists the given mode.
func (mf *ModeFilter) Blacklist(remove string) {
if mf.blacklist == nil {
mf.blacklist = map[string]struct{}{remove: struct{}{}}
return
}
mf.blacklist[remove] = struct{}{}
}

// ListModes provides a list of the available modes
func (mf *ModeFilter) Apply(ml ModeList) (results []Mode) {
if len(mf.whitelist) == 0 {
for _, mode := range ml.list {
name := mode.Name()
if _, ok := mf.blacklist[name]; !ok {
results = append(results, mode)
}
}
return results
}
for _, mode := range ml.list {
name := mode.Name()
if _, ok := mf.whitelist[name]; ok {
if _, ok := mf.blacklist[name]; !ok {
results = append(results, mode)
}
}
}
return results
}

// ApplyList applies a given list, in the form +mode,-mode,...
// Bare modes are whitelisted, +mode is whitelisted, -mode is blacklisted.
func (mf *ModeFilter) ApplyList(list string) {
if list == "" {
return
}
mods := strings.Split(list, ",")
for _, m := range mods {
if m == "" {
continue
}
if m[0] == '-' {
Blacklist(m[1:])
} else if m[0] == '+' {
Whitelist(m[1:])
} else {
Whitelist(m)
}
}
}

type ModeList struct {
list []Mode
}

func (ml *ModeList) Add(m Mode) {
ml.list = append(ml.list, m)
}

var defaultFilter ModeFilter
var defaultList ModeList

// ListModes provides a list of the available modes.
func ListModes() []Mode {
return allModes
return defaultFilter.Apply(defaultList)
}

// Whitelist modifies the default filter.
func Whitelist(add string) {
defaultFilter.Whitelist(add)
}

// Blacklist modifies the default filter.
func Blacklist(remove string) {
defaultFilter.Blacklist(remove)
}

// ApplyList modifies the default filter.
func ApplyList(list string) {
defaultFilter.ApplyList(list)
}
32 changes: 31 additions & 1 deletion modes/modes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package modes

import (
"fmt"
"os"
"strings"
"testing"

"seebs.net/modus/g"
Expand Down Expand Up @@ -31,10 +33,21 @@ func benchmarkOneModeTickDraw(b *testing.B, scene Scene) {

var detailLevels = []int{5, 10, 20}

func TestMain(m *testing.M) {
ApplyList(os.Getenv("MODUS_MODES"))
modes := ListModes()
names := make([]string, len(modes))
for i, m := range modes {
names[i] = m.Name()
}
fmt.Printf("Testing modes: %s\n", strings.Join(names, ", "))
os.Exit(m.Run())
}

func Benchmark_ModeTick(b *testing.B) {
c := g.NewContext(1280, 960, false)
p := g.Palettes["rainbow"]
for _, mode := range allModes {
for _, mode := range ListModes() {
for _, detail := range detailLevels {
scene, err := mode.New(c, detail, p)
if err != nil {
Expand All @@ -47,6 +60,23 @@ func Benchmark_ModeTick(b *testing.B) {
b.Run(fmt.Sprintf("Tick/%s@%d", mode.Name(), detail), func(b *testing.B) {
benchmarkOneModeTick(b, scene)
})
}
}
}

func Benchmark_ModeDraw(b *testing.B) {
c := g.NewContext(1280, 960, false)
p := g.Palettes["rainbow"]
for _, mode := range ListModes() {
for _, detail := range detailLevels {
scene, err := mode.New(c, detail, p)
if err != nil {
b.Fatalf("failed to initialize scene %s@%d: %v", mode.Name(), detail, err)
}
err = scene.Display()
if err != nil {
b.Fatalf("failed to display scene %s@%d: %v", mode.Name(), detail, err)
}
// note: you can't really just benchmark the draw, because often if
// no ticks have happened it'll use cached results.
b.Run(fmt.Sprintf("Draw/%s@%d", mode.Name(), detail), func(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion modes/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var vectorModes = []vectorMode{

func init() {
for _, mode := range vectorModes {
allModes = append(allModes, mode)
defaultList.Add(mode)
}
}

Expand Down

0 comments on commit 8fe05cb

Please sign in to comment.