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 <string>
8#include <vector>
9
10namespace pixelbullet
11{
12class Filesystem;
13class VirtualPath;
14
16class Bitmap
17{
18public:
20 Bitmap() = default;
21
22 Bitmap(const Filesystem& filesystem, const VirtualPath& assetPath)
23 {
24 Load(filesystem, assetPath);
25 }
26
30 Bitmap(const glm::uvec2& size, uint32_t bytesPerPixel = 4)
31 : size_(size)
32 , bytes_per_pixel_(bytesPerPixel)
33 {
34 data_.resize(CalculateLength(size, bytesPerPixel));
35 }
36
41 Bitmap(const std::vector<uint8_t>& data, const glm::uvec2& size, uint32_t bytesPerPixel = 4)
42 : data_(data)
43 , size_(size)
44 , bytes_per_pixel_(bytesPerPixel)
45 {
46 }
47
48 ~Bitmap() = default;
49
55 bool Load(const Filesystem& filesystem, const VirtualPath& assetPath);
56
60 bool Write(const std::filesystem::path& path) const;
61
64 explicit operator bool() const noexcept
65 {
66 return !data_.empty();
67 }
68
70 uint32_t GetLength() const;
71
72 void Clear();
73
74 // Accessors.
75 const std::string& GetFilename() const
76 {
77 return filename_;
78 }
79 void SetFilename(const std::string& f)
80 {
81 filename_ = f;
82 }
83
84 const std::vector<uint8_t>& GetData() const
85 {
86 return data_;
87 }
88 std::vector<uint8_t>& GetData()
89 {
90 return data_;
91 }
92 void SetData(const std::vector<uint8_t>& d)
93 {
94 data_ = d;
95 }
96 void SetData(std::vector<uint8_t>&& d)
97 {
98 data_ = std::move(d);
99 }
100
101 const glm::uvec2& GetSize() const
102 {
103 return size_;
104 }
105 void SetSize(const glm::uvec2& s)
106 {
107 size_ = s;
108 }
109
110 uint32_t GetBytesPerPixel() const
111 {
112 return bytes_per_pixel_;
113 }
114 void SetBytesPerPixel(uint32_t bpp)
115 {
116 bytes_per_pixel_ = bpp;
117 }
118
119private:
121 static uint32_t CalculateLength(const glm::uvec2& size, uint32_t bytesPerPixel);
122
123private:
124 std::string filename_;
125 std::vector<uint8_t> data_;
126 glm::uvec2 size_{ 0, 0 };
127 uint32_t bytes_per_pixel_ = 0;
128};
129} // namespace pixelbullet
A simple Bitmap class that loads and writes images using lodepng.
Definition bitmap.h:17
uint32_t GetLength() const
Returns the total number of bytes in the image.
Definition bitmap.cc:27
bool Write(const std::filesystem::path &path) const
Writes the bitmap as a PNG to the given filesystem path.
Definition bitmap.cc:75
Bitmap(const glm::uvec2 &size, uint32_t bytesPerPixel=4)
Constructs an empty bitmap of the given size.
Definition bitmap.h:30
Bitmap(const std::vector< uint8_t > &data, const glm::uvec2 &size, uint32_t bytesPerPixel=4)
Constructs a bitmap with preloaded image data.
Definition bitmap.h:41
Bitmap()=default
Default constructor.
bool Load(const Filesystem &filesystem, const VirtualPath &assetPath)
Loads an image from the given asset path. This function uses stb_image to load an image from memory....
Definition bitmap.cc:40
Definition filesystem.h:16
Definition virtual_path.h:10