2024-09-15 22:31:54 +02:00

101 lines
2.9 KiB
C++

#pragma once
#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_APPLICATION_HPP_INCLUDED)
#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_APPLICATION_HPP_INCLUDED 1
#include <cstring>
#include <mijin/virtual_filesystem/stacked.hpp>
#include "./sdlpp/event.hpp"
#include "./sdlpp/gpu.hpp"
#include "./sdlpp/window.hpp"
namespace sdl_gpu_test
{
struct AppInitArgs
{
std::span<const char*> programArgs;
};
struct AppCleanupArgs
{
};
struct AppUpdateArgs
{
float secondsSinceStart = 0.f;
float tickSeconds = 0.f;
};
class Application
{
protected:
sdlpp::Window mWindow;
sdlpp::GPUDevice mDevice;
mijin::StackedFileSystemAdapter mFileSystem;
bool mRunning = true;
public:
virtual ~Application() noexcept = default;
virtual void init(const AppInitArgs& args);
virtual void cleanup(const AppCleanupArgs& args);
virtual void update(const AppUpdateArgs& args);
virtual void handleKeyboardEvent(const sdlpp::KeyboardEvent& /* event */) {}
virtual void handleMouseButtonEvent(const sdlpp::MouseButtonEvent& /* event */) {}
virtual void handleMouseMotionEvent(const sdlpp::MouseMotionEvent& /* event */) {}
virtual void handleGamepadButtonEvent(const sdlpp::GamepadButtonEvent& /* event */) {}
void run(std::span<const char*> args);
// utility stuff
[[nodiscard]]
std::string getFileContentsText(const fs::path& path) ;
[[nodiscard]]
mijin::TypelessBuffer getFileContentsBinary(const fs::path& path);
[[nodiscard]]
sdlpp::GPUShader loadShader(const fs::path& path, sdlpp::GPUShaderCreateArgs args);
[[nodiscard]]
sdlpp::GPUTexture loadTexture(const fs::path& path, sdlpp::GPUTextureCreateArgs& inout_args);
void uploadTextureData(const sdlpp::GPUTexture& texture, const struct Bitmap& bitmap) const;
template<typename TVertex>
void uploadVertexData(const sdlpp::GPUBuffer& vertexBuffer, std::span<TVertex> vertices) const;
};
template<typename TVertex>
void Application::uploadVertexData(const sdlpp::GPUBuffer& vertexBuffer, std::span<TVertex> vertices) const
{
sdlpp::GPUTransferBuffer transferBuffer;
transferBuffer.create(mDevice, {
.usage = sdlpp::GPUTransferBufferUsage::UPLOAD,
.size = static_cast<Uint32>(vertices.size_bytes())
});
void* ptr = transferBuffer.map();
std::memcpy(ptr, vertices.data(), vertices.size_bytes());
transferBuffer.unmap();
sdlpp::GPUCommandBuffer cmdBuffer = mDevice.acquireCommandBuffer();
sdlpp::GPUCopyPass copyPass = cmdBuffer.beginCopyPass();
copyPass.uploadToGPUBuffer(
/* source = */ {
.transferBuffer = transferBuffer
},
/* destination = */ {
.buffer = vertexBuffer,
.size = static_cast<Uint32>(vertices.size_bytes())
}
);
copyPass.end();
cmdBuffer.submit();
}
} // namespace sdl_gpu_test
#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_APPLICATION_HPP_INCLUDED)