static inline void putPixel565(u8* dst, u8 x, u8 y, u16 v) //Put RGB565 pixels in a Bitmap-like formatted buffer
{
dst[(x+(47-y)*48)*3+0]=(v&0x1F)<<3;
dst[(x+(47-y)*48)*3+1]=((v>>5)&0x3F)<<2;
dst[(x+(47-y)*48)*3+2]=((v>>11)&0x1F)<<3;
}
const char* file = "/file.smdh";
char name[64];
char desc[128];
char author[64];
Handle fileHandle;
u32 bytesRead;
FS_archive sdmcArchive=(FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
FS_path filePath=FS_makePath(PATH_CHAR, file);
FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
u32 magic;
FSFILE_Read(fileHandle, &bytesRead, 0, &magic, 4);
if (magic != 0x48444D53) return luaL_error(L, "error opening SMDH file");
unsigned char *buffer = (unsigned char*)(malloc((129) * sizeof (char)));
buffer[128] = 0;
FSFILE_Read(fileHandle, &bytesRead, 8, buffer, 128);
int i = 0;
while (i < 129){
if (buffer[i*2] == 0) break;
else name[i] = buffer[i*2];
i++;
}
name[i] = 0;
FSFILE_Read(fileHandle, &bytesRead, 392, buffer, 128);
i = 0;
while (i < 129){
if (buffer[i*2] == 0) break;
else author[i] = buffer[i*2];
i++;
}
author[i] = 0;
free(buffer);
buffer = (unsigned char*)(malloc((257) * sizeof (char)));
buffer[256] = 0;
FSFILE_Read(fileHandle, &bytesRead, 136, buffer, 256);
i = 0;
while (i < 257){
if (buffer[i*2] == 0) break;
else desc[i] = buffer[i*2];
i++;
}
desc[i] = 0;
free(buffer);
Bitmap *bitmap = (Bitmap*)malloc(sizeof(Bitmap));
bitmap->width = 48;
bitmap->height = 48;
bitmap->pixels = (u8*)malloc(6912);
bitmap->bitperpixel = 24;
u16* icon_buffer = (u16*)malloc(0x1200);
FSFILE_Read(fileHandle, &bytesRead, 0x24C0, icon_buffer, 0x1200);
FSFILE_Close(fileHandle);
svcCloseHandle(fileHandle);
//convert RGB565 to RGB24
int x=0;
int y=0;
int real_x,real_y;
int low_up_x = 4;
int big_up_x = 12;
int low_up_y = 2;
int big_up_y = 6;
bool switch_tile_x = false;
bool switch_tile_y = false;
bool jump = false;
i = 0;
int j=0;
while (y < 48){
i=j;
while (x < 48){
putPixel565(bitmap->pixels, x, y, icon_buffer[i]);
x++;
putPixel565(bitmap->pixels, x, y, icon_buffer[i+1]);
if (switch_tile_x) i = i + big_up_x;
else i = i + low_up_x;
x++;
switch_tile_x = !switch_tile_x;
}
x=0;
if (switch_tile_y){
if (jump) j = j + 183;
else j = j + big_up_y;
jump = !jump;
}else j = j + low_up_y;
switch_tile_y = !switch_tile_y;
y++;
}
free(icon_buffer);