;;; Map tiles for the viewer: one map region of one map, coloured, ;;; generated by a pool of worker threads and drawn as SDL textures. ;;; ;;; Only what the view asks for is generated. Each frame the viewer ;;; says which tiles it can see, nearest the centre first; that list ;;; replaces the work queue, so panning away drops work nobody needs. ;;; The cache keeps recently drawn tiles and evicts the least recently ;;; drawn when full. ;;; ;;; Every worker owns its own worldgen, because the layers keep RNG ;;; state. A settings change bumps `generation'; workers rebuild their ;;; generator, and results made under an older one are dropped. (pub include pthread.h SDL3/SDL.h) (include stdio.h stdlib.h string.h math.h) ;; TODO sex#58: import only the modules used here once an import brings ;; the types its interface needs; until then, every dependency, in ;; dependency order. (import json) (import maps) (import lcg) (import noise) (pub import shape) (pub import worldgen) (import fastnoise) (pub import rivers) (import terra) (import strata) (import ores) (pub define MAX-TILES 65536) ;;; Not worldgen maps: Rivers' distance to the nearest river, the ;;; terrain height, and the rock at the surface, at a depth or at a Y, ;;; or where one rock lies at any depth; and the prospecting pick's ;;; reading of one mineral. (pub define LAYER-RIVERS 6) (pub define LAYER-HEIGHT 7) (pub define LAYER-ROCK 8) (pub define LAYER-MINERAL 9) (pub define ROCK-AT-SURFACE 0) (pub define ROCK-AT-DEPTH 1) (pub define ROCK-AT-Y 2) ;;; The param's value is then the rock's index. (pub define ROCK-ANY-DEPTH 3) ;;; Mineral tile values: this plus the reading's category. (pub define MINERAL-VALUE-BASE 64) ;;; Rock tile values besides rock indices. (pub define VALUE-WATER 253) (pub define VALUE-AIR 254) ;;; River tiles are sampled at a resolution the viewer picks by zoom, ;;; in the key's `stratum': a column costs too much to sample them all ;;; for an overview. (pub define MAX-WORKERS 32) (define TILE-FREE 0) (define TILE-QUEUED 1) (define TILE-WORKING 2) (define TILE-READY 3) (define TILE-DRAWN 4) ;;; STRATUM is the stratum for MAP-STRATA, the samples per region side ;;; for the sampled layers; PARAM is the rock layer's mode * 1024 + value, ;;; or the mineral layer's mineral. (pub record tile-key ((map int) (stratum int) (rx int) (rz int) (param int))) ;;; Rock and mineral tiles keep their VALUES, rock indices or reading ;;; categories, and are coloured from the palette on upload, so a new ;;; palette only re-uploads them. (pub record tile ((key tile-key) (state int) (generation int) (size int) (pixels (* u32)) (values (* u8)) (texture (* SDL-Texture)) (last-used u64))) (pub record tiles ((lock pthread-mutex-t) (wake pthread-cond-t) (slots [tile MAX-TILES]) (index cell-map) (queue [int MAX-TILES]) (queue-len int) (queue-pos int) (free-slots [int MAX-TILES]) (free-count int) (settings world-settings) (rivers river-config) (assets-dir [char 1024]) (generation int) (workers [pthread-t MAX-WORKERS]) (worker-count int) (quit bool) (frame u64) (waiting int) (palette [u32 256]))) (fn key-hash ((k tile-key)) u64 ;; one-to-one: region x and z 16 bits each, map 4, stratum or ;; resolution 10, param 18 ;; TODO sex#47: write the field masks in hex once there are hex ;; literals. (return (c-bit-or (cast (& (. k rx) 65535) u64) (<< (cast (& (. k rz) 65535) u64) 16) (<< (cast (& (. k map) 15) u64) 32) (<< (cast (& (. k stratum) 1023) u64) 36) (<< (cast (& (. k param) 262143) u64) 46)))) ;;; The tile index is a cell-map keyed by (x, z): the hash is split ;;; over the two halves. ;; TODO sex#47: write the low-half masks below in hex once there are hex ;; literals. (fn index-get ((t (* tiles)) (k tile-key)) int (var h u64 (key-hash k)) (return (.get (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int)))) (fn index-put ((t (* tiles)) (k tile-key) (slot int)) void (var h u64 (key-hash k)) (.put (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int) slot)) (fn index-remove ((t (* tiles)) (k tile-key)) void (var h u64 (key-hash k)) (.remove (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int))) ;;; Colouring ----------------------------------------------------- ;;; A distinct, stable colour per landform index. (fn landform-colour ((i u32)) u32 ;; TODO sex#47: write the multiplier (0x9E3779B1) and masks in hex ;; once there are hex literals. (var h u32 (* (+ i 1) (cast 2654435761 u32))) (return (c-bit-or (<< (+ 48 (& (>> h 24) 191)) 16) (<< (+ 48 (& (>> h 16) 191)) 8) (+ 48 (& (>> h 8) 191))))) (fn lerp-colour ((a u32) (b u32) (t int)) u32 (var r u32 0) (for (var shift int 0) (< shift 24) (+= shift 8) (var ca int (& (>> a shift) 255)) (var cb int (& (>> b shift) 255)) (c-bit-or= r (<< (cast (+ ca (/ (* (- cb ca) t) 255)) u32) shift))) (return r)) (fn colour-of ((g (* const worldgen)) (map int) (v int)) u32 ;; TODO sex#47: write the colours and masks in hex once there are hex ;; literals. (switch map ;; landforms.json gives most landforms the same green, so these ;; are coloured by index instead (case MAP-LANDFORM (return (landform-colour (cast v u32)))) (case MAP-PROVINCE (return (if (&& (>= v 0) (< v (. g provinces count))) [(. g provinces colours) v] 0))) (case MAP-CLIMATE (return (& (cast v u32) 16777215))) (case MAP-OCEAN (return (lerp-colour 6326847 1919884 v))) (default (if (< v 0) (= v 0)) (if (> v 255) (= v 255)) (return (* (cast v u32) 65793))))) ;;; The tile's inner pixels, coloured, as ARGB. (fn render ((g (* worldgen)) (k tile-key) (size (* int))) (* u32) (var m int-map) (worldgen/map g (. k map) (. k stratum) (. k rx) (. k rz) (& m)) (var inner int (- (. m size) (. m pad-tl) (. m pad-br))) (var px (* u32) (malloc (* inner inner (sizeof u32)))) (for (var z int 0) (< z inner) (++ z) (for (var x int 0) (< x inner) (++ x) (var v int [(. m data) (+ (* (+ z (. m pad-tl)) (. m size)) x (. m pad-tl))]) ;; TODO sex#47: write the opaque alpha in hex once there are hex ;; literals. (= [px (+ (* z inner) x)] (c-bit-or 4278190080 (colour-of g (. k map) v))))) (free (. m data)) (= (* size) inner) (return px)) ;;; What a worker keeps between river tiles, for one generation: the ;;; last few plates it built (a wide view spans several, and its tiles ;;; come in any order), and the sampler. (define CACHED-PLATES 4) (record river-cache ((plates [* river-plate 4]) (used [u64 4]) (clock u64) (sampler river-sampler) (segments (* int)) (segment-cap int))) (fn release-river-cache ((c (* river-cache))) void (for (var i int 0) (< i CACHED-PLATES) (++ i) (if [(. c plates) i] (river-plate/free [(. c plates) i]))) (free (. c segments)) (memset c 0 (sizeof river-cache))) (fn cached-plate ((c (* river-cache)) (g (* worldgen)) (config (* const river-config)) (px int) (pz int)) (* river-plate) (++ (. c clock)) (var oldest int 0) (for (var i int 0) (< i CACHED-PLATES) (++ i) (var p (* river-plate) [(. c plates) i]) (if (&& p (== (. p px) px) (== (. p pz) pz)) (do (= [(. c used) i] (. c clock)) (return p))) (if (< [(. c used) i] [(. c used) oldest]) (= oldest i))) (if [(. c plates) oldest] (river-plate/free [(. c plates) oldest])) (var p (* river-plate) (river-plate/make (* config) (& (. g oceans)) (. g settings size-y) px pz)) (= [(. c plates) oldest] p) (= [(. c used) oldest] (. c clock)) (if (> (+ (. p segment-count) 1) (. c segment-cap)) (do (= (. c segment-cap) (+ (. p segment-count) 1)) (= (. c segments) (realloc (. c segments) (* (. c segment-cap) (sizeof int)))))) (return p)) ;; TODO sex#47: write the colours in hex once there are hex literals. (fn river-colour ((distance int) (valley int) (ocean bool)) u32 (if ocean (return 1784427)) (if (== distance 0) (return 3043288)) (if (< distance valley) (return (lerp-colour 8369128 5143098 (/ (* distance 255) valley)))) (return 4022062)) (fn render-rivers ((g (* worldgen)) (cache (* river-cache)) (config (* const river-config)) (k tile-key) (size (* int))) (* u32) (var res int (. k stratum)) (var step int (/ 512 res)) (var px (* u32) (malloc (* res res (sizeof u32)))) (var per-plate int (river-plate/chunks config)) (var valley int (cast (. config max-valley-width) int)) ;; chunk by chunk, so each chunk's segment list is found once (for (var cz int 0) (< cz 16) (++ cz) (for (var cx int 0) (< cx 16) (++ cx) (var chunk-x int (+ (* (. k rx) 16) cx)) (var chunk-z int (+ (* (. k rz) 16) cz)) (var plate-x int (/ chunk-x per-plate)) (var plate-z int (/ chunk-z per-plate)) (var p (* river-plate) (cached-plate cache g config plate-x plate-z)) (var count int (river-plate/chunk-segments p chunk-x chunk-z (. cache segments))) (for (var dz int 0) (< dz (/ 32 step)) (++ dz) (for (var dx int 0) (< dx (/ 32 step)) (++ dx) (var x int (+ (* chunk-x 32) (* dx step) (/ step 2))) (var z int (+ (* chunk-z 32) (* dz step) (/ step 2))) (var sample river-sample (river-sampler/sample (& (. cache sampler)) p (. cache segments) count x z)) (var d int (cast (fmin (. sample distance) 65535) int)) (var ox int (+ (* cx (/ 32 step)) dx)) (var oz int (+ (* cz (/ 32 step)) dz)) (= [px (+ (* oz res) ox)] ;; TODO sex#47: write the opaque alpha in hex once there ;; are hex literals. (c-bit-or 4278190080 (river-colour d valley (river-plate/ocean-at p x z)))))))) (= (* size) res) (return px)) ;;; Terrain height, shaded: water by depth under sea level, land by ;;; height, lit from the north-west. ;; TODO sex#47: write the colours in hex once there are hex literals. (fn height-colour ((h int) (west int) (north int) (sea int) (size-y int)) u32 ;; TODO sex#59: clamp the depth inline once a conditional operand ;; keeps its parentheses. (var depth int (- sea h)) (if (> depth 40) (= depth 40)) (if (< h sea) (return (lerp-colour 6400472 1186659 (/ (* depth 255) 40)))) (var t int (/ (* (- h sea) 255) (- size-y sea))) (var base u32 (if (< t 32) (lerp-colour 5605944 8299066 (* t 8)) (if (< t 96) (lerp-colour 8299066 9922899 (/ (* (- t 32) 255) 64)) (lerp-colour 9922899 15263976 (/ (* (- t 96) 255) 159))))) (var slope int (+ (- h west) (- h north))) (var light int (+ 128 (* slope 24))) (if (< light 0) (= light 0)) (if (> light 255) (= light 255)) (if (> light 128) (return (lerp-colour base 16777215 (/ (* (- light 128) 160) 127)))) (return (lerp-colour 0 base (+ 96 (/ (* light 159) 128))))) (fn render-heights ((tr (* terra)) (k tile-key) (size (* int))) (* u32) (var res int (. k stratum)) (var step int (/ 512 res)) (var hs (* int) (malloc (* res res (sizeof int)))) (var chunk [u16 1024]) (var per-chunk int (/ 32 step)) (if (< per-chunk 1) (= per-chunk 1)) (for (var cz int 0) (< cz 16) (++ cz) (for (var cx int 0) (< cx 16) (++ cx) (if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0)) (continue)) (terra/chunk-heights tr (+ (* (. k rx) 16) cx) (+ (* (. k rz) 16) cz) (if (> step 32) 32 step) chunk) (for (var dz int 0) (< dz per-chunk) (++ dz) (for (var dx int 0) (< dx per-chunk) (++ dx) (= [hs (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)] [chunk (+ (* dz step 32) (* dx step))]))))) (var px (* u32) (malloc (* res res (sizeof u32)))) (for (var z int 0) (< z res) (++ z) (for (var x int 0) (< x res) (++ x) (var h int [hs (+ (* z res) x)]) (var west int (if (> x 0) [hs (+ (* z res) x -1)] h)) (var north int (if (> z 0) [hs (+ (* (- z 1) res) x)] h)) (= [px (+ (* z res) x)] ;; TODO sex#47: write the opaque alpha in hex once there are ;; hex literals. (c-bit-or 4278190080 (height-colour h west north (. tr sea-level) (. tr size-y)))))) (free hs) (= (* size) res) (return px)) ;;; The rock tile: per sample, the rock at the surface, at a depth ;;; below it, or at an absolute Y (water or air above the surface); or, ;;; at any depth, the rock where the column holds it at all and air ;;; where it doesn't. (fn render-rocks ((tr (* terra)) (rocks (* strata)) (k tile-key) (size (* int))) (* u8) (var res int (. k stratum)) (var step int (/ 512 res)) (var mode int (/ (. k param) 1024)) (var value int (% (. k param) 1024)) (var out (* u8) (malloc (* res res))) (var heights [u16 1024]) (var column (* u8) (malloc (+ (. tr size-y) 1))) (var per-chunk int (/ 32 step)) (if (< per-chunk 1) (= per-chunk 1)) (for (var cz int 0) (< cz 16) (++ cz) (for (var cx int 0) (< cx 16) (++ cx) (if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0)) (continue)) (var chunk-x int (+ (* (. k rx) 16) cx)) (var chunk-z int (+ (* (. k rz) 16) cz)) (terra/chunk-heights tr chunk-x chunk-z (if (> step 32) 32 step) heights) (for (var dz int 0) (< dz per-chunk) (++ dz) (for (var dx int 0) (< dx per-chunk) (++ dx) (var lx int (* dx step)) (var lz int (* dz step)) (var h int [heights (+ (* lz 32) lx)]) (strata/column rocks chunk-x chunk-z lx lz h column) (var y int h) (if (== mode ROCK-AT-DEPTH) (= y (- h value)) (== mode ROCK-AT-Y) (= y value)) (var v u8 VALUE-AIR) (if (== mode ROCK-ANY-DEPTH) (for (var ry int 1) (<= ry h) (++ ry) (if (== [column ry] value) (= v (cast value u8)))) (< y 0) (= v ROCK-MANTLE) (<= y h) (= v [column y]) (< y (. tr sea-level)) (= v VALUE-WATER)) (= [out (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)] v))))) (free column) (= (* size) res) (return out)) ;;; The mineral tile: per sample, MINERAL-VALUE-BASE plus the category ;;; of the pick's reading there. (fn render-minerals ((tr (* terra)) (rocks (* strata)) (o (* ores)) (k tile-key) (size (* int))) (* u8) (var res int (. k stratum)) (var step int (/ 512 res)) (var map int-map) (ores/map o (. k param) (. k rx) (. k rz) (& map)) (var out (* u8) (malloc (* res res))) (var heights [u16 1024]) (var column (* u8) (malloc (+ (. tr size-y) 1))) (var per-chunk int (/ 32 step)) (if (< per-chunk 1) (= per-chunk 1)) (for (var cz int 0) (< cz 16) (++ cz) (for (var cx int 0) (< cx 16) (++ cx) (if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0)) (continue)) (var chunk-x int (+ (* (. k rx) 16) cx)) (var chunk-z int (+ (* (. k rz) 16) cz)) (terra/chunk-heights tr chunk-x chunk-z (if (> step 32) 32 step) heights) (for (var dz int 0) (< dz per-chunk) (++ dz) (for (var dx int 0) (< dx per-chunk) (++ dx) (var lx int (* dx step)) (var lz int (* dz step)) (var h int [heights (+ (* lz 32) lx)]) (strata/column rocks chunk-x chunk-z lx lz h column) (var r mineral-reading (ores/reading o (. k param) (& map) (+ (* chunk-x 32) lx) (+ (* chunk-z 32) lz) column h)) (= [out (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)] (cast (+ MINERAL-VALUE-BASE (ores/category (. r total))) u8)))))) (free column) (free (. map data)) (= (* size) res) (return out)) ;;; Workers ------------------------------------------------------- (fn worker-main ((arg (* void))) (* void) (var t (* tiles) arg) (var g (* worldgen) NULL) (var g-generation int -1) (var cache river-cache) (memset (& cache) 0 (sizeof cache)) (var tr (* terra) NULL) (var rocks (* strata) NULL) (var minerals (* ores) NULL) (pthread-mutex-lock (& (. t lock))) (while (! (. t quit)) (if (>= (. t queue-pos) (. t queue-len)) (do (pthread-cond-wait (& (. t wake)) (& (. t lock))) (continue))) (var slot int [(. t queue) (. t queue-pos)]) (++ (. t queue-pos)) (var tl (* tile) (& [(. t slots) slot])) (if (!= (. tl state) TILE-QUEUED) (continue)) (= (. tl state) TILE-WORKING) (var key tile-key (. tl key)) (var generation int (. t generation)) (var settings world-settings (. t settings)) (var rivers river-config (. t rivers)) (pthread-mutex-unlock (& (. t lock))) (if (!= g-generation generation) (do (if tr (terra/free tr)) (= tr NULL) (if minerals (ores/free minerals)) (= minerals NULL) (if rocks (strata/free rocks)) (= rocks NULL) (if g (worldgen/free g)) (release-river-cache (& cache)) (= g (worldgen/make settings (. t assets-dir))) (if g (= tr (terra/make g rivers))) (if g (= rocks (strata/make g (. t assets-dir)))) (if rocks (= minerals (ores/make settings (. t assets-dir) rocks))) (= (. cache sampler) (river-sampler/make (& rivers) (. settings size-y))) (= g-generation generation))) (var size int 0) (var px (* u32) NULL) (var values (* u8) NULL) (if (&& g (== (. key map) LAYER-RIVERS)) (= px (render-rivers g (& cache) (& rivers) key (& size))) (&& tr (== (. key map) LAYER-HEIGHT)) (= px (render-heights tr key (& size))) (&& tr rocks (== (. key map) LAYER-ROCK)) (= values (render-rocks tr rocks key (& size))) (&& tr rocks minerals (== (. key map) LAYER-MINERAL)) (= values (render-minerals tr rocks minerals key (& size))) g (= px (render g key (& size)))) (pthread-mutex-lock (& (. t lock))) (if (&& (|| px values) (== generation (. t generation)) (== (. tl state) TILE-WORKING)) (do (= (. tl pixels) px) (= (. tl values) values) (= (. tl size) size) (= (. tl state) TILE-READY)) (do (free px) (free values) (if (&& (== generation (. t generation)) (== (. tl state) TILE-WORKING)) (= (. tl state) TILE-QUEUED))))) (pthread-mutex-unlock (& (. t lock))) (if minerals (ores/free minerals)) (if rocks (strata/free rocks)) (if tr (terra/free tr)) (if g (worldgen/free g)) (release-river-cache (& cache)) (return NULL)) (ops tiles (pub fn start ((settings world-settings) (rivers river-config) (assets-dir (* const char)) (workers int)) (* tiles) (var t (* tiles) (calloc 1 (sizeof tiles))) (pthread-mutex-init (& (. t lock)) NULL) (pthread-cond-init (& (. t wake)) NULL) (= (. t index) (cell-map/make)) (for (var i int 0) (< i MAX-TILES) (++ i) (= [(. t free-slots) i] (- MAX-TILES 1 i))) (= (. t free-count) MAX-TILES) (= (. t settings) settings) (= (. t rivers) rivers) (snprintf (. t assets-dir) (sizeof (. t assets-dir)) "%s" assets-dir) (if (> workers MAX-WORKERS) (= workers MAX-WORKERS)) (if (< workers 1) (= workers 1)) (= (. t worker-count) workers) (for (var i int 0) (< i workers) (++ i) (pthread-create (& [(. t workers) i]) NULL worker-main t)) (return t)) ;; Empties the cache. Call with the lock held, from the main thread. (fn clear ((self (* tiles))) void (for (var i int 0) (< i MAX-TILES) (++ i) (var tl (* tile) (& [(. self slots) i])) (if (. tl texture) (SDL-DestroyTexture (. tl texture))) (free (. tl pixels)) (free (. tl values)) (memset tl 0 (sizeof tile))) (.release (& (. self index))) (= (. self index) (cell-map/make)) (= (. self queue-len) 0) (= (. self queue-pos) 0) (for (var i int 0) (< i MAX-TILES) (++ i) (= [(. self free-slots) i] (- MAX-TILES 1 i))) (= (. self free-count) MAX-TILES)) ;; New settings: everything cached was made with the old ones. (pub fn set-settings ((self (* tiles)) (settings world-settings) (rivers river-config)) void (pthread-mutex-lock (& (. self lock))) (= (. self settings) settings) (= (. self rivers) rivers) (++ (. self generation)) (.clear self) (pthread-mutex-unlock (& (. self lock)))) ;; A free slot, or the least recently drawn finished one. (fn take-slot ((self (* tiles))) int (if (> (. self free-count) 0) (do (-- (. self free-count)) (return [(. self free-slots) (. self free-count)]))) (var best int -1) (var best-used u64 UINT64_MAX) (for (var i int 0) (< i MAX-TILES) (++ i) (var tl (* tile) (& [(. self slots) i])) (if (&& (|| (== (. tl state) TILE-READY) (== (. tl state) TILE-DRAWN) (== (. tl state) TILE-QUEUED)) (< (. tl last-used) best-used) (< (. tl last-used) (. self frame))) (do (= best i) (= best-used (. tl last-used))))) (if (< best 0) (return -1)) (var tl (* tile) (& [(. self slots) best])) (index-remove self (. tl key)) (if (. tl texture) (SDL-DestroyTexture (. tl texture))) (free (. tl pixels)) (free (. tl values)) (memset tl 0 (sizeof tile)) (return best)) (pub fn begin-frame ((self (* tiles))) void (pthread-mutex-lock (& (. self lock))) (++ (. self frame)) (= (. self queue-len) 0) (= (. self queue-pos) 0) (= (. self waiting) 0)) ;; The texture for KEY if it is ready, and VALUES and SIZE if it is a ;; value tile (both may be NULL); else NULL and the tile is queued ;; behind those requested earlier this frame. (pub fn request ((self (* tiles)) (renderer (* SDL-Renderer)) (k tile-key) (values (* * u8)) (size (* int))) (* SDL-Texture) (var slot int (index-get self k)) (if (< slot 0) (do (= slot (.take-slot self)) (if (< slot 0) (return NULL)) (var tl (* tile) (& [(. self slots) slot])) (= (. tl key) k) (= (. tl state) TILE-QUEUED) (index-put self k slot))) (var tl (* tile) (& [(. self slots) slot])) (= (. tl last-used) (. self frame)) (if (== (. tl state) TILE-READY) (do (if (. tl values) (do (var n int (* (. tl size) (. tl size))) (= (. tl pixels) (malloc (* n (sizeof u32)))) (for (var i int 0) (< i n) (++ i) (= [(. tl pixels) i] [(. self palette) [(. tl values) i]])))) (= (. tl texture) (SDL-CreateTexture renderer SDL-PIXELFORMAT-ARGB8888 SDL-TEXTUREACCESS-STATIC (. tl size) (. tl size))) (SDL-SetTextureScaleMode (. tl texture) SDL-SCALEMODE-NEAREST) (SDL-UpdateTexture (. tl texture) NULL (. tl pixels) (* (. tl size) 4)) (free (. tl pixels)) (= (. tl pixels) NULL) (= (. tl state) TILE-DRAWN))) (if (== (. tl state) TILE-DRAWN) (do ;; a value tile's values, for whoever wants to count them (if values (= (* values) (. tl values))) (if size (= (* size) (. tl size))) (return (. tl texture)))) (++ (. self waiting)) (if (&& (== (. tl state) TILE-QUEUED) (< (. self queue-len) MAX-TILES)) (do (= [(. self queue) (. self queue-len)] slot) (++ (. self queue-len)))) (return NULL)) ;; A new palette for value tiles: drawn ones are coloured again from ;; the values they kept. Main thread only. (pub fn set-palette ((self (* tiles)) (palette (* const u32))) void (pthread-mutex-lock (& (. self lock))) (memcpy (. self palette) palette (sizeof (. self palette))) (for (var i int 0) (< i MAX-TILES) (++ i) (var tl (* tile) (& [(. self slots) i])) (if (&& (. tl values) (== (. tl state) TILE-DRAWN)) (do (SDL-DestroyTexture (. tl texture)) (= (. tl texture) NULL) (= (. tl state) TILE-READY)))) (pthread-mutex-unlock (& (. self lock)))) ;; Hands this frame's queue to the workers. Returns how many tiles ;; requested this frame are not ready yet, queued or being made. (pub fn end-frame ((self (* tiles))) int (var pending int (. self waiting)) (pthread-cond-broadcast (& (. self wake))) (pthread-mutex-unlock (& (. self lock))) (return pending)) (pub fn stop ((self (* tiles))) void (pthread-mutex-lock (& (. self lock))) (= (. self quit) true) (pthread-cond-broadcast (& (. self wake))) (pthread-mutex-unlock (& (. self lock))) (for (var i int 0) (< i (. self worker-count)) (++ i) (pthread-join [(. self workers) i] NULL)) (pthread-mutex-lock (& (. self lock))) (.clear self) (pthread-mutex-unlock (& (. self lock))) (.release (& (. self index))) (free self)))