Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* libnsfb plotter test program */
  2.  
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11.  
  12. #include "libnsfb.h"
  13. #include "libnsfb_plot.h"
  14. #include "libnsfb_event.h"
  15. #include "surface.h"
  16.  
  17. #include <menuet/os.h>
  18.  
  19. #define UNUSED(x) ((x) = (x))
  20.  
  21. extern const struct {
  22.     unsigned int  width;
  23.     unsigned int  height;
  24.     unsigned int  bytes_per_pixel; /* 3:RGB, 4:RGBA */
  25.     unsigned char pixel_data[132 * 135 * 4 + 1];
  26. } nsglobe;
  27.  
  28. static bool
  29. dump(nsfb_t *nsfb, const char *filename)
  30. {
  31.     int fd;
  32.  
  33.     if (filename  == NULL)
  34.         return false;
  35.  
  36.     fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
  37.     if (fd < 0)
  38.         return false;
  39.  
  40.     nsfb_dump(nsfb, fd);
  41.    
  42.     close(fd);
  43.  
  44.     return true;
  45. }
  46.  
  47. int main(int argc, char **argv)
  48. {
  49.                 extern nsfb_surface_rtns_t sdl_rtns;
  50.                 extern nsfb_surface_rtns_t ram_rtns;
  51.        
  52.         _nsfb_register_surface(NSFB_SURFACE_SDL, &sdl_rtns, "sdl");
  53.         _nsfb_register_surface(NSFB_SURFACE_RAM, &ram_rtns, "ram");
  54.        
  55.        
  56.     const char *fename;
  57.     enum nsfb_type_e fetype;
  58.     nsfb_t *nsfb;
  59.     nsfb_t *bmp;
  60.     nsfb_event_t event;
  61.     int waitloop = 3;
  62.  
  63.     nsfb_bbox_t box;
  64.     nsfb_bbox_t box2;
  65.     nsfb_bbox_t box3;
  66.     uint8_t *fbptr;
  67.     int fbstride;
  68.     const char *dumpfile = NULL;
  69.  
  70.         __menuet__debug_out("Init..\n");
  71.  
  72.     if (argc < 2) {
  73.         fename="sdl";
  74.     } else {
  75.         fename = argv[1];
  76.         if (argc >= 3) {
  77.             dumpfile = argv[2];
  78.         }
  79.     }
  80.  
  81.     fetype = nsfb_type_from_name(fename);
  82.     if (fetype == NSFB_SURFACE_NONE) {
  83.         fprintf(stderr, "Unable to convert \"%s\" to nsfb surface type\n", fename);
  84.         return 1;
  85.     }
  86.  
  87.     nsfb = nsfb_new(fetype);
  88.     if (nsfb == NULL) {
  89.         fprintf(stderr, "Unable to allocate \"%s\" nsfb surface\n", fename);
  90.         return 2;
  91.     }
  92.  
  93.     if (nsfb_init(nsfb) == -1) {
  94.         fprintf(stderr, "Unable to initialise nsfb surface\n");
  95.         nsfb_free(nsfb);
  96.         return 4;
  97.     }
  98.  
  99.         __menuet__debug_out("New ram surface...\n");
  100.  
  101.     bmp = nsfb_new(NSFB_SURFACE_RAM);
  102.    
  103.       if (bmp == NULL) {
  104.        __menuet__debug_out("No ram surface :D ..\n");
  105.        return 5;
  106.     }
  107.     __menuet__debug_out("Oh shit :D ..\n");
  108.     nsfb_set_geometry(bmp, nsglobe.width, nsglobe.height, NSFB_FMT_ABGR8888);
  109.     nsfb_init(bmp);
  110.     nsfb_get_buffer(bmp, &fbptr, &fbstride);
  111.  
  112.     memcpy(fbptr, nsglobe.pixel_data, nsglobe.width * nsglobe.height * 4);
  113.  
  114.     /* get the geometry of the whole screen */
  115.     box.x0 = box.y0 = 0;
  116.     nsfb_get_geometry(nsfb, &box.x1, &box.y1, NULL);
  117.  
  118.     /* claim the whole screen for update */
  119.     nsfb_claim(nsfb, &box);
  120.  
  121.     nsfb_plot_clg(nsfb, 0xffffffff);
  122.  
  123.     box3.x0 = 0;
  124.     box3.y0 = 0;
  125.     box3.x1 = 132;
  126.     box3.y1 = 135;
  127.  
  128.     nsfb_plot_copy(bmp, &box3, nsfb, &box3);
  129.  
  130.     box3.x0 = 132;
  131.     box3.y0 = 135;
  132.     box3.x1 = box3.x0 + 264;
  133.     box3.y1 = box3.y0 + 135;
  134.  
  135.     nsfb_plot_copy(bmp, &box3, nsfb, &box3);
  136.  
  137.     box3.x0 = 396;
  138.     box3.y0 = 270;
  139.     box3.x1 = box3.x0 + 264;
  140.     box3.y1 = box3.y0 + 270;
  141.  
  142.     nsfb_plot_copy(bmp, &box3, nsfb, &box3);
  143.  
  144.     box2.x0 = 64;
  145.     box2.y0 = 64;
  146.     box2.x1 = 128;
  147.     box2.y1 = 128;
  148.  
  149.     box3.x0 = 270;
  150.     box3.y0 = 270;
  151.     box3.x1 = box3.x0 + 64;
  152.     box3.y1 = box3.y0 + 64;
  153.  
  154.     nsfb_plot_copy(nsfb, &box2, nsfb, &box3);
  155.  
  156.     nsfb_update(nsfb, &box);
  157.  
  158.     /* wait for quit event or timeout */
  159.     while (waitloop > 0) {
  160.         if (nsfb_event(nsfb, &event, 1000)  == false) {
  161.             break;
  162.         }
  163.         if (event.type == NSFB_EVENT_CONTROL) {
  164.             if (event.value.controlcode == NSFB_CONTROL_TIMEOUT) {
  165.                 /* timeout */
  166.                 waitloop--;
  167.             } else if (event.value.controlcode == NSFB_CONTROL_QUIT) {
  168.                 break;
  169.             }
  170.         }
  171.     }
  172.  
  173.     //dump(nsfb, dumpfile);
  174.  
  175.     nsfb_free(bmp);
  176.  
  177.     nsfb_free(nsfb);
  178.  
  179.     return 0;
  180. }
  181.  
  182. /*
  183.  * Local variables:
  184.  *  c-basic-offset: 4
  185.  *  tab-width: 8
  186.  * End:
  187.  */
  188.