Advertisement
xerpi

ya2d

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