Subversion Repositories Kolibri OS

Rev

Rev 8425 | 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=0;
  10.  
  11.  
  12. struct CANVAS {
  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. "'CANVAS requested %i MB more memory than the system has.
  29. Application could be unstable.' -E";
  30.  
  31. bool CANVAS::Init(dword i_bufx, i_bufy, i_bufw, i_bufh)
  32. {
  33.         bufx = i_bufx;
  34.         bufy = i_bufy;
  35.         bufw = i_bufw;
  36.         bufh = i_bufh;
  37.         if (buf_data) buf_data = free(buf_data);
  38.         IncreaseBufSize();
  39.         if (!buf_data) return false;
  40.         ESDWORD[buf_data] = bufw;
  41.         ESDWORD[buf_data+4] = bufh;
  42.         return true;
  43. }
  44.  
  45. void CANVAS::Fill(dword start_pointer, i_fill_color)
  46. {
  47.         dword max_i = bufw * bufh * 4 - start_pointer/4;
  48.         fill_color = i_fill_color;
  49.         @MEMSETD(buf_data+start_pointer+8, max_i, fill_color);
  50. }
  51.  
  52. void CANVAS::DrawBar(dword x, y, w, h, color)
  53. {
  54.         dword i, j;
  55.         if (y + h >= bufh) IncreaseBufSize();
  56.         for (j=0; j<h; j++)     {
  57.                 for (i = y+j*bufw+x<<2+8+buf_data; i<y+j*bufw+x+w<<2+8+buf_data; i+=4) {
  58.                         ESDWORD[i] = color;
  59.                 }
  60.         }
  61. }
  62.  
  63. void CANVAS::WriteText(dword x, y, byte fontType, dword color, str_offset, strlen)
  64. {
  65.         #define BUGFIX_32000 32000
  66.         dword ydiv=0;
  67.         dword reserve_data_1, reserve_data_2;
  68.         dword new_buf_offset;
  69.         if (y + 30 >= bufh) IncreaseBufSize();
  70.         if (y < BUGFIX_32000) {
  71.                 ESI = strlen;
  72.                 WriteBufText(x, y, fontType, color, str_offset, buf_data);
  73.         }
  74.         else {
  75.                 ydiv = y / BUGFIX_32000 * BUGFIX_32000;
  76.                 y -= ydiv;
  77.                 new_buf_offset = ydiv * bufw * 4 + buf_data;
  78.  
  79.                 reserve_data_1 = ESDWORD[new_buf_offset];
  80.                 reserve_data_2 = ESDWORD[new_buf_offset+4];
  81.  
  82.                 ESDWORD[new_buf_offset] = bufw;
  83.                 ESDWORD[new_buf_offset+4] = bufh - y;
  84.                 ESI = strlen;
  85.                 WriteBufText(x, y, fontType, color, str_offset, new_buf_offset);
  86.  
  87.                 ESDWORD[new_buf_offset] = reserve_data_1;
  88.                 ESDWORD[new_buf_offset+4] = reserve_data_2;
  89.         }
  90. }
  91.  
  92. void CANVAS::PutPixel(dword x, y, color)
  93. {
  94.         dword pos = y*bufw+x*4+8+buf_data;
  95.         ESDWORD[pos] = color;
  96. }
  97.  
  98. void CANVAS::AlignRight(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+w-x*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. void CANVAS::AlignCenter(dword x,y,w,h, content_width)
  112. {
  113.         dword i, j, l;
  114.         dword content_left = w - content_width / 2;
  115.         for (j=0; j<h; j++)
  116.         {
  117.                 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)
  118.                 {
  119.                         ESDWORD[buf_data+8+i] >< ESDWORD[buf_data+8+l];
  120.                 }
  121.         }
  122. }
  123.  
  124. void CANVAS::Show(dword _y_offset, _h)
  125. {
  126.         PutPaletteImage(_y_offset * bufw * 4 + buf_data+8, bufw, _h, bufx, bufy, 32, 0);
  127. }
  128.  
  129. void CANVAS::IncreaseBufSize()
  130. {
  131.         static dword bufh_initial;
  132.         dword alloc_size;
  133.         dword free_ram_size;
  134.         char error_str[256];
  135.  
  136.         if (!buf_data) {
  137.                 alloc_size = bufh * bufw * 4 + 8;
  138.                 buf_data = malloc(alloc_size);
  139.         } else {
  140.                 if (bufh_initial != bufh) bufh_initial = bufh;
  141.                 bufh += 4096*1600/bufw; //+50 Mb
  142.                 alloc_size = bufh * bufw * 4 + 8;
  143.                 buf_data = realloc(buf_data, alloc_size);
  144.                 Fill(bufh_initial * bufw * 4 + 8, fill_color);
  145.         }
  146.         bufh_initial = bufh;
  147.         free_ram_size = GetFreeRAM() * 1024;
  148.         if (alloc_size > free_ram_size) {
  149.                 sprintf(#error_str, #draw_buf_not_enaught_ram, alloc_size - free_ram_size/1048576);
  150.                 notify(#error_str);
  151.         }
  152. }
  153.  
  154.  
  155. #endif