-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvga.cx
More file actions
executable file
·271 lines (271 loc) · 8.77 KB
/
vga.cx
File metadata and controls
executable file
·271 lines (271 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//-*-C-*-
/*******************************************************************************
* ___ public
* ¦OUX¦ C+
* ¦/C+¦ OUX/C+ OS
* --- kernel
* VGA driver
* ©overcq on ‟Gentoo Linux 17.1” “x86_64” 2021‒5‒16 K
*******************************************************************************/
enum E_vga_Z_aa_pixel
{ E_vga_Z_aa_pixel_S_e = 1 << 0,
E_vga_Z_aa_pixel_S_se = 1 << 1,
E_vga_Z_aa_pixel_S_s = 1 << 2,
E_vga_Z_aa_pixel_S_sw = 1 << 3,
E_vga_Z_aa_pixel_S_w = 1 << 4,
E_vga_Z_aa_pixel_S_nw = 1 << 5,
E_vga_Z_aa_pixel_S_n = 1 << 6,
E_vga_Z_aa_pixel_S_ne = 1 << 7
};
//==============================================================================
_private N32 *E_vga_S_framebuffer;
//==============================================================================
_private
N32
E_vga_Z_color_M(
N8 red
, N8 green
, N8 blue
){ return ( (N32)blue << E_main_S_framebuffer.pixel_shifts.blue )
| ( (N32)green << E_main_S_framebuffer.pixel_shifts.green )
| ( (N32)red << E_main_S_framebuffer.pixel_shifts.red );
}
_private
N8
E_vga_Z_color_R_red( N32 color
){ return ( color >> E_main_S_framebuffer.pixel_shifts.red ) & 0xff;
}
_private
N8
E_vga_Z_color_R_green( N32 color
){ return ( color >> E_main_S_framebuffer.pixel_shifts.green ) & 0xff;
}
_private
N8
E_vga_Z_color_R_blue( N32 color
){ return ( color >> E_main_S_framebuffer.pixel_shifts.blue ) & 0xff;
}
_private
N32
E_vga_Z_color_M_gray( N8 luminance
){ return E_vga_Z_color_M( luminance, luminance, luminance );
}
_private
N32
E_vga_R_video_color( N32 color
){ return ((( color >> 16 ) & 0xff ) << E_main_S_framebuffer.pixel_shifts.blue )
| ((( color >> 8 ) & 0xff ) << E_main_S_framebuffer.pixel_shifts.green )
| (( color & 0xff ) << E_main_S_framebuffer.pixel_shifts.red );
}
_private
N32
E_vga_R_color( N32 video_color
){ return ((( video_color >> E_main_S_framebuffer.pixel_shifts.blue ) & 0xff ) << 16 )
| ((( video_color >> E_main_S_framebuffer.pixel_shifts.green ) & 0xff ) << 8 )
| (( video_color >> E_main_S_framebuffer.pixel_shifts.red ) & 0xff );
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_private
N32
E_vga_R_pixel(
N32 x
, N32 y
){ return *( E_main_S_framebuffer.p + y * E_main_S_framebuffer.pixels_per_scan_line + x );
}
_private
void
E_vga_P_pixel(
N32 x
, N32 y
, N32 video_color
){ *( E_vga_S_framebuffer + y * E_main_S_framebuffer.width + x ) = video_color;
}
_private
void
E_vga_I_set_pixel_aa(
N32 x
, N32 y
, N32 color
, F brightness
, N8 get_pixel
){ N background_red, background_green, background_blue;
N n = 0;
N p[8];
p[0] = ( get_pixel & E_vga_Z_aa_pixel_S_e ) && x + 1 != E_main_S_framebuffer.width ? E_vga_R_color( E_vga_R_pixel( x + 1, y )) : color;
p[1] = ( get_pixel & E_vga_Z_aa_pixel_S_se ) && x + 1 != E_main_S_framebuffer.width && y + 1 != E_main_S_framebuffer.height ? E_vga_R_color( E_vga_R_pixel( x + 1, y + 1 )) : color;
p[2] = ( get_pixel & E_vga_Z_aa_pixel_S_s ) && y + 1 != E_main_S_framebuffer.height ? E_vga_R_color( E_vga_R_pixel( x, y + 1 )) : color;
p[3] = ( get_pixel & E_vga_Z_aa_pixel_S_sw ) && x && y + 1 != E_main_S_framebuffer.height ? E_vga_R_color( E_vga_R_pixel( x - 1, y + 1 )) : color;
p[4] = ( get_pixel & E_vga_Z_aa_pixel_S_w ) && x ? E_vga_R_color( E_vga_R_pixel( x - 1, y )) : color;
p[5] = ( get_pixel & E_vga_Z_aa_pixel_S_nw ) && x && y ? E_vga_R_color( E_vga_R_pixel( x - 1, y - 1 )) : color;
p[6] = ( get_pixel & E_vga_Z_aa_pixel_S_n ) && y ? E_vga_R_color( E_vga_R_pixel( x, y - 1 )) : color;
p[7] = ( get_pixel & E_vga_Z_aa_pixel_S_ne ) && x + 1 != E_main_S_framebuffer.width && y ? E_vga_R_color( E_vga_R_pixel( x + 1, y - 1 )) : color;
background_red = background_green = background_blue = 0;
for_n( i, 8 )
if( p[i] != color )
{ background_red += E_vga_Z_color_R_red( p[i] );
background_green += E_vga_Z_color_R_green( p[i] );
background_blue += E_vga_Z_color_R_red( p[i] );
n++;
}
if(n)
{ background_red /= n;
background_green /= n;
background_blue /= n;
}else
{ background_red = E_vga_Z_color_R_red( E_vga_S_background_color );
background_green = E_vga_Z_color_R_green( E_vga_S_background_color );
background_blue = E_vga_Z_color_R_blue( E_vga_S_background_color );
}
N8 red = E_vga_Z_color_R_red(color);
N8 green = E_vga_Z_color_R_green(color);
N8 blue = E_vga_Z_color_R_blue(color);
if( red > background_red )
red = background_red + ( red - background_red ) * brightness;
else
red = background_red - ( background_red - red ) * brightness;
if( green > background_green )
green = background_green + ( green - background_green ) * brightness;
else
green = background_green - ( background_green - green ) * brightness;
if( blue > background_blue )
blue = background_blue + ( blue - background_blue ) * brightness;
else
blue = background_blue - ( background_blue - blue ) * brightness;
E_vga_P_pixel( x, y, E_vga_R_video_color( E_vga_Z_color_M( red, green, blue )));
}
_private
void
E_vga_I_draw_x_line(
N32 x
, N32 y
, N32 width
, N32 video_color
){ for_n( x_i, width )
{ if( x + x_i == E_main_S_framebuffer.width )
break;
*( E_vga_S_framebuffer + y * E_main_S_framebuffer.width + x + x_i ) = video_color;
}
}
_private
void
E_vga_I_draw_y_line(
N32 x
, N32 y
, N32 height
, N32 video_color
){ for_n( y_i, height )
{ if( y + y_i == E_main_S_framebuffer.height )
break;
*( E_vga_S_framebuffer + ( y + y_i ) * E_main_S_framebuffer.width + x ) = video_color;
}
}
_private
void
E_vga_I_draw_line(
N32 x_0
, N32 y_0
, N32 x_1
, N32 y_1
, N32 video_color
){ if( x_0 == x_1 )
E_vga_I_draw_y_line( x_0, y_0, y_1 - y_0 + 1, video_color );
else if( y_0 == y_1 )
E_vga_I_draw_x_line( x_0, y_0, x_1 - x_0 + 1, video_color );
else
{ S32 dx_ = x_1 - x_0, dx = J_abs( dx_ );
S32 dy_ = y_1 - y_0, dy = J_abs( dy_ );
S32 x = x_0, x_end = x_1, y = y_0, y_end = y_1;
S32 di = 1;
if( dy > dx )
{ if( dy_ < 0 )
{ J_swap( S32, y, y_end );
J_swap( S32, x, x_end );
}
E_vga_P_pixel( x, y, video_color );
if( x > x_end )
di = -di;
S32 d = dx - dy / 2;
while( y != y_end )
{ y++;
if( d < 0 )
d += dx;
else
{ d += dx - dy;
x += di;
}
E_vga_P_pixel( x, y, video_color );
}
}else
{ if( dx_ < 0 )
{ J_swap( S32, x, x_end );
J_swap( S32, y, y_end )
}
E_vga_P_pixel( x, y, video_color );
if( y > y_end )
di = -di;
S32 d = dy - dx / 2;
while( x != x_end )
{ x++;
if( d < 0 )
d += dy;
else
{ d += dy - dx;
y += di;
}
E_vga_P_pixel( x, y, video_color );
}
}
}
}
_private
void
E_vga_I_draw_rect(
N32 x
, N32 y
, N32 width
, N32 height
, N8 thickness
, N32 video_color
){ for_n( x_i, width )
{ for_n( i, thickness )
*( E_vga_S_framebuffer + ( y + i ) * E_main_S_framebuffer.width + x + x_i ) = video_color;
}
for_n( y_i, height - 2 * thickness )
{ for_n( i, thickness )
{ *( E_vga_S_framebuffer + ( y + thickness + y_i ) * E_main_S_framebuffer.width + x + i ) = video_color;
*( E_vga_S_framebuffer + ( y + thickness + y_i ) * E_main_S_framebuffer.width + x + width - 1 - i ) = video_color;
}
}
for_n_( x_i, width )
{ for_n_rev( i, thickness )
*( E_vga_S_framebuffer + ( y + height - 1 - i ) * E_main_S_framebuffer.width + x + x_i ) = video_color;
}
}
_private
void
E_vga_I_fill_rect(
N32 x
, N32 y
, N32 width
, N32 height
, N32 video_color
){ for_n( y_i, height )
{ for_n( x_i, width )
*( E_vga_S_framebuffer + ( y + y_i ) * E_main_S_framebuffer.width + x + x_i ) = video_color;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_private
void
E_vga_Q_buffer_I_draw( N32 x
, N32 y
, N32 width
, N32 height
){ volatile N32 *video_address = E_main_S_framebuffer.p + y * E_main_S_framebuffer.pixels_per_scan_line + x;
for_n( y_i, height )
{ for_n( x_i, width )
video_address[ x_i ] = *( E_vga_S_framebuffer + ( y + y_i ) * E_main_S_framebuffer.width + x + x_i );
video_address += E_main_S_framebuffer.pixels_per_scan_line;
}
}
/******************************************************************************/