From 454b9210c18988e8ccf0e7884e7e269a1d26c5f4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 21 Dec 2018 19:44:19 -0800 Subject: [PATCH] adding a simple render test --- tools/viewer/main.go | 88 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/tools/viewer/main.go b/tools/viewer/main.go index 9c7d438..4c774eb 100644 --- a/tools/viewer/main.go +++ b/tools/viewer/main.go @@ -1,39 +1,91 @@ package main -import "github.com/veandco/go-sdl2/sdl" +import ( + "image/color" + "log" + "os" + + "github.com/FooSoft/lazarus/formats/dat" + "github.com/FooSoft/lazarus/formats/dc6" + "github.com/FooSoft/lazarus/graphics" + "github.com/veandco/go-sdl2/sdl" +) + +func loadPalette(path string) (*dat.Palette, error) { + fp, err := os.Open(path) + if err != nil { + return nil, err + } + defer fp.Close() + return dat.NewFromReader(fp) +} + +func loadSprite(path string) (*dc6.Sprite, error) { + fp, err := os.Open(path) + if err != nil { + return nil, err + } + defer fp.Close() + return dc6.NewFromReader(fp) +} + +func loadSurface(spritePath, palettePath string) (*sdl.Surface, error) { + sprite, err := loadSprite(spritePath) + if err != nil { + return nil, err + } + + palette, err := loadPalette(palettePath) + if err != nil { + return nil, err + } + + frame := sprite.Directions[0].Frames[0] + colors := make([]color.RGBA, frame.Width*frame.Height) + for y := 0; y < frame.Height; y++ { + for x := 0; x < frame.Width; x++ { + colors[y*frame.Width+x] = palette.Colors[frame.Data[y*frame.Width+x]] + } + } + + return graphics.NewSurfaceFromRgba(colors, frame.Width, frame.Height) +} func main() { - if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil { - panic(err) - } - defer sdl.Quit() - - window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, - 800, 600, sdl.WINDOW_SHOWN) + window, err := sdl.CreateWindow("Viewer", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN) if err != nil { - panic(err) + log.Fatal(err) } defer window.Destroy() - surface, err := window.GetSurface() + renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { - panic(err) + log.Fatal(err) } - surface.FillRect(nil, 0) + defer renderer.Destroy() - rect := sdl.Rect{X: 0, Y: 0, W: 200, H: 200} - surface.FillRect(&rect, 0xffff0000) - window.UpdateSurface() + sprite, err := loadSurface("/home/alex/loadingscreen.dc6", "/home/alex/pal.dat") + if err != nil { + log.Fatal(err) + } - running := true - for running { + texture, err := renderer.CreateTextureFromSurface(sprite) + if err != nil { + log.Fatal(err) + } + + renderer.Clear() + renderer.Copy(texture, &sdl.Rect{0, 0, 256, 256}, &sdl.Rect{0, 0, 256, 256}) + renderer.Present() + + for running := true; running; { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch event.(type) { case *sdl.QuitEvent: - println("Quit") running = false break } + sdl.Delay(1) } } }