PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
bitmap.h
1#pragma once
2
3#include <glm/vec2.hpp>
4
5#include <cstdint>
6#include <filesystem>
7#include <span>
8#include <string>
9#include <utility>
10#include <vector>
11
12namespace pixelbullet
13{
14class Filesystem;
15class VirtualPath;
16
18class Bitmap
19{
20public:
21 static constexpr std::uint32_t bytes_per_pixel = 4u;
22
23 Bitmap() = default;
24
25 Bitmap(const Filesystem& filesystem, const VirtualPath& asset_path)
26 {
27 static_cast<void>(load(filesystem, asset_path));
28 }
29
30 explicit Bitmap(const glm::uvec2& size)
31 {
32 static_cast<void>(resize(size));
33 }
34
35 Bitmap(const glm::uvec2& size, std::vector<std::uint8_t> pixels)
36 {
37 static_cast<void>(replace_rgba8(size, std::move(pixels)));
38 }
39
40 ~Bitmap() = default;
41
42 [[nodiscard]] bool load(const Filesystem& filesystem, const VirtualPath& asset_path);
43
44 [[nodiscard]] bool write(const std::filesystem::path& path) const;
45
46 explicit operator bool() const noexcept
47 {
48 return has_valid_storage();
49 }
50
51 [[nodiscard]] std::size_t byte_length() const noexcept
52 {
53 return data_.size();
54 }
55
56 void clear() noexcept;
57
58 [[nodiscard]] const std::string& filename() const noexcept
59 {
60 return filename_;
61 }
62
63 [[nodiscard]] const std::vector<std::uint8_t>& data() const noexcept
64 {
65 return data_;
66 }
67
68 [[nodiscard]] std::span<std::uint8_t> mutable_data() noexcept
69 {
70 return data_;
71 }
72
73 [[nodiscard]] const glm::uvec2& size() const noexcept
74 {
75 return size_;
76 }
77
78 [[nodiscard]] bool replace_rgba8(const glm::uvec2& size, std::vector<std::uint8_t> pixels);
79
80 [[nodiscard]] bool has_valid_storage() const noexcept;
81
82private:
83 [[nodiscard]] static bool checked_byte_length(const glm::uvec2& size, std::size_t* byte_length) noexcept;
84
85 [[nodiscard]] bool resize(const glm::uvec2& size);
86
87 void set_filename(std::string filename)
88 {
89 filename_ = std::move(filename);
90 }
91
92private:
93 std::string filename_;
94 std::vector<std::uint8_t> data_;
95 glm::uvec2 size_{ 0u, 0u };
96};
97} // namespace pixelbullet
Definition filesystem.h:19
Definition virtual_path.h:10