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.  
  28. int main(int argc, char **argv)
  29. {
  30.     const char *fename;
  31.     enum nsfb_type_e fetype;
  32.     nsfb_t *nsfb;
  33.  
  34.     int waitloop = 3;
  35.  
  36.     nsfb_event_t event;
  37.     nsfb_bbox_t box;
  38.     uint8_t *fbptr;
  39.     int fbstride;
  40.  
  41.     int sides;
  42.     int radius;
  43.     nsfb_point_t *points;
  44.     int loop;
  45.     int counter;
  46.     int colour;
  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.     nsfb_get_buffer(nsfb, &fbptr, &fbstride);
  76.  
  77.     radius = (box.x1 / 3);
  78.     sides = 5;
  79.     counter = 0;
  80.  
  81.     for (counter = 0; counter < 20; counter++) {
  82.         /* claim the whole screen for update */
  83.         nsfb_claim(nsfb, &box);
  84.  
  85.         nsfb_plot_clg(nsfb, 0xffffffff);
  86.  
  87.         points = malloc(sizeof(nsfb_point_t) * sides);
  88.  
  89.         for (loop = 0; loop < sides;loop++) {
  90.             points[(2 * loop) % sides].x = (box.x1 / 2) +
  91.                     (radius * cos(loop * 2 * M_PI / sides));
  92.             points[(2 * loop) % sides].y = (box.y1 / 2) +
  93.                     (radius * sin(loop * 2 * M_PI / sides));
  94.         }
  95.  
  96.         if (counter % 3 == 0)
  97.             colour = 0xffff0000;
  98.         else if (counter % 3 == 1)
  99.             colour = 0xff00ff00;
  100.         else
  101.             colour = 0xff0000ff;
  102.  
  103.         nsfb_plot_polygon(nsfb, (const int *)points, sides, colour);
  104.         free(points);
  105.  
  106.         sides += 2;
  107.  
  108.         nsfb_update(nsfb, &box);
  109.         sleepMilli(400);
  110.     }
  111.  
  112.     /* wait for quit event or timeout */
  113.     while (waitloop > 0) {
  114.         if (nsfb_event(nsfb, &event, 1000)  == false) {
  115.             break;
  116.         }
  117.         if (event.type == NSFB_EVENT_CONTROL) {
  118.             if (event.value.controlcode == NSFB_CONTROL_TIMEOUT) {
  119.                 /* timeout */
  120.                 waitloop--;
  121.             } else if (event.value.controlcode == NSFB_CONTROL_QUIT) {
  122.                 break;
  123.             }
  124.         }
  125.     }
  126.  
  127.     nsfb_free(nsfb);
  128.  
  129.     return 0;
  130. }
  131.  
  132. /*
  133.  * Local variables:
  134.  *  c-basic-offset: 4
  135.  *  tab-width: 8
  136.  * End:
  137.  */
  138.