Advertisement
xerpi

PSP GU 2D Ortho + FPS

Dec 1st, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. #include <pspdisplay.h>
  2. #include <pspctrl.h>
  3. #include <pspkernel.h>
  4. #include <pspdebug.h>
  5. #include <pspgu.h>
  6. #include <pspgum.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. #define millis() (clock()/1000)
  12.  
  13. PSP_MODULE_INFO("GU", 0, 1, 1);
  14.  
  15. #define BUF_WIDTH  512
  16. #define SCR_WIDTH  480
  17. #define SCR_HEIGHT 272
  18. #define GU_RGB(r,g,b) GU_RGBA(r,g,b,255)
  19.  
  20. #define printf pspDebugScreenPrintf
  21. #define clearScreen() pspDebugScreenSetXY(0,0)
  22.  
  23. unsigned int __attribute__((aligned(16))) guList[262144];
  24. void *fbp0;
  25. //fps stuff
  26. u32 frame_count;
  27. time_t currentTime, lastTime, diffTime;
  28.  
  29. typedef struct
  30. {
  31.     unsigned int color;
  32.     short x, y, z;
  33. }FSVertex;
  34.  
  35. int  initGU(void);
  36. void finishGU(void);
  37. void initFPS();
  38. void SetupProjection(void);
  39. void guBegin(void);
  40. void guEnd(void);
  41. void FPS(float *fps_p);
  42.  
  43. void drawRectangle(short x, short y, short w, short h, unsigned int color);
  44.  
  45. int exit_callback(int arg1, int arg2, void *common);
  46. int CallbackThread(SceSize args, void *argp);
  47. int SetupCallbacks(void);
  48.  
  49. int main(void)
  50. {
  51.     SetupCallbacks();
  52.     initGU();
  53.    
  54.     float fps = 0.0f;
  55.    
  56.     while(1)
  57.     {
  58.         guBegin();
  59.         clearScreen();
  60.        
  61.         printf("FPS: %f   frame: %i diffTime: %lu", fps, frame_count, diffTime);
  62.        
  63.         drawRectangle(10, 10, 50, 20, GU_RGB(255,0,0));
  64.         drawRectangle(109, 140, 20, 80, GU_RGB(0,0,255));
  65.        
  66.         guEnd();
  67.         FPS(&fps);
  68.     }
  69.    
  70.     finishGU();
  71.     return 0;
  72. }
  73.  
  74.  
  75. void drawRectangle(short x, short y, short w, short h, unsigned int color)
  76. {
  77.     FSVertex *vertices = (FSVertex *)sceGuGetMemory(4 * sizeof(FSVertex)); //Allocate memory
  78.    
  79.     vertices[0] = (FSVertex){color, x, y};
  80.     vertices[1] = (FSVertex){color, x, y+h};
  81.     vertices[2] = (FSVertex){color, x+w, y};
  82.     vertices[3] = (FSVertex){color, x+w, y+h};
  83.     sceGuDrawArray(GU_TRIANGLE_STRIP, GU_COLOR_8888|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 4, 0, vertices);
  84.    
  85.     sceKernelDcacheWritebackRange(vertices, 4 * sizeof(FSVertex)); //Free memory
  86. }
  87.  
  88. int initGU(void)
  89. {
  90.     sceGuInit();
  91.     sceGuStart(GU_DIRECT, guList);
  92.     sceGuDrawBuffer(GU_PSM_8888, fbp0, BUF_WIDTH);
  93.     sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void*)0x88000, BUF_WIDTH);
  94.     sceGuDepthBuffer((void*)0x110000, BUF_WIDTH);
  95.     sceGuOffset(2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2));
  96.     sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
  97.     sceGuDepthRange(65535, 0);
  98.    
  99.     sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
  100.     sceGuEnable(GU_SCISSOR_TEST);
  101.     sceGuFrontFace(GU_CW);
  102.     sceGuShadeModel(GU_SMOOTH);
  103.     sceGuDisable(GU_TEXTURE_2D);
  104.        
  105.     //sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  106.     sceGuFinish();
  107.  
  108.     sceGuSync(0, 0);
  109.     sceDisplayWaitVblankStart();
  110.     sceGuDisplay(GU_TRUE);
  111.    
  112.     SetupProjection();
  113.     initFPS();
  114.    
  115.     pspDebugScreenInit();
  116.     pspDebugScreenSetOffset((int)fbp0);
  117.    
  118.     return 1;
  119. }
  120.  
  121. void finishGU(void)
  122. {
  123.     sceGuTerm();
  124. }
  125.  
  126. void initFPS()
  127. {
  128.     currentTime = millis();
  129.     lastTime = currentTime;
  130.     frame_count = 0;   
  131. }
  132.  
  133. void FPS(float *fps_p)
  134. {
  135.     frame_count++;
  136.     currentTime = millis();
  137.     diffTime = currentTime - lastTime;
  138.     if(diffTime >= 1000)
  139.     {
  140.         *fps_p = (float)frame_count/(diffTime/1000.0f);
  141.         frame_count = 0;
  142.         lastTime = millis();
  143.     }
  144. }
  145.  
  146. void SetupProjection(void)
  147. {
  148.     sceGumMatrixMode(GU_PROJECTION);
  149.         sceGumLoadIdentity();
  150.         sceGumOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, -1, 1); //2D
  151.     sceGumMatrixMode(GU_VIEW);
  152.         sceGumLoadIdentity();
  153.     sceGumMatrixMode(GU_MODEL);
  154.         sceGumLoadIdentity();
  155. }
  156.  
  157.  
  158. void guBegin(void)
  159. {
  160.     sceGuStart(GU_DIRECT, guList);
  161.     sceGuClearColor(0);
  162.     sceGuClearDepth(0);
  163.     sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
  164.     SetupProjection();
  165. }
  166.  
  167. void guEnd(void)
  168. {
  169.     sceGuFinish();
  170.     sceGuSync(0,0);
  171.     sceDisplayWaitVblankStart();
  172.     fbp0 = sceGuSwapBuffers();
  173. }
  174.  
  175. /*callbacks*/
  176.  
  177. int exit_callback(int arg1, int arg2, void *common)
  178. {
  179.     sceKernelExitGame();
  180.     return 0;
  181. }
  182.  
  183. int CallbackThread(SceSize args, void *argp)
  184. {
  185.     int cbid;
  186.     cbid = sceKernelCreateCallback("exit_callback", exit_callback, NULL);
  187.     sceKernelRegisterExitCallback(cbid);
  188.     sceKernelSleepThreadCB();
  189.     return 0;
  190. }
  191.  
  192. int SetupCallbacks(void)
  193. {
  194.     int thid = 0;
  195.     thid = sceKernelCreateThread("callback_update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  196.     if(thid >= 0)
  197.     {
  198.         sceKernelStartThread(thid, 0, 0);
  199.     }
  200.  
  201.     return thid;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement