Added simple terrain.
This commit is contained in:
parent
a56ceae2e3
commit
37589d5396
BIN
cmd/tins2020/res/images/dirt.png
Normal file
BIN
cmd/tins2020/res/images/dirt.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
BIN
cmd/tins2020/res/images/grass.png
Normal file
BIN
cmd/tins2020/res/images/grass.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
BIN
cmd/tins2020/res/images/snow.png
Normal file
BIN
cmd/tins2020/res/images/snow.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
@ -1,15 +1,41 @@
|
||||
package main
|
||||
|
||||
import "opslag.de/schobers/tins2020"
|
||||
import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"opslag.de/schobers/tins2020"
|
||||
)
|
||||
|
||||
type terrainRenderer struct {
|
||||
terrain *tins2020.Map
|
||||
center tins2020.PointF
|
||||
zoom float32
|
||||
}
|
||||
|
||||
func newTerrainRenderer(terrain *tins2020.Map) *terrainRenderer {
|
||||
return &terrainRenderer{terrain}
|
||||
return &terrainRenderer{terrain: terrain}
|
||||
}
|
||||
|
||||
func (r *terrainRenderer) Render(ctx *tins2020.Context) {
|
||||
tempToTexture := func(temp float64) *tins2020.Texture {
|
||||
if temp < .2 {
|
||||
return ctx.Textures.Texture("snow")
|
||||
}
|
||||
if temp > .8 {
|
||||
return ctx.Textures.Texture("dirt")
|
||||
}
|
||||
return ctx.Textures.Texture("grass")
|
||||
}
|
||||
|
||||
// horizontal: [191, 321) = 130
|
||||
// vertical: [267,332) = 65
|
||||
|
||||
for y := int32(0); y < 10; y++ {
|
||||
left := -y * 65
|
||||
top := y * 32
|
||||
for x := int32(0); x < 10; x++ {
|
||||
temp := r.terrain.Temp.Value(int(x), int(y))
|
||||
text := tempToTexture(temp)
|
||||
text.Copy(ctx.Renderer, &sdl.Rect{X: left + x*65, Y: top + x*32, W: 512, H: 512})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,20 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func logSDLVersion() {
|
||||
var version sdl.Version
|
||||
sdl.GetVersion(&version)
|
||||
log.Printf("SDL version: %d.%d.%d", version.Major, version.Minor, version.Patch)
|
||||
}
|
||||
|
||||
func run() error {
|
||||
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
||||
return err
|
||||
}
|
||||
defer sdl.Quit()
|
||||
|
||||
logSDLVersion()
|
||||
|
||||
if err := ttf.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -40,8 +48,9 @@ func run() error {
|
||||
}
|
||||
}
|
||||
|
||||
sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "1")
|
||||
window, err := sdl.CreateWindow("TINS 2020", ctx.Settings.Window.Location.X, ctx.Settings.Window.Location.Y,
|
||||
800, 600, sdl.WINDOW_SHOWN)
|
||||
800, 600, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -59,6 +68,17 @@ func run() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ctx.Textures.Load(
|
||||
"dirt", "images/dirt.png",
|
||||
"grass", "images/grass.png",
|
||||
"snow", "images/snow.png",
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
game := tins2020.NewGame()
|
||||
tr := newTerrainRenderer(game.Terrain)
|
||||
|
||||
running := true
|
||||
for running {
|
||||
@ -79,8 +99,12 @@ func run() error {
|
||||
|
||||
renderer.SetDrawColor(0, 0, 0, 255)
|
||||
renderer.Clear()
|
||||
renderer.SetDrawColor(255, 0, 0, 255)
|
||||
renderer.FillRect(&sdl.Rect{0, 0, 200, 200})
|
||||
// renderer.SetDrawColor(255, 0, 0, 255)
|
||||
// renderer.FillRect(&sdl.Rect{0, 0, 200, 200})
|
||||
// ctx.Textures.Texture("grass").Copy(renderer, &sdl.Rect{X: 200, Y: 100, W: 456, H: 356})
|
||||
// ctx.Textures.Texture("dirt").Copy(renderer, &sdl.Rect{X: 143, Y: 122, W: 456, H: 356})
|
||||
// ctx.Textures.Texture("snow").Copy(renderer, &sdl.Rect{X: 257, Y: 122, W: 456, H: 356})
|
||||
tr.Render(ctx)
|
||||
renderer.Present()
|
||||
}
|
||||
return nil
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Renderer *sdl.Renderer
|
||||
Fonts Fonts
|
||||
Resources Resources
|
||||
Textures Textures
|
||||
@ -26,8 +27,9 @@ func NewContext(res *rice.Box) (*Context, error) {
|
||||
}
|
||||
|
||||
func (c *Context) Init(renderer *sdl.Renderer) {
|
||||
c.Renderer = renderer
|
||||
c.Fonts.Init(c.Resources.Copy())
|
||||
c.Textures.Init(renderer)
|
||||
c.Textures.Init(renderer, c.Resources.Copy())
|
||||
}
|
||||
|
||||
func (c *Context) Destroy() {
|
||||
|
2
game.go
2
game.go
@ -27,7 +27,7 @@ func NewNoiseMap(seed int64) *NoiseMap {
|
||||
return &NoiseMap{
|
||||
noise: noise.NewPerlin(seed),
|
||||
alpha: 2,
|
||||
beta: 2,
|
||||
beta: 2.13,
|
||||
harmonics: 4,
|
||||
}
|
||||
}
|
||||
|
73
textures.go
73
textures.go
@ -1,34 +1,91 @@
|
||||
package tins2020
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/veandco/go-sdl2/img"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"opslag.de/schobers/fs/vfs"
|
||||
)
|
||||
|
||||
type Texture struct {
|
||||
texture *sdl.Texture
|
||||
rect *sdl.Rect
|
||||
}
|
||||
|
||||
func NewTextureFromSurface(renderer *sdl.Renderer, surface *sdl.Surface) (*Texture, error) {
|
||||
texture, err := renderer.CreateTextureFromSurface(surface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Texture{texture: texture, rect: &sdl.Rect{X: 0, Y: 0, W: surface.W, H: surface.H}}, nil
|
||||
}
|
||||
|
||||
func (t *Texture) Rect() *sdl.Rect { return t.rect }
|
||||
|
||||
func (t *Texture) Copy(renderer *sdl.Renderer, target *sdl.Rect) {
|
||||
renderer.Copy(t.texture, t.rect, target)
|
||||
}
|
||||
|
||||
// func (t *Texture) CopyF(renderer *sdl.Renderer, target *sdl.FRect) {
|
||||
// renderer.CopyF(t.texture, t.rect, target) // Depends on SDL >=2.0.10
|
||||
// }
|
||||
|
||||
func (t *Texture) Destroy() { t.texture.Destroy() }
|
||||
|
||||
type Textures struct {
|
||||
dir vfs.CopyDir
|
||||
renderer *sdl.Renderer
|
||||
textures map[string]*sdl.Texture
|
||||
textures map[string]*Texture
|
||||
}
|
||||
|
||||
func (t *Textures) Init(renderer *sdl.Renderer) {
|
||||
func (t *Textures) Init(renderer *sdl.Renderer, dir vfs.CopyDir) {
|
||||
t.dir = dir
|
||||
t.renderer = renderer
|
||||
t.textures = map[string]*sdl.Texture{}
|
||||
t.textures = map[string]*Texture{}
|
||||
}
|
||||
|
||||
func (t *Textures) Load(name, path string, size int) error {
|
||||
surface, err := img.Load(name)
|
||||
func (t *Textures) Load(name, path string, other ...string) error {
|
||||
err := t.load(name, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(other) == 0 {
|
||||
return nil
|
||||
}
|
||||
if len(other)%2 != 0 {
|
||||
return errors.New("expected name/path pairs")
|
||||
}
|
||||
for i := 0; i < len(other); i += 2 {
|
||||
err = t.load(other[i], other[i+1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("error loading '%s'; error: %v", other[i], err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Textures) load(name, path string) error {
|
||||
texturePath, err := t.dir.Retrieve(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
surface, err := img.Load(texturePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer surface.Free()
|
||||
texture, err := t.renderer.CreateTextureFromSurface(surface)
|
||||
if t.textures == nil {
|
||||
t.textures = map[string]*sdl.Texture{}
|
||||
texture, err := NewTextureFromSurface(t.renderer, surface)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.textures[name] = texture
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Textures) Texture(name string) *Texture { return t.textures[name] }
|
||||
|
||||
func (t *Textures) Destroy() {
|
||||
if t.textures == nil {
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user