Merge "minui: Clean up the use of rotation
."
am: a9ef617aa7
Change-Id: Id87dce642ff0c8352c4dea2266cbd08558b20145
This commit is contained in:
commit
f22aeefaed
2 changed files with 50 additions and 27 deletions
|
@ -40,11 +40,12 @@ static constexpr uint32_t alpha_mask = 0xff000000;
|
||||||
|
|
||||||
// gr_draw is owned by backends.
|
// gr_draw is owned by backends.
|
||||||
static const GRSurface* gr_draw = nullptr;
|
static const GRSurface* gr_draw = nullptr;
|
||||||
static GRRotation rotation = ROTATION_NONE;
|
static GRRotation rotation = GRRotation::NONE;
|
||||||
|
|
||||||
static bool outside(int x, int y) {
|
static bool outside(int x, int y) {
|
||||||
return x < 0 || x >= (rotation % 2 ? gr_draw->height : gr_draw->width) || y < 0 ||
|
auto swapped = (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT);
|
||||||
y >= (rotation % 2 ? gr_draw->width : gr_draw->height);
|
return x < 0 || x >= (swapped ? gr_draw->height : gr_draw->width) || y < 0 ||
|
||||||
|
y >= (swapped ? gr_draw->width : gr_draw->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
const GRFont* gr_sys_font() {
|
const GRFont* gr_sys_font() {
|
||||||
|
@ -89,36 +90,44 @@ static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
|
||||||
|
|
||||||
// Increments pixel pointer right, with current rotation.
|
// Increments pixel pointer right, with current rotation.
|
||||||
static void incr_x(uint32_t** p, int row_pixels) {
|
static void incr_x(uint32_t** p, int row_pixels) {
|
||||||
if (rotation % 2) {
|
if (rotation == GRRotation::LEFT) {
|
||||||
*p = *p + (rotation == 1 ? 1 : -1) * row_pixels;
|
*p = *p - row_pixels;
|
||||||
} else {
|
} else if (rotation == GRRotation::RIGHT) {
|
||||||
*p = *p + (rotation ? -1 : 1);
|
*p = *p + row_pixels;
|
||||||
|
} else if (rotation == GRRotation::DOWN) {
|
||||||
|
*p = *p - 1;
|
||||||
|
} else { // GRRotation::NONE
|
||||||
|
*p = *p + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increments pixel pointer down, with current rotation.
|
// Increments pixel pointer down, with current rotation.
|
||||||
static void incr_y(uint32_t** p, int row_pixels) {
|
static void incr_y(uint32_t** p, int row_pixels) {
|
||||||
if (rotation % 2) {
|
if (rotation == GRRotation::LEFT) {
|
||||||
*p = *p + (rotation == 1 ? -1 : 1);
|
*p = *p + 1;
|
||||||
} else {
|
} else if (rotation == GRRotation::RIGHT) {
|
||||||
*p = *p + (rotation ? -1 : 1) * row_pixels;
|
*p = *p - 1;
|
||||||
|
} else if (rotation == GRRotation::DOWN) {
|
||||||
|
*p = *p - row_pixels;
|
||||||
|
} else { // GRRotation::NONE
|
||||||
|
*p = *p + row_pixels;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns pixel pointer at given coordinates with rotation adjustment.
|
// Returns pixel pointer at given coordinates with rotation adjustment.
|
||||||
static uint32_t* pixel_at(const GRSurface* surf, int x, int y, int row_pixels) {
|
static uint32_t* pixel_at(const GRSurface* surf, int x, int y, int row_pixels) {
|
||||||
switch (rotation) {
|
switch (rotation) {
|
||||||
case ROTATION_NONE:
|
case GRRotation::NONE:
|
||||||
return reinterpret_cast<uint32_t*>(surf->data) + y * row_pixels + x;
|
return reinterpret_cast<uint32_t*>(surf->data) + y * row_pixels + x;
|
||||||
case ROTATION_RIGHT:
|
case GRRotation::RIGHT:
|
||||||
return reinterpret_cast<uint32_t*>(surf->data) + x * row_pixels + (surf->width - y);
|
return reinterpret_cast<uint32_t*>(surf->data) + x * row_pixels + (surf->width - y);
|
||||||
case ROTATION_DOWN:
|
case GRRotation::DOWN:
|
||||||
return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - y) * row_pixels +
|
return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - y) * row_pixels +
|
||||||
(surf->width - 1 - x);
|
(surf->width - 1 - x);
|
||||||
case ROTATION_LEFT:
|
case GRRotation::LEFT:
|
||||||
return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y;
|
return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y;
|
||||||
default:
|
default:
|
||||||
printf("invalid rotation %d", rotation);
|
printf("invalid rotation %d", static_cast<int>(rotation));
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -256,7 +265,7 @@ void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
|
||||||
|
|
||||||
if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
|
if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
|
||||||
|
|
||||||
if (rotation) {
|
if (rotation != GRRotation::NONE) {
|
||||||
int src_row_pixels = source->row_bytes / source->pixel_bytes;
|
int src_row_pixels = source->row_bytes / source->pixel_bytes;
|
||||||
int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
|
int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
|
||||||
uint32_t* src_py = reinterpret_cast<uint32_t*>(source->data) + sy * source->row_bytes / 4 + sx;
|
uint32_t* src_py = reinterpret_cast<uint32_t*>(source->data) + sy * source->row_bytes / 4 + sx;
|
||||||
|
@ -361,7 +370,19 @@ int gr_init() {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
gr_rotate(DEFAULT_ROTATION);
|
#define __STRINGIFY(x) #x
|
||||||
|
#define STRINGIFY(x) __STRINGIFY(x)
|
||||||
|
|
||||||
|
std::string rotation_str(STRINGIFY(DEFAULT_ROTATION));
|
||||||
|
if (rotation_str == "ROTATION_RIGHT") {
|
||||||
|
gr_rotate(GRRotation::RIGHT);
|
||||||
|
} else if (rotation_str == "ROTATION_DOWN") {
|
||||||
|
gr_rotate(GRRotation::DOWN);
|
||||||
|
} else if (rotation_str == "ROTATION_LEFT") {
|
||||||
|
gr_rotate(GRRotation::LEFT);
|
||||||
|
} else { // "ROTATION_NONE"
|
||||||
|
gr_rotate(GRRotation::NONE);
|
||||||
|
}
|
||||||
|
|
||||||
if (gr_draw->pixel_bytes != 4) {
|
if (gr_draw->pixel_bytes != 4) {
|
||||||
printf("gr_init: Only 4-byte pixel formats supported\n");
|
printf("gr_init: Only 4-byte pixel formats supported\n");
|
||||||
|
@ -379,13 +400,15 @@ void gr_exit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int gr_fb_width() {
|
int gr_fb_width() {
|
||||||
return rotation % 2 ? gr_draw->height - 2 * overscan_offset_y
|
return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
|
||||||
: gr_draw->width - 2 * overscan_offset_x;
|
? gr_draw->height - 2 * overscan_offset_y
|
||||||
|
: gr_draw->width - 2 * overscan_offset_x;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gr_fb_height() {
|
int gr_fb_height() {
|
||||||
return rotation % 2 ? gr_draw->width - 2 * overscan_offset_x
|
return (rotation == GRRotation::LEFT || rotation == GRRotation::RIGHT)
|
||||||
: gr_draw->height - 2 * overscan_offset_y;
|
? gr_draw->width - 2 * overscan_offset_x
|
||||||
|
: gr_draw->height - 2 * overscan_offset_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gr_fb_blank(bool blank) {
|
void gr_fb_blank(bool blank) {
|
||||||
|
|
|
@ -41,11 +41,11 @@ struct GRFont {
|
||||||
int char_height;
|
int char_height;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum GRRotation {
|
enum class GRRotation : int {
|
||||||
ROTATION_NONE = 0,
|
NONE = 0,
|
||||||
ROTATION_RIGHT = 1,
|
RIGHT = 1,
|
||||||
ROTATION_DOWN = 2,
|
DOWN = 2,
|
||||||
ROTATION_LEFT = 3,
|
LEFT = 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initializes the graphics backend and loads font file. Returns 0 on success, or -1 on error. Note
|
// Initializes the graphics backend and loads font file. Returns 0 on success, or -1 on error. Note
|
||||||
|
|
Loading…
Reference in a new issue