From 37589d5396929fb124caa8b5c157f3ab05292988 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 9 May 2020 07:48:02 +0200 Subject: [PATCH] Added simple terrain. --- cmd/tins2020/res/images/dirt.png | Bin 0 -> 1550 bytes cmd/tins2020/res/images/grass.png | Bin 0 -> 1687 bytes cmd/tins2020/res/images/snow.png | Bin 0 -> 1580 bytes cmd/tins2020/terrainrenderer.go | 30 +++++++++++- cmd/tins2020/tins2020.go | 30 ++++++++++-- context.go | 4 +- game.go | 2 +- textures.go | 73 ++++++++++++++++++++++++++---- 8 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 cmd/tins2020/res/images/dirt.png create mode 100644 cmd/tins2020/res/images/grass.png create mode 100644 cmd/tins2020/res/images/snow.png diff --git a/cmd/tins2020/res/images/dirt.png b/cmd/tins2020/res/images/dirt.png new file mode 100644 index 0000000000000000000000000000000000000000..6195e9d2ae9aacf96bd7fa7776695a22f7d26b5f GIT binary patch literal 1550 zcmc&!{ZrBh7)O#?kkziCu3EM`uXeDqwY8$OFE%?gO-U^bO=t%yz5tagO$c9Fq73yk zXXjnPDn%3l-_xdKW@vRsufvx}@}(p|Ktd3a@8~bsFWc?8=eg(e-19u2&pp39CHQEZ z4}2pW0)hBMgonjKAW#p4LOxsJNdxtPL!M;BN5vn;F{9o!q_Yw+nm4>-`-AT6EQDqfA)081=O48kBr`+@&oarH)6=%#gW=xA{Z|l;g>S;5gbkDF^&d@bx7@D)pGPcB(#&MZ{QInD@(wCMkWOoH$vZHd#*_tVsMV zC6u2UI#ii7Bchd1un)=;=UW(D3c#ac=jF^fY1V9Grm>kmP)5`>W$Gna$200WJcs*c z$419N#7ZTvx@`;mi#D;cnvx?{7Alp6?S9ly2*kT5A}lmMXUsTGJ2#f%4||$@)ZXhK z_@(kHIsBfq<~IMuk6E?Z3=mX_123-}_rF}U>aYpJDo;IYVh08fUq9dx`QHs|)v%{D zciq0wGDUT4fpCgYp#2X*{k2<_+(}ojLgA+zfZ_F7S95gJEvStTY`8$Q?5GP62|lX8 zpI{$0bN-~ejyc7B;6Cs>6kg>(Sawi=yO1-M@8gA?7DBFf7~9)pjZJcQi2dcEofq|H%0ti$adY216EFV_*13u|SeWZ!b7l~Qeae&JyF^J zJ3?M{LYp&EyJssWj-<(-eYkgP*<)LbJG~1@UV??A&9f1gV3WU0fa(O$kcVw?-^yM# zUd3U*g5#44P3q0X+HCeY zqy1n_=uvnDsi42VN3o*O12%yZ1pPnGdfxp;bRZCd7Tt>2!BHE|~IG zOOAWBqRm=g)Gmcvv*=(W8WO#p!&!HAXuMQm9~iWCA%$09BOj666uoIVCwCc+S3xNR zavbs-wW5pcDb>)-5Ad*O&uwTd8zC=2udmhz>_}EXUv@(lk^3+s_nbI&Gs&ydRCS%& zir#gp&bBVPMgmyHYdZD4T{Q&lHiD2O;jmg+%f^KRrbyueGGPh+qzq)fat?yE@!De- zVNTf{x71N|2RhRKPqd$F?9^phZiXZW|A1 zhZ~|M7VPt>;425<%Xxu*8m&^;RwWfIbm;v!>yhCSzyLRd_;H>huW>z)3z+{o0{-p9O+;9i96zObeQxIx$tkr716Q) literal 0 HcmV?d00001 diff --git a/cmd/tins2020/res/images/grass.png b/cmd/tins2020/res/images/grass.png new file mode 100644 index 0000000000000000000000000000000000000000..83fbe33aaa56819864ec112657d5d30c20a439e6 GIT binary patch literal 1687 zcmc&#`7_%I6p!e)C84@nSDB(y+M$+3)KT(foh?aLwMb}eomE7)aTM`Qq*Y2CQ>rZ` z-O&(77^+pNYfEjr*0J<()m7^%Dy$=t?|0)b*qNQ3**EW+`ON3@!+SIDrIQFQikQO~ z6bhy2=IZE)LZPKC8YL$yEw8x8enp{Vu__7p}lwbzM z3i7c`qF9YR4mnD8w&gHR7bh~vR)Y^OO_qk@vT4o~dtI{g$rwMIM+w(BF{eus9ct1X zYSN{Y`HBcB#i2Ico}UKtZb${~xGbr@-4mu=DHKbw)+0L)RYW=Q*tdDtK_1(k0^0L$ z;khiZ@+K}b(t)3*7eu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G_=I7tDHAr#2nv(2oD- fQ2Nhr(Kakgz4hXZ!UAsh@1WeA2#$P*fRw)h`qaRW literal 0 HcmV?d00001 diff --git a/cmd/tins2020/res/images/snow.png b/cmd/tins2020/res/images/snow.png new file mode 100644 index 0000000000000000000000000000000000000000..376a66c11c088ecfc791b07dd4fb9fdb14fe43be GIT binary patch literal 1580 zcmc(fiBHpa6u`?5f66LMBK#ui1k5}ES7KZyqS6_RLL7pERSwI%s#XIY8%Kd31@8+& zMWY*SITShC%1|L2tW}QU1W|Mpp+L(aEv0JHe}d2E*yg$Q{vJw z%Z@(le2eZ~UzHKrUwq`Iv~+0N{`T$xeHkw^mJTDXoJ-SB+I5rmmxIHDllB^AlYYvs z(=?03#nS-LC}ZUbI%^65I{;eHlGv{3YGgt1^>>NGvfDU<{)S zI2b1=6A5U@1O>)s$O5s{20(kCR$L{Y1i*+9<2Y#~C<8_oRmjAZvKPJD_Fk=(0!Ex1 zrxPW`28=LN@;mza4Hz{xLs&j>sLuWYX|eRwR6|~BsMXio4IGxO3%o2x%ejADv_(%u>n`V%JSu#Zzo*QT z`NxVJoC4_iu-dw%?IY`W_tPZh+@SKYWJ@NmKpgaGL;w2=P}X)fp6v!(bPmzyh|3Zs z^IRSmex_DH`eBaHYO7*995KEbgrEE7EX2j<-?(+_nnA?!eF4a-GXyJO165WK9W8CJ zr7SPp`VAYt5Jxug+v#MTWn(`v{;zgiBy7DyXZWF1TJ6q>0thQeP z6%uYTXA!r>8NRt|J*5Izo={O9q;&dgRBWH^I^-h^FV7;jYCrD$UHwIcYBVVVV_xe9_7RNrGdCe7``O&&YUroFmHUKxEZCN`^)?^PkG?`bO$U9Enb( z`~%~GY@x}LzI%2{`e@5S`p`iTHDAcm-;0G zO%!~=ltk4DruhO5G3Re@jTbrq literal 0 HcmV?d00001 diff --git a/cmd/tins2020/terrainrenderer.go b/cmd/tins2020/terrainrenderer.go index 06a8344..bea8915 100644 --- a/cmd/tins2020/terrainrenderer.go +++ b/cmd/tins2020/terrainrenderer.go @@ -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}) + } + } } diff --git a/cmd/tins2020/tins2020.go b/cmd/tins2020/tins2020.go index 8b781f5..56e67b5 100644 --- a/cmd/tins2020/tins2020.go +++ b/cmd/tins2020/tins2020.go @@ -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 diff --git a/context.go b/context.go index 9ef6408..a903a74 100644 --- a/context.go +++ b/context.go @@ -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() { diff --git a/game.go b/game.go index 70deb72..1225279 100644 --- a/game.go +++ b/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, } } diff --git a/textures.go b/textures.go index 1ada552..2e52aea 100644 --- a/textures.go +++ b/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