changeset 1298:1feaa8a97acf

did: halfway complete refactoring to move from all-include to some-include+imports, no API changes
author sam <sam@basx.dev>
date Tue, 06 Aug 2024 17:31:13 +0700
parents 4403569112b5
children 6d0162bfe48a
files semicongine.nim semicongine/audio/mixer.nim semicongine/audio/resources.nim semicongine/core/buildconfig.nim semicongine/core/matrix.nim semicongine/core/utils.nim semicongine/core/vector.nim semicongine/image.nim semicongine/input.nim semicongine/rendering.nim semicongine/rendering/platform/linux.nim semicongine/rendering/platform/windows.nim semicongine/rendering/vulkan/api.nim semicongine/resources.nim semicongine/storage.nim semicongine/text.nim semicongine/text/font.nim
diffstat 17 files changed, 154 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,57 +1,40 @@
-import std/algorithm
-import std/dynlib
-import std/endians
-import std/enumerate
-import std/hashes
-import std/json
-import std/locks
-import std/logging
-import std/marshal
-import std/math
-import std/macros
-import std/monotimes
-import std/os
-import std/options
-import std/parsecfg
-import std/parseutils
-import std/paths
-import std/random
-import std/sequtils
-import std/sets
-import std/strformat
-import std/streams
-import std/strutils
-import std/tables
-import std/times
-import std/typetraits
-import std/unicode
+import ./semicongine/core
+export core
+
+import ./semicongine/resources
+export resources
+
+import ./semicongine/image
+export image
 
-
-include ./semicongine/rendering/vulkan/api
-include ./semicongine/core
-
-setLogFilter(ENGINE_LOGLEVEL)
-
-include ./semicongine/resources
+import ./semicongine/events
+import ./semicongine/rendering
+export events
+export rendering
 
-include ./semicongine/image
-
-include ./semicongine/events
-include ./semicongine/rendering
+import ./semicongine/storage
+import ./semicongine/input
+export storage
+export input
 
-include ./semicongine/storage
-include ./semicongine/input
-
-include ./semicongine/audio
+import ./semicongine/audio
+export audio
 
 # texture packing is required for font atlas
-include ./semicongine/contrib/algorithms/texture_packing
-include ./semicongine/text
+import ./semicongine/text
+export text
 
-include ./semicongine/gltf
+import ./semicongine/gltf
+export gltf
 
 when not defined(WITHOUT_CONTRIB):
-  include ./semicongine/contrib/steam
-  include ./semicongine/contrib/settings
-  include ./semicongine/contrib/algorithms/collision
-  include ./semicongine/contrib/algorithms/noise
+  import ./semicongine/contrib/steam
+  import ./semicongine/contrib/settings
+  import ./semicongine/contrib/algorithms/texture_packing
+  import ./semicongine/contrib/algorithms/collision
+  import ./semicongine/contrib/algorithms/noise
+  export steam
+  export settings
+  export texture_packing
+  export collision
+  export noise
--- a/semicongine/audio/mixer.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/audio/mixer.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,11 @@
+import std/locks
+import std/logging
+import std/math
+import std/monotimes
+import std/strformat
+import std/tables
+import std/times
+
 const NBUFFERS = 32
 const BUFFERSAMPLECOUNT = 256
 const AUDIO_SAMPLE_RATE* = 44100
--- a/semicongine/audio/resources.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/audio/resources.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,11 @@
+import std/endians
+import std/os
+import std/streams
+import std/strutils
+
+import ../core
+import ../resources
+
 type
   Encoding {.size: sizeof(uint32).} = enum
     # Unspecified = 0
--- a/semicongine/core/buildconfig.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/core/buildconfig.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,5 @@
+import std/logging
+
 const ENGINENAME = "semiconginev2"
 
 # checks required build options:
@@ -20,7 +22,10 @@
   const LOGLEVEL {.strdefine.}: string = "Warn"
 
 const ENGINE_LOGLEVEL* = parseEnum[Level]("lvl" & LOGLEVEL)
+setLogFilter(ENGINE_LOGLEVEL)
+
 # resource bundleing settings, need to be configured per project
+const DEFAULT_PACKAGE* = "default"
 const PACKAGETYPE* {.strdefine.}: string = "exe" # dir, zip, exe
 static:
   assert PACKAGETYPE in ["dir", "zip", "exe"], ENGINENAME & " requires one of -d:PACKAGETYPE=dir -d:PACKAGETYPE=zip -d:PACKAGETYPE=exe"
--- a/semicongine/core/matrix.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/core/matrix.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,4 +1,4 @@
-export math
+import std/math
 
 type
   # layout is row-first
@@ -452,23 +452,6 @@
   result[3, 2] = (-a[3, 0] * s3 + a[3, 1] * s1 - a[3, 2] * s0) * invdet;
   result[3, 3] = ( a[2, 0] * s3 - a[2, 1] * s1 + a[2, 2] * s0) * invdet;
 
-# call e.g. TMat32[int]().randomized() to get a random matrix
-template makeRandomMatrixInit(mattype: typedesc) =
-  proc Randomized*[T: SomeInteger](m: mattype[T]): mattype[T] =
-    for i in 0 ..< result.data.len:
-      result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0])))
-  proc Randomized*[T: SomeFloat](m: mattype[T]): mattype[T] =
-    for i in 0 ..< result.data.len:
-      result.data[i] = rand(T(1.0))
-
-makeRandomMatrixInit(TMat2)
-makeRandomMatrixInit(TMat23)
-makeRandomMatrixInit(TMat32)
-makeRandomMatrixInit(TMat3)
-makeRandomMatrixInit(TMat34)
-makeRandomMatrixInit(TMat43)
-makeRandomMatrixInit(TMat4)
-
 func projection*(fovy, aspect, zNear, zFar: float32): Mat4 =
   let tanHalfFovy = 1 / tan(fovy / 2)
   return Mat4(data: [
--- a/semicongine/core/utils.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/core/utils.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,9 @@
+import std/strutils
+import std/strformat
+import std/paths
+import std/os
+import std/typetraits
+
 type
   HorizontalAlignment* = enum
     Left
@@ -18,7 +24,7 @@
   if list.len > 0: addr(list[0]) else: nil
 
 # required for some external libraries
-proc nativeFree(p: pointer) {.importc: "free".}
+proc nativeFree*(p: pointer) {.importc: "free".}
 
 proc StaticExecChecked*(command: string, input = ""): string {.compileTime.} =
   let (output, exitcode) = gorgeEx(
--- a/semicongine/core/vector.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/core/vector.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,6 @@
+import std/math
+import std/tables
+import std/macros
 
 type
   TVec1*[T: SomeNumber] = array[1, T]
@@ -373,22 +376,8 @@
 
 createVectorAttribAccessorFuncs()
 
-# call e.g. Vec2[int]().randomized() to get a random matrix
-template makeRandomVectorInit(mattype: typedesc) =
-  proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] =
-    for i in 0 ..< result.len:
-      result[i] = rand(low(typeof(m[0])) .. high(typeof(m[0])))
-  proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] =
-    for i in 0 ..< result.len:
-      result[i] = rand(1.0)
-
-makeRandomVectorInit(TVec1)
-makeRandomVectorInit(TVec2)
-makeRandomVectorInit(TVec3)
-makeRandomVectorInit(TVec4)
-
-converter Vec2VkExtent*(vec: TVec2[uint32]): VkExtent2D = VkExtent2D(width: vec[0], height: vec[1])
-converter Vec3VkExtent*(vec: TVec2[uint32]): VkExtent3D = VkExtent3D(width: vec[0], height: vec[1], depth: vec[2])
+# converter Vec2VkExtent*(vec: TVec2[uint32]): VkExtent2D = VkExtent2D(width: vec[0], height: vec[1])
+# converter Vec3VkExtent*(vec: TVec2[uint32]): VkExtent3D = VkExtent3D(width: vec[0], height: vec[1], depth: vec[2])
 
 func angleBetween*(a, b: Vec3f): float32 =
   arccos(a.dot(b) / (a.length * b.length))
--- a/semicongine/image.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/image.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,9 @@
+import std/os
+
+import ./core
+import ./resources
+import ./rendering/vulkan/api
+
 {.emit: "#define STB_IMAGE_STATIC".}
 {.emit: "#define STB_IMAGE_IMPLEMENTATION".}
 {.emit: "#include \"" & currentSourcePath.parentDir() & "/thirdparty/stb/stb_image.h\"".}
--- a/semicongine/input.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/input.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,11 @@
+import std/strutils
+import std/tables
+
+import ./core
+import ./events
+import ./rendering
+import ./storage
+
 type
   Input = object
     keyIsDown: set[Key]
--- a/semicongine/rendering.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/rendering.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,15 @@
+import std/enumerate
+import std/hashes
+import std/macros
+import std/os
+import std/sequtils
+import std/strutils
+import std/typetraits
+
+import ./rendering/vulkan/api
+
+import ./image
+
 # in this file:
 # - const defintions for rendering
 # - custom pragma defintions for rendering
@@ -40,7 +52,7 @@
     device*: VkDevice
     physicalDevice*: VkPhysicalDevice
     surface: VkSurfaceKHR
-    window: NativeWindow
+    window*: NativeWindow
     graphicsQueueFamily*: uint32
     graphicsQueue*: VkQueue
     debugMessenger: VkDebugUtilsMessengerEXT
--- a/semicongine/rendering/platform/linux.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/rendering/platform/linux.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,9 +1,17 @@
+import std/logging
+import std/options
+import std/strformat
+import std/tables
+
 import ../../thirdparty/x11/xlib
 import ../../thirdparty/x11/xutil
 import ../../thirdparty/x11/keysym
 import ../../thirdparty/x11/x as x11
 import ../../thirdparty/x11/xkblib
 
+import ../../core
+import ../../events
+
 const REQUIRED_PLATFORM_EXTENSIONS = @["VK_KHR_xlib_surface"]
 
 # got values (keycodes) from xev
@@ -222,7 +230,7 @@
 proc createNativeSurface(instance: VkInstance, window: NativeWindow): VkSurfaceKHR =
   var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR(
     sType: VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
-    dpy: cast[ptr Display](window.display),
-    window: cast[Window](window.window),
+    dpy: cast[ptr api.Display](window.display),
+    window: cast[api.Window](window.window),
   )
   checkVkResult vkCreateXlibSurfaceKHR(instance, addr(surfaceCreateInfo), nil, addr(result))
--- a/semicongine/rendering/platform/windows.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/rendering/platform/windows.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,6 +1,11 @@
+import std/tables
+import std/options
+
 import ../../../thirdparty/winim/winim/inc/[windef, winuser, wincon, winbase]
 import ../../../thirdparty/winim/winim/[winstr, utils]
 
+import ../../events
+
 const REQUIRED_PLATFORM_EXTENSIONS = @["VK_KHR_win32_surface"]
 
 const KeyTypeMap* = {
--- a/semicongine/rendering/vulkan/api.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/rendering/vulkan/api.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,4 +1,11 @@
-type
+import std/dynlib
+import std/logging
+import std/macros
+import std/strutils
+import std/typetraits
+import std/tables
+
+type
   VkHandle* = distinct uint
   VkNonDispatchableHandle* = distinct uint
 when defined(linux):
--- a/semicongine/resources.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/resources.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,18 @@
+import std/algorithm
+import std/dirs
+import std/json
+import std/parsecfg
+import std/os
+import std/paths
+import std/sequtils
+import std/sets
+import std/streams
+import std/strformat
+import std/strutils
+import std/tables
+
+import ./core
+
 type
   ResourceBundlingType = enum
     Dir # Directories
@@ -6,8 +21,6 @@
 
 const
   thebundletype = parseEnum[ResourceBundlingType](PACKAGETYPE.toLowerAscii().capitalizeAscii())
-  ASCII_CHARSET = PrintableChars.toSeq.toRunes
-  DEFAULT_PACKAGE = "default"
 
 # resource loading
 
@@ -28,7 +41,7 @@
   proc packageRoot(package: string): string =
     resourceRoot().joinPath(package)
 
-  proc loadResource_intern(path: string, package: string): Stream =
+  proc loadResource_intern*(path: string, package: string): Stream =
     let realpath = package.packageRoot().joinPath(path)
     if not realpath.fileExists():
       raise newException(Exception, &"Resource {path} not found (checked {realpath})")
@@ -56,7 +69,7 @@
   proc packageRoot(package: string): string =
     resourceRoot().joinPath(package)
 
-  proc loadResource_intern(path: string, package: string): Stream =
+  proc loadResource_intern*(path: string, package: string): Stream =
     let archive = openZipArchive(package.packageRoot() & ".zip")
     try:
       result = newStringStream(archive.extractFile(path))
@@ -108,10 +121,10 @@
           let package = packageDir.splitPath.tail
           result[package] = Table[string, string]()
           for resourcefile in walkDirRec(packageDir, relative = true):
-            result[package][resourcefile.replace('\\', '/')] = staticRead(packageDir.joinPath(resourcefile))
+            result[package][resourcefile.string.replace('\\', '/')] = staticRead(packageDir.joinPath(resourcefile))
   const bundledResources = loadResources()
 
-  proc loadResource_intern(path: string, package: string): Stream =
+  proc loadResource_intern*(path: string, package: string): Stream =
     if not (path in bundledResources[package]):
       raise newException(Exception, &"Resource {path} not found")
     newStringStream(bundledResources[package][path])
--- a/semicongine/storage.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/storage.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,12 @@
+import std/marshal
+import std/os
+import std/paths
+import std/strformat
+import std/strutils
+import std/tables
+
+import ./core
+
 import ./thirdparty/db_connector/db_sqlite
 
 const STORAGE_NAME = Path("storage.db")
--- a/semicongine/text.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/text.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -1,3 +1,11 @@
+import std/os
+import std/tables
+import std/unicode
+
+import ./core
+import ./rendering
+import ./image
+
 const
   NEWLINE = Rune('\n')
   SPACE = Rune(' ')
--- a/semicongine/text/font.nim	Tue Aug 06 14:31:59 2024 +0700
+++ b/semicongine/text/font.nim	Tue Aug 06 17:31:13 2024 +0700
@@ -2,6 +2,8 @@
 {.emit: "#define STB_TRUETYPE_IMPLEMENTATION".}
 {.emit: "#include \"" & currentSourcePath.parentDir().parentDir() & "/thirdparty/stb/stb_truetype.h\"".}
 
+const ASCII_CHARSET = PrintableChars.toSeq.toRunes
+
 type stbtt_fontinfo {.importc, incompleteStruct.} = object
 
 proc stbtt_InitFont(info: ptr stbtt_fontinfo, data: ptr char, offset: cint): cint {.importc, nodecl.}