changeset 21:316923e9247c

add: clean examples, update build configs
author Sam <sam@basx.dev>
date Tue, 10 Jan 2023 00:24:37 +0700
parents beb86492b178
children b45a5d338cd0
files Makefile config.nims examples/alotof_triangles.nim examples/hello_triangle.nim notes
diffstat 5 files changed, 160 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Jan 10 00:23:40 2023 +0700
+++ b/Makefile	Tue Jan 10 00:24:37 2023 +0700
@@ -1,8 +1,4 @@
 SOURCES := $(shell find src -name '*.nim')
-DEBUG_OPTIONS := --debugger:native --checks:on --assertions:on
-RELEASE_OPTIONS := -d:release --checks:off --assertions:off
-WINDOWS_DEBUG_OPTIONS := --cc:vcc --passC:'/MDd' --passL:'ucrtd.lib'
-WINDOWS_RELEASE_OPTIONS := --cc:vcc --passC:'/MD' --passL:'ucrt.lib'
 
 # HACK to get cross-compilation working --------------------------------
 
@@ -29,13 +25,22 @@
 
 # build hello_triangle
 build/debug/linux/hello_triangle: ${SOURCES}
-	nim build_linux_debug ${DEBUG_OPTIONS} -o:$@ examples/hello_triangle.nim
+	nim build_linux_debug -o:$@ examples/hello_triangle.nim
 build/release/linux/hello_triangle: ${SOURCES}
-	nim build_linux_release c ${RELEASE_OPTIONS} -o:$@ examples/hello_triangle.nim
-build/debug/windows/hello_triangle.exe:  ${SOURCES} build/nim_windows
-	${WINE_NIM} c ${DEBUG_OPTIONS} ${WINDOWS_DEBUG_OPTIONS} -o:$@ examples/hello_triangle.nim
+	nim build_linux_release -o:$@ examples/hello_triangle.nim
+build/debug/windows/hello_triangle.exe: ${SOURCES} build/nim_windows
+	${WINE_NIM} build_windows_debug -o:$@ examples/hello_triangle.nim
 build/release/windows/hello_triangle.exe: ${SOURCES} build/nim_windows
-	${WINE_NIM} c ${RELEASE_OPTIONS} ${WINDOWS_RELEASE_OPTIONS} -o:$@ examples/hello_triangle.nim
+	${WINE_NIM} build_windows_release -o:$@ examples/hello_triangle.nim
+
+build/debug/linux/alotof_triangles: ${SOURCES}
+	nim build_linux_debug -o:$@ examples/alotof_triangles.nim
+build/release/linux/alotof_triangles: ${SOURCES}
+	nim build_linux_release -o:$@ examples/alotof_triangles.nim
+build/debug/windows/alotof_triangles.exe: ${SOURCES} build/nim_windows
+	${WINE_NIM} build_windows_debug -o:$@ examples/alotof_triangles.nim
+build/release/windows/alotof_triangles.exe: ${SOURCES} build/nim_windows
+	${WINE_NIM} build_windows_release -o:$@ examples/alotof_triangles.nim
 
 build_all_linux: build/debug/linux/hello_triangle build/release/linux/hello_triangle
 build_all_windows: build/debug/windows/hello_triangle.exe build/release/windows/hello_triangle.exe
--- a/config.nims	Tue Jan 10 00:23:40 2023 +0700
+++ b/config.nims	Tue Jan 10 00:24:37 2023 +0700
@@ -19,16 +19,40 @@
   switch("checks", "off")
   switch("assertions", "off")
 
+proc compilerFlagsDebugWindows() =
+  switch("cc", "vcc")
+  switch("passC", "/MDd")
+  switch("passL", "ucrtd.lib")
+
+proc compilerFlagsReleaseWindows() =
+  switch("cc", "vcc")
+  switch("passC", "/MD")
+  switch("passL", "ucrt.lib")
+
 task build_linux_debug, "build linux debug":
-  # compilerFlags()
+  compilerFlags()
   compilerFlagsDebug()
   buildbase.joinPath("debug/linux").mkDir()
   setCommand "c"
 
 task build_linux_release, "build linux release":
-  # compilerFlags()
+  compilerFlags()
   compilerFlagsRelease()
   buildbase.joinPath("release/linux").mkDir()
   setCommand "c"
 
+task build_windows_debug, "build windows debug":
+  compilerFlags()
+  compilerFlagsDebug()
+  compilerFlagsDebugWindows()
+  buildbase.joinPath("debug/windows").mkDir()
+  setCommand "c"
+
+task build_windows_release, "build windows release":
+  compilerFlags()
+  compilerFlagsRelease()
+  compilerFlagsReleaseWindows()
+  buildbase.joinPath("release/windows").mkDir()
+  setCommand "c"
+
 compilerFlags()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/alotof_triangles.nim	Tue Jan 10 00:24:37 2023 +0700
@@ -0,0 +1,78 @@
+import std/random
+
+import zamikongine/engine
+import zamikongine/math/vector
+import zamikongine/math/matrix
+import zamikongine/vertex
+import zamikongine/mesh
+import zamikongine/thing
+import zamikongine/shader
+
+type
+  VertexDataA = object
+    position11: VertexAttribute[Vec2[float32]]
+    color22: VertexAttribute[Vec3[float32]]
+
+when isMainModule:
+  randomize()
+  var myengine = igniteEngine()
+  const baseTriangle = [
+    Vec3([-0.3'f32, -0.3'f32, 1'f32]),
+    Vec3([ 0.3'f32,  0.3'f32, 1'f32]),
+    Vec3([-0.3'f32,  0.3'f32, 1'f32]),
+  ]
+
+  var scene = new Thing
+
+  for i in 1 .. 300:
+    var randommesh = new Mesh[VertexDataA]
+    # TODO: create randomized position11 from baseTriangle with random transformation matrix
+    var transform = (Mat33[float32]().randomized() * 2'f32) - 1'f32
+    randommesh.vertexData = VertexDataA(
+      position11: VertexAttribute[Vec2[float32]](
+        data: @[
+          Vec2[float32](transform * baseTriangle[0]),
+          Vec2[float32](transform * baseTriangle[1]),
+          Vec2[float32](transform * baseTriangle[2]),
+        ]
+      ),
+      color22: VertexAttribute[Vec3[float32]](
+        data: @[
+          Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
+          Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
+          Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
+        ]
+      )
+    )
+
+    var randomindexedmesh = new IndexedMesh[VertexDataA, uint16]
+    randomindexedmesh.vertexData = VertexDataA(
+      position11: VertexAttribute[Vec2[float32]](
+        data: @[
+          Vec2[float32](transform * baseTriangle[0]),
+          Vec2[float32](transform * baseTriangle[1]),
+          Vec2[float32](transform * baseTriangle[2]),
+        ]
+      ),
+      color22: VertexAttribute[Vec3[float32]](
+        data: @[
+          Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
+          Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
+          Vec3([float32(rand(1)), float32(rand(1)), float32(rand(1))]),
+        ]
+      )
+    )
+    randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16], [0'u16, 2'u16, 1'u16]]
+    var childthing = new Thing
+    childthing.parts.add randommesh
+    childthing.parts.add randomindexedmesh
+    scene.children.add childthing
+
+  setupPipeline[VertexDataA, uint16](
+    myengine,
+    scene,
+    generateVertexShaderCode[VertexDataA]("main", "position11", "color22"),
+    generateFragmentShaderCode[VertexDataA]("main"),
+  )
+  myengine.fullThrottle()
+  myengine.trash()
--- a/examples/hello_triangle.nim	Tue Jan 10 00:23:40 2023 +0700
+++ b/examples/hello_triangle.nim	Tue Jan 10 00:24:37 2023 +0700
@@ -6,58 +6,45 @@
 import zamikongine/shader
 
 type
+  # define type of vertex
   VertexDataA = object
-    position11: VertexAttribute[Vec2[float32]]
-    color22: VertexAttribute[Vec3[float32]]
+    position: VertexAttribute[Vec2[float32]]
+    color: VertexAttribute[Vec3[float32]]
+
+# vertex data (types must match the above VertexAttributes)
+const
+  triangle_pos = @[
+    Vec2([-0.5'f32, -0.5'f32]),
+    Vec2([ 0.5'f32,  0.5'f32]),
+    Vec2([-0.5'f32,  0.5'f32]),
+  ]
+  triangle_color = @[
+    Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
+    Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
+    Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
+  ]
 
 when isMainModule:
   var myengine = igniteEngine()
-  var mymesh1 = new Mesh[VertexDataA]
-  mymesh1.vertexData = VertexDataA(
-    position11: VertexAttribute[Vec2[float32]](
-      data: @[
-        Vec2([-0.5'f32, -0.5'f32]),
-        Vec2([ 0.5'f32,  0.5'f32]),
-        Vec2([-0.5'f32,  0.5'f32]),
-      ]
-    ),
-    color22: VertexAttribute[Vec3[float32]](
-      data: @[
-        Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
-        Vec3([0.0'f32, 1.0'f32, 0.0'f32]),
-        Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
-      ]
-    )
+
+  # build a mesh
+  var trianglemesh = new Mesh[VertexDataA]
+  trianglemesh.vertexData = VertexDataA(
+    position: VertexAttribute[Vec2[float32]](data: triangle_pos),
+    color: VertexAttribute[Vec3[float32]](data: triangle_color),
   )
-  var mymesh2 = new IndexedMesh[VertexDataA, uint16]
-  mymesh2.vertexData = VertexDataA(
-    position11: VertexAttribute[Vec2[float32]](
-      data: @[
-        Vec2([ 0.0'f32, -0.7'f32]),
-        Vec2([ 0.6'f32,  0.1'f32]),
-        Vec2([ 0.3'f32,  0.4'f32]),
-      ]
-    ),
-    color22: VertexAttribute[Vec3[float32]](
-      data: @[
-        Vec3([1.0'f32, 1.0'f32, 0.0'f32]),
-        Vec3([1.0'f32, 0.0'f32, 0.0'f32]),
-        Vec3([0.0'f32, 1.0'f32, 1.0'f32]),
-      ]
-    )
-  )
-  mymesh2.indices = @[[0'u16, 1'u16, 2'u16]]
-  var athing = new Thing
-  athing.parts.add mymesh1
-  var childthing = new Thing
-  childthing.parts.add mymesh2
-  athing.children.add childthing
+  # build a single-object scene graph
+  var triangle = new Thing
+  # add the triangle mesh to the object
+  triangle.parts.add trianglemesh
 
+  # upload data, prepare shaders, etc
   setupPipeline[VertexDataA, uint16](
     myengine,
-    athing,
-    generateVertexShaderCode[VertexDataA]("main", "position11", "color22"),
+    triangle,
+    generateVertexShaderCode[VertexDataA]("main", "position", "color"),
     generateFragmentShaderCode[VertexDataA]("main"),
   )
+  # show something
   myengine.fullThrottle()
   myengine.trash()
--- a/notes	Tue Jan 10 00:23:40 2023 +0700
+++ b/notes	Tue Jan 10 00:24:37 2023 +0700
@@ -1,7 +1,4 @@
-For implementation of font rendering:
-https://developer.apple.com/fonts/TrueType-Reference-Manual/
-
-ideas:
+Game ideas:
 - mining-game with structure simulation, crashing mineshafts, etc.
 - top-down 2d shooter (wild west?) with one room per scene, fixed camera
 - Top-down 2d shooter with autoshoot (-> what is the challenge? position? cover? effects?)
@@ -14,10 +11,16 @@
 
 High prio:
 - Texture handling
+- Mesh files (Wavefront OBJ, MTL)
+- Image files (BMP RGB + BMP Graysscale for transparency)
+
 - Input handling (X11, Win32)
-- Audio (?)
-- Mesh files (Wavefront OBJ, MTL)
-- Image files (BMP)
+
+- Config files (std/parsecfg)
+
 - Audio files (WAV)
-- Config files (TOML)
-- Resource-packs (ZIP)
+- Audio (Alsa, Windows Waveform API?)
+- Text rendering
+
+TODO:
+- move all of Makefile to config.nims