group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
HudSvgAtlas.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "HudSvgRaster.hpp"
7
8#include <SDL3/SDL_gpu.h>
9
10#include <filesystem>
11#include <optional>
12#include <unordered_map>
13#include <vector>
14
16{
17 float u0 = 0.f;
18 float v0 = 0.f;
19 float u1 = 0.f;
20 float v1 = 0.f;
21 int width = 0;
22 int height = 0;
23};
24
26{
27public:
28 bool init(SDL_GPUDevice* device, std::filesystem::path assetDir);
29 void quit();
30
31 [[nodiscard]] std::optional<HudSvgSprite> sprite(HudIcon id, int width, int height);
32 [[nodiscard]] SDL_GPUTexture* texture() const { return texture_; }
33 [[nodiscard]] SDL_GPUSampler* sampler() const { return sampler_; }
34
35private:
36 struct CacheKey
37 {
39 int width = 0;
40 int height = 0;
41
42 bool operator==(const CacheKey& other) const
43 {
44 return id == other.id && width == other.width && height == other.height;
45 }
46 };
47
49 {
50 std::size_t operator()(const CacheKey& key) const
51 {
52 const auto id = static_cast<std::size_t>(key.id);
53 return id ^ (static_cast<std::size_t>(key.width) << 8u) ^ (static_cast<std::size_t>(key.height) << 24u);
54 }
55 };
56
57 SDL_GPUDevice* device_ = nullptr;
58 SDL_GPUTexture* texture_ = nullptr;
59 SDL_GPUSampler* sampler_ = nullptr;
60 std::filesystem::path assetDir_;
61
62 static constexpr int kAtlasSize = 4096;
63 static constexpr int kPadding = 2;
64
65 int cursorX_ = 0;
66 int cursorY_ = 0;
67 int rowH_ = 0;
68
69 std::unordered_map<CacheKey, HudSvgSprite, CacheKeyHash> sprites_;
70
71 [[nodiscard]] bool createGpuResources();
72 void resetPacking();
73 [[nodiscard]] std::optional<HudSvgSprite> allocate(HudIcon id, int width, int height);
74 [[nodiscard]] bool uploadSlot(const HudSvgBitmap& bitmap, const HudSvgSprite& sprite);
75};
CPU SVG raster helpers used by the HUD SVG atlas and tests.
HudIcon
Icon identifiers for the HUD icon atlas.
Definition HudTypes.hpp:63
@ None
Definition HudTypes.hpp:64
Definition HudSvgAtlas.hpp:26
int rowH_
Definition HudSvgAtlas.hpp:67
SDL_GPUTexture * texture_
Definition HudSvgAtlas.hpp:58
SDL_GPUDevice * device_
Definition HudSvgAtlas.hpp:57
SDL_GPUTexture * texture() const
Definition HudSvgAtlas.hpp:32
bool uploadSlot(const HudSvgBitmap &bitmap, const HudSvgSprite &sprite)
Definition HudSvgAtlas.cpp:143
SDL_GPUSampler * sampler() const
Definition HudSvgAtlas.hpp:33
void resetPacking()
Definition HudSvgAtlas.cpp:100
bool init(SDL_GPUDevice *device, std::filesystem::path assetDir)
Definition HudSvgAtlas.cpp:11
std::filesystem::path assetDir_
Definition HudSvgAtlas.hpp:60
bool createGpuResources()
Definition HudSvgAtlas.cpp:67
static constexpr int kAtlasSize
Definition HudSvgAtlas.hpp:62
std::unordered_map< CacheKey, HudSvgSprite, CacheKeyHash > sprites_
Definition HudSvgAtlas.hpp:69
SDL_GPUSampler * sampler_
Definition HudSvgAtlas.hpp:59
static constexpr int kPadding
Definition HudSvgAtlas.hpp:63
int cursorY_
Definition HudSvgAtlas.hpp:66
std::optional< HudSvgSprite > allocate(HudIcon id, int width, int height)
Definition HudSvgAtlas.cpp:109
void quit()
Definition HudSvgAtlas.cpp:18
std::optional< HudSvgSprite > sprite(HudIcon id, int width, int height)
Definition HudSvgAtlas.cpp:33
int cursorX_
Definition HudSvgAtlas.hpp:65
Definition HudSvgAtlas.hpp:49
std::size_t operator()(const CacheKey &key) const
Definition HudSvgAtlas.hpp:50
Definition HudSvgAtlas.hpp:37
int width
Definition HudSvgAtlas.hpp:39
HudIcon id
Definition HudSvgAtlas.hpp:38
bool operator==(const CacheKey &other) const
Definition HudSvgAtlas.hpp:42
int height
Definition HudSvgAtlas.hpp:40
Definition HudSvgRaster.hpp:13
Definition HudSvgAtlas.hpp:16
int width
Definition HudSvgAtlas.hpp:21
float u0
Definition HudSvgAtlas.hpp:17
float v0
Definition HudSvgAtlas.hpp:18
float v1
Definition HudSvgAtlas.hpp:20
float u1
Definition HudSvgAtlas.hpp:19
int height
Definition HudSvgAtlas.hpp:22