Subversion Repositories Kolibri OS

Rev

Rev 8439 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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