Guest User

Untitled

a guest
Aug 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /* Now double buffered with animation.
  2. */
  3.  
  4. #include <psl1ght/lv2.h>
  5.  
  6. #include <stdio.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <unistd.h>
  11.  
  12. #include <sysutil/video.h>
  13. #include <rsx/gcm.h>
  14. #include <rsx/reality.h>
  15.  
  16. #include <io/pad.h>
  17.  
  18. #include <psl1ght/lv2.h>
  19.  
  20. gcmContextData *context; // Context to keep track of the RSX buffer.
  21.  
  22. VideoResolution res; // Screen Resolution
  23.  
  24. int currentBuffer = 0;
  25. s32 *buffer[2]; // The buffer we will be drawing into.
  26.  
  27. void waitFlip() { // Block the PPU thread untill the previous flip operation has finished.
  28. while(gcmGetFlipStatus() != 0)
  29. usleep(200);
  30. gcmResetFlipStatus();
  31. }
  32.  
  33. void flip(s32 buffer) {
  34. assert(gcmSetFlip(context, buffer) == 0);
  35. realityFlushBuffer(context);
  36. gcmSetWaitFlip(context); // Prevent the RSX from continuing until the flip has finished.
  37. }
  38.  
  39. // Initilize everything. You can probally skip over this function.
  40. void init_screen() {
  41. // Allocate a 1Mb buffer, alligned to a 1Mb boundary to be our shared IO memory with the RSX.
  42. void *host_addr = memalign(1024*1024, 1024*1024);
  43. assert(host_addr != NULL);
  44.  
  45. // Initilise Reality, which sets up the command buffer and shared IO memory
  46. context = realityInit(0x10000, 1024*1024, host_addr);
  47. assert(context != NULL);
  48.  
  49. VideoState state;
  50. assert(videoGetState(0, 0, &state) == 0); // Get the state of the display
  51. assert(state.state == 0); // Make sure display is enabled
  52.  
  53. // Get the current resolution
  54. assert(videoGetResolution(state.displayMode.resolution, &res) == 0);
  55.  
  56. // Configure the buffer format to xRGB
  57. VideoConfiguration vconfig;
  58. memset(&vconfig, 0, sizeof(VideoConfiguration));
  59. vconfig.resolution = state.displayMode.resolution;
  60. vconfig.format = VIDEO_BUFFER_FORMAT_XRGB;
  61. vconfig.pitch = res.width * 4;
  62.  
  63. assert(videoConfigure(0, &vconfig, NULL, 0) == 0);
  64. assert(videoGetState(0, 0, &state) == 0);
  65.  
  66. s32 buffer_size = 4 * res.width * res.height; // each pixel is 4 bytes
  67. printf("buffers will be 0x%x bytes\n", buffer_size);
  68.  
  69. gcmSetFlipMode(GCM_FLIP_VSYNC); // Wait for VSYNC to flip
  70.  
  71. // Allocate two buffers for the RSX to draw to the screen (double buffering)
  72. buffer[0] = rsxMemAlign(16, buffer_size);
  73. buffer[1] = rsxMemAlign(16, buffer_size);
  74. assert(buffer[0] != NULL && buffer[1] != NULL);
  75.  
  76. u32 offset[2];
  77. assert(realityAddressToOffset(buffer[0], &offset[0]) == 0);
  78. assert(realityAddressToOffset(buffer[1], &offset[1]) == 0);
  79. // Setup the display buffers
  80. assert(gcmSetDisplayBuffer(0, offset[0], res.width * 4, res.width, res.height) == 0);
  81. assert(gcmSetDisplayBuffer(1, offset[1], res.width * 4, res.width, res.height) == 0);
  82.  
  83. gcmResetFlipStatus();
  84. flip(1);
  85. }
  86.  
  87. void drawFrame(int *buffer, long frame) {
  88. s32 i, j, x;
  89. for(i = 0; i < res.height; i++) {
  90. s32 color = (i / (res.height * 1.0) * 256);
  91. // This should make a nice black to green graident
  92. color = (color << 8) | ((frame % 255) << 16);
  93. for(j = 0; j < res.width; j++)
  94. buffer[i* res.width + j] = color;
  95. }
  96.  
  97. for(x = 3000; x < 7000; x++)
  98. {
  99. buffer[x] = 0xFF | ((x% 64) << 10) | ((x % 64) << 18) | ((x % 64) << 26);
  100. }
  101. }
  102.  
  103. s32 main(s32 argc, const char* argv[])
  104. {
  105. PadInfo padinfo;
  106. PadData paddata;
  107. int i;
  108.  
  109. init_screen();
  110. ioPadInit(7);
  111.  
  112. long frame = 0; // To keep track of how many frames we have rendered.
  113.  
  114. // Ok, everything is setup. Now for the main loop.
  115. while(1){
  116. // Check the pads.
  117. ioPadGetInfo(&padinfo);
  118. for(i=0; i<MAX_PADS; i++){
  119. if(padinfo.status[i]){
  120. ioPadGetData(i, &paddata);
  121.  
  122. if(paddata.BTN_CROSS){
  123. return 0;
  124. }
  125. }
  126.  
  127. }
  128.  
  129. waitFlip(); // Wait for the last flip to finish, so we can draw to the old buffer
  130. drawFrame(buffer[currentBuffer], frame++); // Draw into the unused buffer
  131. flip(currentBuffer); // Flip buffer onto screen
  132. currentBuffer = !currentBuffer;
  133. }
  134.  
  135. return 0;
  136. }
Add Comment
Please, Sign In to add comment