Advertisement
Guest User

Sample

a guest
Oct 19th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.66 KB | None | 0 0
  1. /* ya2d sample by xerpi */
  2.  
  3. #include <pspkernel.h>
  4. #include <pspctrl.h>
  5. #include <pspdisplay.h>
  6. #include <pspdebug.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <pspgu.h>
  13. #include <pspgum.h>
  14. #include <vram.h>
  15. #include <tinyfont.h>
  16. #include <ya2d.h>
  17.  
  18. PSP_MODULE_INFO("ya2d Sample", 0, 1, 1);
  19. PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
  20.  
  21. /* Exit callback */
  22. int exit_callback(int arg1, int arg2, void *common)
  23. {
  24.     return 0;
  25. }
  26.  
  27. /* Callback thread */
  28. int CallbackThread(SceSize args, void *argp)
  29. {
  30.     int cbid;
  31.     cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  32.     sceKernelRegisterExitCallback(cbid);
  33.     sceKernelSleepThreadCB();
  34.     return 0;
  35. }
  36.  
  37. /* Sets up the callback thread and returns its thread id */
  38. int SetupCallbacks(void)
  39. {
  40.     int thid = 0;
  41.     thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  42.     if(thid >= 0) {
  43.         sceKernelStartThread(thid, 0, 0);
  44.     }
  45.     return thid;
  46. }
  47.  
  48. int main(int argc, char* argv[])
  49. {
  50.     // srand(time(NULL));
  51.    
  52.     SetupCallbacks();
  53.     sceCtrlSetSamplingCycle(0);
  54.     sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
  55.    
  56.     ya2d_init();
  57.    
  58.     struct ya2d_texture *t;
  59.     t = ya2d_load_PNG_file("test3.png", YA2D_PLACE_VRAM);
  60.     if (t == NULL) goto exit;
  61.    
  62.     struct ya2d_texture *c;
  63.     c = ya2d_load_PNG_file("test4.png", YA2D_PLACE_VRAM);
  64.     if (c == NULL) goto exit;
  65.    
  66.     ya2d_swizzle_texture(t);
  67.    
  68.     float angle = 0.0f;
  69.     float cross_x = 50, cross_y = 50;
  70.     SceCtrlData pad, old_pad; old_pad.Buttons = 0;
  71.     int centered = 0, rotate = 0, vsync = 0;
  72.     unsigned char trans = 0;
  73.    
  74.     ya2d_set_vsync(1);
  75.    
  76.     struct ya2d_camera *CAMERA;
  77.     //we center camera on the screen
  78.     CAMERA = ya2d_init_camera(240.0f, 136.0f, 4.0f, 1.0f, 1.0f);
  79.     if(CAMERA == NULL) goto exit;
  80.    
  81.     while(1)
  82.     {
  83.         sceCtrlPeekBufferPositive(&pad, 1);
  84.         ya2d_start_drawing();
  85.         ya2d_clear_screen(0);
  86.        
  87.         if (pad.Buttons & PSP_CTRL_RTRIGGER) angle += 0.005f;
  88.         else if (pad.Buttons & PSP_CTRL_LTRIGGER) angle -= 0.005f;
  89.        
  90.         if (pad.Buttons & PSP_CTRL_CROSS & ~old_pad.Buttons) centered = !centered;
  91.         if (pad.Buttons & PSP_CTRL_TRIANGLE & ~old_pad.Buttons) rotate = !rotate;
  92.         if (pad.Buttons & PSP_CTRL_SQUARE & ~old_pad.Buttons) ya2d_set_vsync(vsync = !vsync);
  93.        
  94.         if (pad.Buttons & PSP_CTRL_RIGHT) {
  95.             if (trans < 255) trans++;
  96.         }
  97.         if (pad.Buttons & PSP_CTRL_LEFT) {
  98.             if (trans > 0) trans--;
  99.         }
  100.    
  101.         if (fabs(pad.Lx-128) > 60) CAMERA->x += CAMERA->speed * (pad.Lx-128)/100.0f;
  102.         if (fabs(pad.Ly-128) > 60) CAMERA->y += CAMERA->speed * (pad.Ly-128)/100.0f;
  103.        
  104.         ya2d_draw_pixel(NULL, 16, 16, 0xFFFFFFFF);
  105.         ya2d_draw_pixel(CAMERA, 16, 16, 0xFFFFFFFF);
  106.  
  107.         /*
  108.             EQUIVALENT OF
  109.             ya2d_draw_texture_scale(t, cross_x, cross_y, 0.5f, 0.5f);
  110.         */     
  111.         // ya2d_draw_texture_scale_size_defined(t, cross_x, cross_y, 128, 128);
  112.  
  113.         /*
  114.             DRAW GRID FROM
  115.            
  116.             CAMERA (NULL if we want draw on screen)
  117.             START X (UP LEFT COORD)
  118.             START Y (UP LEFT COORD)
  119.             SIZE OF A CASE FROM GRID IN X
  120.             SIZE OF A CASE FROM GRID IN Y
  121.             NUMBER OF CASES X
  122.             NUMBER OF CASES Y
  123.             LINE SIZE
  124.             COLOR
  125.         */
  126.         ya2d_draw_grid(NULL, 4, 4, 32, 32, 4, 4, 1, 0xFFFFFFFF);
  127.         ya2d_draw_grid(CAMERA, 136, 136, 4, 4, 4096, 4096, 1, 0xFF0000FF);
  128.         ya2d_draw_grid_centered(CAMERA, 100, 100, 32, 32, 4, 4, 8, 0xFF0000FF);
  129.        
  130.         int i;
  131.         char l = 'A';
  132.         int x = 2;
  133.         for(i = 40; i >= 8; i -= 2)
  134.         {
  135.             tinyfont_draw_stringf(x, 2, i, GU_RGBA(0,0,255,255), "%c ", l);
  136.             l++;
  137.             x += i;
  138.         }
  139.        
  140.         tinyfont_draw_stringf(10, 272-20, 10, GU_RGBA(0,0,255,255), "FPS: %.2f, camera coord = {%d,%d}", ya2d_get_fps(), (int)CAMERA->x, (int)CAMERA->y);
  141.        
  142.         // tinyfont_set_size(8);
  143.         // tinyfont_draw_stringf(12, 30,  GU_RGBA(0,0,255,255), "x: %.2f | y: %.2f", cross_x, cross_y);
  144.         // if (centered) tinyfont_draw_string(265, 42,  GU_RGBA(255,0,255,255), "centered");
  145.         // if (rotate)   tinyfont_draw_string(265, 54,  GU_RGBA(0,255,255,255), "rotate");
  146.         // if (vsync)    tinyfont_draw_string(265, 66,  GU_RGBA(255,0,0,255), "vsync");
  147.         // tinyfont_draw_string16x16(10, 60,  GU_RGBA(35,153,45,255), "16x16 Draw test!");
  148.        
  149.         tinyfont_draw_rotated_string(240, 136, 12, GU_RGBA(255,255,0,255), angle, "Rotated text!!");
  150.        
  151.         ya2d_finish_drawing();
  152.         ya2d_swapbuffers();
  153.         ya2d_calc_fps();
  154.         old_pad = pad;
  155.     }
  156.    
  157.     ya2d_free_texture(t);
  158. exit:
  159.     ya2d_shutdown();
  160.     sceKernelExitGame();
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement