Guest User

libwiiu example - Dual-screen rendering

a guest
Mar 19th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. void _start()
  2. {
  3.     /****************************>            Fix Stack            <****************************/
  4.     //Load a good stack
  5.     asm(
  6.         "lis %r1, 0x1ab5 ;"
  7.         "ori %r1, %r1, 0xd138 ;"
  8.         );
  9.     /****************************>           Get Handles           <****************************/
  10.     //Get a handle to coreinit.rpl
  11.     unsigned int coreinit_handle;
  12.     OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle);
  13.     /****************************>       External Prototypes       <****************************/
  14.     //OSScreen functions
  15.     void(*OSScreenInit)();
  16.     unsigned int(*OSScreenGetBufferSizeEx)(unsigned int bufferNum);
  17.     unsigned int(*OSScreenSetBufferEx)(unsigned int bufferNum, void * addr);
  18.     //OS Memory functions
  19.     void*(*memset)(void * dest, uint32_t value, uint32_t bytes);
  20.     void*(*OSAllocFromSystem)(uint32_t size, int align);
  21.     void(*OSFreeToSystem)(void *ptr);
  22.     //IM functions
  23.     int(*IM_Open)();
  24.     int(*IM_Close)(int fd);
  25.     int(*IM_SetDeviceState)(int fd, void *mem, int state, int a, int b);
  26.     /****************************>             Exports             <****************************/
  27.     //OSScreen functions
  28.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenInit", &OSScreenInit);
  29.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenGetBufferSizeEx", &OSScreenGetBufferSizeEx);
  30.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenSetBufferEx", &OSScreenSetBufferEx);
  31.     //OS Memory functions
  32.     OSDynLoad_FindExport(coreinit_handle, 0, "memset", &memset);
  33.     OSDynLoad_FindExport(coreinit_handle, 0, "OSAllocFromSystem", &OSAllocFromSystem);
  34.     OSDynLoad_FindExport(coreinit_handle, 0, "OSFreeToSystem", &OSFreeToSystem);
  35.     //IM functions
  36.     OSDynLoad_FindExport(coreinit_handle, 0, "IM_Open", &IM_Open);
  37.     OSDynLoad_FindExport(coreinit_handle, 0, "IM_Close", &IM_Close);
  38.     OSDynLoad_FindExport(coreinit_handle, 0, "IM_SetDeviceState", &IM_SetDeviceState);
  39.     /****************************>          Initial Setup          <****************************/
  40.     //Restart system to get lib access
  41.     int fd = IM_Open();
  42.     void *mem = OSAllocFromSystem(0x100, 64);
  43.     memset(mem, 0, 0x100);
  44.     //set restart flag to force quit browser
  45.     IM_SetDeviceState(fd, mem, 3, 0, 0);
  46.     IM_Close(fd);
  47.     OSFreeToSystem(mem);
  48.     //wait a bit for browser end
  49.     unsigned int t1 = 0x1FFFFFFF;
  50.     while(t1--) ;
  51.     //Call the Screen initilzation function.
  52.     OSScreenInit();
  53.     //Grab the buffer size for each screen (TV and gamepad)
  54.     int buf0_size = OSScreenGetBufferSizeEx(0);
  55.     int buf1_size = OSScreenGetBufferSizeEx(1);
  56.     //Set the buffer area.
  57.     OSScreenSetBufferEx(0, (void *)0xF4000000);
  58.     OSScreenSetBufferEx(1, (void *)0xF4000000 + buf0_size);
  59.  
  60.     unsigned int(*OSScreenClearBufferEx)(unsigned int bufferNum, unsigned int temp);
  61.     unsigned int(*OSScreenPutFontEx)(unsigned int bufferNum, unsigned int posX, unsigned int line, void * buffer);
  62.     void(*DCFlushRange)(void *buffer, unsigned int length);
  63.     unsigned int(*OSScreenFlipBuffersEx)(unsigned int bufferNum);
  64.     unsigned int(*OSScreenPutPixelEx)(unsigned int buffer, unsigned int x, unsigned int y, unsigned int colour);
  65.  
  66.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenClearBufferEx", &OSScreenClearBufferEx);
  67.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenPutFontEx", &OSScreenPutFontEx);
  68.     OSDynLoad_FindExport(coreinit_handle, 0, "DCFlushRange", &DCFlushRange);
  69.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenFlipBuffersEx", &OSScreenFlipBuffersEx);
  70.     OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenPutPixelEx", &OSScreenPutPixelEx);
  71.  
  72.     //Clear both framebuffers.
  73.     int ii = 0;
  74.     for (ii; ii < 2; ii++)
  75.     {
  76.         OSScreenClearBufferEx(0, 0x000000FF); // Make sure that the TV screen's frame buffer is full of black (0x000000FF is equivalent to red=0, green=0, blue=0, alpha=255)
  77.         DCFlushRange((void *)0xF4000000, buf0_size); // Flush the TV buffer's cache before finally...
  78.         OSScreenFlipBuffersEx(0); // Rendering the changes to the TV screen only!
  79.  
  80.         OSScreenClearBufferEx(1, 0x000000FF); // Do the same for the GamePad screen. (above, the 0 represents buffer 0 - or the TV screen, the 1 here represents buffer 1 - the first GamePad)
  81.         DCFlushRange((void *)0xF4000000 + buf0_size, buf1_size);
  82.         OSScreenFlipBuffersEx(1);
  83.     }
  84.  
  85.     int x = 0, y = 0; // Variables for drawing squares to the screen.
  86.    
  87.     /* Now, let's start our rendering! (to the TV screen, obviously) */
  88.     OSScreenClearBufferEx(0, 0xFF0000FF); // Make the TV screen red (red=255, green=0, blue=0, alpha=255)
  89.     for(x = 0; x < 50; x++) {
  90.         for(y = 0; y < 50; y++) {
  91.             OSScreenPutPixelEx(0, x, y, 0x0000FFFF); // Insert a blue pixel at the x and y coordinates (r=0 g=0 b=255 a=255)
  92.         }
  93.     }
  94.     OSScreenPutFontEx(0, 3, 2, "This is the TV Screen."); // Write text to the TV
  95.     DCFlushRange((void *)0xF4000000, buf0_size); // Flush the buffer cache
  96.     OSScreenFlipBuffersEx(0); // Render changes
  97.  
  98.     /* Render to the GamePad screen, now. */
  99.     OSScreenClearBufferEx(1, 0x00FF00FF); // Green background (you get the hang of hex colors eventually)
  100.     for(x = 100; x < 150; x++) {
  101.         for(y = 100; y < 150; y++) {
  102.             OSScreenPutPixelEx(1, x, y, 0x0000FFFF);
  103.         }
  104.     }
  105.     OSScreenPutFontEx(1, 5, 4, "This is the Wii U GamePad Screen.");
  106.     DCFlushRange((void *)0xF4000000 + buf0_size, buf1_size);
  107.     OSScreenFlipBuffersEx(1);
  108.  
  109.     /* Wait a few seconds before exiting... */
  110.     t1 = 0xF0000000; // Approximately eight seconds (maybe?)
  111.     while(t1--);
  112.  
  113.     for (ii; ii < 2; ii++)
  114.     {
  115.         OSScreenClearBufferEx(0, 0x000000FF); // Make sure that the TV screen's frame buffer is full of black (0x000000FF is equivalent to red=0, green=0, blue=0, alpha=255)
  116.         DCFlushRange((void *)0xF4000000, buf0_size); // Flush the TV buffer's cache before finally...
  117.         OSScreenFlipBuffersEx(0); // Rendering the changes to the TV screen only!
  118.  
  119.         OSScreenClearBufferEx(1, 0x000000FF); // Do the same for the GamePad screen. (above, the 0 represents buffer 0 - or the TV screen, the 1 here represents buffer 1 - the first GamePad)
  120.         DCFlushRange((void *)0xF4000000 + buf0_size, buf1_size);
  121.         OSScreenFlipBuffersEx(1);
  122.     }
  123.     _Exit(); // Return to HOME Menu
  124. }
Add Comment
Please, Sign In to add comment