Code cleanup

This commit is contained in:
Maschell
2024-11-27 20:44:36 +01:00
parent 5893ac2f18
commit 6f7afe387e
95 changed files with 1517 additions and 1212 deletions

View File

@@ -13,23 +13,23 @@
// buffer width
#define DRC_WIDTH 0x380
bool DrawUtils::isBackBuffer;
bool DrawUtils::mIsBackBuffer;
uint8_t *DrawUtils::tvBuffer = nullptr;
uint32_t DrawUtils::tvSize = 0;
uint8_t *DrawUtils::drcBuffer = nullptr;
uint32_t DrawUtils::drcSize = 0;
uint32_t DrawUtils::usedTVWidth = 1280;
float DrawUtils::usedTVScale = 1.5f;
static SFT pFont = {};
uint8_t *DrawUtils::mTVBuffer = nullptr;
uint32_t DrawUtils::mTVSize = 0;
uint8_t *DrawUtils::mDRCBuffer = nullptr;
uint32_t DrawUtils::mDRCSize = 0;
uint32_t DrawUtils::mUsedTVWidth = 1280;
float DrawUtils::mUsedTVScale = 1.5f;
static SFT pFont = {};
static Color font_col(0xFFFFFFFF);
void DrawUtils::initBuffers(void *tvBuffer_, uint32_t tvSize_, void *drcBuffer_, uint32_t drcSize_) {
DrawUtils::tvBuffer = (uint8_t *) tvBuffer_;
DrawUtils::tvSize = tvSize_;
DrawUtils::drcBuffer = (uint8_t *) drcBuffer_;
DrawUtils::drcSize = drcSize_;
void DrawUtils::initBuffers(void *tvBuffer, const uint32_t tvSize, void *drcBuffer, const uint32_t drcSize) {
DrawUtils::mTVBuffer = static_cast<uint8_t *>(tvBuffer);
DrawUtils::mTVSize = tvSize;
DrawUtils::mDRCBuffer = static_cast<uint8_t *>(drcBuffer);
DrawUtils::mDRCSize = drcSize;
bool bigScale = true;
switch (TVEGetCurrentPort()) {
@@ -67,53 +67,53 @@ void DrawUtils::initBuffers(void *tvBuffer_, uint32_t tvSize_, void *drcBuffer_,
auto tvScanBufferWidth = DCReadReg32(SCREEN_TV, D1GRPH_X_END_REG);
if (tvScanBufferWidth == 640) { // 480i/480p/576i 4:3
DrawUtils::usedTVWidth = 640;
DrawUtils::mUsedTVWidth = 640;
SetDCPitchReg(SCREEN_TV, 640);
DrawUtils::usedTVScale = bigScale ? 0.75 : 0.75f;
DrawUtils::mUsedTVScale = bigScale ? 0.75 : 0.75f;
} else if (tvScanBufferWidth == 854) { // 480i/480p/576i 16:9
DrawUtils::usedTVWidth = 896;
DrawUtils::mUsedTVWidth = 896;
SetDCPitchReg(SCREEN_TV, 896);
DrawUtils::usedTVScale = bigScale ? 1.0 : 1.0f;
DrawUtils::mUsedTVScale = bigScale ? 1.0 : 1.0f;
} else if (tvScanBufferWidth == 1280) { // 720p 16:9
DrawUtils::usedTVWidth = 1280;
DrawUtils::mUsedTVWidth = 1280;
SetDCPitchReg(SCREEN_TV, 1280);
if (bigScale) {
DrawUtils::usedTVScale = 1.5;
DrawUtils::mUsedTVScale = 1.5;
} else {
DrawUtils::usedTVScale = 0.75f;
DrawUtils::mUsedTVScale = 0.75f;
if (tvResolution == AVM_TV_RESOLUTION_480I_PAL60 || tvResolution == AVM_TV_RESOLUTION_480I) {
AVMTvAspectRatio tvAspectRatio;
if (AVMGetTVAspectRatio(&tvAspectRatio) && tvAspectRatio == AVM_TV_ASPECT_RATIO_16_9) {
DEBUG_FUNCTION_LINE_WARN("force big scaling for 480i + 16:9");
DrawUtils::usedTVScale = 1.5;
DrawUtils::mUsedTVScale = 1.5;
}
}
}
} else if (tvScanBufferWidth == 1920) { // 1080i/1080p 16:9
DrawUtils::usedTVWidth = 1920;
DrawUtils::mUsedTVWidth = 1920;
SetDCPitchReg(SCREEN_TV, 1920);
DrawUtils::usedTVScale = bigScale ? 2.25 : 1.125f;
DrawUtils::mUsedTVScale = bigScale ? 2.25 : 1.125f;
} else {
DrawUtils::usedTVWidth = tvScanBufferWidth;
DrawUtils::mUsedTVWidth = tvScanBufferWidth;
SetDCPitchReg(SCREEN_TV, tvScanBufferWidth);
DrawUtils::usedTVScale = 1.0f;
DrawUtils::mUsedTVScale = 1.0f;
DEBUG_FUNCTION_LINE_WARN("Unknown tv width detected, config menu might not show properly");
}
}
void DrawUtils::beginDraw() {
uint32_t pixel = *(uint32_t *) tvBuffer;
const uint32_t pixel = *reinterpret_cast<uint32_t *>(mTVBuffer);
// check which buffer is currently used
OSScreenPutPixelEx(SCREEN_TV, 0, 0, 0xABCDEF90);
if (*(uint32_t *) tvBuffer == 0xABCDEF90) {
isBackBuffer = false;
if (*reinterpret_cast<uint32_t *>(mTVBuffer) == 0xABCDEF90) {
mIsBackBuffer = false;
} else {
isBackBuffer = true;
mIsBackBuffer = true;
}
// restore the pixel we used for checking
*(uint32_t *) tvBuffer = pixel;
*reinterpret_cast<uint32_t *>(mTVBuffer) = pixel;
}
void DrawUtils::endDraw() {
@@ -125,58 +125,58 @@ void DrawUtils::endDraw() {
OSScreenFlipBuffersEx(SCREEN_TV);
}
void DrawUtils::clear(Color col) {
void DrawUtils::clear(const Color col) {
OSScreenClearBufferEx(SCREEN_TV, col.color);
OSScreenClearBufferEx(SCREEN_DRC, col.color);
}
void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
void DrawUtils::drawPixel(const uint32_t x, const uint32_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) {
if (a == 0) {
return;
}
float opacity = a / 255.0f;
const float opacity = a / 255.0f;
// put pixel in the drc buffer
uint32_t i = (x + y * DRC_WIDTH) * 4;
if (i + 3 < drcSize / 2) {
if (isBackBuffer) {
i += drcSize / 2;
if (i + 3 < mDRCSize / 2) {
if (mIsBackBuffer) {
i += mDRCSize / 2;
}
if (a == 0xFF) {
drcBuffer[i] = r;
drcBuffer[i + 1] = g;
drcBuffer[i + 2] = b;
mDRCBuffer[i] = r;
mDRCBuffer[i + 1] = g;
mDRCBuffer[i + 2] = b;
} else {
drcBuffer[i] = r * opacity + drcBuffer[i] * (1 - opacity);
drcBuffer[i + 1] = g * opacity + drcBuffer[i + 1] * (1 - opacity);
drcBuffer[i + 2] = b * opacity + drcBuffer[i + 2] * (1 - opacity);
mDRCBuffer[i] = r * opacity + mDRCBuffer[i] * (1 - opacity);
mDRCBuffer[i + 1] = g * opacity + mDRCBuffer[i + 1] * (1 - opacity);
mDRCBuffer[i + 2] = b * opacity + mDRCBuffer[i + 2] * (1 - opacity);
}
}
// scale and put pixel in the tv buffer
for (uint32_t yy = (y * DrawUtils::usedTVScale); yy < ((y * DrawUtils::usedTVScale) + (uint32_t) DrawUtils::usedTVScale); yy++) {
for (uint32_t xx = (x * DrawUtils::usedTVScale); xx < ((x * DrawUtils::usedTVScale) + (uint32_t) DrawUtils::usedTVScale); xx++) {
uint32_t i = (xx + yy * DrawUtils::usedTVWidth) * 4;
if (i + 3 < tvSize / 2) {
if (isBackBuffer) {
i += tvSize / 2;
for (uint32_t yy = (y * DrawUtils::mUsedTVScale); yy < ((y * DrawUtils::mUsedTVScale) + (uint32_t) DrawUtils::mUsedTVScale); yy++) {
for (uint32_t xx = (x * DrawUtils::mUsedTVScale); xx < ((x * DrawUtils::mUsedTVScale) + (uint32_t) DrawUtils::mUsedTVScale); xx++) {
uint32_t i = (xx + yy * DrawUtils::mUsedTVWidth) * 4;
if (i + 3 < mTVSize / 2) {
if (mIsBackBuffer) {
i += mTVSize / 2;
}
if (a == 0xFF) {
tvBuffer[i] = r;
tvBuffer[i + 1] = g;
tvBuffer[i + 2] = b;
mTVBuffer[i] = r;
mTVBuffer[i + 1] = g;
mTVBuffer[i + 2] = b;
} else {
tvBuffer[i] = r * opacity + tvBuffer[i] * (1 - opacity);
tvBuffer[i + 1] = g * opacity + tvBuffer[i + 1] * (1 - opacity);
tvBuffer[i + 2] = b * opacity + tvBuffer[i + 2] * (1 - opacity);
mTVBuffer[i] = r * opacity + mTVBuffer[i] * (1 - opacity);
mTVBuffer[i + 1] = g * opacity + mTVBuffer[i + 1] * (1 - opacity);
mTVBuffer[i + 2] = b * opacity + mTVBuffer[i + 2] * (1 - opacity);
}
}
}
}
}
void DrawUtils::drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col) {
void DrawUtils::drawRectFilled(const uint32_t x, const uint32_t y, const uint32_t w, const uint32_t h, const Color col) {
for (uint32_t yy = y; yy < y + h; yy++) {
for (uint32_t xx = x; xx < x + w; xx++) {
drawPixel(xx, yy, col);
@@ -184,22 +184,22 @@ void DrawUtils::drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, C
}
}
void DrawUtils::drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col) {
void DrawUtils::drawRect(const uint32_t x, const uint32_t y, const uint32_t w, const uint32_t h, const uint32_t borderSize, const Color col) {
drawRectFilled(x, y, w, borderSize, col);
drawRectFilled(x, y + h - borderSize, w, borderSize, col);
drawRectFilled(x, y, borderSize, h, col);
drawRectFilled(x + w - borderSize, y, borderSize, h, col);
}
void DrawUtils::drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t *data) {
void DrawUtils::drawBitmap(const uint32_t x, const uint32_t y, const uint32_t target_width, const uint32_t target_height, const uint8_t *data) {
if (data[0] != 'B' || data[1] != 'M') {
// invalid header
return;
}
uint32_t dataPos = __builtin_bswap32(*(uint32_t *) &(data[0x0A]));
uint32_t width = __builtin_bswap32(*(uint32_t *) &(data[0x12]));
uint32_t height = __builtin_bswap32(*(uint32_t *) &(data[0x16]));
uint32_t dataPos = __builtin_bswap32(*(uint32_t *) &(data[0x0A]));
const uint32_t width = __builtin_bswap32(*(uint32_t *) &(data[0x12]));
const uint32_t height = __builtin_bswap32(*(uint32_t *) &(data[0x16]));
if (dataPos == 0) {
dataPos = 54;
@@ -218,13 +218,13 @@ void DrawUtils::drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32
}
static void png_read_data(png_structp png_ptr, png_bytep outBytes, png_size_t byteCountToRead) {
void **data = (void **) png_get_io_ptr(png_ptr);
void **data = static_cast<void **>(png_get_io_ptr(png_ptr));
memcpy(outBytes, *data, byteCountToRead);
*((uint8_t **) data) += byteCountToRead;
*reinterpret_cast<uint8_t **>(data) += byteCountToRead;
}
void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
void DrawUtils::drawPNG(const uint32_t x, const uint32_t y, const uint8_t *data) {
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (png_ptr == nullptr) {
return;
@@ -240,17 +240,17 @@ void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
png_read_info(png_ptr, info_ptr);
uint32_t width = 0;
uint32_t height = 0;
int bitDepth = 0;
int colorType = -1;
uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, nullptr, nullptr, nullptr);
uint32_t width = 0;
uint32_t height = 0;
int bitDepth = 0;
int colorType = -1;
const uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, nullptr, nullptr, nullptr);
if (retval != 1) {
return;
}
uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr);
auto *rowData = new uint8_t[bytesPerRow];
const uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr);
auto *rowData = new uint8_t[bytesPerRow];
for (uint32_t yy = y; yy < y + height; yy++) {
png_read_row(png_ptr, (png_bytep) rowData, nullptr);
@@ -302,17 +302,17 @@ void DrawUtils::setFontSize(uint32_t size) {
sft_lmetrics(&pFont, &metrics);
}
void DrawUtils::setFontColor(Color col) {
void DrawUtils::setFontColor(const Color col) {
font_col = col;
}
static void draw_freetype_bitmap(SFT_Image *bmp, int32_t x, int32_t y) {
static void draw_freetype_bitmap(const SFT_Image *bmp, const int32_t x, const int32_t y) {
int32_t i, j, p, q;
int32_t x_max = x + bmp->width;
int32_t y_max = y + bmp->height;
auto *src = (uint8_t *) bmp->pixels;
const auto *src = static_cast<uint8_t *>(bmp->pixels);
for (i = x, p = 0; i < x_max; i++, p++) {
for (j = y, q = 0; j < y_max; j++, q++) {
@@ -320,13 +320,13 @@ static void draw_freetype_bitmap(SFT_Image *bmp, int32_t x, int32_t y) {
continue;
}
float opacity = src[q * bmp->width + p] / 255.0f;
const float opacity = src[q * bmp->width + p] / 255.0f;
DrawUtils::drawPixel(i, j, font_col.r, font_col.g, font_col.b, font_col.a * opacity);
}
}
}
void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRight) {
void DrawUtils::print(const uint32_t x, const uint32_t y, const char *string, const bool alignRight) {
auto *buffer = new wchar_t[strlen(string) + 1];
size_t num = mbstowcs(buffer, string, strlen(string));
@@ -342,9 +342,9 @@ void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRigh
delete[] buffer;
}
void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight) {
auto penX = (int32_t) x;
auto penY = (int32_t) y;
void DrawUtils::print(const uint32_t x, const uint32_t y, const wchar_t *string, const bool alignRight) {
auto penX = static_cast<int32_t>(x);
auto penY = static_cast<int32_t>(y);
if (alignRight) {
penX -= getTextWidth(string);
@@ -392,8 +392,8 @@ void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignR
DEBUG_FUNCTION_LINE_ERR("Failed to render glyph");
return;
} else {
draw_freetype_bitmap(&img, (int32_t) (penX + mtx.leftSideBearing), (int32_t) (penY + mtx.yOffset));
penX += (int32_t) mtx.advanceWidth;
draw_freetype_bitmap(&img, static_cast<int32_t>(penX + mtx.leftSideBearing), penY + mtx.yOffset);
penX += static_cast<int32_t>(mtx.advanceWidth);
}
}
}
@@ -402,8 +402,7 @@ void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignR
uint32_t DrawUtils::getTextWidth(const char *string) {
auto *buffer = new wchar_t[strlen(string) + 1];
size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) {
if (const size_t num = mbstowcs(buffer, string, strlen(string)); num > 0) {
buffer[num] = 0;
} else {
wchar_t *tmp = buffer;
@@ -411,7 +410,7 @@ uint32_t DrawUtils::getTextWidth(const char *string) {
;
}
uint32_t width = getTextWidth(buffer);
const uint32_t width = getTextWidth(buffer);
delete[] buffer;
return width;
@@ -427,9 +426,9 @@ uint32_t DrawUtils::getTextWidth(const wchar_t *string) {
if (sft_gmetrics(&pFont, gid, &mtx) < 0) {
DEBUG_FUNCTION_LINE_ERR("bad glyph metrics");
}
width += (int32_t) mtx.advanceWidth;
width += static_cast<int32_t>(mtx.advanceWidth);
}
}
return (uint32_t) width;
return width;
}