Advertisement
xerpi

PSP GU Ortho 2D, VRAM static, FPS

Oct 5th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.61 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 "vram.h"
  8. #include <stdio.h>
  9. #include <time.h>
  10.  
  11. PSP_MODULE_INFO("GU", 0, 1, 1);
  12.  
  13. #define millis() (clock()/1000)
  14. #define BUF_WIDTH  (512)
  15. #define SCR_WIDTH  (480)
  16. #define SCR_HEIGHT (272)
  17. #define GU_RGB(r,g,b) GU_RGBA(r,g,b,255)
  18. #define printf pspDebugScreenPrintf
  19. static unsigned int __attribute__((aligned(16))) guList[262144];
  20. void *fbp0 = NULL, *fbp1 = NULL, *zbp = NULL;
  21. u32 frame_count;
  22. time_t currentTime, lastTime, diffTime;
  23. float fps = 0.0f;
  24.  
  25. typedef struct
  26. {
  27.     unsigned int color;
  28.     short x, y, z;
  29. }FVertex;
  30.  
  31. void initFPS();
  32. void FPS(float *fps_p);
  33. int initGU(void);
  34. void SetupProjection(void);
  35. void guBegin(void);
  36. void guEnd(void);
  37.  
  38. void drawRectangle(int x, int y, int w, int h, unsigned int color);
  39.  
  40. int exit_callback(int arg1, int arg2, void *common);
  41. int CallbackThread(SceSize args, void *argp);
  42. int SetupCallbacks(void);
  43.  
  44.  
  45. int main(void)
  46. {
  47.     SetupCallbacks();
  48.     initGU();
  49.     SetupProjection();
  50.     initFPS();
  51.    
  52.     while(1)
  53.     {
  54.         guBegin();
  55.         drawRectangle(10, 10, 50, 20, GU_RGB(255,0,0));
  56.         drawRectangle(109, 140, 20, 80, GU_RGB(0,0,255));  
  57.         guEnd();
  58.         FPS(&fps);
  59.     }
  60.    
  61.     sceGuTerm();
  62.     return 0;
  63. }
  64.  
  65.  
  66. void drawRectangle(int x, int y, int w, int h, unsigned int color)
  67. {
  68.     FVertex *vertices = (FVertex *)sceGuGetMemory(4 * sizeof(FVertex)); //Allocate memory
  69.    
  70.     vertices[0] = (FVertex){color, x, y, 0};
  71.     vertices[1] = (FVertex){color, x, y+h, 0};
  72.     vertices[2] = (FVertex){color, x+w, y, 0};
  73.     vertices[3] = (FVertex){color, x+w, y+h, 0};
  74.     sceGuDrawArray(GU_TRIANGLE_STRIP, GU_COLOR_8888|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 4, 0, vertices);
  75.    
  76.     sceKernelDcacheWritebackRange(vertices, 4 * sizeof(FVertex)); //Free memory
  77. }
  78.  
  79. int initGU(void)
  80. {
  81.     fbp0 = (void*)getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
  82.     fbp1 = (void*)getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
  83.     zbp =  (void*)getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);
  84.    
  85.     sceGuInit();
  86.     sceGuStart(GU_DIRECT, guList);
  87.    
  88.     sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH);
  89.     sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
  90.     sceGuDepthBuffer(zbp,BUF_WIDTH);
  91.    
  92.     sceGuOffset( 2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2)); //Situar el espacio virtual de la PSP
  93.     sceGuViewport( 2048, 2048, SCR_WIDTH, SCR_HEIGHT); //Situar el punto de vista
  94.     sceGuDepthRange(65535, 0); //Bufer para los calculos de profundidad
  95.     sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT); //Establece los limites para no redendizar lo que no se mostrara en pantalla
  96.  
  97.     sceGuEnable(GU_SCISSOR_TEST);
  98.     sceGuFrontFace(GU_CW);
  99.     sceGuShadeModel(GU_SMOOTH);
  100.     sceGuDisable(GU_TEXTURE_2D);
  101.        
  102.     //sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  103.     sceGuFinish(); //Se acaba de establecer el 3D
  104.  
  105.     sceGuSync(0, 0); //Esperar hasta que todo lo anterior se acabe de ejecutar
  106.     sceDisplayWaitVblankStart(); //Esperar para cumplir los 59,97 FPS
  107.     sceGuDisplay(GU_TRUE); //Hace que se exponga el 3D
  108.     sceKernelDcacheWritebackAll();  
  109.  
  110.     return 1;
  111. }
  112.  
  113. void SetupProjection(void)
  114. {
  115.     sceGumMatrixMode(GU_PROJECTION); //Iniciar la perspectiva Matrix
  116.     sceGumLoadIdentity(); //Lee la identidad Matrix
  117.     sceGumOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, -1, 1); //Modo ortho para 2D
  118.     sceGumMatrixMode(GU_VIEW); //Iniciar modo de vision
  119.     sceGumLoadIdentity();
  120.     sceGumMatrixMode(GU_MODEL);
  121.     sceGumLoadIdentity();
  122.     sceGuClearColor(GU_COLOR(0.0f, 0.0f, 0.0f, 1.0f)); //Limpiar pantalla con negro
  123. }
  124.  
  125.  
  126. void guBegin(void)
  127. {
  128.     sceGuStart(GU_DIRECT, guList); //Iniciar el 3D
  129.     sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT ); //Limpia el buffer 3D
  130. }
  131.  
  132.  
  133. void guEnd(void)
  134. {
  135.     sceGuFinish();
  136.     sceGuSync(0,0);
  137.     fbp0 = sceGuSwapBuffers();
  138. }
  139.  
  140.  
  141. void initFPS()
  142. {
  143.     currentTime = millis();
  144.     lastTime = currentTime;
  145.     frame_count = 0;      
  146. }
  147.  
  148. void FPS(float *fps_p)
  149. {
  150.     frame_count++;
  151.     currentTime = millis();
  152.     diffTime = currentTime - lastTime;
  153.     if(diffTime >= 1000) {
  154.             *fps_p = (float)frame_count/(diffTime/1000.0f);
  155.             frame_count = 0;
  156.             lastTime = millis();
  157.     }
  158. }
  159.  
  160.  
  161. int exit_callback(int arg1, int arg2, void *common)
  162. {
  163.     sceGuTerm();
  164.     sceKernelExitGame();
  165.     return 0;
  166. }
  167.  
  168. int CallbackThread(SceSize args, void *argp)
  169. {
  170.     int cbid;
  171.     cbid = sceKernelCreateCallback("exit_callback", exit_callback, NULL);
  172.     sceKernelRegisterExitCallback(cbid);
  173.     sceKernelSleepThreadCB();
  174.     return 0;
  175. }
  176.  
  177. int SetupCallbacks(void)
  178. {
  179.     int thid = 0;
  180.     thid = sceKernelCreateThread("callback_update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  181.     if(thid >= 0)
  182.     {
  183.         sceKernelStartThread(thid, 0, 0);
  184.     }
  185.  
  186.     return thid;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement