Subversion Repositories Kolibri OS

Rev

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

  1. #ifndef INCLUDE_DRAW_BUF_H
  2. #define INCLUDE_DRAW_BUF_H
  3. #print "[include <draw_buf.h>]\n"
  4.  
  5. #ifndef INCLUDE_KOLIBRI_H
  6. #include "../lib/kolibri.h"
  7. #endif
  8.  
  9. dword buf_data;
  10.  
  11.  
  12. struct DrawBufer {
  13.         dword bufx, bufy, bufw, bufh;
  14.         dword fill_color;
  15.  
  16.         bool Init();
  17.         void Show();
  18.         void Fill();
  19.         void DrawBar();
  20.         void WriteText();
  21.         void PutPixel();
  22.         void AlignCenter();
  23.         void AlignRight();
  24.         void IncreaseBufSize();
  25. };
  26.  
  27. char draw_buf_not_enaught_ram[] =
  28. "'DrawBufer needs more memory than currenly available.
  29. Application could be unstable.
  30.  
  31. Requested size: %i Mb
  32. Free RAM: %i Mb' -E";
  33.  
  34. bool DrawBufer::Init(dword i_bufx, i_bufy, i_bufw, i_bufh)
  35. {
  36.         bufx = i_bufx;
  37.         bufy = i_bufy;
  38.         bufw = i_bufw;
  39.         bufh = i_bufh;
  40.         buf_data = free(buf_data);
  41.         IncreaseBufSize();
  42.         //debugval("buf_data",buf_data);
  43.         if (!buf_data) return false;
  44.         ESDWORD[buf_data] = bufw;
  45.         ESDWORD[buf_data+4] = bufh;
  46.         return true;
  47. }
  48.  
  49. void DrawBufer::Fill(dword start_pointer, i_fill_color)
  50. {
  51.         dword i;
  52.         dword max_i = bufw * bufh * 4 + buf_data + 8;
  53.         fill_color = i_fill_color;
  54.         for (i=buf_data+start_pointer+8; i<max_i; i+=4) ESDWORD[i] = fill_color;
  55. }
  56.  
  57. void DrawBufer::DrawBar(dword x, y, w, h, color)
  58. {
  59.         dword i, j;
  60.         if (y + h >= bufh) IncreaseBufSize();
  61.         for (j=0; j<h; j++)     {
  62.                 for (i = y+j*bufw+x<<2+8+buf_data; i<y+j*bufw+x+w<<2+8+buf_data; i+=4) {
  63.                         ESDWORD[i] = color;
  64.                 }
  65.         }
  66. }
  67.  
  68. void DrawBuf_WriteText(dword x, y, byte fontType, dword color, str_offset)
  69. {
  70.         EDI = buf_data;
  71.         WriteText(x, y, fontType, color, str_offset);
  72. }
  73. void DrawBufer::WriteText(dword x, y, byte fontType, dword color, str_offset)
  74. {
  75.         if (y + 30 >= bufh) IncreaseBufSize();
  76.         DrawBuf_WriteText(x, y, fontType, color, str_offset);
  77. }
  78.  
  79. void DrawBufer::PutPixel(dword x, y, color)
  80. {
  81.         dword pos = y*bufw+x*4+8+buf_data;
  82.         ESDWORD[pos] = color;
  83. }
  84.  
  85. void DrawBufer::AlignRight(dword x,y,w,h, content_width)
  86. {
  87.         dword i, j, l;
  88.         dword content_left = w - content_width / 2;
  89.         for (j=0; j<h; j++)
  90.         {
  91.                 for (i=j*w+w-x*4, l=j*w+content_width+x*4; (i>=j*w+content_left*4) && (l>=j*w*4); i-=4, l-=4)
  92.                 {
  93.                         ESDWORD[buf_data+8+i] >< ESDWORD[buf_data+8+l];
  94.                 }
  95.         }
  96. }
  97.  
  98. void DrawBufer::AlignCenter(dword x,y,w,h, content_width)
  99. {
  100.         dword i, j, l;
  101.         dword content_left = w - content_width / 2;
  102.         for (j=0; j<h; j++)
  103.         {
  104.                 for (i=j*w+content_width+content_left*4, l=j*w+content_width+x*4; (i>=j*w+content_left*4) && (l>=j*w*4); i-=4, l-=4)
  105.                 {
  106.                         ESDWORD[buf_data+8+i] >< ESDWORD[buf_data+8+l];
  107.                 }
  108.         }
  109. }
  110.  
  111. /*
  112. void DrawBufer::Zoom2x(int zoom)
  113. {
  114.         int i, s;
  115.         dword point_x, max_i, zline_w, s_inc;
  116.  
  117.         point_x = 0;
  118.         max_i = bufw * bufh * 4 + buf_data+8;
  119.         s_inc = zoom * 4;
  120.         zline_w = zbufw * 4;
  121.  
  122.         for (i=buf_data+8, s=zbuf_data+8; i<max_i; i+=4, s+= s_inc) {
  123.                 ESDWORD[s] = ESDWORD[i];
  124.                 ESDWORD[s+4] = ESDWORD[i];
  125.                 ESDWORD[s+zline_w] = ESDWORD[i];
  126.                 ESDWORD[s+zline_w+4] = ESDWORD[i];
  127.                 if (zoom==3)
  128.                 {
  129.                         ESDWORD[s+8] = ESDWORD[i];
  130.                         ESDWORD[zline_w+s+8] = ESDWORD[i];
  131.                         ESDWORD[zline_w*2+s] = ESDWORD[i];
  132.                         ESDWORD[zline_w*2+s+4] = ESDWORD[i];
  133.                         ESDWORD[zline_w*2+s+8] = ESDWORD[i];
  134.                 }
  135.  
  136.                 point_x++;
  137.                 if (point_x >= bufw)
  138.                 {
  139.                         s += zoom - 1 * zline_w;
  140.                         point_x = 0;
  141.                 }
  142.         }
  143. }
  144. */
  145.  
  146.  
  147. void DrawBufer::Show()
  148. {
  149.         PutPaletteImage(buf_data+8, bufw, bufh, bufx, bufy, 32, 0);    
  150. }
  151.  
  152. void DrawBufer::IncreaseBufSize()
  153. {
  154.         static dword alloc_counter;
  155.         static dword bufh_initial;
  156.         dword alloc_size;
  157.         dword free_ram_size;
  158.         char error_str[256];
  159.  
  160.         if (!buf_data) {
  161.                 alloc_counter = 1;
  162.                 bufh_initial = bufh;
  163.                 alloc_size = bufw * bufh * 4 + 8;
  164.                 buf_data = malloc(alloc_size);
  165.         }
  166.         else {
  167.                 alloc_counter++;
  168.                 bufh = bufh_initial * alloc_counter;
  169.                 alloc_size = bufw * bufh * 4 + 8;
  170.                 buf_data = realloc(buf_data, alloc_size);
  171.                 Fill(alloc_counter - 1 * bufw * bufh_initial * 4 + 8, fill_color);
  172.         }
  173.  
  174.         free_ram_size = GetFreeRAM() * 1024;
  175.         if (alloc_size >= free_ram_size) {
  176.                 sprintf(#error_str, #draw_buf_not_enaught_ram, alloc_size/1048576, free_ram_size/1048576);
  177.                 notify(#error_str);
  178.         }
  179. }
  180.  
  181.  
  182.  
  183. #endif