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.  
  11. #include "libnsfb.h"
  12. #include "libnsfb_plot.h"
  13. #include "libnsfb_event.h"
  14.  
  15. #define UNUSED(x) ((x) = (x))
  16.  
  17. const struct {
  18.     unsigned int w;
  19.     unsigned int h;
  20.     unsigned char data[16];
  21. } Mglyph1 = {
  22.     8, 16, {
  23.         0x00, /* 00000000 */
  24.         0x00, /* 00000000 */
  25.         0xc6, /* 11000110 */
  26.         0xee, /* 11101110 */
  27.         0xfe, /* 11111110 */
  28.         0xfe, /* 11111110 */
  29.         0xd6, /* 11010110 */
  30.         0xc6, /* 11000110 */
  31.         0xc6, /* 11000110 */
  32.         0xc6, /* 11000110 */
  33.         0xc6, /* 11000110 */
  34.         0xc6, /* 11000110 */
  35.         0x00, /* 00000000 */
  36.         0x00, /* 00000000 */
  37.         0x00, /* 00000000 */
  38.         0x00, /* 00000000 */
  39.     }
  40. };
  41.  
  42. const struct {
  43.     unsigned int w;
  44.     unsigned int h;
  45.     unsigned char data[16 * 8];
  46. } Mglyph8 = {
  47.     8, 16, {
  48.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000000 */
  49.         0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, /* 00000000 */
  50.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11000110 */
  51.         0xaa, 0xff, 0xff, 0x00, 0xff, 0xff, 0xaa, 0x00, /* 11101110 */
  52.         0xaa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xaa, 0x00, /* 11111110 */
  53.         0xaa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xaa, 0x00, /* 11111110 */
  54.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11010110 */
  55.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11000110 */
  56.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11000110 */
  57.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11000110 */
  58.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11000110 */
  59.         0xaa, 0xff, 0x00, 0x00, 0x00, 0xff, 0xaa, 0x00, /* 11000110 */
  60.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000000 */
  61.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000000 */
  62.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000000 */
  63.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000000 */
  64.     }
  65. };
  66.  
  67.  
  68. static bool
  69. dump(nsfb_t *nsfb, const char *filename)
  70. {
  71.     int fd;
  72.  
  73.     if (filename  == NULL)
  74.         return false;
  75.  
  76.     fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
  77.     if (fd < 0)
  78.         return false;
  79.  
  80.     nsfb_dump(nsfb, fd);
  81.    
  82.     close(fd);
  83.  
  84.     return true;
  85. }
  86.  
  87. int main(int argc, char **argv)
  88. {
  89.     const char *fename;
  90.     enum nsfb_type_e fetype;
  91.     nsfb_t *nsfb;
  92.     nsfb_event_t event;
  93.     int waitloop = 3;
  94.  
  95.     nsfb_bbox_t box;
  96.     nsfb_bbox_t box2;
  97.     nsfb_bbox_t box3;
  98.     uint8_t *fbptr;
  99.     int fbstride;
  100.     int p[] = { 300,300,  350,350, 400,300, 450,250, 400,200};
  101.     int loop;
  102.     nsfb_plot_pen_t pen;
  103.     const char *dumpfile = NULL;
  104.  
  105.     if (argc < 2) {
  106.         fename="sdl";
  107.     } else {
  108.         fename = argv[1];
  109.         if (argc >= 3) {
  110.             dumpfile = argv[2];
  111.         }
  112.     }
  113.  
  114.     fetype = nsfb_type_from_name(fename);
  115.     if (fetype == NSFB_SURFACE_NONE) {
  116.         fprintf(stderr, "Unable to convert \"%s\" to nsfb surface type\n", fename);
  117.         return 1;
  118.     }
  119.  
  120.     nsfb = nsfb_new(fetype);
  121.     if (nsfb == NULL) {
  122.         fprintf(stderr, "Unable to allocate \"%s\" nsfb surface\n", fename);
  123.         return 2;
  124.     }
  125.  
  126.     if (nsfb_init(nsfb) == -1) {
  127.         fprintf(stderr, "Unable to initialise nsfb surface\n");
  128.         nsfb_free(nsfb);
  129.         return 4;
  130.     }
  131.  
  132.     /* get the geometry of the whole screen */
  133.     box.x0 = box.y0 = 0;
  134.     nsfb_get_geometry(nsfb, &box.x1, &box.y1, NULL);
  135.  
  136.     nsfb_get_buffer(nsfb, &fbptr, &fbstride);
  137.  
  138.     /* claim the whole screen for update */
  139.     nsfb_claim(nsfb, &box);
  140.  
  141.     /* first test, repeatedly clear the graphics area, should result in the
  142.      * same operation as a single clear to the final colour
  143.      */
  144.     for (loop = 0; loop < 256;loop++) {
  145.         nsfb_plot_clg(nsfb, 0xffffff00 | loop);
  146.     }
  147.  
  148.     /* draw black radial lines from the origin */
  149.     pen.stroke_colour = 0xff000000;
  150.     for (loop = 0; loop < box.x1; loop += 20) {
  151.         box2 = box;
  152.         box2.x1 = loop;
  153.         nsfb_plot_line(nsfb, &box2, &pen);
  154.     }
  155.    
  156.     /* draw blue radial lines from the bottom right */
  157.     pen.stroke_colour = 0xffff0000;
  158.     for (loop = 0; loop < box.x1; loop += 20) {
  159.         box2 = box;
  160.         box2.x0 = loop;
  161.         nsfb_plot_line(nsfb, &box2, &pen);
  162.     }
  163.    
  164.     /* draw green radial lines from the bottom left */
  165.     pen.stroke_colour = 0xff00ff00;
  166.     for (loop = 0; loop < box.x1; loop += 20) {
  167.         box2.x0 = box.x0;
  168.         box2.x1 = loop;
  169.         box2.y0 = box.y1;
  170.         box2.y1 = box.y0;
  171.         nsfb_plot_line(nsfb, &box2, &pen);
  172.     }
  173.  
  174.     /* draw red radial lines from the top right */
  175.     pen.stroke_colour = 0xff0000ff;
  176.     for (loop = 0; loop < box.x1; loop += 20) {
  177.         box2.x0 = box.x1;
  178.         box2.x1 = loop;
  179.         box2.y0 = box.y0;
  180.         box2.y1 = box.y1;
  181.         nsfb_plot_line(nsfb, &box2, &pen);
  182.     }
  183.  
  184.     /* draw an unclipped rectangle */
  185.     box2.x0 = box2.y0 = 100;
  186.     box2.x1 = box2.y1 = 300;
  187.  
  188.     nsfb_plot_rectangle_fill(nsfb, &box2, 0xff0000ff);
  189.  
  190.     nsfb_plot_rectangle(nsfb, &box2, 1, 0xff00ff00, false, false);
  191.  
  192.     nsfb_plot_polygon(nsfb, p, 5, 0xffff0000);
  193.  
  194.     nsfb_plot_set_clip(nsfb, &box2);
  195.  
  196.     box3.x0 = box3.y0 = 200;
  197.     box3.x1 = box3.y1 = 400;
  198.  
  199.     nsfb_plot_rectangle_fill(nsfb, &box3, 0xff00ffff);
  200.  
  201.     nsfb_plot_rectangle(nsfb, &box3, 1, 0xffffff00, false, false);
  202.  
  203.     for (loop = 100; loop < 400;loop++) {
  204.         nsfb_plot_point(nsfb, loop, 150, 0xffaa1111);
  205.         nsfb_plot_point(nsfb, loop, 160, 0x99aa1111);
  206.     }
  207.  
  208.     nsfb_plot_set_clip(nsfb, NULL);
  209.  
  210.     box3.x0 = box3.y0 = 400;
  211.     box3.x1 = box3.y1 = 600;
  212.  
  213.     nsfb_plot_ellipse_fill(nsfb, &box3, 0xffff0000);
  214.  
  215.     nsfb_plot_ellipse(nsfb, &box3, 0xff0000ff);
  216.  
  217.     box3.x0 = 500;
  218.     box3.x1 = 700;
  219.     box3.y0 = 400;
  220.     box3.y1 = 500;
  221.  
  222.     nsfb_plot_ellipse_fill(nsfb, &box3, 0xffff0000);
  223.  
  224.     nsfb_plot_ellipse(nsfb, &box3, 0xff0000ff);
  225.  
  226.     box3.x0 = 600;
  227.     box3.x1 = 700;
  228.     box3.y0 = 300;
  229.     box3.y1 = 500;
  230.  
  231.     nsfb_plot_ellipse_fill(nsfb, &box3, 0xff0000ff);
  232.  
  233.     nsfb_plot_ellipse(nsfb, &box3, 0xffff0000);
  234.  
  235.     box2.x0 = 400;
  236.     box2.y0 = 400;
  237.     box2.x1 = 500;
  238.     box2.y1 = 500;
  239.  
  240.     box3.x0 = 600;
  241.     box3.y0 = 200;
  242.     box3.x1 = 700;
  243.     box3.y1 = 300;
  244.  
  245.     nsfb_plot_copy(nsfb, &box2, nsfb, &box3);
  246.  
  247.     /* test glyph plotting */
  248.     for (loop = 100; loop < 200; loop+= Mglyph1.w) {
  249.         box3.x0 = loop;
  250.         box3.y0 = 20;
  251.         box3.x1 = box3.x0 + Mglyph8.w;
  252.         box3.y1 = box3.y0 + Mglyph8.h;
  253.  
  254.         nsfb_plot_glyph1(nsfb, &box3,  Mglyph1.data, Mglyph1.w, 0xff000000);
  255.     }
  256.  
  257.     /* test glyph plotting */
  258.     for (loop = 100; loop < 200; loop+= Mglyph8.w) {
  259.         box3.x0 = loop;
  260.         box3.y0 = 50;
  261.         box3.x1 = box3.x0 + Mglyph8.w;
  262.         box3.y1 = box3.y0 + Mglyph8.h;
  263.  
  264.         nsfb_plot_glyph8(nsfb, &box3,  Mglyph8.data, Mglyph8.w, 0xff000000);
  265.     }
  266.  
  267.     nsfb_update(nsfb, &box);
  268.  
  269.     /* random rectangles in clipped area*/
  270.     box2.x0 = 400;
  271.     box2.y0 = 50;
  272.     box2.x1 = 600;
  273.     box2.y1 = 100;
  274.  
  275.     nsfb_plot_set_clip(nsfb, &box2);
  276.  
  277.     srand(1234);
  278.  
  279.     for (loop=0; loop < 10000; loop++) {
  280.         nsfb_claim(nsfb, &box2);
  281.         box3.x0 = rand() / (RAND_MAX / box.x1);
  282.         box3.y0 = rand() / (RAND_MAX / box.y1);
  283.         box3.x1 = rand() / (RAND_MAX / 400);
  284.         box3.y1 = rand() / (RAND_MAX / 400);
  285.         nsfb_plot_rectangle_fill(nsfb, &box3, 0xff000000 | rand());
  286.         nsfb_update(nsfb, &box2);
  287.     }
  288.  
  289.     /* wait for quit event or timeout */
  290.     while (waitloop > 0) {
  291.         if (nsfb_event(nsfb, &event, 1000)  == false) {
  292.             break;
  293.         }
  294.         if (event.type == NSFB_EVENT_CONTROL) {
  295.             if (event.value.controlcode == NSFB_CONTROL_TIMEOUT) {
  296.                 /* timeout */
  297.                 waitloop--;
  298.             } else if (event.value.controlcode == NSFB_CONTROL_QUIT) {
  299.                 break;
  300.             }
  301.         }
  302.     }
  303.  
  304.     dump(nsfb, dumpfile);
  305.  
  306.     nsfb_free(nsfb);
  307.  
  308.     return 0;
  309. }
  310.  
  311. /*
  312.  * Local variables:
  313.  *  c-basic-offset: 4
  314.  *  tab-width: 8
  315.  * End:
  316.  */
  317.