Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. #include "gb.h"
  3.  
  4. //=========================
  5.  
  6. void gb_pixel_set(GB_BMP *b, int x, int y, unsigned c)
  7. {
  8. // ïîñòàâèòü òî÷êó
  9.  
  10. unsigned s;
  11.  
  12. if ((x+1 > b->w )||(y+1 > b->h))
  13.         return;
  14.  
  15. if ((x < 0)||(y < 0))
  16.         return;
  17.  
  18. s = 3*( y*(b->w) + x );
  19.  
  20. *( b -> bmp + s ) = c & 0xff;
  21. *( b -> bmp + s + 1) = (c >> 8) & 0xff;
  22. *( b -> bmp + s + 2) = (c >> 16)& 0xff;
  23.  
  24. }
  25.  
  26. //=========================
  27.  
  28. int gb_pixel_get(GB_BMP *b, int x, int y, unsigned *c)
  29. {
  30. // ïîëó÷èòü òî÷êó
  31.  
  32. unsigned red, green, blue, s;
  33.  
  34. if ((x < 0) || (y < 0))
  35.         return 0;
  36.  
  37. if ((x+1 > b->w )||(y+1 > b->h))
  38.         return 0;
  39.  
  40. s = 3*( y*(b->w) + x );
  41.  
  42. blue  = *( b -> bmp + s );
  43. green = *( b -> bmp + s + 1);
  44. red   = *( b -> bmp + s + 2);
  45.  
  46. *c = ((red << 16) & 0xff0000) | ((green << 8) & 0xff00) | (blue & 0xff);
  47.  
  48. return 1;
  49. }
  50.  
  51. //=========================
  52.  
  53. void gb_line(GB_BMP *b, int x1, int y1, int x2, int y2, unsigned c)
  54. {
  55. // ëèíèÿ çàäàííîãî öâåòà ñ èñïîëüçîâàíèåì
  56. // àëãîpèòìà Áðåçåíõýìà
  57.  
  58. int t, dist;
  59. int xerr=0, yerr=0, delta_x, delta_y;
  60. int incx, incy;
  61.  
  62. // âû÷èñëåíèå pàññòîÿíèÿ â îáîèõ íàïpàâëåíèÿõ
  63. delta_x = x2 - x1;
  64. delta_y = y2 - y1;
  65.  
  66. // îïpåäåëåíèå íàïpàâëåíèÿ øàãà,
  67. // øàã âû÷èñëÿåòñÿ ëèáî ïî âåpòèêàëüíîé, ëèáî ãîpèçîíòàëüíîé
  68. // ëèíèè
  69. if (delta_x > 0)
  70.         incx = 1;
  71. else  
  72.         if (0 == delta_x)
  73.                 incx = 0;
  74.         else
  75.                 incx = -1;
  76.  
  77. if (delta_y > 0)
  78.         incy = 1;
  79. else
  80.         if ( 0 == delta_y)
  81.                 incy = 0;
  82.         else
  83.                 incy = -1;
  84.  
  85. // îïpåäåëåíèå íàèáîëüøåãî pàññòîÿíèÿ
  86. if (delta_x < 0)
  87.         delta_x *= -1;
  88. if (delta_y < 0)
  89.         delta_y *= -1;
  90.  
  91. if (delta_x > delta_y)
  92.         dist = delta_x;
  93. else
  94.         dist = delta_y;
  95.  
  96.  
  97. // âû÷åp÷èâàíèå ëèíèè
  98. for (t = 0; t <= dist+1; t++)
  99.         {
  100.         gb_pixel_set(b, x1, y1, c);
  101.         xerr+=delta_x;
  102.         yerr+=delta_y;
  103.  
  104.         if (xerr > dist)
  105.                 {
  106.                 xerr -= dist;
  107.                 x1 += incx;
  108.                 }
  109.  
  110.         if (yerr > dist)
  111.                 {
  112.                 yerr -= dist;
  113.                 y1 += incy;
  114.                 }
  115.  
  116.         }
  117. }
  118.  
  119. //=========================
  120.  
  121. void gb_rect(GB_BMP *b, int x, int y, int w, int h, unsigned c)
  122. {
  123. // ïðÿìîóãîëüíèê
  124.  
  125. gb_line (b, x,     y,     x+w-1, y,     c);
  126. gb_line (b, x,     y+h-1, x+w-1, y+h-1, c);
  127. gb_line (b, x,     y,     x,     y+h-1, c);
  128. gb_line (b, x+w-1, y,     x+w-1, y+h-1, c);
  129. }
  130.  
  131. //=========================
  132.  
  133. void gb_circle(GB_BMP *b, int x, int y, int r, unsigned c)
  134. {
  135.  
  136. int _x;
  137. int _y;
  138. int d;
  139.  
  140. _x = 0;
  141. _y = r;
  142. d = 3-2*r;
  143. while( _y >= _x)
  144.         {
  145.         gb_pixel_set(b,  _x + x,  _y + y, c);
  146.         gb_pixel_set(b,  _x + x, -_y + y, c);
  147.         gb_pixel_set(b, -_x + x,  _y + y, c);
  148.         gb_pixel_set(b, -_x + x, -_y + y, c);
  149.         gb_pixel_set(b,  _y + x,  _x + y, c);
  150.         gb_pixel_set(b,  _y + x, -_x + y, c);
  151.         gb_pixel_set(b, -_y + x,  _x + y, c);
  152.         gb_pixel_set(b, -_y + x, -_x + y, c);
  153.         if( d<0 )
  154.                 d = d+4*_x+6;
  155.         else
  156.                 {
  157.                 d = d+4*(_x-_y)+10;
  158.                 _y--;
  159.                 }
  160.         _x++;
  161.         }
  162.  
  163. }
  164.  
  165. //=========================
  166.  
  167. void gb_bar(GB_BMP *b, int x, int y, int w, int h, unsigned c)
  168. {
  169. // çàêðàøåííûé ïðÿìîóãîëüíèê
  170.  
  171. unsigned s;
  172. int i, j;
  173.  
  174. if ((x > b->w)||(y > b->h))
  175.         return;
  176.  
  177. for (j = 0; j < w; j++)
  178. for (i = 0; i < h; i++)
  179. //      gb_pixel_set(b, x+j, y+i, c);
  180.         {
  181.         s = 3*( (y+i)*(b->w) + x + j );
  182.  
  183.         *( b -> bmp + s ) = c & 0xff;
  184.         *( b -> bmp + s + 1) = (c >> 8) & 0xff;
  185.         *( b -> bmp + s + 2) = (c >> 16)& 0xff;
  186.  
  187.         }
  188. }
  189.  
  190. //=========================
  191.  
  192. void gb_image_set(GB_BMP *b_dest, int x_d, int y_d, GB_BMP *b_src, int x_s, int y_s, int w, int h)
  193. {
  194. // âûâîä èçîáðàæåíèÿ
  195.  
  196. int x, y;
  197. unsigned d;
  198.  
  199. if ((x_d > b_dest->w)||(y_d > b_dest->h))
  200.         return;
  201.  
  202. if ((x_s > b_src->w)||(y_s > b_src->h))
  203.         return;
  204.  
  205. for (y = 0; y < h; y++)
  206.         for (x = 0; x < w; x++)
  207.                 if ( gb_pixel_get(b_src, x_s+x, y_s+y, &d) )
  208.                         gb_pixel_set(b_dest, x_d+x, y_d+y, d);
  209.  
  210.  
  211. }
  212.  
  213. //=========================
  214.  
  215. void gb_image_set_t(GB_BMP *b_dest, int x_d, int y_d, GB_BMP *b_src, int x_s, int y_s, int w, int h, unsigned c)
  216. {
  217. // âûâîä èçîáðàæåíèÿ ñ ïðîçðà÷íûì öâåòîì
  218.  
  219. int x, y;
  220. unsigned d;
  221.  
  222. if ((x_d > b_dest->w)||(y_d > b_dest->h))
  223.         return;
  224.  
  225. if ((x_s > b_src->w)||(y_s > b_src->h))
  226.         return;
  227.  
  228. for (y = 0; y < h; y++)
  229.         for (x = 0; x < w; x++)
  230.                 if ( gb_pixel_get(b_src, x_s+x, y_s+y, &d) )
  231.                         if (c != d)
  232.                                 gb_pixel_set(b_dest, x_d+x, y_d+y, d);
  233.  
  234.  
  235. }
  236.  
  237. //=========================
  238.  
  239. #define NULL ((void*)0)
  240.  
  241. typedef struct
  242. {
  243. void    *name;
  244. void    *function;
  245. } export_t;
  246.  
  247. //=========================
  248.  
  249. char szGb_pixel_set[]   =  "gb_pixel_set";
  250. char szGb_pixel_get[]   =  "gb_pixel_get";
  251. char szGb_line[]        =  "gb_line";
  252. char szGb_rect[]        =  "gb_rect";
  253. char szGb_bar[]         =  "gb_bar";
  254. char szGb_circle[]      =  "gb_circle";
  255. char szGb_image_set[]   =  "gb_image_set";
  256. char szGb_image_set_t[] =  "gb_image_set_t";
  257.  
  258. export_t EXPORTS[] =
  259. {
  260. { szGb_pixel_set,   (void*) gb_pixel_set},
  261. { szGb_pixel_get,   (void*) gb_pixel_get},
  262. { szGb_line,        (void*) gb_line},
  263. { szGb_rect,        (void*) gb_rect},
  264. { szGb_bar,         (void*) gb_bar},
  265. { szGb_circle,      (void*) gb_circle},
  266. { szGb_image_set,   (void*) gb_image_set},
  267. { szGb_image_set_t, (void*) gb_image_set_t},
  268. { NULL, NULL },
  269. };
  270.  
  271.