Homebrew Homebrew Development

  • Thread starter Thread starter aliak11
  • Start date Start date
  • Views Views 1,475,000
  • Replies Replies 6,048
  • Likes Likes 54
This is the code where i draw the background, i got only stripes on my screen, and not the color i selected.

Code:
    union{ u32 color; struct{ u8 a, b, g, r; } rgba; } rgbaColor;
    rgbaColor.color = color;
 
    int i;
    for (i = 0; i<fbWidth*fbHeight; i++)
    {
        *(fbAdr++) = (u8)(rgbaColor.rgba.b);
        *(fbAdr++) = (u8)(rgbaColor.rgba.g);
        *(fbAdr++) = (u8)(rgbaColor.rgba.r);
    }

Are you using the console output (or only calling init_console) ? It sets RGB565 mode instead of RGB888.
 
Oh, Wow, somethimes i do such stupid things, i used consoleInit(GFX_TOP, NULL);
What should i use instead?
 
If you use the console to write text, you have to format your color value to be only two byte size instead of three.

Otherwise, you need a custom funtion to output text and a binary font. There are a lot of example of this published in this thread.
 
If you use the console to write text, you have to format your color value to be only two byte size instead of three.

Otherwise, you need a custom funtion to output text and a binary font. There are a lot of example of this published in this thread.

It would be super helpfull if you could point me in the right directory.
I don't know how to do this
 
You can check some of the code on my github (eg. Te3DS) for drawing primitives and text ouput exaples, but pay attention that it was written on a barebone system, so I use fixed poiters to Framebufeer and other resources.

Now you can use ctrulib that makes using 3DS services easyer, and there are useful exaples of code inside the ctrulib package on smealum github.
 
Two bytes not two bits...
Instead, you can use RGB565 color mode for framebuffer (with gfxInit) or you can convert your RGB565 color to a RGB24 ones (like what HB Menu/lpp-3ds does for SMDH icons)
Code:
static inline void putPixel565(u8* dst, u8 x, u8 y, u16 v)
{
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;
}
static int lua_readsmdh(lua_State *L){
int argc = lua_gettop(L);
if (argc != 1) return luaL_error(L, "wrong number of arguments");
const char* file = luaL_checkstring(L, 1);
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 tile_size = 16;
int tile_number = 1;
int extra_x = 0;
int extra_y = 0;
i=0;
int tile_x[16] = {0,1,0,1,2,3,2,3,0,1,0,1,2,3,2,3};
int tile_y[16] = {0,0,1,1,0,0,1,1,2,2,3,3,2,2,3,3};
while (tile_number < 37){
while (i < (tile_size)){
putPixel565(bitmap->pixels, tile_x[i-((tile_number-1)*64)] + extra_x, tile_y[i-((tile_number-1)*64)] + extra_y, icon_buffer[i]);
putPixel565(bitmap->pixels, 4+tile_x[i-((tile_number-1)*64)] + extra_x, tile_y[i-((tile_number-1)*64)] + extra_y, icon_buffer[i+16]);
putPixel565(bitmap->pixels, tile_x[i-((tile_number-1)*64)] + extra_x, 4+tile_y[i-((tile_number-1)*64)] + extra_y, icon_buffer[i+32]);
putPixel565(bitmap->pixels, 4+tile_x[i-((tile_number-1)*64)] + extra_x, 4+tile_y[i-((tile_number-1)*64)] + extra_y, icon_buffer[i+48]);
i++;
}
if (tile_number % 6 == 0){
extra_x = 0;
extra_y = extra_y + 8;
}else extra_x = extra_x + 8;
tile_number++;
tile_size = tile_size + 64;
i = i + 48;
}
free(icon_buffer);
lua_newtable(L);
lua_newtable(L);
lua_pushstring(L, "title");
lua_pushstring(L, name);
lua_settable(L, -3);
lua_pushstring(L, "desc");
lua_pushstring(L, desc);
lua_settable(L, -3);
lua_pushstring(L, "author");
lua_pushstring(L, author);
lua_settable(L, -3);
lua_pushstring(L, "icon");
lua_pushinteger(L, (u32)bitmap);
lua_settable(L, -3);
return 1;
}
 
In the libctru source code, look into console.c, there ia a function RGB8_to_565 to convert the color. But pay attention that the max value for the color have to be 63.

To write the color into the framebuffer for the pixel at coordinates x,y you can do something like:

((short *) fb )[y+x*height] = RGB8_to_565(r,g,b);
 
Two bytes not two bits...
Instead, you can use RGB565 color mode for framebuffer (with gfxInit) or you can convert your RGB565 color to a RGB24 ones (like what HB Menu/lpp-3ds does for SMDH icons)


I used you function this function
Code:
void putPixel565(u8 x, u8 y, u16 v) //0xFFFF
{
    u8* dst = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
   
    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;
}

I do this in my main:
Code:
int main(int argc, char **argv)
{
  gfxInitDefault();
  consoleInit(GFX_TOP, NULL);
 
  int i, j;
  for(i = 10; i < 40; i++)
      for(j = 10; j < 40; j++)
            putPixel565(i, j, 0xFFFF);
And i used it directly in my main, it is only executed once.
But, for some reason, i don't get a square, i got 5 vertical lines above each other of 5-6 pixels width, and it is flickering really fast.
 
I solved the flickering by only executing gfxFlushBuffers(); and gfxSwapBuffers(); when i changed something.
 

Site & Scene News

Popular threads in this forum