|
|
|
;; Image
|
|
|
|
;; Interface for loading images of various formats
|
|
|
|
;; Currently, stb_image.h does all the heavy lifting
|
|
|
|
(export-and-evaluate
|
|
|
|
(comptime-cond
|
|
|
|
('STBImageDefinedInRaylib
|
|
|
|
(add-c-search-directory-module "Dependencies"))
|
|
|
|
(true
|
|
|
|
(add-c-search-directory-module "Dependencies/stb"))))
|
|
|
|
(import "STB.cake") ;; Download STB if necessary
|
|
|
|
|
|
|
|
;; If another dependency includes stb_image (it's quite popular), then we don't need to add the
|
|
|
|
;; implementations here (and cause multiple definitions)
|
|
|
|
(comptime-cond
|
|
|
|
('STBImageDefinedInRaylib)
|
|
|
|
(true
|
|
|
|
(c-preprocessor-define STB_IMAGE_IMPLEMENTATION)))
|
|
|
|
(c-preprocessor-define STBI_FAILURE_USERMSG)
|
|
|
|
;; This is an annoying hack: Raylib uses a different version of stb, so if we import Raylib.cake
|
|
|
|
;; and Image.cake (must be in that order!), we need to use Raylib's version, because I don't want
|
|
|
|
;; to modify raylib.
|
|
|
|
(comptime-cond
|
|
|
|
('STBImageDefinedInRaylib
|
|
|
|
;; I don't want to expose raylib/src/external because it breaks other modules in GameLib
|
|
|
|
(c-import &with-decls "raylib/src/external/stb_image.h"
|
|
|
|
&with-defs "raylib/src/external/stb_image.h"))
|
|
|
|
(true
|
|
|
|
(c-import &with-decls "stb_image.h"
|
|
|
|
&with-defs "stb_image.h")))
|
|
|
|
|
|
|
|
(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 3)
|
|
|
|
(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 with message: %s\n" image-to-load
|
|
|
|
(stbi_failure_reason))
|
|
|
|
(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 3) 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))))
|