spacebox/src/Pixels.hpp

62 lines
1.7 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#pragma once
#include "SDL.h"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/common.hpp"
#include "glm/gtx/integer.hpp"
#include "Box.hpp"
#include "Color.hpp"
#include "Log.hpp"
#include "extension.hpp"
#include "math.hpp"
struct Pixels
{
const int TEXTURE_ACCESS_SCREEN = 128;
void* source = nullptr;
SDL_PixelFormat* format = nullptr;
SDL_Texture* texture;
SDL_Renderer* renderer;
int texture_access = 0;
SDL_Rect rect;
bool allocated = false;
Pixels(SDL_Renderer*, SDL_Texture* texture, const Box&);
Pixels(SDL_Renderer*, SDL_Texture* texture);
Pixels(SDL_Renderer*);
int get_bytes_per_row() const;
Color get(int x, int y);
void set(const SDL_Color&, int x, int y);
void apply();
~Pixels();
template <typename T = void*>
T operator()(int x = 0, int y = 0)
{
std::uint8_t* access = static_cast<std::uint8_t*>(source);
if (x < 0 || x >= rect.w)
{
x = glm::mod(x, static_cast<int>(rect.w));
}
if (y < 0 || y >= rect.y)
{
y = glm::mod(y, static_cast<int>(rect.h));
}
return reinterpret_cast<T>(access + y * get_bytes_per_row() + x * format->BytesPerPixel);
}
};