49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#ifndef Pixels_h_
|
|
#define Pixels_h_
|
|
|
|
#include "SDL.h"
|
|
#include "Box.hpp"
|
|
#include "Color.hpp"
|
|
#include "extension.hpp"
|
|
|
|
struct Sprite;
|
|
|
|
struct Pixels
|
|
{
|
|
|
|
void* source = nullptr;
|
|
SDL_PixelFormat* format = nullptr;
|
|
SDL_Texture* texture;
|
|
int texture_access = 0;
|
|
SDL_Rect rect;
|
|
bool allocated = false;
|
|
|
|
Pixels(SDL_Renderer*, SDL_Texture* texture);
|
|
Pixels(SDL_Renderer*, SDL_Texture* texture, const SDL_Rect&);
|
|
Pixels(Sprite&);
|
|
Pixels(Sprite&, const SDL_Rect&);
|
|
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, int y)
|
|
{
|
|
std::uint8_t* access = static_cast<std::uint8_t*>(source);
|
|
if (x < 0 || x >= rect.w)
|
|
{
|
|
x = sfw::mod(x, static_cast<int>(rect.w));
|
|
}
|
|
if (y < 0 || y >= rect.y)
|
|
{
|
|
y = sfw::mod(y, static_cast<int>(rect.h));
|
|
}
|
|
return reinterpret_cast<T>(access + y * get_bytes_per_row() + x * format->BytesPerPixel);
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|