-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored image class & improved tile export
- Loading branch information
1 parent
7a10355
commit 8528298
Showing
4 changed files
with
233 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,68 @@ | ||
#pragma once | ||
|
||
#include <cassert> | ||
#include <span> | ||
#include <string> | ||
|
||
#include <glm/glm.hpp> | ||
#include <utility> | ||
|
||
class Image { | ||
uint32_t* data_ = nullptr; | ||
int width = 0, height = 0; | ||
int width_ = 0, height_ = 0; | ||
|
||
public: | ||
Image() {} | ||
Image() = default; | ||
Image(std::span<const uint8_t> data); | ||
Image(const std::string& path); | ||
Image(int width, int height); | ||
Image(glm::ivec2 size) : Image(size.x, size.y) {} | ||
|
||
Image(Image&& other) noexcept; | ||
Image(const Image&) = delete; | ||
Image& operator=(const Image&) = delete; | ||
Image(const Image& other); | ||
Image& operator=(const Image& other); | ||
|
||
~Image(); | ||
|
||
void save_png(const std::string& path); | ||
void save_png(const std::string& path) const; | ||
|
||
// copy this image to another image | ||
void copy_to(Image& other, int x, int y) const; | ||
Image slice(int x, int y, int width, int height) const; | ||
void fill(int x, int y, int width, int height, uint32_t color); | ||
|
||
int width() const { return width_; } | ||
int height() const { return height_; } | ||
std::pair<int, int> size() const { return {width_, height_}; } | ||
|
||
glm::ivec2 size() const { return {width, height}; } | ||
uint32_t* data() { return data_; } | ||
const uint32_t* data() const { return data_; } | ||
|
||
uint32_t operator()(int x, int y) const { | ||
assert(x >= 0 && x < width && y >= 0 && y < height); | ||
return data_[x + y * width]; | ||
assert(x >= 0 && x < width_ && y >= 0 && y < height_); | ||
return data_[x + y * width_]; | ||
} | ||
uint32_t& operator()(int x, int y) { | ||
assert(x >= 0 && x < width && y >= 0 && y < height); | ||
return data_[x + y * width]; | ||
assert(x >= 0 && x < width_ && y >= 0 && y < height_); | ||
return data_[x + y * width_]; | ||
} | ||
|
||
Image& operator=(Image&& other) noexcept { | ||
free(data_); | ||
|
||
data_ = other.data_; | ||
width_ = other.width_; | ||
height_ = other.height_; | ||
|
||
other.data_ = nullptr; | ||
other.width_ = 0; | ||
other.height_ = 0; | ||
|
||
return *this; | ||
} | ||
|
||
bool operator==(const Image& other) const { | ||
if(width != other.width || height != other.height) return false; | ||
for(int i = 0; i < width * height; ++i) { | ||
if(width_ != other.width_ || height_ != other.height_) return false; | ||
for(int i = 0; i < width_ * height_; ++i) { | ||
if(data_[i] != other.data_[i]) return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
Image pack(const std::vector<Image>& images, int width, int height); |
Oops, something went wrong.