Browse Source

Added Image.cake for stb_image

master
Macoy Madson 2 months ago
parent
commit
7316b9d403
  1. 36
      src/Image.cake
  2. 26
      src/STB.cake
  3. BIN
      test/assets/town.jpg
  4. 9
      test/src/GameLibTests.cake

36
src/Image.cake

@ -0,0 +1,36 @@
;; Image
;; Interface for loading images of various formats
;; Currently, stb_image.h does all the heavy lifting
(import &comptime-only "STB.cake")
(use-stb-image clone-stb-headers-image)
(comptime-cond
('auto-test
(defun-nodecl test--stb-image (&return int)
(var image-to-load (* (const char)) "assets/town.jpg")
(var width int 0)
(var height int 0)
(var num-pixel-components int 0)
(var num-desired-channels int 0) ;; Default
(var pixel-data (* (unsigned char))
(stbi_load image-to-load (addr width) (addr height) (addr num-pixel-components)
num-desired-channels))
(unless pixel-data
(fprintf stderr "error: failed to load %s\n" image-to-load)
(return 1))
(fprintf stderr "size of %s: %dx%d\n" image-to-load width height)
(fprintf stderr "num components in %s: %d\n" image-to-load num-pixel-components)
(fprintf stderr "first three pixels:\n")
(each-in-range 3 i
(var rgb-components ([] 3 (unsigned char)) (array 0))
(memcpy rgb-components (addr (at (* i num-pixel-components) pixel-data))
(sizeof rgb-components))
(fprintf stderr "[%d] %d %d %d\n"
i
(at 0 rgb-components)
(at 1 rgb-components)
(at 2 rgb-components)))
(stbi_image_free pixel-data)
(return 0))))

26
src/STB.cake

@ -16,7 +16,6 @@
"https://github.com/nothings/stb"
"Dependencies/stb")
;; Enforce only defining the implemention once
(comptime-cond
('stb-ds-defined)
@ -27,3 +26,28 @@
(c-import &with-decls "stb_ds.h"
&with-defs "stb_ds.h"))
(return true))
(defmacro use-stb-image (clone-func-name symbol)
(tokenize-push output
;; TODO: Make this an "infect" to the importer rather than having to be global
(add-c-search-directory-global "Dependencies/stb")
;; TODO: Remove once importing this module no longer changes all compile commands
(add-build-config-label "STB")
;; (add-c-search-directory-module "Dependencies/stb")
;; TODO: Remove parameter, replace with gensym/check for redefine?
(add-dependency-git-submodule (token-splice clone-func-name)
"https://github.com/nothings/stb"
"Dependencies/stb")
;; Enforce only defining the implemention once
(comptime-cond
('stb-image-defined)
(true
(c-preprocessor-define STB_IMAGE_IMPLEMENTATION)
(comptime-define-symbol 'stb-image-defined)))
(c-import &with-decls "stb_image.h"
&with-defs "stb_image.h"))
(return true))

BIN
test/assets/town.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

9
test/src/GameLibTests.cake

@ -37,8 +37,8 @@
(true
"src/Config_Linux.cake")))
;; (var test-minimal bool true)
(var test-minimal bool false)
(var test-minimal bool true)
;; (var test-minimal bool false)
;; (var test-opengl-only bool true)
(var test-opengl-only bool false)
@ -53,7 +53,7 @@
(array platform-config
"../src/AutoTest.cake" "../src/Introspection.cake"
"../src/Dictionary.cake" "../src/DynamicArray.cake"
"../src/TaskSystem.cake")))
"../src/TaskSystem.cake" "../src/Image.cake")))
(test-opengl-only
(gamelib-run-test
@ -68,7 +68,8 @@
"../src/AutoTest.cake" "../src/SDL.cake" "../src/Math.cake"
"../src/Aubio.cake" "../src/ImGui.cake" "../src/Dictionary.cake"
"../src/DynamicArray.cake" "../src/Introspection.cake" "../src/OpenGL.cake"
"../src/Tracy.cake" "../src/TaskSystem.cake" "../src/ProfilerAutoInstrument.cake"))
"../src/Tracy.cake" "../src/TaskSystem.cake" "../src/Image.cake"
"../src/ProfilerAutoInstrument.cake"))
(when test-ogre
(gamelib-run-test "Ogre" (array platform-config "src/OgreApp.cake"))

Loading…
Cancel
Save