diff --git a/CMakeLists.txt b/CMakeLists.txt index fa306dc..37a2f2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ find_package(glfw3 REQUIRED) find_package(glad REQUIRED) find_package(asio REQUIRED) find_package(glm REQUIRED) +find_package(stb REQUIRED) # OpenGL abstraction library add_library(cbt_opengl STATIC @@ -32,7 +33,7 @@ target_include_directories(cbt_opengl PRIVATE ".") target_compile_features(cbt_opengl PRIVATE cxx_std_23) target_compile_options(cbt_opengl PRIVATE ${BASE_OPTIONS}) target_compile_definitions(cbt_opengl PRIVATE ${BASE_DEFINITIONS}) -target_link_libraries(cbt_opengl PUBLIC fmt::fmt glfw::glfw glad::glad) +target_link_libraries(cbt_opengl PUBLIC fmt::fmt glfw::glfw glad::glad stb::stb) # Scene base library add_library(cbt_scene STATIC diff --git a/cbt/window.cpp b/cbt/window.cpp index 6ac96dd..2f7b061 100644 --- a/cbt/window.cpp +++ b/cbt/window.cpp @@ -3,6 +3,8 @@ #include #endif +#include + #define GLFW_INCLUDE_NONE #include "GLFW/glfw3.h" @@ -12,6 +14,9 @@ #endif #include "fmt/std.h" +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "stb_image_write.h" +#include "glad/glad.h" #include "cbt/window.hpp" @@ -89,4 +94,37 @@ auto window::stop() -> void { } } +auto window::screenshot(std::string filepath) const -> bool { + if (!m_window || !valid()) { + return false; + } + + int width = 0; + int height = 0; + glfwGetFramebufferSize(m_window, &width, &height); + if (width <= 0 || height <= 0) { + return false; + } + + std::vector pixels(static_cast(width * height * 4)); + glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data()); + + // Flip vertically (OpenGL reads bottom-up; PNG is top-down) + for (int y = 0; y < height / 2; ++y) { + int y2 = height - 1 - y; + for (int x = 0; x < width * 4; ++x) { + std::swap(pixels[static_cast(y * width * 4 + x)], + pixels[static_cast(y2 * width * 4 + x)]); + } + } + + if (stbi_write_png(filepath.c_str(), width, height, 4, pixels.data(), width * 4) != 0) { + fmt::print("Screenshot saved to {}\n", filepath); + return true; + } + + fmt::print("Failed to write screenshot to {}\n", filepath); + return false; +} + } diff --git a/cbt/window.hpp b/cbt/window.hpp index 2a37733..8654165 100644 --- a/cbt/window.hpp +++ b/cbt/window.hpp @@ -18,6 +18,7 @@ public: auto poll_events() -> void; auto raw() const -> GLFWwindow*; auto stop() -> void; + auto screenshot(std::string filepath = "screenshot.png") const -> bool; private: GLFWwindow* m_window = nullptr; diff --git a/cuber.cpp b/cuber.cpp index 5da9cae..3ad7ba1 100644 --- a/cuber.cpp +++ b/cuber.cpp @@ -22,6 +22,7 @@ auto main(int argc, char const* argv[]) -> int { if (arg == "--help" || arg == "-h") { fmt::print("Usage: {} [--duration ] [--help|-h]\n", argv[0]); fmt::print(" --duration Auto-terminate after N seconds (for testing/CI)\n"); + fmt::print(" S key Take screenshot (saved as screenshot.png)\n"); return 0; } if (arg == "--duration" && i + 1 < argc) { @@ -80,6 +81,9 @@ auto main(int argc, char const* argv[]) -> int { if (glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) { win.stop(); } + if (glfwGetKey(win.raw(), GLFW_KEY_S) == GLFW_PRESS) { + win.screenshot(); + } auto now = std::chrono::steady_clock::now(); auto dt = std::chrono::duration(now - prev).count(); diff --git a/deps/Findstb.cmake b/deps/Findstb.cmake index cbd0d2e..cdadca3 100644 --- a/deps/Findstb.cmake +++ b/deps/Findstb.cmake @@ -1,75 +1,49 @@ -# ============================================================================== -# Find stb -# ============================================================================== -# This module fetches the stb single-file public domain libraries. -# -# Targets provided: -# stb::stb - The stb library target -# -# Variables set: -# stb_FOUND - TRUE if stb is available -# stb_LIBRARIES - The stb library target (stb::stb) -# stb_INCLUDE_DIR - Include directories for stb -# stb_VERSION - Version of stb (commit-based) -# -# Usage notes: -# stb headers are header-only but require an implementation macro to be -# defined in exactly ONE translation unit (.cpp file) before including the -# header, e.g.: -# -# #define STB_IMAGE_IMPLEMENTATION -# #include -# -# Each stb header has its own implementation macro. Do NOT define the macro -# in header files or in more than one translation unit or you will get -# duplicate symbol linker errors. -# ============================================================================== - -if (DEFINED _FINDSTB_INCLUDED) - return() -endif() -set(_FINDSTB_INCLUDED TRUE) - -# Use the version passed to find_package(), or default to 2.30 -if (DEFINED stb_FIND_VERSION AND NOT stb_FIND_VERSION STREQUAL "") - set(STB_VERSION "${stb_FIND_VERSION}") -else() - set(STB_VERSION "2fb8c5a3deb2110c89669f8d6f36e5833b556b44") -endif() - -message(STATUS "Fetching stb ${STB_VERSION}") - -include(FetchContent) - -find_program(GIT_EXECUTABLE git) -if (GIT_EXECUTABLE) - set(STB_FETCH_METHOD "GIT") -else() - message(FATAL_ERROR "Fetch with zip not supported.") -endif() - -if (STB_FETCH_METHOD STREQUAL "GIT") - FetchContent_Declare( - stb - GIT_REPOSITORY https://github.com/nothings/stb.git - GIT_TAG ${STB_VERSION} - ) -endif() - -FetchContent_MakeAvailable(stb) - -if (NOT TARGET stb) - add_library(stb INTERFACE) - target_include_directories(stb SYSTEM INTERFACE "${stb_SOURCE_DIR}") -endif() - -if (NOT TARGET stb::stb) - add_library(stb::stb ALIAS stb) -endif() - -set(stb_FOUND TRUE) -set(stb_LIBRARIES stb::stb) -set(stb_VERSION "${STB_VERSION}") -set(stb_INCLUDE_DIR "${stb_SOURCE_DIR}") - -set(STB_LICENSE_FILE "${stb_SOURCE_DIR}/LICENSE" CACHE FILEPATH "Path to stb license file") +# ============================================================================== +# Find stb +# ============================================================================== +# This module fetches the stb single-file public domain libraries +# (stb_image_write.h for PNG writing, etc.). +# +# Targets provided: +# stb::stb - Interface library (add #define STB_IMAGE_WRITE_IMPLEMENTATION +# in exactly one .cpp before including "stb_image_write.h") +# +# Variables set: +# stb_FOUND - TRUE if stb is available +# stb_INCLUDE_DIR - Include directories for stb +# stb_VERSION - Version of stb (commit) +# ============================================================================== + +if (DEFINED _FINDSTB_INCLUDED) + return() +endif() +set(_FINDSTB_INCLUDED TRUE) + +# Pin to a recent stable commit +set(STB_VERSION "master") + +message(STATUS "Fetching stb ${STB_VERSION}") + +include(FetchContent) + +FetchContent_Declare( + stb + GIT_REPOSITORY https://github.com/nothings/stb.git + GIT_TAG ${STB_VERSION} +) + +FetchContent_MakeAvailable(stb) + +add_library(stb INTERFACE) +target_include_directories(stb INTERFACE "${stb_SOURCE_DIR}") + +add_library(stb::stb ALIAS stb) + +set(stb_FOUND TRUE) +set(stb_INCLUDE_DIR "${stb_SOURCE_DIR}") +set(stb_VERSION "${STB_VERSION}") + +# Mark as SYSTEM includes +set_target_properties(stb PROPERTIES + INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${stb_SOURCE_DIR}" +)