Subversion Repositories Kolibri OS

Rev

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

  1. /* libnsfb ploygon plotter test program */
  2.  
  3. #ifndef _POSIX_C_SOURCE
  4. #define _POSIX_C_SOURCE 199506L
  5. #endif
  6.  
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. #include "libnsfb.h"
  13. #include "libnsfb_plot.h"
  14. #include "libnsfb_event.h"
  15.  
  16.  
  17. #include <time.h>
  18. // Sleep milliseconds
  19. static void sleepMilli(long ms)
  20. {
  21.         const struct timespec ts = {ms / 1000, ms % 1000 * 1000000};
  22.         nanosleep(&ts, NULL);  
  23. }
  24.  
  25. #define UNUSED(x) ((x) = (x))
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.     const char *fename;
  30.     enum nsfb_type_e fetype;
  31.     nsfb_t *nsfb;
  32.  
  33.     int waitloop = 3;
  34.  
  35.     nsfb_event_t event;
  36.     nsfb_bbox_t box;
  37.     uint8_t *fbptr;
  38.     int fbstride;
  39.  
  40.     int sides;
  41.     int radius;
  42.     nsfb_point_t *points;
  43.     int loop;
  44.     nsfb_plot_pen_t pen;
  45.  
  46.     double rotate;
  47.  
  48.     if (argc < 2) {
  49.         fename="sdl";
  50.     } else {
  51.         fename = argv[1];
  52.     }
  53.  
  54.     fetype = nsfb_type_from_name(fename);
  55.     if (fetype == NSFB_SURFACE_NONE) {
  56.         fprintf(stderr, "Unable to convert \"%s\" to nsfb surface type\n", fename);
  57.         return 1;
  58.     }
  59.  
  60.     nsfb = nsfb_new(fetype);
  61.     if (nsfb == NULL) {
  62.         fprintf(stderr, "Unable to allocate \"%s\" nsfb surface\n", fename);
  63.         return 2;
  64.     }
  65.  
  66.     if (nsfb_init(nsfb) == -1) {
  67.         fprintf(stderr, "Unable to initialise nsfb surface\n");
  68.         nsfb_free(nsfb);
  69.         return 4;
  70.     }
  71.  
  72.     /* get the geometry of the whole screen */
  73.     box.x0 = box.y0 = 0;
  74.     nsfb_get_geometry(nsfb, &box.x1, &box.y1, NULL);
  75.  
  76.     nsfb_get_buffer(nsfb, &fbptr, &fbstride);
  77.  
  78.  
  79.     pen.stroke_colour = 0xff000000;
  80.     pen.stroke_type = NFSB_PLOT_OPTYPE_SOLID;
  81.  
  82.  
  83.     for (rotate =0; rotate < (2 * M_PI); rotate += (M_PI / 8)) {
  84.             /* claim the whole screen for update */
  85.             nsfb_claim(nsfb, &box);
  86.  
  87.             nsfb_plot_clg(nsfb, 0xffffffff);
  88.  
  89.             radius = (box.y1 / 2);
  90.  
  91.             for (sides = 18; sides >=9; sides-=2) {
  92.                     points = malloc(sizeof(nsfb_point_t) * sides);
  93.  
  94.                     for (loop = 0; loop < sides;loop+=2) {
  95.                             points[loop].x = (box.x1 / 2) +
  96.                                     (radius * cos((loop * 2 * M_PI / sides) + rotate));
  97.                             points[loop].y = (box.y1 / 2) +
  98.                                     (radius * sin((loop * 2 * M_PI / sides) + rotate));
  99.  
  100.                             points[loop+1].x = (box.x1 / 2) +
  101.                                      ((radius / 3) * cos(((loop+1) * 2 * M_PI / sides) + rotate));
  102.                             points[loop+1].y = (box.y1 / 2) +
  103.                                      ((radius / 3) * sin(((loop+1) * 2 * M_PI / sides) + rotate));
  104.                     }
  105.  
  106.                     nsfb_plot_polygon(nsfb, (const int *)points, sides,
  107.                                       0xff000000 | (0xffffff / (sides * 2)));
  108.  
  109.                     nsfb_plot_polylines(nsfb, sides, points, &pen);
  110.  
  111.                     free(points);
  112.                     radius -= 40;
  113.             }
  114.  
  115.             nsfb_update(nsfb, &box);
  116.             sleepMilli(100);
  117.     }
  118.    
  119.     /* wait for quit event or timeout */
  120.     while (waitloop > 0) {
  121.         if (nsfb_event(nsfb, &event, 1000)  == false) {
  122.             break;
  123.         }
  124.         if (event.type == NSFB_EVENT_CONTROL) {
  125.             if (event.value.controlcode == NSFB_CONTROL_TIMEOUT) {
  126.                 /* timeout */
  127.                 waitloop--;
  128.             } else if (event.value.controlcode == NSFB_CONTROL_QUIT) {
  129.                 break;
  130.             }
  131.         }
  132.     }
  133.  
  134.     nsfb_free(nsfb);
  135.  
  136.     return 0;
  137. }
  138.  
  139. /*
  140.  * Local variables:
  141.  *  c-basic-offset: 4
  142.  *  tab-width: 8
  143.  * End:
  144.  */
  145.