From b8c39e38bc0efa58a0369f8c829a6778de0bfb3e Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Sat, 29 Jan 2022 10:04:28 -0500 Subject: [PATCH] Fix Image.cake and Raylib.cake conflicting * Use raylib's stb_image.h and implementation if Raylib is imported. This prevents multiple definitions and ensures the API matches (because already, I'm on a different version of stb_image.h as raylib). * Make raylib support image formats I care about. Note that there's a bug in raylib right now which is addressed by: https://github.com/raysan5/raylib/pull/2318 * Reduce number of "hot loop" to speed up testing --- src/Image.cake | 29 ++++++++++++++++++++++++----- src/Raylib.cake | 9 ++++++++- src/Tracy.cake | 2 +- test/src/GameLibTests.cake | 8 +++++--- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Image.cake b/src/Image.cake index d03d03c..e8c13f5 100644 --- a/src/Image.cake +++ b/src/Image.cake @@ -1,13 +1,32 @@ ;; Image ;; Interface for loading images of various formats ;; Currently, stb_image.h does all the heavy lifting -(export-and-evaluate (add-c-search-directory-module "Dependencies/stb")) -(import "STB.cake") +(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 -(c-preprocessor-define STB_IMAGE_IMPLEMENTATION) +;; 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) -(c-import &with-decls "stb_image.h" - &with-defs "stb_image.h") +;; 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 diff --git a/src/Raylib.cake b/src/Raylib.cake index 51d263a..bdbc797 100644 --- a/src/Raylib.cake +++ b/src/Raylib.cake @@ -2,6 +2,8 @@ (add-cakelisp-search-directory "Dependencies/cakelisp/runtime") (import "CHelpers.cake" "BuildTools.cake" "Dependencies.cake") +(comptime-define-symbol 'STBImageDefinedInRaylib) + (export-and-evaluate (add-c-search-directory-module "Dependencies/raylib/src") (c-import "raylib.h")) @@ -36,7 +38,12 @@ (makeDirectory raylib-output-dir) (run-process-sequential-or - ("cmake" "../../Dependencies/raylib" :in-directory raylib-output-dir) + ("cmake" + ;; Enable file formats raylib normally disables, because if raylib defines stb image, we use + ;; the same definitions. + "-DCUSTOMIZE_BUILD=ON" "-DSUPPORT_FILEFORMAT_JPG=ON" + "-DSUPPORT_FILEFORMAT_BMP=ON" "-DSUPPORT_FILEFORMAT_TGA=ON" + "../../Dependencies/raylib" :in-directory raylib-output-dir) (Log "failed at Raylib configure step. This requires a sh/bash-style shell to execute.") (return false)) diff --git a/src/Tracy.cake b/src/Tracy.cake index 70bdbd5..9c4dd23 100644 --- a/src/Tracy.cake +++ b/src/Tracy.cake @@ -66,7 +66,7 @@ (sleep 1)) (var i int 0) - (var num-times (const int) 10) + (var num-times (const int) 2) (while (< i num-times) (ZoneScopedN "hot loop") (fprintf stderr "hot loop %d / %d\n" i num-times) diff --git a/test/src/GameLibTests.cake b/test/src/GameLibTests.cake index 86a5ab9..f636e42 100644 --- a/test/src/GameLibTests.cake +++ b/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) @@ -69,7 +69,9 @@ "../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/Image.cake" + "../src/Tracy.cake" "../src/TaskSystem.cake" + ;; Note that Raylib must come first so Image knows not to redefine stb_image implementation + "../src/Raylib.cake" "../src/Image.cake" "../src/DataBundle.cake" "../src/TinyCCompiler.cake" "../src/FreeType.cake" "../src/ImGuiAutoColor.cake" "../src/ProfilerAutoInstrument.cake"))