home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / toollib_2 / Examples / GFXView / Builds / c / draw_pic next >
Encoding:
Text File  |  1995-11-24  |  2.7 KB  |  101 lines

  1. /*** draw_pic.c ***/
  2. /* Routines for loading and displaying a Draw file
  3.  * (c) Paul Field 1995
  4.  */
  5.  
  6.  
  7. #include "draw_pic.h"
  8.  
  9. #include <assert.h>
  10. #include "drawfile.h"
  11. #include "osfile.h"
  12.  
  13. #include "allocate.h"
  14. #include "exception.h"
  15. #include "shifting.h"
  16.  
  17.  
  18.  
  19. struct draw_pic_str
  20.  { drawfile_diagram *draw;
  21.  };
  22.  
  23.  
  24.  
  25. draw_pic *draw_pic_create(const char *file_name)
  26.  { int      size;
  27.    bits     file_type;
  28.    draw_pic *pic;
  29.  
  30.    osfile_read_stamped_no_path(file_name, NULL, NULL, &size, NULL, &file_type);
  31.    assert(file_type == osfile_TYPE_DRAW);
  32.  
  33.    pic = memory_allocate(sizeof(*pic));
  34.    pic->draw = NULL;   /* It's OK to 'free' a NULL */
  35.  
  36.    try
  37.     { shifting_allocate((shifting_ptr)&pic->draw, size);
  38.       osfile_load_stamped_no_path(file_name, (byte *)pic->draw, NULL, NULL, NULL, NULL);
  39.     }
  40.    catch
  41.     { draw_pic_destroy(pic);
  42.       throw();
  43.     }
  44.    catch_end
  45.  
  46.    return pic;
  47.  }
  48.  
  49.  
  50.  
  51. void draw_pic_destroy(draw_pic *pic)
  52.  { if (pic != NULL)
  53.     { shifting_free((shifting_ptr)&pic->draw);
  54.       memory_free(pic);
  55.     }
  56.  }
  57.  
  58.  
  59.  
  60. void draw_pic_draw(const draw_pic *pic, int pic_origin_x, int pic_origin_y, unsigned scale_percent, const os_box *clip)
  61.  { os_trfm trfm;
  62.    int     fix_scale;
  63.    int     zero_x;
  64.    int     zero_y;
  65.  
  66.    assert(pic != NULL);
  67.  
  68.    /* Calculate fixed-point scale factors */
  69.    fix_scale = (scale_percent << 16)/100;
  70.  
  71.    /* Convert the origin coordinate from OS to draw units */
  72.    pic_origin_x *= draw_OS_UNIT;
  73.    pic_origin_y *= draw_OS_UNIT;
  74.  
  75.  
  76.    /* pic_origin is the screen position at which we want the bottom left of the
  77.     * draw diagram to appear at. To draw the diagram we need to calculate the screen position
  78.     * at which draw coordinate (0,0) should appear.
  79.     * If the bottom left of the diagram is (x,y) then we simply move by (-x,-y) to get to (0,0)
  80.     * so, to calculate the screen position of (0,0) we simply move by (-x,-y) from pic_origin.
  81.     */
  82.    zero_x = pic_origin_x - (pic->draw->bbox.x0 * scale_percent)/100;
  83.    zero_y = pic_origin_y - (pic->draw->bbox.y0 * scale_percent)/100;
  84.  
  85.  
  86.    /* Create transformation matrix to implement the scaling and translation required */
  87.    trfm.entries[0][0] = fix_scale;  trfm.entries[0][1] = 0;
  88.    trfm.entries[1][0] = 0;          trfm.entries[1][1] = fix_scale;
  89.    trfm.entries[2][0] = zero_x;     trfm.entries[2][1] = zero_y;
  90.  
  91.    drawfile_render(0, pic->draw, shifting_size((shifting_ptr)&pic->draw), &trfm, clip, 0);
  92.  }
  93.  
  94.  
  95.  
  96. void draw_pic_size(const draw_pic *pic, unsigned *width, unsigned *height)
  97.  { /* draw_OS_UNIT-1 is added so that the division rounds up */
  98.    *width  = ((pic->draw->bbox.x1 - pic->draw->bbox.x0) + draw_OS_UNIT-1)/draw_OS_UNIT;
  99.    *height = ((pic->draw->bbox.y1 - pic->draw->bbox.y0) + draw_OS_UNIT-1)/draw_OS_UNIT;
  100.  }
  101.