comparison semiconginev2/resources.nim @ 1242:e8b3dc80e48e

add: PNG loading and tests for it
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 15:53:32 +0700
parents 176383220123
children
comparison
equal deleted inserted replaced
1241:a0ed1a918fda 1242:e8b3dc80e48e
139 yielded.incl components[0] 139 yielded.incl components[0]
140 140
141 proc LoadResource*(path: string, package = DEFAULT_PACKAGE): Stream = 141 proc LoadResource*(path: string, package = DEFAULT_PACKAGE): Stream =
142 loadResource_intern(path, package = package) 142 loadResource_intern(path, package = package)
143 143
144 #[
145 proc LoadImage*[T](path: string, package = DEFAULT_PACKAGE): Image[RGBAPixel] =
146 if path.splitFile().ext.toLowerAscii == ".bmp":
147 loadResource_intern(path, package = package).ReadBMP()
148 elif path.splitFile().ext.toLowerAscii == ".png":
149 loadResource_intern(path, package = package).ReadPNG()
150 else:
151 raise newException(Exception, "Unsupported image file type: " & path)
152
153 proc LoadJson*(path: string, package = DEFAULT_PACKAGE): JsonNode = 144 proc LoadJson*(path: string, package = DEFAULT_PACKAGE): JsonNode =
154 path.loadResource_intern(package = package).readAll().parseJson() 145 path.loadResource_intern(package = package).readAll().parseJson()
155 146
156 proc LoadConfig*(path: string, package = DEFAULT_PACKAGE): Config = 147 proc LoadConfig*(path: string, package = DEFAULT_PACKAGE): Config =
157 path.loadResource_intern(package = package).loadConfig(filename = path) 148 path.loadResource_intern(package = package).loadConfig(filename = path)
158 149
150 # PNG-images
151
152 {.compile: currentSourcePath.parentDir() & "/contrib/lodepng.c".}
153 proc lodepng_decode_memory(out_data: ptr cstring, w: ptr cuint, h: ptr cuint, in_data: cstring, insize: csize_t, colorType: cint, bitdepth: cuint): cuint {.importc.}
154
155 proc lodepng_encode_memory(out_data: ptr cstring, outsize: ptr csize_t, image: cstring, w: cuint, h: cuint, colorType: cint, bitdepth: cuint): cuint {.importc.}
156
157 #[
159 proc LoadMeshes*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): seq[MeshTree] = 158 proc LoadMeshes*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): seq[MeshTree] =
160 loadResource_intern(path, package = package).ReadglTF(defaultMaterial) 159 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)
161 160
162 proc LoadFirstMesh*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): Mesh = 161 proc LoadFirstMesh*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): Mesh =
163 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)[0].toSeq[0] 162 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)[0].toSeq[0]
164
165 ]# 163 ]#
166 164
167 proc Packages*(): seq[string] = 165 proc Packages*(): seq[string] =
168 modList_intern() 166 modList_intern()
169 167