adding a simple render test
This commit is contained in:
parent
8c5051dd4f
commit
454b9210c1
@ -1,39 +1,91 @@
|
|||||||
package main
|
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() {
|
func main() {
|
||||||
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
window, err := sdl.CreateWindow("Viewer", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer sdl.Quit()
|
|
||||||
|
|
||||||
window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
|
|
||||||
800, 600, sdl.WINDOW_SHOWN)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer window.Destroy()
|
defer window.Destroy()
|
||||||
|
|
||||||
surface, err := window.GetSurface()
|
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
|
||||||
if err != nil {
|
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}
|
sprite, err := loadSurface("/home/alex/loadingscreen.dc6", "/home/alex/pal.dat")
|
||||||
surface.FillRect(&rect, 0xffff0000)
|
if err != nil {
|
||||||
window.UpdateSurface()
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
running := true
|
texture, err := renderer.CreateTextureFromSurface(sprite)
|
||||||
for running {
|
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() {
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case *sdl.QuitEvent:
|
case *sdl.QuitEvent:
|
||||||
println("Quit")
|
|
||||||
running = false
|
running = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
sdl.Delay(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user