changeset 1476:5d91e8328c3b

did: small fix for text-centering, add: timestamp to world-storage api
author sam <sam@basx.dev>
date Sat, 19 Apr 2025 16:32:54 +0700
parents e4eed5f9ac33
children 9600b96be7a8
files semicongine/storage.nim semicongine/text.nim
diffstat 2 files changed, 19 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/storage.nim	Mon Apr 07 23:58:41 2025 +0700
+++ b/semicongine/storage.nim	Sat Apr 19 16:32:54 2025 +0700
@@ -1,4 +1,5 @@
 import std/marshal
+import std/algorithm
 import std/os
 import std/dirs
 import std/paths
@@ -110,6 +111,7 @@
     sql(
       &"""CREATE TABLE IF NOT EXISTS {table} (
     key INT NOT NULL UNIQUE,
+    time INT NOT NULL,
     value BLOB NOT NULL
   )"""
     )
@@ -170,9 +172,10 @@
   defer:
     db.close()
   let key = $(int(now().toTime().toUnixFloat() * 1000))
-  let stm = db.prepare(&"""INSERT INTO {table} VALUES(?, ?)""")
+  let stm = db.prepare(&"""INSERT INTO {table} VALUES(?, ?, ?)""")
   stm.bindParam(1, key)
-  stm.bindParam(2, data)
+  stm.bindParam(2, int(now().utc.toTime.toUnixFloat * 1000))
+  stm.bindParam(3, data)
   db.exec(stm)
   stm.finalize()
   if deleteOld:
@@ -240,7 +243,7 @@
   var s = newStringStream(dbResult)
   loadValue[T](s)
 
-proc listWorlds*(): seq[string] =
+proc listWorlds*(): seq[(DateTime, string)] =
   let dir = Path(getDataDir()) / Path(AppName()) / Path(WORLD_DIR)
 
   if dir.dirExists():
@@ -248,7 +251,16 @@
       dir = string(dir), relative = true, checkDir = true, skipSpecial = true
     ):
       if kind in [pcFile, pcLinkToFile] and path.endsWith(".db"):
-        result.add path[0 .. ^4]
+        let db = path[0 .. ^4].ensureExists(DEFAULT_WORLD_TABLE_NAME)
+        defer:
+          db.close()
+        let dbResult = db.getValue(
+          sql(
+            &"""SELECT time FROM {DEFAULT_WORLD_TABLE_NAME} ORDER BY time DESC LIMIT 1"""
+          )
+        )
+        result.add ((parseInt(dbResult) / 1000).fromUnixFloat().local(), path[0 .. ^4])
+  result.sort(Descending)
 
 proc purgeWorld*(worldName: string) =
   worldName.path().string.removeFile()
--- a/semicongine/text.nim	Mon Apr 07 23:58:41 2025 +0700
+++ b/semicongine/text.nim	Sat Apr 19 16:32:54 2025 +0700
@@ -142,11 +142,12 @@
 
     globalScale = textbuffer.texts[textI].scale * textbuffer.baseScale
     box = textbuffer.textDimension(text) * textbuffer.texts[textI].scale
-    xH = textbuffer.font.xHeight * globalScale
+    xH = textbuffer.font.xHeight * 0.8 * globalScale
+      # the 0.8 is just an approximation to get a good vertical center
     aratio = getAspectRatio()
     origin = vec3(
       position.x - (anchor.x * 0.5 + 0.5) * box.x / aratio,
-      position.y + (anchor.y * -0.5 + 0.5) * box.y - xH * 0.5 -
+      position.y + (anchor.y * -0.5 + 0.5) * box.y - xH -
         textbuffer.font.lineHeight * globalScale * 0.5,
       position.z,
     )