2015-07-22 21:32:54 +03:00
|
|
|
#ifndef INCLUDE_DRAW_BUF_H
|
|
|
|
#define INCLUDE_DRAW_BUF_H
|
2015-08-04 18:48:36 +03:00
|
|
|
#print "[include <draw_buf.h>]\n"
|
2015-07-22 21:32:54 +03:00
|
|
|
|
|
|
|
#ifndef INCLUDE_KOLIBRI_H
|
|
|
|
#include "../lib/kolibri.h"
|
|
|
|
#endif
|
2014-01-19 21:12:45 +04:00
|
|
|
|
2020-05-05 04:16:36 +03:00
|
|
|
dword buf_data=0;
|
2015-03-16 17:20:32 +03:00
|
|
|
|
2014-01-18 15:46:58 +04:00
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
struct CANVAS {
|
2016-12-21 13:56:06 +03:00
|
|
|
dword bufx, bufy, bufw, bufh;
|
2020-03-18 03:45:36 +03:00
|
|
|
dword fill_color;
|
2014-01-18 15:46:58 +04:00
|
|
|
|
2016-11-10 20:48:36 +03:00
|
|
|
bool Init();
|
2014-03-21 04:21:09 +04:00
|
|
|
void Show();
|
2014-01-18 15:46:58 +04:00
|
|
|
void Fill();
|
|
|
|
void DrawBar();
|
2020-03-18 03:45:36 +03:00
|
|
|
void WriteText();
|
2014-03-21 04:21:09 +04:00
|
|
|
void PutPixel();
|
2014-01-18 15:46:58 +04:00
|
|
|
void AlignCenter();
|
|
|
|
void AlignRight();
|
2020-03-18 03:45:36 +03:00
|
|
|
void IncreaseBufSize();
|
2014-01-18 15:46:58 +04:00
|
|
|
};
|
|
|
|
|
2016-12-14 01:08:14 +03:00
|
|
|
char draw_buf_not_enaught_ram[] =
|
2020-12-18 02:01:09 +03:00
|
|
|
"'CANVAS requested %i MB more memory than the system has.
|
2020-12-09 02:32:54 +03:00
|
|
|
Application could be unstable.' -E";
|
2016-12-14 01:08:14 +03:00
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
bool CANVAS::Init(dword i_bufx, i_bufy, i_bufw, i_bufh)
|
2014-01-18 15:46:58 +04:00
|
|
|
{
|
|
|
|
bufx = i_bufx;
|
|
|
|
bufy = i_bufy;
|
2016-12-21 13:56:06 +03:00
|
|
|
bufw = i_bufw;
|
|
|
|
bufh = i_bufh;
|
2020-05-05 04:16:36 +03:00
|
|
|
if (buf_data) buf_data = free(buf_data);
|
2020-03-18 03:45:36 +03:00
|
|
|
IncreaseBufSize();
|
2016-11-10 20:48:36 +03:00
|
|
|
if (!buf_data) return false;
|
2014-01-18 15:46:58 +04:00
|
|
|
ESDWORD[buf_data] = bufw;
|
2014-03-21 04:21:09 +04:00
|
|
|
ESDWORD[buf_data+4] = bufh;
|
2016-11-10 20:48:36 +03:00
|
|
|
return true;
|
2014-01-18 15:46:58 +04:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::Fill(dword start_pointer, i_fill_color)
|
2014-01-18 15:46:58 +04:00
|
|
|
{
|
2020-12-13 23:52:56 +03:00
|
|
|
dword max_i = bufw * bufh * 4 - start_pointer/4;
|
2020-04-05 10:33:55 +03:00
|
|
|
fill_color = i_fill_color;
|
2020-12-13 23:52:56 +03:00
|
|
|
@MEMSETD(buf_data+start_pointer+8, max_i, fill_color);
|
2014-01-18 15:46:58 +04:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::DrawBar(dword x, y, w, h, color)
|
2014-01-18 15:46:58 +04:00
|
|
|
{
|
2016-12-21 13:56:06 +03:00
|
|
|
dword i, j;
|
2020-03-18 03:45:36 +03:00
|
|
|
if (y + h >= bufh) IncreaseBufSize();
|
|
|
|
for (j=0; j<h; j++) {
|
2016-12-21 13:56:06 +03:00
|
|
|
for (i = y+j*bufw+x<<2+8+buf_data; i<y+j*bufw+x+w<<2+8+buf_data; i+=4) {
|
|
|
|
ESDWORD[i] = color;
|
|
|
|
}
|
2014-01-18 15:46:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::WriteText(dword x, y, byte fontType, dword color, str_offset, strlen)
|
2020-03-18 03:45:36 +03:00
|
|
|
{
|
2020-03-20 04:28:59 +03:00
|
|
|
#define BUGFIX_32000 32000
|
2020-04-05 10:33:55 +03:00
|
|
|
dword ydiv=0;
|
2020-03-20 04:28:59 +03:00
|
|
|
dword reserve_data_1, reserve_data_2;
|
|
|
|
dword new_buf_offset;
|
2020-03-18 03:45:36 +03:00
|
|
|
if (y + 30 >= bufh) IncreaseBufSize();
|
2020-03-20 04:28:59 +03:00
|
|
|
if (y < BUGFIX_32000) {
|
2020-05-07 18:43:50 +03:00
|
|
|
ESI = strlen;
|
2020-03-20 04:28:59 +03:00
|
|
|
WriteBufText(x, y, fontType, color, str_offset, buf_data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ydiv = y / BUGFIX_32000 * BUGFIX_32000;
|
|
|
|
y -= ydiv;
|
|
|
|
new_buf_offset = ydiv * bufw * 4 + buf_data;
|
|
|
|
|
|
|
|
reserve_data_1 = ESDWORD[new_buf_offset];
|
|
|
|
reserve_data_2 = ESDWORD[new_buf_offset+4];
|
|
|
|
|
|
|
|
ESDWORD[new_buf_offset] = bufw;
|
|
|
|
ESDWORD[new_buf_offset+4] = bufh - y;
|
2020-05-07 18:43:50 +03:00
|
|
|
ESI = strlen;
|
2020-03-20 04:28:59 +03:00
|
|
|
WriteBufText(x, y, fontType, color, str_offset, new_buf_offset);
|
|
|
|
|
|
|
|
ESDWORD[new_buf_offset] = reserve_data_1;
|
|
|
|
ESDWORD[new_buf_offset+4] = reserve_data_2;
|
|
|
|
}
|
2020-03-18 03:45:36 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::PutPixel(dword x, y, color)
|
2014-03-21 04:21:09 +04:00
|
|
|
{
|
2016-12-21 13:56:06 +03:00
|
|
|
dword pos = y*bufw+x*4+8+buf_data;
|
2014-03-21 04:21:09 +04:00
|
|
|
ESDWORD[pos] = color;
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::AlignRight(dword x,y,w,h, content_width)
|
2014-01-18 15:46:58 +04:00
|
|
|
{
|
2016-12-21 13:56:06 +03:00
|
|
|
dword i, j, l;
|
|
|
|
dword content_left = w - content_width / 2;
|
2014-01-18 15:46:58 +04:00
|
|
|
for (j=0; j<h; j++)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ESDWORD[buf_data+8+i] >< ESDWORD[buf_data+8+l];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::AlignCenter(dword x,y,w,h, content_width)
|
2014-01-18 15:46:58 +04:00
|
|
|
{
|
2016-12-21 13:56:06 +03:00
|
|
|
dword i, j, l;
|
|
|
|
dword content_left = w - content_width / 2;
|
2014-01-18 15:46:58 +04:00
|
|
|
for (j=0; j<h; j++)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ESDWORD[buf_data+8+i] >< ESDWORD[buf_data+8+l];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::Show(dword _y_offset, _h)
|
2015-03-16 17:20:32 +03:00
|
|
|
{
|
2020-12-13 23:52:56 +03:00
|
|
|
PutPaletteImage(_y_offset * bufw * 4 + buf_data+8, bufw, _h, bufx, bufy, 32, 0);
|
2015-03-16 17:20:32 +03:00
|
|
|
}
|
2015-07-22 21:32:54 +03:00
|
|
|
|
2020-12-18 02:01:09 +03:00
|
|
|
void CANVAS::IncreaseBufSize()
|
2020-03-18 03:45:36 +03:00
|
|
|
{
|
|
|
|
static dword bufh_initial;
|
|
|
|
dword alloc_size;
|
|
|
|
dword free_ram_size;
|
|
|
|
char error_str[256];
|
|
|
|
|
|
|
|
if (!buf_data) {
|
2020-12-16 19:30:39 +03:00
|
|
|
alloc_size = bufh * bufw * 4 + 8;
|
2020-03-18 03:45:36 +03:00
|
|
|
buf_data = malloc(alloc_size);
|
2020-12-16 19:30:39 +03:00
|
|
|
} else {
|
|
|
|
if (bufh_initial != bufh) bufh_initial = bufh;
|
|
|
|
bufh += 4096*1600/bufw; //+50 Mb
|
|
|
|
alloc_size = bufh * bufw * 4 + 8;
|
2020-03-18 03:45:36 +03:00
|
|
|
buf_data = realloc(buf_data, alloc_size);
|
2020-12-16 19:30:39 +03:00
|
|
|
Fill(bufh_initial * bufw * 4 + 8, fill_color);
|
2020-03-18 03:45:36 +03:00
|
|
|
}
|
2020-12-16 19:30:39 +03:00
|
|
|
bufh_initial = bufh;
|
2020-03-18 03:45:36 +03:00
|
|
|
free_ram_size = GetFreeRAM() * 1024;
|
2020-12-09 02:32:54 +03:00
|
|
|
if (alloc_size > free_ram_size) {
|
|
|
|
sprintf(#error_str, #draw_buf_not_enaught_ram, alloc_size - free_ram_size/1048576);
|
2020-03-18 03:45:36 +03:00
|
|
|
notify(#error_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-22 21:32:54 +03:00
|
|
|
#endif
|