Subversion Repositories Kolibri OS

Rev

Rev 8439 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5598 pavelyakov 1
#ifndef INCLUDE_DRAW_BUF_H
2
#define INCLUDE_DRAW_BUF_H
5676 pavelyakov 3
#print "[include ]\n"
4486 leency 4
 
5598 pavelyakov 5
#ifndef INCLUDE_KOLIBRI_H
6
#include "../lib/kolibri.h"
7
#endif
8
 
7866 leency 9
dword buf_data=0;
4475 leency 10
 
5519 leency 11
 
8439 leency 12
struct CANVAS {
6803 leency 13
	dword bufx, bufy, bufw, bufh;
7738 leency 14
	dword fill_color;
4475 leency 15
 
6698 leency 16
	bool Init();
4666 leency 17
	void Show();
4475 leency 18
	void Fill();
19
	void DrawBar();
7738 leency 20
	void WriteText();
4666 leency 21
	void PutPixel();
4475 leency 22
	void AlignCenter();
23
	void AlignRight();
7738 leency 24
	void IncreaseBufSize();
4475 leency 25
};
26
 
6795 leency 27
char draw_buf_not_enaught_ram[] =
8439 leency 28
"'CANVAS requested %i MB more memory than the system has.
8350 leency 29
Application could be unstable.' -E";
6795 leency 30
 
8439 leency 31
bool CANVAS::Init(dword i_bufx, i_bufy, i_bufw, i_bufh)
4475 leency 32
{
33
	bufx = i_bufx;
34
	bufy = i_bufy;
6803 leency 35
	bufw = i_bufw;
36
	bufh = i_bufh;
7866 leency 37
	if (buf_data) buf_data = free(buf_data);
7738 leency 38
	IncreaseBufSize();
6698 leency 39
	if (!buf_data) return false;
4475 leency 40
	ESDWORD[buf_data] = bufw;
4666 leency 41
	ESDWORD[buf_data+4] = bufh;
6698 leency 42
	return true;
4666 leency 43
}
44
 
8439 leency 45
void CANVAS::Fill(dword start_pointer, i_fill_color)
4475 leency 46
{
8396 leency 47
	dword max_i = bufw * bufh * 4 - start_pointer/4;
8451 leency 48
	fill_color = i_fill_color | 0xFF000000; //set background color non-transparent
8396 leency 49
	@MEMSETD(buf_data+start_pointer+8, max_i, fill_color);
4475 leency 50
}
51
 
8439 leency 52
void CANVAS::DrawBar(dword x, y, w, h, color)
4475 leency 53
{
6803 leency 54
	dword i, j;
7738 leency 55
	if (y + h >= bufh) IncreaseBufSize();
56
	for (j=0; j
6803 leency 57
		for (i = y+j*bufw+x<<2+8+buf_data; i
58
			ESDWORD[i] = color;
59
		}
4475 leency 60
	}
61
}
62
 
8439 leency 63
void CANVAS::WriteText(dword x, y, byte fontType, dword color, str_offset, strlen)
7738 leency 64
{
7739 leency 65
	#define BUGFIX_32000 32000
7764 leency 66
	dword ydiv=0;
7739 leency 67
	dword reserve_data_1, reserve_data_2;
68
	dword new_buf_offset;
7738 leency 69
	if (y + 30 >= bufh) IncreaseBufSize();
7739 leency 70
	if (y < BUGFIX_32000) {
7889 leency 71
		ESI = strlen;
7739 leency 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;
7889 leency 84
		ESI = strlen;
7739 leency 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
	}
7738 leency 90
}
91
 
8439 leency 92
void CANVAS::PutPixel(dword x, y, color)
4666 leency 93
{
6803 leency 94
	dword pos = y*bufw+x*4+8+buf_data;
4666 leency 95
	ESDWORD[pos] = color;
96
}
97
 
8439 leency 98
void CANVAS::AlignRight(dword x,y,w,h, content_width)
4475 leency 99
{
6803 leency 100
	dword i, j, l;
101
	dword content_left = w - content_width / 2;
4475 leency 102
	for (j=0; 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
 
8439 leency 111
void CANVAS::AlignCenter(dword x,y,w,h, content_width)
4475 leency 112
{
6803 leency 113
	dword i, j, l;
114
	dword content_left = w - content_width / 2;
4475 leency 115
	for (j=0; 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
 
8439 leency 124
void CANVAS::Show(dword _y_offset, _h)
5519 leency 125
{
8396 leency 126
	PutPaletteImage(_y_offset * bufw * 4 + buf_data+8, bufw, _h, bufx, bufy, 32, 0);
5519 leency 127
}
128
 
8439 leency 129
void CANVAS::IncreaseBufSize()
7738 leency 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) {
8425 leency 137
		alloc_size = bufh * bufw * 4 + 8;
7738 leency 138
		buf_data = malloc(alloc_size);
8425 leency 139
	} else {
140
		if (bufh_initial != bufh) bufh_initial = bufh;
141
		bufh += 4096*1600/bufw; //+50 Mb
142
		alloc_size = bufh * bufw * 4 + 8;
7738 leency 143
		buf_data = realloc(buf_data, alloc_size);
8425 leency 144
		Fill(bufh_initial * bufw * 4 + 8, fill_color);
7738 leency 145
	}
8425 leency 146
	bufh_initial = bufh;
7738 leency 147
	free_ram_size = GetFreeRAM() * 1024;
8350 leency 148
	if (alloc_size > free_ram_size) {
149
		sprintf(#error_str, #draw_buf_not_enaught_ram, alloc_size - free_ram_size/1048576);
7738 leency 150
		notify(#error_str);
151
	}
152
}
153
 
154
 
5598 pavelyakov 155
#endif