PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
Bitmap.hpp
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{
12 class VirtualPath;
13
15 class Bitmap
16 {
17 public:
19 Bitmap() = default;
20
23 explicit Bitmap(const VirtualPath& assetPath)
24 {
25 Load(assetPath);
26 }
27
31 Bitmap(const glm::uvec2& size, uint32_t bytesPerPixel = 4)
32 : m_Size(size)
33 , m_BytesPerPixel(bytesPerPixel)
34 {
35 m_Data.resize(CalculateLength(size, bytesPerPixel));
36 }
37
42 Bitmap(const std::vector<uint8_t>& data, const glm::uvec2& size, uint32_t bytesPerPixel = 4)
43 : m_Data(data)
44 , m_Size(size)
45 , m_BytesPerPixel(bytesPerPixel)
46 {
47 }
48
49 ~Bitmap() = default;
50
56 bool Load(const VirtualPath& assetPath);
57
61 bool Write(const VirtualPath& assetPath) const;
62
65 explicit operator bool() const noexcept
66 {
67 return !m_Data.empty();
68 }
69
71 uint32_t GetLength() const;
72
73 void Clear();
74
75 // Accessors.
76 const std::string& GetFilename() const
77 {
78 return m_Filename;
79 }
80 void SetFilename(const std::string& f)
81 {
82 m_Filename = f;
83 }
84
85 const std::vector<uint8_t>& GetData() const
86 {
87 return m_Data;
88 }
89 std::vector<uint8_t>& GetData()
90 {
91 return m_Data;
92 }
93 void SetData(const std::vector<uint8_t>& d)
94 {
95 m_Data = d;
96 }
97 void SetData(std::vector<uint8_t>&& d)
98 {
99 m_Data = std::move(d);
100 }
101
102 const glm::uvec2& GetSize() const
103 {
104 return m_Size;
105 }
106 void SetSize(const glm::uvec2& s)
107 {
108 m_Size = s;
109 }
110
111 uint32_t GetBytesPerPixel() const
112 {
113 return m_BytesPerPixel;
114 }
115 void SetBytesPerPixel(uint32_t bpp)
116 {
117 m_BytesPerPixel = bpp;
118 }
119
120 private:
122 static uint32_t CalculateLength(const glm::uvec2& size, uint32_t bytesPerPixel);
123
124 private:
125 std::string m_Filename;
126 std::vector<uint8_t> m_Data;
127 glm::uvec2 m_Size{ 0, 0 };
128 uint32_t m_BytesPerPixel = 0;
129 };
130} // namespace PixelBullet
A simple Bitmap class that loads and writes images using stb.
Definition Bitmap.hpp:16
Bitmap(const std::vector< uint8_t > &data, const glm::uvec2 &size, uint32_t bytesPerPixel=4)
Constructs a bitmap with preloaded image data.
Definition Bitmap.hpp:42
Bitmap()=default
Default constructor.
uint32_t GetLength() const
Returns the total number of bytes in the image.
Definition Bitmap.cpp:18
Bitmap(const glm::uvec2 &size, uint32_t bytesPerPixel=4)
Constructs an empty bitmap of the given size.
Definition Bitmap.hpp:31
bool Write(const VirtualPath &assetPath) const
Writes the bitmap as a PNG to the given asset path.
Definition Bitmap.cpp:71
bool Load(const VirtualPath &assetPath)
Loads an image from the given asset path. This function uses stb_image to load an image from memory....
Definition Bitmap.cpp:31
Bitmap(const VirtualPath &assetPath)
Constructs a bitmap from an asset path.
Definition Bitmap.hpp:23
Definition VirtualPath.hpp:11