Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pspdisplay.h>
- #include <pspctrl.h>
- #include <pspkernel.h>
- #include <pspdebug.h>
- #include <pspgu.h>
- #include <pspgum.h>
- #include "vram.h"
- #include <stdio.h>
- #include <time.h>
- PSP_MODULE_INFO("GU", 0, 1, 1);
- #define millis() (clock()/1000)
- #define BUF_WIDTH (512)
- #define SCR_WIDTH (480)
- #define SCR_HEIGHT (272)
- #define GU_RGB(r,g,b) GU_RGBA(r,g,b,255)
- #define printf pspDebugScreenPrintf
- static unsigned int __attribute__((aligned(16))) guList[262144];
- void *fbp0 = NULL, *fbp1 = NULL, *zbp = NULL;
- u32 frame_count;
- time_t currentTime, lastTime, diffTime;
- float fps = 0.0f;
- typedef struct
- {
- unsigned int color;
- short x, y, z;
- }FVertex;
- void initFPS();
- void FPS(float *fps_p);
- int initGU(void);
- void SetupProjection(void);
- void guBegin(void);
- void guEnd(void);
- void drawRectangle(int x, int y, int w, int h, unsigned int color);
- int exit_callback(int arg1, int arg2, void *common);
- int CallbackThread(SceSize args, void *argp);
- int SetupCallbacks(void);
- int main(void)
- {
- SetupCallbacks();
- initGU();
- SetupProjection();
- initFPS();
- while(1)
- {
- guBegin();
- drawRectangle(10, 10, 50, 20, GU_RGB(255,0,0));
- drawRectangle(109, 140, 20, 80, GU_RGB(0,0,255));
- guEnd();
- FPS(&fps);
- }
- sceGuTerm();
- return 0;
- }
- void drawRectangle(int x, int y, int w, int h, unsigned int color)
- {
- FVertex *vertices = (FVertex *)sceGuGetMemory(4 * sizeof(FVertex)); //Allocate memory
- vertices[0] = (FVertex){color, x, y, 0};
- vertices[1] = (FVertex){color, x, y+h, 0};
- vertices[2] = (FVertex){color, x+w, y, 0};
- vertices[3] = (FVertex){color, x+w, y+h, 0};
- sceGuDrawArray(GU_TRIANGLE_STRIP, GU_COLOR_8888|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 4, 0, vertices);
- sceKernelDcacheWritebackRange(vertices, 4 * sizeof(FVertex)); //Free memory
- }
- int initGU(void)
- {
- fbp0 = (void*)getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
- fbp1 = (void*)getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
- zbp = (void*)getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);
- sceGuInit();
- sceGuStart(GU_DIRECT, guList);
- sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH);
- sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
- sceGuDepthBuffer(zbp,BUF_WIDTH);
- sceGuOffset( 2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2)); //Situar el espacio virtual de la PSP
- sceGuViewport( 2048, 2048, SCR_WIDTH, SCR_HEIGHT); //Situar el punto de vista
- sceGuDepthRange(65535, 0); //Bufer para los calculos de profundidad
- sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT); //Establece los limites para no redendizar lo que no se mostrara en pantalla
- sceGuEnable(GU_SCISSOR_TEST);
- sceGuFrontFace(GU_CW);
- sceGuShadeModel(GU_SMOOTH);
- sceGuDisable(GU_TEXTURE_2D);
- //sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
- sceGuFinish(); //Se acaba de establecer el 3D
- sceGuSync(0, 0); //Esperar hasta que todo lo anterior se acabe de ejecutar
- sceDisplayWaitVblankStart(); //Esperar para cumplir los 59,97 FPS
- sceGuDisplay(GU_TRUE); //Hace que se exponga el 3D
- sceKernelDcacheWritebackAll();
- return 1;
- }
- void SetupProjection(void)
- {
- sceGumMatrixMode(GU_PROJECTION); //Iniciar la perspectiva Matrix
- sceGumLoadIdentity(); //Lee la identidad Matrix
- sceGumOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, -1, 1); //Modo ortho para 2D
- sceGumMatrixMode(GU_VIEW); //Iniciar modo de vision
- sceGumLoadIdentity();
- sceGumMatrixMode(GU_MODEL);
- sceGumLoadIdentity();
- sceGuClearColor(GU_COLOR(0.0f, 0.0f, 0.0f, 1.0f)); //Limpiar pantalla con negro
- }
- void guBegin(void)
- {
- sceGuStart(GU_DIRECT, guList); //Iniciar el 3D
- sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT ); //Limpia el buffer 3D
- }
- void guEnd(void)
- {
- sceGuFinish();
- sceGuSync(0,0);
- fbp0 = sceGuSwapBuffers();
- }
- void initFPS()
- {
- currentTime = millis();
- lastTime = currentTime;
- frame_count = 0;
- }
- void FPS(float *fps_p)
- {
- frame_count++;
- currentTime = millis();
- diffTime = currentTime - lastTime;
- if(diffTime >= 1000) {
- *fps_p = (float)frame_count/(diffTime/1000.0f);
- frame_count = 0;
- lastTime = millis();
- }
- }
- int exit_callback(int arg1, int arg2, void *common)
- {
- sceGuTerm();
- sceKernelExitGame();
- return 0;
- }
- int CallbackThread(SceSize args, void *argp)
- {
- int cbid;
- cbid = sceKernelCreateCallback("exit_callback", exit_callback, NULL);
- sceKernelRegisterExitCallback(cbid);
- sceKernelSleepThreadCB();
- return 0;
- }
- int SetupCallbacks(void)
- {
- int thid = 0;
- thid = sceKernelCreateThread("callback_update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
- if(thid >= 0)
- {
- sceKernelStartThread(thid, 0, 0);
- }
- return thid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement