// SPDX-License-Identifier: MIT // Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root). // // The real-time window (bozo/app/window.h). Written for BozoEngine on SDL3 (the fork's window and present loop lived in // a file that cannot move: phase4-inventory.md section 2.2). #include "bozo/app/window.h" #include "bozo/app/app.h" #include "bozo/app/device_pad.h" #include "bozo/app/frame_pacer.h" #include "bozo/app/input_bindings.h" #include "bozo/app/panel.h" #include "reshade_bridge.h" #include "bozo/app/png.h" #include "bozo/app/profiler.h" #include "bozo/app/video_settings.h" #include "bozo/render/gl.h" #include "bozo/render/renderer.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace bozo::app { // The settings window's Profiler tab (profiler_tab.cpp) and its reports' folder. void registerProfilerTab(); std::string profilesFolder(); namespace { struct GameThread { const Game *game = nullptr; int result = 0; std::atomic done{false}; }; int SDLCALL gameThreadMain(void *data) { auto *t = static_cast(data); profiler::registerThread("game"); // (the Profiler tab's and PS2X_PROFILE's) t->result = t->game->run(); profiler::unregisterThread(); t->done.store(true, std::memory_order_release); return t->result; } void *procAddress(const char *name) { return reinterpret_cast(SDL_GL_GetProcAddress(name)); } double seconds(std::chrono::steady_clock::time_point a, std::chrono::steady_clock::time_point b) { return std::chrono::duration(b - a).count(); } // The PNGs written on a thread of their own: the window's thread only reads the pixels back (a burst of ten a // second, a sequence of consecutive presents, would otherwise hitch the presents). class CaptureWriter { public: CaptureWriter() : m_thread([this] { run(); }) {} void readOffscreen(bool offscreen) { m_offscreen = offscreen; } ~CaptureWriter() { { std::lock_guard lock(m_mutex); m_stop = true; } m_wake.notify_all(); m_thread.join(); } // The back buffer (pixels w x h) now, saved as path; `compressed`: through SDL_SavePNG (a player's // screenshot, F12; a save state's picture, scaled down to `maxWidth` when it is set), else the quick // uncompressed writer (captures, many a second). void capture(const std::string &path, int w, int h, bool compressed = false, int maxWidth = 0) { const render::gl::Api &g = render::gl::api(); Job job; job.path = path; job.width = w; job.height = h; job.compressed = compressed; job.maxWidth = maxWidth; job.pixels.resize(static_cast(w) * h * 4u); // From the back buffer, or from the offscreen picture the window presents into when nothing may depend // on the window being visible (WindowOptions::captureScene / hidden, PS2X_CAPTURE_SCENE=1). g.ReadBuffer(m_offscreen ? render::gl::GLenum(0x8CE0) /* GL_COLOR_ATTACHMENT0 */ : render::gl::kBack); g.PixelStorei(render::gl::kPackAlignment, 4); g.ReadPixels(0, 0, w, h, render::gl::kRgba, render::gl::kUnsignedByte, job.pixels.data()); { std::lock_guard lock(m_mutex); m_jobs.push_back(std::move(job)); } m_wake.notify_one(); } private: struct Job { std::string path; int width = 0, height = 0; bool compressed = false; int maxWidth = 0; std::vector pixels; // bottom-up, as GL reads them }; void run() { for (;;) { Job job; { std::unique_lock lock(m_mutex); m_wake.wait(lock, [this] { return m_stop || !m_jobs.empty(); }); if (m_jobs.empty()) return; // stopping, everything written job = std::move(m_jobs.front()); m_jobs.pop_front(); } const size_t stride = static_cast(job.width) * 4u; std::vector rows(job.pixels.size()); for (int y = 0; y < job.height; ++y) // bottom-up to top-down std::copy_n(job.pixels.data() + static_cast(job.height - 1 - y) * stride, stride, rows.data() + static_cast(y) * stride); bool ok = false; if (job.compressed) { for (size_t i = 3; i < rows.size(); i += 4) rows[i] = 0xFF; // the picture's alpha is the renderer's, not the player's if (SDL_Surface *surface = SDL_CreateSurfaceFrom(job.width, job.height, SDL_PIXELFORMAT_RGBA32, rows.data(), static_cast(stride))) { SDL_Surface *saved = surface; if (job.maxWidth > 0 && job.width > job.maxWidth) if (SDL_Surface *small = SDL_ScaleSurface(surface, job.maxWidth, std::max(1, job.height * job.maxWidth / job.width), SDL_SCALEMODE_LINEAR)) saved = small; ok = SDL_SavePNG(saved, job.path.c_str()); if (saved != surface) SDL_DestroySurface(saved); SDL_DestroySurface(surface); } } else ok = writePng(job.path, rows.data(), static_cast(job.width), static_cast(job.height)); std::fprintf(stderr, "[window] %s %s\n", ok ? "saved" : "could not save", job.path.c_str()); } } std::mutex m_mutex; std::condition_variable m_wake; std::deque m_jobs; bool m_stop = false; bool m_offscreen = false; // captures read the offscreen picture (WindowOptions::captureScene) std::thread m_thread; }; // A save state's picture is taken at the next present after its save was asked (F5, the Save states tab or // captureStatePicture). std::atomic g_pictureSlot{-1}; std::atomic g_pictureCrop{0.0f}; // setPictureCrop std::atomic g_pictureAspect{0.0f}; // setPictureAspect // F12's file: /screenshots/screenshot__