Advertisement
xerpi

ya2d shit

Mar 2nd, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.13 KB | None | 0 0
  1. #include "ya2d.h"
  2.  
  3. /* Variables */
  4.     //GU
  5.         unsigned int __attribute__((aligned(16))) ya2d_guList[262144];
  6.         void *ya2d_fbp0 = NULL, *ya2d_fbp1 = NULL, *ya2d_zbp = NULL;
  7.     //Frame counting
  8.         u32    ya2d_frameCount;
  9.         time_t ya2d_currentTime, ya2d_lastTime, ya2d_diffTime;
  10.     //ya2d
  11.         u8 ya2d_inited = 0;
  12.  
  13. /* Functions */
  14.  
  15.  
  16.     int ya2d_init()
  17.     {
  18.         if(ya2d_inited) return 1;
  19.  
  20.         if(ya2d_fbp0 == NULL) ya2d_fbp0 = getStaticVramBuffer(YA2D_BUF_WIDTH,YA2D_SCR_HEIGHT,GU_PSM_8888);
  21.         if(ya2d_fbp1 == NULL) ya2d_fbp1 = getStaticVramBuffer(YA2D_BUF_WIDTH,YA2D_SCR_HEIGHT,GU_PSM_8888);
  22.         if(ya2d_zbp  == NULL) ya2d_zbp  = getStaticVramBuffer(YA2D_BUF_WIDTH,YA2D_SCR_HEIGHT,GU_PSM_4444);
  23.  
  24.         sceGuInit();
  25.         sceGuStart(GU_DIRECT, ya2d_guList);
  26.         sceGuDrawBuffer(GU_PSM_8888, ya2d_fbp0, YA2D_BUF_WIDTH);
  27.         sceGuDispBuffer(YA2D_SCR_WIDTH, YA2D_SCR_HEIGHT, ya2d_fbp1, YA2D_BUF_WIDTH);
  28.         sceGuDepthBuffer(ya2d_zbp, YA2D_BUF_WIDTH);
  29.         sceGuOffset(2048 - (YA2D_SCR_WIDTH/2), 2048 - (YA2D_SCR_HEIGHT/2));
  30.         sceGuViewport(2048, 2048, YA2D_SCR_WIDTH, YA2D_SCR_HEIGHT);
  31.         sceGuDepthRange(65535, 0);
  32.  
  33.         sceGuScissor(0, 0, YA2D_SCR_WIDTH, YA2D_SCR_HEIGHT);
  34.  
  35.         sceGuEnable(GU_SCISSOR_TEST);
  36.         sceGuDepthFunc(GU_GEQUAL);
  37.         sceGuEnable(GU_DEPTH_TEST);
  38.         sceGuFrontFace(GU_CW);
  39.         sceGuShadeModel(GU_SMOOTH);
  40.         sceGuEnable(GU_CULL_FACE);
  41.         sceGuEnable(GU_CLIP_PLANES);
  42.         sceGuFinish();
  43.         sceGuSync(0,0);
  44.         sceDisplayWaitVblankStart();
  45.         sceGuDisplay(GU_TRUE);
  46.  
  47.         sceGumMatrixMode(GU_PROJECTION);
  48.             sceGumLoadIdentity();
  49.             sceGumOrtho(0, 480, 0, 272, -1, 1);
  50.         sceGumMatrixMode(GU_VIEW);
  51.             sceGumLoadIdentity();
  52.         sceGumMatrixMode(GU_MODEL);
  53.             sceGumLoadIdentity();
  54.  
  55.         //Debug console
  56.             pspDebugScreenInit();
  57.             ya2d_resetTerminal();
  58.  
  59.         ya2d_inited = 1;
  60.         return 1;
  61.     }
  62.  
  63.  
  64.     int ya2d_deinit()
  65.     {
  66.         sceGuTerm();
  67.         ya2d_inited = 0;
  68.         return 1;
  69.     }
  70.  
  71.     void ya2d_clearScreen()
  72.     {
  73.         sceGuStart(GU_DIRECT, ya2d_guList);
  74.         sceGuClearColor(0);
  75.         sceGuClearDepth(0);
  76.         sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
  77.     }
  78.  
  79.     void ya2d_flipScreen()
  80.     {
  81.         sceGuFinish();
  82.         sceGuSync(0,0);
  83.         sceDisplayWaitVblankStart();
  84.         ya2d_fbp0 = sceGuSwapBuffers();
  85.     }
  86.  
  87.     void ya2d_resetTerminal()
  88.     {
  89.         pspDebugScreenSetOffset((int)ya2d_fbp0);
  90.         pspDebugScreenClear();
  91.         //pspDebugScreenSetXY(0,0);
  92.     }
  93.  
  94.     void ya2d_drawRect(int x, int y, int w, int h, u32 color)
  95.     {
  96.         sceGuDisable(GU_TEXTURE_2D);
  97.         ya2d_FastVertex *vertices = (ya2d_FastVertex *)sceGuGetMemory(5 * sizeof(ya2d_FastVertex));
  98.  
  99.         vertices[0] = (ya2d_FastVertex){color, x, y};
  100.         vertices[1] = (ya2d_FastVertex){color, x+w, y};
  101.         vertices[2] = (ya2d_FastVertex){color, x+w, y+h};
  102.         vertices[3] = (ya2d_FastVertex){color, x, y+h};
  103.         vertices[4] = vertices[0];
  104.  
  105.         sceGumDrawArray(GU_LINE_STRIP, GU_COLOR_8888|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 5, 0, vertices);
  106.  
  107.         sceKernelDcacheWritebackRange(vertices, 5 * sizeof(ya2d_FastVertex));
  108.     }
  109.  
  110.     void ya2d_drawFillRect(int x, int y, int h, int w, u32 color)
  111.     {
  112.         sceGuDisable(GU_TEXTURE_2D);
  113.         ya2d_FastVertex *vertices = (ya2d_FastVertex *)sceGuGetMemory(4 * sizeof(ya2d_FastVertex));
  114.  
  115.         vertices[0] = (ya2d_FastVertex){color, x, y};
  116.         vertices[1] = (ya2d_FastVertex){color, x, y+h};
  117.         vertices[2] = (ya2d_FastVertex){color, x+w, y};
  118.         vertices[3] = (ya2d_FastVertex){color, x+w, y+h};
  119.  
  120.         sceGumDrawArray(GU_TRIANGLE_STRIP, GU_COLOR_8888|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 4, 0, vertices);
  121.  
  122.         sceKernelDcacheWritebackRange(vertices, 4 * sizeof(ya2d_FastVertex));
  123.     }
  124.  
  125.     int ya2d_loadPNGfromFile(char *filename, ya2d_Texture *texp)
  126.     {
  127.         uint8_t      header[YA2D_PNG_SIG_LEN];
  128.         png_structp  png_ptr  = NULL;
  129.         png_infop    info_ptr = NULL;
  130.         SceUID       fp;
  131.  
  132.         if(!(fp = sceIoOpen(filename, PSP_O_RDONLY, 0777)))
  133.             goto exit_error;
  134.  
  135.         sceIoRead(fp, header, YA2D_PNG_SIG_LEN);
  136.         if (png_sig_cmp((png_bytep)header, 0, YA2D_PNG_SIG_LEN))
  137.             goto exit_close;
  138.  
  139.         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  140.         if(png_ptr == NULL)
  141.             goto exit_close;
  142.  
  143.         info_ptr = png_create_info_struct(png_ptr);
  144.         if(info_ptr == NULL)
  145.             goto exit_destroy;
  146.  
  147.         if(setjmp(png_jmpbuf(png_ptr)))
  148.             goto exit_destroy;
  149.  
  150.         //ya2d_freeTexture(texp);
  151.  
  152.         png_set_read_fn(png_ptr, (void *)&fp, (png_rw_ptr)_ya2d_png_read_fn);
  153.         png_set_sig_bytes(png_ptr, YA2D_PNG_SIG_LEN);
  154.         png_read_info(png_ptr, info_ptr);
  155.         png_get_IHDR(png_ptr, info_ptr, &texp->imageWidth, &texp->imageHeight, &texp->bitDepth, &texp->colorType, NULL, NULL, NULL);
  156.  
  157.         texp->textureWidth  = (texp->imageWidth);
  158.         texp->textureHeight = (texp->imageHeight);
  159.  
  160.         if(texp->bitDepth == 16)
  161.         {
  162.             png_set_strip_16(png_ptr);
  163.             texp->bitDepth = 8;
  164.         }
  165.         else if(texp->bitDepth < 8) //1 byte per pixel component
  166.         {
  167.             png_set_packing(png_ptr);
  168.             texp->bitDepth = 8;
  169.         }
  170.  
  171.         if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
  172.         switch(texp->colorType)
  173.         {
  174.             case PNG_COLOR_TYPE_RGB_ALPHA:
  175.             case PNG_COLOR_TYPE_GRAY_ALPHA:
  176.                 texp->hasAlpha = 1;
  177.                 break;
  178.             case PNG_COLOR_TYPE_PALETTE:
  179.                 png_set_palette_to_rgb(png_ptr);
  180.                 texp->hasAlpha = 0;
  181.                 break;
  182.             case PNG_COLOR_TYPE_GRAY:
  183.                 if (texp->bitDepth < 8)
  184.                 {
  185.                     png_set_expand_gray_1_2_4_to_8(png_ptr);
  186.                     texp->bitDepth = 8;
  187.                 }
  188.                 break;
  189.             default:
  190.                 texp->hasAlpha = 0;
  191.                 break;
  192.         }
  193.         png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
  194.         png_read_update_info (png_ptr, info_ptr);
  195.  
  196.         texp->rowBytes = png_get_rowbytes(png_ptr, info_ptr);
  197.         texp->dataLength = texp->rowBytes * texp->textureHeight;
  198.         texp->data = (uint8_t *)memalign(16, texp->dataLength * 4);
  199.  
  200.         //memset(texp->data, 0x0, texp->dataLength);
  201.         u32 *line = (u32*) malloc(texp->rowBytes);
  202.         int x, y;
  203.         for (y = 0; y < texp->imageHeight; y++)
  204.         {
  205.             png_read_row(png_ptr, (u8*) line, NULL);
  206.             for (x = 0; x < texp->imageWidth; x++)
  207.             {
  208.                 u32 color = line[x];
  209.                 texp->data[x + y * texp->textureWidth] =  color;
  210.             }
  211.         }
  212.  
  213.         free(line);
  214.  
  215.         png_read_end(png_ptr, NULL);
  216.         png_destroy_read_struct(&png_ptr, NULL, NULL);
  217.         sceIoClose(fp);
  218.         return 1;
  219.  
  220.     exit_destroy:
  221.         png_destroy_read_struct(&png_ptr, NULL, NULL);
  222.     exit_close:
  223.         sceIoClose(fp);
  224.     exit_error:
  225.         return 0;
  226.     }
  227.  
  228.  
  229.     void ya2d_freeTexture(ya2d_Texture *texp)
  230.     {
  231.         if(texp->data != NULL)
  232.         {
  233.             free(texp->data);
  234.             texp->data = NULL;
  235.         }
  236.     }
  237.  
  238.  
  239.  
  240.     void _ya2d_png_read_fn(png_structp png_ptr, png_bytep buffer, uint32_t bytesToRead)
  241.     {
  242.         SceUID *fp = (SceUID *)png_get_io_ptr(png_ptr);
  243.         if(fp == NULL)
  244.             return;
  245.         uint32_t bytesReaded = sceIoRead((SceUID)*fp, (void*)buffer, bytesToRead);
  246.         if(bytesReaded != bytesToRead)
  247.             return;
  248.     }
  249.  
  250.     void ya2d_drawTexture(ya2d_Texture *texp, int x, int y)
  251.     {
  252.         if(!texp->data) return;
  253.  
  254.         sceGuEnable(GU_TEXTURE_2D);
  255.         sceGuShadeModel(GU_SMOOTH);
  256.  
  257.         sceGuAlphaFunc(GU_GREATER, 0, 255);
  258.         sceGuDepthFunc(GU_LEQUAL);
  259.         sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  260.         //setup texture
  261.             sceGuTexMode(GU_PSM_8888,0,0,0);
  262.             sceGuTexFunc(GU_TFX_DECAL, GU_TCC_RGBA);//apply as decal
  263.             sceGuTexFilter(GU_LINEAR, GU_LINEAR);   //linear good quality
  264.             sceGuTexScale(1.0f,1.0f); //no scaling
  265.             sceGuTexOffset(0.0f,0.0f);
  266.  
  267.         sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
  268.        
  269.         if(texp->hasAlpha)
  270.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
  271.         else
  272.             sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
  273.  
  274.         sceGuTexImage(0, texp->textureWidth, texp->textureHeight, texp->rowBytes, texp->data);
  275.  
  276.         ya2d_TextureVertex *vertices = (ya2d_TextureVertex *)sceGuGetMemory(2 * sizeof(ya2d_TextureVertex));
  277.  
  278.         vertices[0] = (ya2d_TextureVertex){0, 0, x, y};
  279.         vertices[1] = (ya2d_TextureVertex){texp->imageWidth, +texp->imageHeight, x+texp->imageWidth, y+texp->imageHeight};
  280.  
  281.         sceGumDrawArray(GU_SPRITES, GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 2, 0, vertices);
  282.  
  283.         sceKernelDcacheWritebackRange(vertices, 2 * sizeof(ya2d_TextureVertex));
  284.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement