Formatting

This commit is contained in:
Maschell 2022-01-22 14:44:39 +01:00
parent 7e5f174076
commit ad693c94ff
3 changed files with 101 additions and 107 deletions

View File

@ -134,10 +134,9 @@ void bootHomebrewChannel() {
// test if the OHBC or HBC is installed // test if the OHBC or HBC is installed
if (IOSUHAX_FSA_GetStat(fsaFd, "/vol/storage_slccmpt01/title/00010001/4f484243/content/00000000.app", &stat) >= 0) { if (IOSUHAX_FSA_GetStat(fsaFd, "/vol/storage_slccmpt01/title/00010001/4f484243/content/00000000.app", &stat) >= 0) {
titleId = 0x000100014f484243; // 'OHBC' titleId = 0x000100014F484243L; // 'OHBC'
} } else if (IOSUHAX_FSA_GetStat(fsaFd, "/vol/storage_slccmpt01/title/00010001/4c554c5a/content/00000000.app", &stat) >= 0) {
else if (IOSUHAX_FSA_GetStat(fsaFd, "/vol/storage_slccmpt01/title/00010001/4c554c5a/content/00000000.app", &stat) >= 0) { titleId = 0x000100014C554C5AL; // 'LULZ'
titleId = 0x000100014c554c5a; // 'LULZ'
} else { } else {
DEBUG_FUNCTION_LINE("Cannot find HBC, booting vWii System Menu"); DEBUG_FUNCTION_LINE("Cannot find HBC, booting vWii System Menu");
} }

View File

@ -16,9 +16,9 @@
bool DrawUtils::isBackBuffer; bool DrawUtils::isBackBuffer;
uint8_t* DrawUtils::tvBuffer = nullptr; uint8_t *DrawUtils::tvBuffer = nullptr;
uint32_t DrawUtils::tvSize = 0; uint32_t DrawUtils::tvSize = 0;
uint8_t* DrawUtils::drcBuffer = nullptr; uint8_t *DrawUtils::drcBuffer = nullptr;
uint32_t DrawUtils::drcSize = 0; uint32_t DrawUtils::drcSize = 0;
// Don't put those into the class or we have to include ft everywhere // Don't put those into the class or we have to include ft everywhere
@ -26,7 +26,7 @@ static FT_Library ft_lib = nullptr;
static FT_Face ft_face = nullptr; static FT_Face ft_face = nullptr;
static Color font_col = {0xFFFFFFFF}; static Color font_col = {0xFFFFFFFF};
void* DrawUtils::InitOSScreen(){ void *DrawUtils::InitOSScreen() {
OSScreenInit(); OSScreenInit();
uint32_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV); uint32_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV);
@ -45,32 +45,29 @@ void* DrawUtils::InitOSScreen(){
return screenBuffer; return screenBuffer;
} }
void DrawUtils::initBuffers(void* tvBuffer, uint32_t tvSize, void* drcBuffer, uint32_t drcSize) void DrawUtils::initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize) {
{ DrawUtils::tvBuffer = (uint8_t *) tvBuffer;
DrawUtils::tvBuffer = (uint8_t*) tvBuffer;
DrawUtils::tvSize = tvSize; DrawUtils::tvSize = tvSize;
DrawUtils::drcBuffer = (uint8_t*) drcBuffer; DrawUtils::drcBuffer = (uint8_t *) drcBuffer;
DrawUtils::drcSize = drcSize; DrawUtils::drcSize = drcSize;
} }
void DrawUtils::beginDraw() void DrawUtils::beginDraw() {
{ uint32_t pixel = *(uint32_t *) tvBuffer;
uint32_t pixel = *(uint32_t*) tvBuffer;
// check which buffer is currently used // check which buffer is currently used
OSScreenPutPixelEx(SCREEN_TV, 0, 0, 0xABCDEF90); OSScreenPutPixelEx(SCREEN_TV, 0, 0, 0xABCDEF90);
if (*(uint32_t*) tvBuffer == 0xABCDEF90) { if (*(uint32_t *) tvBuffer == 0xABCDEF90) {
isBackBuffer = false; isBackBuffer = false;
} else { } else {
isBackBuffer = true; isBackBuffer = true;
} }
// restore the pixel we used for checking // restore the pixel we used for checking
*(uint32_t*) tvBuffer = pixel; *(uint32_t *) tvBuffer = pixel;
} }
void DrawUtils::endDraw() void DrawUtils::endDraw() {
{
// OSScreenFlipBuffersEx already flushes the cache? // OSScreenFlipBuffersEx already flushes the cache?
// DCFlushRange(tvBuffer, tvSize); // DCFlushRange(tvBuffer, tvSize);
// DCFlushRange(drcBuffer, drcSize); // DCFlushRange(drcBuffer, drcSize);
@ -79,14 +76,12 @@ void DrawUtils::endDraw()
OSScreenFlipBuffersEx(SCREEN_TV); OSScreenFlipBuffersEx(SCREEN_TV);
} }
void DrawUtils::clear(Color col) void DrawUtils::clear(Color col) {
{
OSScreenClearBufferEx(SCREEN_TV, col.color); OSScreenClearBufferEx(SCREEN_TV, col.color);
OSScreenClearBufferEx(SCREEN_DRC, 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(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
{
float opacity = a / 255.0f; float opacity = a / 255.0f;
// put pixel in the drc buffer // put pixel in the drc buffer
@ -96,12 +91,11 @@ void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t
i += drcSize / 2; i += drcSize / 2;
} }
if (a == 0xFF) { if (a == 0xFF) {
drcBuffer[i ] = r; drcBuffer[i] = r;
drcBuffer[i + 1] = g; drcBuffer[i + 1] = g;
drcBuffer[i + 2] = b; drcBuffer[i + 2] = b;
} } else {
else { drcBuffer[i] = r * opacity + drcBuffer[i] * (1 - opacity);
drcBuffer[i ] = r * opacity + drcBuffer[i ] * (1 - opacity);
drcBuffer[i + 1] = g * opacity + drcBuffer[i + 1] * (1 - opacity); drcBuffer[i + 1] = g * opacity + drcBuffer[i + 1] * (1 - opacity);
drcBuffer[i + 2] = b * opacity + drcBuffer[i + 2] * (1 - opacity); drcBuffer[i + 2] = b * opacity + drcBuffer[i + 2] * (1 - opacity);
} }
@ -109,29 +103,27 @@ void DrawUtils::drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t
// scale and put pixel in the tv buffer // scale and put pixel in the tv buffer
for (uint32_t yy = (y * 1.5); yy < ((y * 1.5) + 1); yy++) { for (uint32_t yy = (y * 1.5); yy < ((y * 1.5) + 1); yy++) {
for (uint32_t xx = (x * 1.5); xx < ((x * 1.5) + 1); xx++) { for (uint32_t xx = (x * 1.5); xx < ((x * 1.5) + 1); xx++) {
uint32_t i = (xx + yy * TV_WIDTH) * 4; uint32_t i = (xx + yy * TV_WIDTH) * 4;
if (i + 3 < tvSize / 2) { if (i + 3 < tvSize / 2) {
if (isBackBuffer) { if (isBackBuffer) {
i += tvSize / 2; i += tvSize / 2;
} }
if (a == 0xFF) { if (a == 0xFF) {
tvBuffer[i ] = r; tvBuffer[i] = r;
tvBuffer[i + 1] = g; tvBuffer[i + 1] = g;
tvBuffer[i + 2] = b; tvBuffer[i + 2] = b;
} } else {
else { tvBuffer[i] = r * opacity + tvBuffer[i] * (1 - opacity);
tvBuffer[i ] = r * opacity + tvBuffer[i ] * (1 - opacity);
tvBuffer[i + 1] = g * opacity + tvBuffer[i + 1] * (1 - opacity); tvBuffer[i + 1] = g * opacity + tvBuffer[i + 1] * (1 - opacity);
tvBuffer[i + 2] = b * opacity + tvBuffer[i + 2] * (1 - opacity); tvBuffer[i + 2] = b * opacity + tvBuffer[i + 2] * (1 - opacity);
} }
} }
} }
} }
} }
void DrawUtils::drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col) void DrawUtils::drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col) {
{
for (uint32_t yy = y; yy < y + h; yy++) { for (uint32_t yy = y; yy < y + h; yy++) {
for (uint32_t xx = x; xx < x + w; xx++) { for (uint32_t xx = x; xx < x + w; xx++) {
drawPixel(xx, yy, col); drawPixel(xx, yy, col);
@ -139,30 +131,28 @@ 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(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col) {
{
drawRectFilled(x, y, w, borderSize, col); drawRectFilled(x, y, w, borderSize, col);
drawRectFilled(x, y + h - borderSize, w, borderSize, col); drawRectFilled(x, y + h - borderSize, w, borderSize, col);
drawRectFilled(x, y, borderSize, h, col); drawRectFilled(x, y, borderSize, h, col);
drawRectFilled(x + w - borderSize, 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(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t *data) {
{ if (data[0] != 'B' || data[1] != 'M') {
if ( data[0] != 'B' || data[1] != 'M' ) { // invalid header
// invalid header return;
return; }
}
uint32_t dataPos = __builtin_bswap32(*(uint32_t*)&(data[0x0A])); uint32_t dataPos = __builtin_bswap32(*(uint32_t *) &(data[0x0A]));
uint32_t width = __builtin_bswap32(*(uint32_t*)&(data[0x12])); uint32_t width = __builtin_bswap32(*(uint32_t *) &(data[0x12]));
uint32_t height = __builtin_bswap32(*(uint32_t*)&(data[0x16])); uint32_t height = __builtin_bswap32(*(uint32_t *) &(data[0x16]));
if (dataPos == 0) { if (dataPos == 0) {
dataPos = 54; dataPos = 54;
} }
data += dataPos; data += dataPos;
// TODO flip image since bitmaps are stored upside down // TODO flip image since bitmaps are stored upside down
@ -174,28 +164,26 @@ 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) 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 = (void**) png_get_io_ptr(png_ptr);
memcpy(outBytes, *data, byteCountToRead); memcpy(outBytes, *data, byteCountToRead);
*((uint8_t**) data) += byteCountToRead; *((uint8_t **) data) += byteCountToRead;
} }
void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t* data) void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t *data) {
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(png_ptr == NULL) { if (png_ptr == NULL) {
return; return;
} }
png_infop info_ptr = png_create_info_struct(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr);
if(info_ptr == NULL) { if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, NULL, NULL); png_destroy_read_struct(&png_ptr, NULL, NULL);
return; return;
} }
png_set_read_fn(png_ptr, (void*) &data, png_read_data); png_set_read_fn(png_ptr, (void *) &data, png_read_data);
png_read_info(png_ptr, info_ptr); png_read_info(png_ptr, info_ptr);
@ -204,22 +192,21 @@ void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t* data)
int bitDepth = 0; int bitDepth = 0;
int colorType = -1; int colorType = -1;
uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, NULL, NULL, NULL); uint32_t retval = png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType, NULL, NULL, NULL);
if(retval != 1) { if (retval != 1) {
return; return;
} }
uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr); uint32_t bytesPerRow = png_get_rowbytes(png_ptr, info_ptr);
uint8_t* rowData = new uint8_t[bytesPerRow]; uint8_t *rowData = new uint8_t[bytesPerRow];
for(uint32_t yy = y; yy < y + height; yy++) { for (uint32_t yy = y; yy < y + height; yy++) {
png_read_row(png_ptr, (png_bytep)rowData, NULL); png_read_row(png_ptr, (png_bytep) rowData, NULL);
for (uint32_t xx = x; xx < x + width; xx++) { for (uint32_t xx = x; xx < x + width; xx++) {
if (colorType == PNG_COLOR_TYPE_RGB_ALPHA) { if (colorType == PNG_COLOR_TYPE_RGB_ALPHA) {
uint32_t i = (xx - x) * 4; uint32_t i = (xx - x) * 4;
drawPixel(xx, yy, rowData[i], rowData[i + 1], rowData[i + 2], rowData[i + 3]); drawPixel(xx, yy, rowData[i], rowData[i + 1], rowData[i + 2], rowData[i + 3]);
} } else if (colorType == PNG_COLOR_TYPE_RGB) {
else if (colorType == PNG_COLOR_TYPE_RGB) {
uint32_t i = (xx - x) * 3; uint32_t i = (xx - x) * 3;
drawPixel(xx, yy, rowData[i], rowData[i + 1], rowData[i + 2], 0xFF); drawPixel(xx, yy, rowData[i], rowData[i + 1], rowData[i + 2], 0xFF);
} }
@ -230,39 +217,34 @@ void DrawUtils::drawPNG(uint32_t x, uint32_t y, const uint8_t* data)
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
} }
void DrawUtils::initFont() void DrawUtils::initFont() {
{ void *font = NULL;
void* font = NULL;
uint32_t size = 0; uint32_t size = 0;
OSGetSharedData(OS_SHAREDDATATYPE_FONT_STANDARD, 0, &font, &size); OSGetSharedData(OS_SHAREDDATATYPE_FONT_STANDARD, 0, &font, &size);
if (font && size) { if (font && size) {
FT_Init_FreeType(&ft_lib); FT_Init_FreeType(&ft_lib);
FT_New_Memory_Face(ft_lib, (FT_Byte*) font, size, 0, &ft_face); FT_New_Memory_Face(ft_lib, (FT_Byte *) font, size, 0, &ft_face);
} }
} }
void DrawUtils::deinitFont() void DrawUtils::deinitFont() {
{
FT_Done_Face(ft_face); FT_Done_Face(ft_face);
FT_Done_FreeType(ft_lib); FT_Done_FreeType(ft_lib);
} }
void DrawUtils::setFontSize(uint32_t size) void DrawUtils::setFontSize(uint32_t size) {
{
FT_Set_Pixel_Sizes(ft_face, 0, size); FT_Set_Pixel_Sizes(ft_face, 0, size);
} }
void DrawUtils::setFontColor(Color col) void DrawUtils::setFontColor(Color col) {
{
font_col = col; font_col = col;
} }
static void draw_freetype_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y) static void draw_freetype_bitmap(FT_Bitmap *bitmap, FT_Int x, FT_Int y) {
{ FT_Int i, j, p, q;
FT_Int i, j, p, q; FT_Int x_max = x + bitmap->width;
FT_Int x_max = x + bitmap->width; FT_Int y_max = y + bitmap->rows;
FT_Int y_max = y + bitmap->rows;
for (i = x, p = 0; i < x_max; i++, p++) { for (i = x, p = 0; i < x_max; i++, p++) {
for (j = y, q = 0; j < y_max; j++, q++) { for (j = y, q = 0; j < y_max; j++, q++) {
@ -276,16 +258,14 @@ static void draw_freetype_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y)
} }
} }
void DrawUtils::print(uint32_t x, uint32_t y, const char* string, bool alignRight) void DrawUtils::print(uint32_t x, uint32_t y, const char *string, bool alignRight) {
{ wchar_t *buffer = new wchar_t[strlen(string) + 1];
wchar_t* buffer = new wchar_t[strlen(string) + 1];
size_t num = mbstowcs(buffer, string, strlen(string)); size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) { if (num > 0) {
buffer[num] = 0; buffer[num] = 0;
} } else {
else { wchar_t *tmp = buffer;
wchar_t* tmp = buffer;
while ((*tmp++ = *string++)); while ((*tmp++ = *string++));
} }
@ -293,8 +273,7 @@ void DrawUtils::print(uint32_t x, uint32_t y, const char* string, bool alignRigh
delete[] buffer; delete[] buffer;
} }
void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t* string, bool alignRight) void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight) {
{
FT_GlyphSlot slot = ft_face->glyph; FT_GlyphSlot slot = ft_face->glyph;
FT_Vector pen = {(int) x, (int) y}; FT_Vector pen = {(int) x, (int) y};
@ -319,16 +298,14 @@ void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t* string, bool alignR
} }
} }
uint32_t DrawUtils::getTextWidth(const char* string) uint32_t DrawUtils::getTextWidth(const char *string) {
{ wchar_t *buffer = new wchar_t[strlen(string) + 1];
wchar_t* buffer = new wchar_t[strlen(string) + 1];
size_t num = mbstowcs(buffer, string, strlen(string)); size_t num = mbstowcs(buffer, string, strlen(string));
if (num > 0) { if (num > 0) {
buffer[num] = 0; buffer[num] = 0;
} } else {
else { wchar_t *tmp = buffer;
wchar_t* tmp = buffer;
while ((*tmp++ = *string++)); while ((*tmp++ = *string++));
} }
@ -338,8 +315,7 @@ uint32_t DrawUtils::getTextWidth(const char* string)
return width; return width;
} }
uint32_t DrawUtils::getTextWidth(const wchar_t* string) uint32_t DrawUtils::getTextWidth(const wchar_t *string) {
{
FT_GlyphSlot slot = ft_face->glyph; FT_GlyphSlot slot = ft_face->glyph;
uint32_t width = 0; uint32_t width = 0;

View File

@ -10,8 +10,12 @@ union Color {
Color(uint32_t color) { Color(uint32_t color) {
this->color = color; this->color = color;
} }
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
this->r = r; this->g = g; this->b = b; this->a = a; this->r = r;
this->g = g;
this->b = b;
this->a = a;
} }
uint32_t color; uint32_t color;
@ -25,34 +29,49 @@ union Color {
class DrawUtils { class DrawUtils {
public: public:
static void* InitOSScreen(); static void *InitOSScreen();
static void initBuffers(void* tvBuffer, uint32_t tvSize, void* drcBuffer, uint32_t drcSize);
static void initBuffers(void *tvBuffer, uint32_t tvSize, void *drcBuffer, uint32_t drcSize);
static void beginDraw(); static void beginDraw();
static void endDraw(); static void endDraw();
static void clear(Color col); static void clear(Color col);
static void drawPixel(uint32_t x, uint32_t y, Color col) { drawPixel(x, y, col.r, col.g, col.b, col.a); } static void drawPixel(uint32_t x, uint32_t y, Color col) { drawPixel(x, y, col.r, col.g, col.b, col.a); }
static void drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a); static void drawPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
static void drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col); static void drawRectFilled(uint32_t x, uint32_t y, uint32_t w, uint32_t h, Color col);
static void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col); static void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t borderSize, Color col);
static void drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t* data); static void drawBitmap(uint32_t x, uint32_t y, uint32_t target_width, uint32_t target_height, const uint8_t *data);
static void drawPNG(uint32_t x, uint32_t y, const uint8_t* data);
static void drawPNG(uint32_t x, uint32_t y, const uint8_t *data);
static void initFont(); static void initFont();
static void deinitFont(); static void deinitFont();
static void setFontSize(uint32_t size); static void setFontSize(uint32_t size);
static void setFontColor(Color col); static void setFontColor(Color col);
static void print(uint32_t x, uint32_t y, const char* string, bool alignRight = false);
static void print(uint32_t x, uint32_t y, const wchar_t* string, bool alignRight = false); static void print(uint32_t x, uint32_t y, const char *string, bool alignRight = false);
static uint32_t getTextWidth(const char* string);
static uint32_t getTextWidth(const wchar_t* string); static void print(uint32_t x, uint32_t y, const wchar_t *string, bool alignRight = false);
static uint32_t getTextWidth(const char *string);
static uint32_t getTextWidth(const wchar_t *string);
private: private:
static bool isBackBuffer; static bool isBackBuffer;
static uint8_t* tvBuffer; static uint8_t *tvBuffer;
static uint32_t tvSize; static uint32_t tvSize;
static uint8_t* drcBuffer; static uint8_t *drcBuffer;
static uint32_t drcSize; static uint32_t drcSize;
}; };