This commit is contained in:
Alex Yatskov 2019-01-01 10:30:35 -08:00
parent 55817d4511
commit ac0f65fb3e
7 changed files with 38 additions and 50 deletions

View File

@ -7,7 +7,7 @@ import (
"github.com/FooSoft/lazarus/math"
)
type Palette struct {
type DatPalette struct {
Colors [256]math.Color3b
}
@ -17,13 +17,13 @@ type color struct {
R byte
}
func NewFromReader(reader io.Reader) (*Palette, error) {
func NewFromReader(reader io.Reader) (*DatPalette, error) {
var colors [256]color
if err := binary.Read(reader, binary.LittleEndian, &colors); err != nil {
return nil, err
}
palette := new(Palette)
palette := new(DatPalette)
for i, color := range colors {
palette.Colors[i] = math.Color3b{R: color.R, G: color.G, B: color.B}
}
@ -31,8 +31,8 @@ func NewFromReader(reader io.Reader) (*Palette, error) {
return palette, nil
}
func NewFromGrayscale() *Palette {
palette := new(Palette)
func NewFromGrayscale() *DatPalette {
palette := new(DatPalette)
for i := 0; i < 256; i++ {
value := uint8(i)
palette.Colors[i] = math.Color3b{R: value, G: value, B: value}

View File

@ -38,12 +38,12 @@ type Frame struct {
Data []byte
}
type Sprite struct {
type Dc6Animation struct {
Directions []Direction
}
func NewFromReader(reader io.ReadSeeker) (*Sprite, error) {
sprite := new(Sprite)
func NewFromReader(reader io.ReadSeeker) (*Dc6Animation, error) {
sprite := new(Dc6Animation)
var fileHead fileHeader
if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil {

View File

@ -1,16 +1,7 @@
package mpq
/*
// Build Tags
// Windows
#cgo windows CFLAGS: -D_MPQ_WINDOWS
#cgo windows LDFLAGS: -Lstormlib -lstorm -lwininet -lz -lbz2 -lstdc++
// Linux
#cgo linux CFLAGS: -D_MPQ_LINUX
#cgo linux LDFLAGS: -L./stormlib/ -lstorm -lz -lbz2 -lstdc++
*/
// #cgo windows CFLAGS: -D_MPQ_WINDOWS
// #cgo windows LDFLAGS: -Lstormlib -lstorm -lwininet -lz -lbz2 -lstdc++
// #cgo linux CFLAGS: -D_MPQ_LINUX
// #cgo linux LDFLAGS: -L./stormlib/ -lstorm -lz -lbz2 -lstdc++
import "C"

View File

@ -1,15 +1,5 @@
package mpq
// #ifdef _MPQ_WINDOWS
// #include <windows.h>
// #include <stdlib.h>
// #endif
// #ifdef _MPQ_LINUX
// #include <stdlib.h>
// #define WINAPI
// DWORD GetLastError();
// #endif
//
// #define DWORD unsigned int
// #define LPDWORD unsigned int *
// #define LPOVERLAPPED void *
@ -18,13 +8,21 @@ package mpq
// #define bool unsigned char
// #define LONG int
//
// #include <stdlib.h>
// #ifdef _MPQ_WINDOWS
// #include <windows.h>
// #endif
// #ifdef _MPQ_LINUX
// #define WINAPI
// DWORD GetLastError();
// #endif
//
// bool WINAPI SFileOpenArchive(const TCHAR * szMpqName, DWORD dwPriority, DWORD dwFlags, HANDLE * phMpq);
// bool WINAPI SFileCloseArchive(HANDLE hMpq);
// bool WINAPI SFileOpenFileEx(HANDLE hMpq, const char * szFileName, DWORD dwSearchScope, HANDLE * phFile);
// DWORD WINAPI SFileSetFilePointer(HANDLE hFile, LONG lFilePos, LONG * plFilePosHigh, DWORD dwMoveMethod);
// bool WINAPI SFileReadFile(HANDLE hFile, void * lpBuffer, DWORD dwToRead, LPDWORD pdwRead, LPOVERLAPPED lpOverlapped);
// bool WINAPI SFileCloseFile(HANDLE hFile);
//
import "C"
import (
"bytes"
@ -42,13 +40,13 @@ type File interface {
Close() error
}
type Archive interface {
type MpqArchive interface {
OpenFile(path string) (File, error)
GetPaths() []string
Close() error
}
func NewFromFile(path string) (Archive, error) {
func NewFromFile(path string) (MpqArchive, error) {
cs := C.CString(path)
defer C.free(unsafe.Pointer(cs))

View File

@ -4,8 +4,7 @@ Copy the stormlib folder to an easy to access directory. I used /mingw64/home/th
(from mingw64 console)
$ cd /mingw64/home/thegtproject/stormlib
$ cmake -DCMAKE_SYSTEM_NAME="windows" -DCMAKE_INSTALL_PREFIX="/mingw64" .
$ make && make install
Done.
Done.

View File

@ -14,7 +14,7 @@ import (
"github.com/FooSoft/lazarus/formats/dc6"
)
func loadPalette(path string) (*dat.Palette, error) {
func loadPalette(path string) (*dat.DatPalette, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
@ -23,7 +23,7 @@ func loadPalette(path string) (*dat.Palette, error) {
return dat.NewFromReader(fp)
}
func loadSprite(path string) (*dc6.Sprite, error) {
func loadSprite(path string) (*dc6.Dc6Animation, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
@ -32,7 +32,7 @@ func loadSprite(path string) (*dc6.Sprite, error) {
return dc6.NewFromReader(fp)
}
func extractSprite(spritePath string, palette *dat.Palette, targetDir string) error {
func extractSprite(spritePath string, palette *dat.DatPalette, targetDir string) error {
sprite, err := loadSprite(spritePath)
if err != nil {
return err

View File

@ -14,7 +14,7 @@ import (
"github.com/FooSoft/lazarus/platform"
)
func loadPalette(path string) (*dat.Palette, error) {
func loadPalette(path string) (*dat.DatPalette, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
@ -23,7 +23,7 @@ func loadPalette(path string) (*dat.Palette, error) {
return dat.NewFromReader(fp)
}
func loadSprite(path string) (*dc6.Sprite, error) {
func loadAnimation(path string) (*dc6.Dc6Animation, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
@ -33,9 +33,9 @@ func loadSprite(path string) (*dc6.Sprite, error) {
}
type scene struct {
sprite *dc6.Sprite
palette *dat.Palette
texture graphics.Texture
animation *dc6.Dc6Animation
palette *dat.DatPalette
texture graphics.Texture
directionIndex int
frameIndex int
@ -65,8 +65,8 @@ func (s *scene) Advance(window *platform.Window) error {
imgui.DialogBegin("DC6 Viewer")
imgui.Image(s.texture)
direction := s.sprite.Directions[directionIndex]
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.sprite.Directions)-1) {
direction := s.animation.Directions[directionIndex]
if imgui.SliderInt("Direction", &directionIndex, 0, len(s.animation.Directions)-1) {
frameIndex = 0
}
frame := direction.Frames[frameIndex]
@ -88,7 +88,7 @@ func (s *scene) Advance(window *platform.Window) error {
}
func (s *scene) updateTexture(window *platform.Window) error {
frame := s.sprite.Directions[s.directionIndex].Frames[s.frameIndex]
frame := s.animation.Directions[s.directionIndex].Frames[s.frameIndex]
colors := make([]math.Color3b, frame.Size.X*frame.Size.Y)
for y := 0; y < frame.Size.Y; y++ {
for x := 0; x < frame.Size.X; x++ {
@ -129,13 +129,13 @@ func main() {
os.Exit(2)
}
sprite, err := loadSprite(flag.Arg(0))
animation, err := loadAnimation(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var palette *dat.Palette
var palette *dat.DatPalette
if len(*palettePath) > 0 {
palette, err = loadPalette(*palettePath)
if err != nil {
@ -146,7 +146,7 @@ func main() {
palette = dat.NewFromGrayscale()
}
scene := &scene{sprite: sprite, palette: palette}
scene := &scene{animation: animation, palette: palette}
window, err := platform.NewWindow("Viewer", math.Vec2i{X: 1024, Y: 768}, scene)
if err != nil {
fmt.Fprintln(os.Stderr, err)