show the generated region maps in the viewer
Tiles are single maps of single regions, generated on worker threads that each own a worldgen, since the layers keep RNG state. Only the visible tiles are queued, nearest first, so the viewer never computes more than the window shows and sleeps once it has drawn it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
464
src/viewer.sex
464
src/viewer.sex
@@ -1,11 +1,47 @@
|
||||
;;; The map viewer window: SDL3 for the window, events and rendering,
|
||||
;;; Nuklear for the panels drawn over the map.
|
||||
;;; Nuklear for the panels drawn over the map, tiles for the map.
|
||||
;;;
|
||||
;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT] [--layer N]
|
||||
;;; [--screenshot FILE.png] [WORLD.vcdbs | serverconfig.json]
|
||||
;;;
|
||||
;;; --screenshot draws until no tile is pending, saves the window and
|
||||
;;; quits, which is how the viewer is checked without looking at it.
|
||||
|
||||
(include "nk.h")
|
||||
(include stdio.h stdlib.h string.h math.h "nk.h" "stb_image_write.h")
|
||||
(import cs)
|
||||
(import json)
|
||||
(import maps)
|
||||
(import lcg)
|
||||
(import noise)
|
||||
(import shape)
|
||||
(import worldgen)
|
||||
(import settings)
|
||||
(import tiles)
|
||||
|
||||
(define WINDOW-WIDTH 1280)
|
||||
(define WINDOW-HEIGHT 800)
|
||||
(define FONT-SIZE 13.0)
|
||||
(define REGION 512)
|
||||
;; Blocks per window point, either way. Zoomed out further, a window
|
||||
;; shows more regions than the tile cache holds.
|
||||
(define MIN-ZOOM 0.0625)
|
||||
(define MAX-ZOOM 32.0)
|
||||
|
||||
(var layer-names [(* const char) 6]
|
||||
#("Landform" "Climate" "Geologic province" "Upheaval" "Ocean" "Rock strata thickness"))
|
||||
(var climate-names [(* const char) 5] #("temperate" "hot" "warm" "cool" "icy"))
|
||||
|
||||
;;; What the region under the cursor holds, generated on the main
|
||||
;;; thread when the cursor moves to another region.
|
||||
(record hover ((valid bool) (rx int) (rz int) (maps region-maps)))
|
||||
|
||||
(record app
|
||||
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
|
||||
(assets-dir [char 1024]) (world-path [char 1024])
|
||||
(source world-source) (edit world-source) (g (* worldgen)) (tiles (* tiles))
|
||||
(center-x double) (center-z double) (zoom double) (layer int) (stratum int)
|
||||
(mouse-x float) (mouse-y float) (dragging bool) (ui-hovered bool)
|
||||
(hover hover) (pending int) (status [char 256]) (screenshot (* const char))))
|
||||
|
||||
(fn fail ((what (* const char))) int
|
||||
(SDL-Log "%s: %s" what (SDL-GetError))
|
||||
@@ -23,60 +59,408 @@
|
||||
(/= (. font handle height) scale)
|
||||
(nk-style-set-font ctx (& (. font handle))))
|
||||
|
||||
(fn draw-ui ((ctx (* struct nk-context)) (frame-ms double)) void
|
||||
(if (nk-begin ctx "vs-map-viewer" (nk-rect 10 10 260 120)
|
||||
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE
|
||||
NK-WINDOW-MINIMIZABLE))
|
||||
;;; World ↔ window -------------------------------------------------
|
||||
|
||||
(fn window-size ((a (* app)) (w (* int)) (h (* int))) void
|
||||
(SDL-GetWindowSize (. a window) w h))
|
||||
|
||||
(fn to-world-x ((a (* app)) (sx float) (w int)) double
|
||||
(return (+ (. a center-x) (* (- sx (/ w 2.0)) (. a zoom)))))
|
||||
|
||||
(fn to-world-z ((a (* app)) (sy float) (h int)) double
|
||||
(return (+ (. a center-z) (* (- sy (/ h 2.0)) (. a zoom)))))
|
||||
|
||||
(fn to-screen ((a (* app)) (world double) (center double) (extent int)) float
|
||||
(return (cast (+ (/ (- world center) (. a zoom)) (/ extent 2.0)) float)))
|
||||
|
||||
(fn go-to-spawn ((a (* app))) void
|
||||
(= (. a center-x) (/ (. a source settings size-x) 2.0))
|
||||
(= (. a center-z) (/ (. a source settings size-z) 2.0)))
|
||||
|
||||
;;; Applying settings ----------------------------------------------
|
||||
|
||||
(fn release-hover ((a (* app))) void
|
||||
(if (. a hover valid)
|
||||
(region-maps/free (& (. a hover maps))))
|
||||
(= (. a hover valid) false))
|
||||
|
||||
;;; Makes SOURCE the world shown: a fresh generator for the panels,
|
||||
;;; and every tile regenerated.
|
||||
(fn apply ((a (* app)) (source world-source)) bool
|
||||
(var g (* worldgen) (worldgen/make (. source settings) (. a assets-dir)))
|
||||
(if (! g)
|
||||
(do
|
||||
(nk-layout-row-dynamic ctx 20 1)
|
||||
(nk-label ctx "Vintage Story 1.22.7 + Rivers 5.0.3" NK-TEXT-LEFT)
|
||||
(nk-labelf ctx NK-TEXT-LEFT "frame %.1f ms" frame-ms)))
|
||||
(snprintf (. a status) (sizeof (. a status)) "cannot load worldgen assets from %s"
|
||||
(. a assets-dir))
|
||||
(return false)))
|
||||
(if (. a g)
|
||||
(worldgen/free (. a g)))
|
||||
(= (. a g) g)
|
||||
(= (. a source) source)
|
||||
(= (. a edit) source)
|
||||
(release-hover a)
|
||||
(if (>= (. a stratum) (. g strata count))
|
||||
(= (. a stratum) 0))
|
||||
(.set-settings (. a tiles) (. source settings))
|
||||
(snprintf (. a status) (sizeof (. a status)) "seed %d" (. source settings seed))
|
||||
(return true))
|
||||
|
||||
(fn load-world ((a (* app))) void
|
||||
(var source world-source)
|
||||
(if (world-source/load (. a world-path) (& source))
|
||||
(do
|
||||
(apply a source)
|
||||
(go-to-spawn a))
|
||||
(snprintf (. a status) (sizeof (. a status)) "cannot load %s" (. a world-path))))
|
||||
|
||||
;;; Map ------------------------------------------------------------
|
||||
|
||||
(record wanted ((rx int) (rz int) (distance double)))
|
||||
|
||||
(fn by-distance ((pa (* const void)) (pb (* const void))) int
|
||||
(var wa (* const wanted) pa)
|
||||
(var wb (* const wanted) pb)
|
||||
(var da double (. wa distance))
|
||||
(var db double (. wb distance))
|
||||
(return (- (> da db) (< da db))))
|
||||
|
||||
;;; Requests the visible tiles, nearest the centre first, and draws
|
||||
;;; those that are ready.
|
||||
(fn draw-map ((a (* app))) void
|
||||
(var w int)
|
||||
(var h int)
|
||||
(window-size a (& w) (& h))
|
||||
(var s (* world-settings) (& (. a source settings)))
|
||||
(var x0 double (to-world-x a 0 w))
|
||||
(var z0 double (to-world-z a 0 h))
|
||||
(var x1 double (to-world-x a w w))
|
||||
(var z1 double (to-world-z a h h))
|
||||
(var rx0 int (cast (fmax 0 (floor (/ x0 REGION))) int))
|
||||
(var rz0 int (cast (fmax 0 (floor (/ z0 REGION))) int))
|
||||
(var rx1 int (cast (fmin (- (/ (. s size-x) REGION) 1) (floor (/ x1 REGION))) int))
|
||||
(var rz1 int (cast (fmin (- (/ (. s size-z) REGION) 1) (floor (/ z1 REGION))) int))
|
||||
(var count int (* (fmax 0 (+ (- rx1 rx0) 1)) (fmax 0 (+ (- rz1 rz0) 1))))
|
||||
(var list (* wanted) (malloc (* (+ count 1) (sizeof wanted))))
|
||||
(var n int 0)
|
||||
(for (var rz int rz0) (<= rz rz1) (++ rz)
|
||||
(for (var rx int rx0) (<= rx rx1) (++ rx)
|
||||
(var dx double (- (* (+ rx 0.5) REGION) (. a center-x)))
|
||||
(var dz double (- (* (+ rz 0.5) REGION) (. a center-z)))
|
||||
(= [list n] #(wanted : rx rz (+ (* dx dx) (* dz dz))))
|
||||
(++ n)))
|
||||
(qsort list n (sizeof wanted) by-distance)
|
||||
|
||||
(.begin-frame (. a tiles))
|
||||
(for (var i int 0) (< i n) (++ i)
|
||||
(var k tile-key)
|
||||
(= (. k map) (. a layer))
|
||||
(= (. k stratum) (. a stratum))
|
||||
(= (. k rx) (. [list i] rx))
|
||||
(= (. k rz) (. [list i] rz))
|
||||
(var tex (* SDL-Texture) (.request (. a tiles) (. a renderer) k))
|
||||
(if (! tex)
|
||||
(continue))
|
||||
(var sx0 float (roundf (to-screen a (* (. k rx) REGION) (. a center-x) w)))
|
||||
(var sz0 float (roundf (to-screen a (* (. k rz) REGION) (. a center-z) h)))
|
||||
(var sx1 float (roundf (to-screen a (* (+ (. k rx) 1) REGION) (. a center-x) w)))
|
||||
(var sz1 float (roundf (to-screen a (* (+ (. k rz) 1) REGION) (. a center-z) h)))
|
||||
(var dst SDL-FRect #(SDL-FRect : sx0 sz0 (- sx1 sx0) (- sz1 sz0)))
|
||||
(SDL-RenderTexture (. a renderer) tex NULL (& dst)))
|
||||
(= (. a pending) (.end-frame (. a tiles)))
|
||||
(free list))
|
||||
|
||||
;;; Spawn and the story locations, as small squares with their names.
|
||||
(fn draw-markers ((a (* app))) void
|
||||
(var w int)
|
||||
(var h int)
|
||||
(window-size a (& w) (& h))
|
||||
(var r (* SDL-Renderer) (. a renderer))
|
||||
(var g (* worldgen) (. a g))
|
||||
(SDL-SetRenderDrawColor r 255 255 255 255)
|
||||
(for (var i int -1) (< i (. g story-count)) (++ i)
|
||||
(var x double (/ (. a source settings size-x) 2.0))
|
||||
(var z double (/ (. a source settings size-z) 2.0))
|
||||
(var name (* const char) "spawn")
|
||||
(if (>= i 0)
|
||||
(do
|
||||
(= x (. [(. g stories) i] x))
|
||||
(= z (. [(. g stories) i] z))
|
||||
(= name (. [(. g stories) i] code))))
|
||||
(var sx float (to-screen a x (. a center-x) w))
|
||||
(var sy float (to-screen a z (. a center-z) h))
|
||||
(var box SDL-FRect #(SDL-FRect : (- sx 3) (- sy 3) 7 7))
|
||||
(SDL-RenderRect r (& box))
|
||||
(SDL-RenderDebugText r (+ sx 6) (- sy 4) name)))
|
||||
|
||||
;;; Panels ---------------------------------------------------------
|
||||
|
||||
(fn property-float ((ctx (* struct nk-context)) (name (* const char)) (v (* float))
|
||||
(lo float) (hi float) (step float)) void
|
||||
(nk-property-float ctx name lo v hi step (/ step 4)))
|
||||
|
||||
(fn draw-settings ((a (* app))) void
|
||||
(var ctx (* struct nk-context) (. a ctx))
|
||||
(var e (* world-settings) (& (. a edit settings)))
|
||||
(if (nk-begin ctx "World" (nk-rect 10 10 290 520)
|
||||
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE
|
||||
NK-WINDOW-SCALABLE))
|
||||
(do
|
||||
(nk-layout-row-dynamic ctx 22 1)
|
||||
(nk-label ctx "World or server config file" NK-TEXT-LEFT)
|
||||
(nk-layout-row-template-begin ctx 24)
|
||||
(nk-layout-row-template-push-dynamic ctx)
|
||||
(nk-layout-row-template-push-static ctx 50)
|
||||
(nk-layout-row-template-end ctx)
|
||||
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a world-path)
|
||||
(sizeof (. a world-path)) nk-filter-default)
|
||||
(if (nk-button-label ctx "Load")
|
||||
(load-world a))
|
||||
|
||||
(nk-layout-row-dynamic ctx 8 1)
|
||||
(nk-spacing ctx 1)
|
||||
(nk-layout-row-template-begin ctx 24)
|
||||
(nk-layout-row-template-push-static ctx 40)
|
||||
(nk-layout-row-template-push-dynamic ctx)
|
||||
(nk-layout-row-template-end ctx)
|
||||
(nk-label ctx "Seed" NK-TEXT-LEFT)
|
||||
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a edit seed-text)
|
||||
(sizeof (. a edit seed-text)) nk-filter-default)
|
||||
(nk-layout-row-dynamic ctx 22 1)
|
||||
(nk-property-int ctx "World width" 512 (& (. e size-x)) 8192000 512 512)
|
||||
(nk-property-int ctx "World length" 512 (& (. e size-z)) 8192000 512 512)
|
||||
(nk-property-int ctx "World height" 256 (& (. e size-y)) 1024 64 64)
|
||||
(property-float ctx "Landcover" (& (. e landcover)) 0 1 0.05)
|
||||
(property-float ctx "Ocean scale" (& (. e oceanscale)) 0.1 10 0.1)
|
||||
(property-float ctx "Landform scale" (& (. e landform-scale)) 0.1 4 0.1)
|
||||
(property-float ctx "Upheaval commonness" (& (. e upheaval-commonness)) 0 1 0.05)
|
||||
(property-float ctx "Geologic activity" (& (. e geologic-activity)) 0.01 1 0.05)
|
||||
(nk-property-int ctx "Polar-equator distance" 1000 (& (. e polar-equator-distance))
|
||||
1000000 1000 100)
|
||||
(property-float ctx "Global temperature" (& (. e global-temperature)) 0 5 0.1)
|
||||
(property-float ctx "Global precipitation" (& (. e global-precipitation)) 0 5 0.1)
|
||||
(property-float ctx "Story distance scale" (& (. e story-dist-scaling)) 0.05 2 0.05)
|
||||
(var current int 0)
|
||||
(for (var i int 0) (< i 5) (++ i)
|
||||
(if (== (strcmp (. e starting-climate) [climate-names i]) 0)
|
||||
(= current i)))
|
||||
(nk-layout-row-dynamic ctx 22 2)
|
||||
(nk-label ctx "Starting climate" NK-TEXT-LEFT)
|
||||
(var chosen int (nk-combo ctx climate-names 5 current 20 (nk-vec2 140 120)))
|
||||
(snprintf (. e starting-climate) 16 "%s" [climate-names chosen])
|
||||
|
||||
(nk-layout-row-dynamic ctx 26 2)
|
||||
(if (nk-button-label ctx "Apply")
|
||||
(do
|
||||
(var source world-source (. a edit))
|
||||
(= (. source settings seed) (world-seed (. source seed-text)))
|
||||
(apply a source)))
|
||||
(if (nk-button-label ctx "Go to spawn")
|
||||
(go-to-spawn a))))
|
||||
(nk-end ctx))
|
||||
|
||||
(fn draw-layers ((a (* app))) void
|
||||
(var ctx (* struct nk-context) (. a ctx))
|
||||
(var w int)
|
||||
(var h int)
|
||||
(window-size a (& w) (& h))
|
||||
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 220)
|
||||
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
|
||||
(do
|
||||
(nk-layout-row-dynamic ctx 20 1)
|
||||
(for (var i int 0) (< i 6) (++ i)
|
||||
(if (nk-option-label ctx [layer-names i] (== (. a layer) i))
|
||||
(= (. a layer) i)))
|
||||
(if (== (. a layer) MAP-STRATA)
|
||||
(do
|
||||
(var g (* worldgen) (. a g))
|
||||
(nk-layout-row-dynamic ctx 22 1)
|
||||
(= (. a stratum)
|
||||
(nk-combo ctx (cast (. g strata codes) (* * const char)) (. g strata count)
|
||||
(. a stratum) 20 (nk-vec2 200 300)))))))
|
||||
(nk-end ctx))
|
||||
|
||||
;;; A map's value at a block of its region.
|
||||
(fn sample ((m (* const int-map)) (rx int) (rz int) (bx int) (bz int)) int
|
||||
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
|
||||
(var px int (+ (. m pad-tl) (/ (* (- bx (* rx REGION)) inner) REGION)))
|
||||
(var pz int (+ (. m pad-tl) (/ (* (- bz (* rz REGION)) inner) REGION)))
|
||||
(return [(. m data) (+ (* pz (. m size)) px)]))
|
||||
|
||||
(fn draw-info ((a (* app))) void
|
||||
(var ctx (* struct nk-context) (. a ctx))
|
||||
(var w int)
|
||||
(var h int)
|
||||
(window-size a (& w) (& h))
|
||||
(var s (* world-settings) (& (. a source settings)))
|
||||
(var bx int (cast (floor (to-world-x a (. a mouse-x) w)) int))
|
||||
(var bz int (cast (floor (to-world-z a (. a mouse-y) h)) int))
|
||||
(var inside bool (&& (>= bx 0) (>= bz 0) (< bx (. s size-x)) (< bz (. s size-z))))
|
||||
(var rx int (/ bx REGION))
|
||||
(var rz int (/ bz REGION))
|
||||
(if (&& inside (|| (! (. a hover valid)) (!= (. a hover rx) rx) (!= (. a hover rz) rz)))
|
||||
(do
|
||||
(release-hover a)
|
||||
(worldgen/region (. a g) rx rz (& (. a hover maps)))
|
||||
(= (. a hover valid) true)
|
||||
(= (. a hover rx) rx)
|
||||
(= (. a hover rz) rz)))
|
||||
(if (nk-begin ctx "Info" (nk-rect 10 (- h 230) 290 220)
|
||||
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
|
||||
(do
|
||||
(nk-layout-row-dynamic ctx 18 1)
|
||||
(nk-labelf ctx NK-TEXT-LEFT "x %d z %d (from centre %d, %d)" bx bz
|
||||
(- bx (/ (. s size-x) 2)) (- bz (/ (. s size-z) 2)))
|
||||
(nk-labelf ctx NK-TEXT-LEFT "region %d, %d zoom %.3g blocks/pt" rx rz (. a zoom))
|
||||
(if (&& inside (. a hover valid))
|
||||
(do
|
||||
(var g (* worldgen) (. a g))
|
||||
(var m (* region-maps) (& (. a hover maps)))
|
||||
(var lf int (sample (& (. m landform)) rx rz bx bz))
|
||||
(nk-labelf ctx NK-TEXT-LEFT "landform %s" (. [(. g landform-table all) lf] code))
|
||||
(var pv int (sample (& (. m geoprov)) rx rz bx bz))
|
||||
(nk-labelf ctx NK-TEXT-LEFT "province %s" [(. g provinces codes) pv])
|
||||
(var c int (sample (& (. m climate)) rx rz bx bz))
|
||||
(nk-labelf ctx NK-TEXT-LEFT "climate %d C at sea level, rain %d, activity %d"
|
||||
(scaled-temperature (& (>> c 16) 255)) (& (>> c 8) 255) (& c 255))
|
||||
(nk-labelf ctx NK-TEXT-LEFT "ocean %d upheaval %d"
|
||||
(sample (& (. m ocean)) rx rz bx bz)
|
||||
(sample (& (. m upheaval)) rx rz bx bz))))
|
||||
(nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending))
|
||||
(nk-label ctx (. a status) NK-TEXT-LEFT)))
|
||||
(nk-end ctx))
|
||||
|
||||
;;; Events ---------------------------------------------------------
|
||||
|
||||
;;; Zooms by FACTOR keeping the block under the cursor where it is.
|
||||
(fn zoom-at ((a (* app)) (factor double)) void
|
||||
(var w int)
|
||||
(var h int)
|
||||
(window-size a (& w) (& h))
|
||||
(var wx double (to-world-x a (. a mouse-x) w))
|
||||
(var wz double (to-world-z a (. a mouse-y) h))
|
||||
(= (. a zoom) (fmin MAX-ZOOM (fmax MIN-ZOOM (* (. a zoom) factor))))
|
||||
(= (. a center-x) (- wx (* (- (. a mouse-x) (/ w 2.0)) (. a zoom))))
|
||||
(= (. a center-z) (- wz (* (- (. a mouse-y) (/ h 2.0)) (. a zoom)))))
|
||||
|
||||
(fn handle-event ((a (* app)) (event (* SDL-Event))) bool
|
||||
(var type u32 (. event type))
|
||||
(if (== type SDL-EVENT-QUIT)
|
||||
(return false))
|
||||
(SDL-ConvertEventToRenderCoordinates (. a renderer) event)
|
||||
(if (== type SDL-EVENT-MOUSE-MOTION)
|
||||
(do
|
||||
(if (. a dragging)
|
||||
(do
|
||||
(-= (. a center-x) (* (. event motion xrel) (. a zoom)))
|
||||
(-= (. a center-z) (* (. event motion yrel) (. a zoom)))))
|
||||
(= (. a mouse-x) (. event motion x))
|
||||
(= (. a mouse-y) (. event motion y)))
|
||||
(&& (== type SDL-EVENT-MOUSE-BUTTON-DOWN) (! (. a ui-hovered))
|
||||
(== (. event button button) SDL-BUTTON-LEFT))
|
||||
(= (. a dragging) true)
|
||||
(== type SDL-EVENT-MOUSE-BUTTON-UP)
|
||||
(= (. a dragging) false)
|
||||
(&& (== type SDL-EVENT-MOUSE-WHEEL) (! (. a ui-hovered)))
|
||||
(zoom-at a (pow 1.25 (- (. event wheel y)))))
|
||||
(nk-sdl-handle-event (. a ctx) event)
|
||||
(return true))
|
||||
|
||||
(fn save-screenshot ((a (* app))) bool
|
||||
(var shot (* SDL-Surface) (SDL-RenderReadPixels (. a renderer) NULL))
|
||||
(if (! shot)
|
||||
(return false))
|
||||
(var rgba (* SDL-Surface) (SDL-ConvertSurface shot SDL-PIXELFORMAT-ABGR8888))
|
||||
(SDL-DestroySurface shot)
|
||||
(if (! rgba)
|
||||
(return false))
|
||||
(var ok bool (!= (stbi-write-png (. a screenshot) (. rgba w) (. rgba h) 4 (. rgba pixels)
|
||||
(. rgba pitch)) 0))
|
||||
(SDL-DestroySurface rgba)
|
||||
(return ok))
|
||||
|
||||
;;; Main -----------------------------------------------------------
|
||||
|
||||
(pub fn main ((argc int) (argv [* char])) int
|
||||
(var a (* app) (calloc 1 (sizeof app)))
|
||||
(snprintf (. a assets-dir) (sizeof (. a assets-dir)) "assets")
|
||||
(snprintf (. a world-path) (sizeof (. a world-path)) "serverconfig.json")
|
||||
(for (var i int 1) (< i argc) (++ i)
|
||||
(var more bool (< (+ i 1) argc))
|
||||
(if (&& (== (strcmp [argv i] "--assets") 0) more)
|
||||
(do
|
||||
(++ i)
|
||||
(snprintf (. a assets-dir) (sizeof (. a assets-dir)) "%s" [argv i]))
|
||||
(&& (== (strcmp [argv i] "--zoom") 0) more)
|
||||
(do (++ i) (= (. a zoom) (atof [argv i])))
|
||||
(&& (== (strcmp [argv i] "--layer") 0) more)
|
||||
(do (++ i) (= (. a layer) (atoi [argv i])))
|
||||
(&& (== (strcmp [argv i] "--screenshot") 0) more)
|
||||
(do (++ i) (= (. a screenshot) [argv i]))
|
||||
(snprintf (. a world-path) (sizeof (. a world-path)) "%s" [argv i])))
|
||||
|
||||
(if (! (SDL-Init SDL-INIT-VIDEO))
|
||||
(return (fail "SDL_Init")))
|
||||
|
||||
(var window (* SDL-Window) NULL)
|
||||
(var renderer (* SDL-Renderer) NULL)
|
||||
(if (! (SDL-CreateWindowAndRenderer "vs-map-viewer" WINDOW-WIDTH WINDOW-HEIGHT
|
||||
(| SDL-WINDOW-RESIZABLE
|
||||
SDL-WINDOW-HIGH-PIXEL-DENSITY)
|
||||
(& window) (& renderer)))
|
||||
(| SDL-WINDOW-RESIZABLE SDL-WINDOW-HIGH-PIXEL-DENSITY)
|
||||
(& (. a window)) (& (. a renderer))))
|
||||
(return (fail "SDL_CreateWindowAndRenderer")))
|
||||
(SDL-SetRenderVSync renderer 1)
|
||||
(SDL-SetRenderVSync (. a renderer) 1)
|
||||
(var scale float (SDL-GetWindowDisplayScale (. a window)))
|
||||
(SDL-SetRenderScale (. a renderer) scale scale)
|
||||
(= (. a ctx) (nk-sdl-init (. a window) (. a renderer) (nk-sdl-allocator)))
|
||||
(setup-font (. a ctx) scale)
|
||||
|
||||
(var scale float (SDL-GetWindowDisplayScale window))
|
||||
(SDL-SetRenderScale renderer scale scale)
|
||||
|
||||
(var ctx (* struct nk-context) (nk-sdl-init window renderer (nk-sdl-allocator)))
|
||||
(setup-font ctx scale)
|
||||
(noise-init)
|
||||
(var source world-source)
|
||||
(if (! (world-source/load (. a world-path) (& source)))
|
||||
(do
|
||||
(= (. source settings) (world-settings/defaults))
|
||||
(snprintf (. source seed-text) (sizeof (. source seed-text)) "0")))
|
||||
(var workers int (- (SDL-GetNumLogicalCPUCores) 1))
|
||||
(= (. a tiles) (tiles/start (. source settings) (. a assets-dir) workers))
|
||||
(if (<= (. a zoom) 0)
|
||||
(= (. a zoom) 4))
|
||||
(if (! (apply a source))
|
||||
(do
|
||||
(fprintf stderr "%s\n" (. a status))
|
||||
(return 1)))
|
||||
(go-to-spawn a)
|
||||
|
||||
(var running bool true)
|
||||
(var last-ns u64 (SDL-GetTicksNS))
|
||||
(while running
|
||||
(var now-ns u64 (SDL-GetTicksNS))
|
||||
(var frame-ms double (/ (cast (- now-ns last-ns) double) 1e6))
|
||||
(= last-ns now-ns)
|
||||
;; Nothing left to generate: sleep until something happens.
|
||||
(if (&& (== (. a pending) 0) (! (. a dragging)) (! (. a screenshot)))
|
||||
(SDL-WaitEventTimeout NULL 500))
|
||||
(var event SDL-Event)
|
||||
(nk-input-begin ctx)
|
||||
(nk-input-begin (. a ctx))
|
||||
(while (SDL-PollEvent (& event))
|
||||
(if (== (. event type) SDL-EVENT-QUIT)
|
||||
(= running false))
|
||||
(SDL-ConvertEventToRenderCoordinates renderer (& event))
|
||||
(nk-sdl-handle-event ctx (& event)))
|
||||
(nk-sdl-update-TextInput ctx)
|
||||
(nk-input-end ctx)
|
||||
(if (! (handle-event a (& event)))
|
||||
(= running false)))
|
||||
(nk-sdl-update-TextInput (. a ctx))
|
||||
(nk-input-end (. a ctx))
|
||||
|
||||
(draw-ui ctx frame-ms)
|
||||
(draw-settings a)
|
||||
(draw-layers a)
|
||||
(draw-info a)
|
||||
(= (. a ui-hovered) (|| (nk-window-is-any-hovered (. a ctx)) (nk-item-is-any-active (. a ctx))))
|
||||
|
||||
(SDL-SetRenderDrawColor renderer 24 32 40 255)
|
||||
(SDL-RenderClear renderer)
|
||||
(nk-sdl-render ctx NK-ANTI-ALIASING-ON)
|
||||
(SDL-RenderPresent renderer))
|
||||
(SDL-SetRenderDrawColor (. a renderer) 24 32 40 255)
|
||||
(SDL-RenderClear (. a renderer))
|
||||
(draw-map a)
|
||||
(draw-markers a)
|
||||
(nk-sdl-render (. a ctx) NK-ANTI-ALIASING-ON)
|
||||
(if (&& (. a screenshot) (== (. a pending) 0))
|
||||
(do
|
||||
(if (! (save-screenshot a))
|
||||
(fprintf stderr "%s: %s\n" (. a screenshot) (SDL-GetError)))
|
||||
(= running false)))
|
||||
(SDL-RenderPresent (. a renderer)))
|
||||
|
||||
(nk-sdl-shutdown ctx)
|
||||
(SDL-DestroyRenderer renderer)
|
||||
(SDL-DestroyWindow window)
|
||||
(.stop (. a tiles))
|
||||
(release-hover a)
|
||||
(worldgen/free (. a g))
|
||||
(nk-sdl-shutdown (. a ctx))
|
||||
(SDL-DestroyRenderer (. a renderer))
|
||||
(SDL-DestroyWindow (. a window))
|
||||
(SDL-Quit)
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user