Source the palette name from data

This commit is contained in:
2020-06-01 01:24:44 +01:00
parent c1268e8d57
commit 3866ee07a8
8 changed files with 35 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"image/color"
"log" "log"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -13,6 +14,7 @@ import (
"code.ur.gs/lupine/ordoor/internal/idx" "code.ur.gs/lupine/ordoor/internal/idx"
"code.ur.gs/lupine/ordoor/internal/maps" "code.ur.gs/lupine/ordoor/internal/maps"
"code.ur.gs/lupine/ordoor/internal/menus" "code.ur.gs/lupine/ordoor/internal/menus"
"code.ur.gs/lupine/ordoor/internal/palettes"
"code.ur.gs/lupine/ordoor/internal/sets" "code.ur.gs/lupine/ordoor/internal/sets"
) )
@@ -33,6 +35,10 @@ func main() {
} }
engine := cfg.DefaultEngine() engine := cfg.DefaultEngine()
gamePath := engine.DataDir gamePath := engine.DataDir
palette, ok := palettes.Get(engine.Palette)
if !ok {
log.Fatalf("Unknown palette name: %v", engine.Palette)
}
loadData(filepath.Join(gamePath, "Data")) loadData(filepath.Join(gamePath, "Data"))
@@ -43,7 +49,7 @@ func main() {
loadMapsFrom(filepath.Join(gamePath, "Maps")) loadMapsFrom(filepath.Join(gamePath, "Maps"))
loadMapsFrom(filepath.Join(gamePath, "MultiMaps")) loadMapsFrom(filepath.Join(gamePath, "MultiMaps"))
loadSets(filepath.Join(gamePath, "Sets")) loadSets(filepath.Join(gamePath, "Sets"))
loadMenus(filepath.Join(gamePath, "Menu")) loadMenus(filepath.Join(gamePath, "Menu"), palette)
loadFonts(filepath.Join(gamePath, "Fonts")) loadFonts(filepath.Join(gamePath, "Fonts"))
loadIdx(filepath.Join(gamePath, "Idx", "WarHammer.idx")) loadIdx(filepath.Join(gamePath, "Idx", "WarHammer.idx"))
} }
@@ -163,10 +169,10 @@ func loadSets(setsPath string) {
} }
} }
func loadMenus(menusPath string) { func loadMenus(menusPath string, palette color.Palette) {
log.Printf("Loading menus from %s", menusPath) log.Printf("Loading menus from %s", menusPath)
menus, err := menus.LoadMenus(menusPath) menus, err := menus.LoadMenus(menusPath, palette)
if err != nil { if err != nil {
log.Fatalf("Failed to parse %s/*.mnu as menus: %v", menusPath, err) log.Fatalf("Failed to parse %s/*.mnu as menus: %v", menusPath, err)
} }

View File

@@ -4,6 +4,7 @@ default_engine = "ordoor"
[engines.geas] # Wages of War -> Gifts of Peace -> Geas [engines.geas] # Wages of War -> Gifts of Peace -> Geas
data_dir = "./WoW" data_dir = "./WoW"
palette = "WagesOfWar"
[engines.ordoor] # Chaos Gate -> Order Door -> Ordoor [engines.ordoor] # Chaos Gate -> Order Door -> Ordoor
data_dir = "./CG" data_dir = "./CG"

View File

@@ -2,6 +2,7 @@ package assetstore
import ( import (
"fmt" "fmt"
"image/color"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@@ -10,6 +11,7 @@ import (
"code.ur.gs/lupine/ordoor/internal/config" "code.ur.gs/lupine/ordoor/internal/config"
"code.ur.gs/lupine/ordoor/internal/data" "code.ur.gs/lupine/ordoor/internal/data"
"code.ur.gs/lupine/ordoor/internal/idx" "code.ur.gs/lupine/ordoor/internal/idx"
"code.ur.gs/lupine/ordoor/internal/palettes"
) )
type entryMap map[string]map[string]string type entryMap map[string]map[string]string
@@ -29,7 +31,7 @@ type entryMap map[string]map[string]string
// or instantiate two separate asset stores. // or instantiate two separate asset stores.
type AssetStore struct { type AssetStore struct {
RootDir string RootDir string
// Palette Palette color.Palette
// Case-insensitive file lookup. // Case-insensitive file lookup.
// {"":{"anim":"Anim", "obj":"Obj", ...}, "anim":{ "warhammer.ani":"WarHammer.ani" }, ...} // {"":{"anim":"Anim", "obj":"Obj", ...}, "anim":{ "warhammer.ani":"WarHammer.ani" }, ...}
@@ -56,8 +58,14 @@ func New(engine *config.Engine) (*AssetStore, error) {
return nil, fmt.Errorf("Unconfigured engine passed to assetstore") return nil, fmt.Errorf("Unconfigured engine passed to assetstore")
} }
palette, ok := palettes.Get(engine.Palette)
if !ok {
return nil, fmt.Errorf("Couldn't find palette %q for engine", engine.Palette)
}
store := &AssetStore{ store := &AssetStore{
RootDir: engine.DataDir, RootDir: engine.DataDir,
Palette: palette,
} }
// fill entryMap // fill entryMap

View File

@@ -62,7 +62,7 @@ func (a *AssetStore) Menu(name string) (*Menu, error) {
return nil, err return nil, err
} }
raw, err := menus.LoadMenu(filename) raw, err := menus.LoadMenu(filename, a.Palette)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -117,7 +117,7 @@ func (o *Object) Sprite(idx int) (*Sprite, error) {
} }
raw := o.raw.Sprites[idx] raw := o.raw.Sprites[idx]
img, err := ebiten.NewImageFromImage(raw.ToImage(), ebiten.FilterDefault) img, err := ebiten.NewImageFromImage(raw.ToImage(o.assets.Palette), ebiten.FilterDefault)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"image" "image"
"image/color"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@@ -12,7 +13,6 @@ import (
"strings" "strings"
"code.ur.gs/lupine/ordoor/internal/data/rle" "code.ur.gs/lupine/ordoor/internal/data/rle"
"code.ur.gs/lupine/ordoor/internal/palettes"
) )
type SpriteHeader struct { type SpriteHeader struct {
@@ -53,12 +53,12 @@ type Sprite struct {
Data []byte Data []byte
} }
func (s *Sprite) ToImage() *image.Paletted { func (s *Sprite) ToImage(palette color.Palette) *image.Paletted {
return &image.Paletted{ return &image.Paletted{
Pix: s.Data, Pix: s.Data,
Stride: int(s.Width), Stride: int(s.Width),
Rect: image.Rect(0, 0, int(s.Width), int(s.Height)), Rect: image.Rect(0, 0, int(s.Width), int(s.Height)),
Palette: palettes.DefaultPalette(), Palette: palette,
} }
} }

View File

@@ -9,7 +9,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"code.ur.gs/lupine/ordoor/internal/palettes"
"code.ur.gs/lupine/ordoor/internal/util/asciiscan" "code.ur.gs/lupine/ordoor/internal/util/asciiscan"
) )
@@ -143,7 +142,7 @@ func (p *Properties) Point() image.Point {
return image.Point{} return image.Point{}
} }
func LoadMenu(filename string) (*Menu, error) { func LoadMenu(filename string, palette color.Palette) (*Menu, error) {
name := filepath.Base(filename) name := filepath.Base(filename)
name = strings.TrimSuffix(name, filepath.Ext(name)) name = strings.TrimSuffix(name, filepath.Ext(name))
name = strings.ToLower(name) name = strings.ToLower(name)
@@ -163,7 +162,7 @@ func LoadMenu(filename string) (*Menu, error) {
return nil, err return nil, err
} }
if err := loadProperties(out, scanner); err != nil { if err := loadProperties(out, scanner, palette); err != nil {
return nil, err return nil, err
} }
@@ -189,7 +188,7 @@ func loadObjects(menu *Menu, scanner *asciiscan.Scanner) error {
return nil return nil
} }
func loadProperties(menu *Menu, scanner *asciiscan.Scanner) error { func loadProperties(menu *Menu, scanner *asciiscan.Scanner, palette color.Palette) error {
for { for {
ok, err := scanner.PeekProperty() ok, err := scanner.PeekProperty()
@@ -218,9 +217,9 @@ func loadProperties(menu *Menu, scanner *asciiscan.Scanner) error {
switch strings.ToUpper(k) { switch strings.ToUpper(k) {
case "BACKGROUND COLOR": case "BACKGROUND COLOR":
menu.BackgroundColor = palettes.DefaultPalette()[vInt] menu.BackgroundColor = palette[vInt]
case "HYPERTEXT COLOR": case "HYPERTEXT COLOR":
menu.HypertextColor = palettes.DefaultPalette()[vInt] menu.HypertextColor = palette[vInt]
case "FONT TYPE": case "FONT TYPE":
menu.FontType = vInt menu.FontType = vInt
default: default:
@@ -319,7 +318,7 @@ func loadRecords(baseDir string, menu *Menu, scanner *asciiscan.Scanner) error {
return nil return nil
} }
func LoadMenus(dir string) (map[string]*Menu, error) { func LoadMenus(dir string, palette color.Palette) (map[string]*Menu, error) {
fis, err := ioutil.ReadDir(dir) fis, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -337,7 +336,7 @@ func LoadMenus(dir string) (map[string]*Menu, error) {
continue continue
} }
built, err := LoadMenu(filepath.Join(dir, relname)) built, err := LoadMenu(filepath.Join(dir, relname), palette)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %v", filepath.Join(dir, relname), err) return nil, fmt.Errorf("%s: %v", filepath.Join(dir, relname), err)
} }

View File

@@ -5,9 +5,6 @@ import (
"sync" "sync"
) )
// Override this to change the palette globally
const DefaultPaletteName = "ChaosGate"
var ( var (
Transparent = color.RGBA{R: 0, G: 0, B: 0, A: 0} Transparent = color.RGBA{R: 0, G: 0, B: 0, A: 0}
@@ -16,6 +13,8 @@ var (
initPalettes = sync.Once{} initPalettes = sync.Once{}
) )
func DefaultPalette() color.Palette { func Get(name string) (color.Palette, bool) {
return Palettes[DefaultPaletteName] p, ok := Palettes[name]
return p, ok
} }