Max OS 0.3
Loading...
Searching...
No Matches
graphicsContext.cpp
Go to the documentation of this file.
1
10
11using namespace MaxOS::common;
12
13
18
19
20 // VirtualBox VGA palette
21 m_colour_pallet[0x00] = Colour(0x00, 0x00, 0x00); // Black
22 m_colour_pallet[0x01] = Colour(0x00, 0x00, 0xA8); // Duke Blue
23 m_colour_pallet[0x02] = Colour(0x00, 0xA8, 0x00); // Islamic Green
24 m_colour_pallet[0x03] = Colour(0x00, 0xA8, 0xA8); // Persian Green
25 m_colour_pallet[0x04] = Colour(0xA8, 0x00, 0x00); // Dark Candy Apple Red
26 m_colour_pallet[0x05] = Colour(0xA8, 0x00, 0xA8); // Heliotrope Magenta
27
28 m_colour_pallet[0x06] = Colour(0xA8, 0xA8, 0x00); // Light Gold
29 m_colour_pallet[0x07] = Colour(0xA8, 0xA8, 0xA8); // Dark Gray (X11)
30 m_colour_pallet[0x08] = Colour(0x00, 0x00, 0x57); // Cetacean Blue
31 m_colour_pallet[0x09] = Colour(0x00, 0x00, 0xFF); // Blue
32 m_colour_pallet[0x0A] = Colour(0x00, 0xA8, 0x57); // Green (Pigment)
33 m_colour_pallet[0x0B] = Colour(0x00, 0xA8, 0xFF); // Vivid Cerulean
34 m_colour_pallet[0x0C] = Colour(0xA8, 0x00, 0x57); // Jazz berry Jam
35 m_colour_pallet[0x0D] = Colour(0xA8, 0x00, 0x57); // Jazz berry Jam
36 m_colour_pallet[0x0E] = Colour(0xA8, 0xA8, 0x57); // Olive Green
37 m_colour_pallet[0x0F] = Colour(0xA8, 0xA8, 0xFF); // Maximum Blue Purple
38
39 m_colour_pallet[0x10] = Colour(0x00, 0x57, 0x00); // Dark Green (X11)
40 m_colour_pallet[0x11] = Colour(0x00, 0x57, 0xA8); // Cobalt Blue
41 m_colour_pallet[0x12] = Colour(0x00, 0xFF, 0x00); // Electric Green
42 m_colour_pallet[0x13] = Colour(0x00, 0xFF, 0xA8); // Medium Spring Green
43 m_colour_pallet[0x14] = Colour(0xA8, 0x57, 0x00); // Windsor Tan
44 m_colour_pallet[0x15] = Colour(0xA8, 0x57, 0xA8); // Purpureus
45 m_colour_pallet[0x16] = Colour(0xA8, 0xFF, 0x00); // Spring Bud
46 m_colour_pallet[0x17] = Colour(0xA8, 0xFF, 0xA8); // Mint Green
47 m_colour_pallet[0x18] = Colour(0x00, 0x57, 0x57); // Midnight Green (Eagle Green)
48 m_colour_pallet[0x19] = Colour(0x00, 0x57, 0xFF); // Blue (RYB)
49 m_colour_pallet[0x1A] = Colour(0x00, 0xFF, 0x57); // Malachite
50 m_colour_pallet[0x1B] = Colour(0x00, 0xFF, 0xFF); // Aqua
51 m_colour_pallet[0x1C] = Colour(0xA8, 0x57, 0x57); // Middle Red Purple
52 m_colour_pallet[0x1D] = Colour(0xA8, 0x57, 0xFF); // Lavender Indigo
53 m_colour_pallet[0x1E] = Colour(0xA8, 0xFF, 0x57); // Olive Green
54 m_colour_pallet[0x1F] = Colour(0xA8, 0xFF, 0xFF); // Celeste
55
56 m_colour_pallet[0x20] = Colour(0x57, 0x00, 0x00); // Blood Red
57 m_colour_pallet[0x21] = Colour(0x57, 0x00, 0xA8); // Metallic Violet
58 m_colour_pallet[0x22] = Colour(0x57, 0xA8, 0x00); // Kelly Green
59 m_colour_pallet[0x23] = Colour(0x57, 0xA8, 0xA8); // Cadet Blue
60 m_colour_pallet[0x24] = Colour(0xFF, 0x00, 0x00); // Red
61 m_colour_pallet[0x25] = Colour(0xFF, 0x00, 0xA8); // Fashion Fuchsia
62 m_colour_pallet[0x26] = Colour(0xFF, 0xA8, 0x00); // Chrome Yellow
63 m_colour_pallet[0x27] = Colour(0xFF, 0xA8, 0xA8); // Light Salmon Pink
64 m_colour_pallet[0x28] = Colour(0x57, 0x00, 0x57); // Imperial Purple
65 m_colour_pallet[0x29] = Colour(0x57, 0x00, 0xFF); // Electric Indigo
66 m_colour_pallet[0x2A] = Colour(0x57, 0xA8, 0x57); // Apple
67 m_colour_pallet[0x2B] = Colour(0x57, 0xA8, 0xFF); // Blue Jeans
68 m_colour_pallet[0x2C] = Colour(0xFF, 0x00, 0x57); // Folly
69 m_colour_pallet[0x2D] = Colour(0xFF, 0x00, 0xFF); // Fuchsia
70 m_colour_pallet[0x2E] = Colour(0xFF, 0xA8, 0x57); // Rajah
71 m_colour_pallet[0x2F] = Colour(0xFF, 0xA8, 0xFF); // Rich Brilliant Lavender
72
73 m_colour_pallet[0x30] = Colour(0x57, 0x57, 0x00); // Dark Bronze (Coin)
74 m_colour_pallet[0x31] = Colour(0x57, 0x57, 0xA8); // Liberty
75 m_colour_pallet[0x32] = Colour(0x57, 0xFF, 0x00); // Chlorophyll Green
76 m_colour_pallet[0x33] = Colour(0x57, 0xFF, 0xA8); // Medium Aquamarine
77 m_colour_pallet[0x34] = Colour(0xFF, 0x57, 0x00); // Orange (Pantone)
78 m_colour_pallet[0x35] = Colour(0xFF, 0x57, 0xA8); // Brilliant Rose
79 m_colour_pallet[0x36] = Colour(0xFF, 0xFF, 0x00); // Yellow
80 m_colour_pallet[0x37] = Colour(0xFF, 0xFF, 0xA8); // Calamansi
81 m_colour_pallet[0x38] = Colour(0x57, 0x57, 0x57); // Davy's Grey
82 m_colour_pallet[0x39] = Colour(0x57, 0x57, 0xFF); // Very Light Blue
83 m_colour_pallet[0x3A] = Colour(0x57, 0xFF, 0x57); // Screamin' Green
84 m_colour_pallet[0x3B] = Colour(0x57, 0xFF, 0xFF); // Electric Blue
85 m_colour_pallet[0x3C] = Colour(0xFF, 0x57, 0x57); // Sunset Orange
86 m_colour_pallet[0x3D] = Colour(0xFF, 0x57, 0xFF); // Shocking Pink (Crayola)
87 m_colour_pallet[0x3E] = Colour(0xFF, 0xFF, 0x57); // Shocking Pink (Crayola)
88 m_colour_pallet[0x3F] = Colour(0xFF, 0xFF, 0xFF); // White
89
90
91 // Set the rest of the palette to black
92 for(uint8_t color_code = 255; color_code >= 0x40; --color_code)
94
95
96}
97
98GraphicsContext::~GraphicsContext() = default;
99
108
109 // Call the correct put_pixel function based on the color depth
110 switch(m_color_depth) {
111 case 8:
112 render_pixel_8_bit(x, y, colour);
113 break;
114 case 16:
115 render_pixel_16_bit(x, y, colour);
116 break;
117 case 24:
118 render_pixel_24_bit(x, y, colour);
119 break;
120 case 32:
121 render_pixel_32_bit(x, y, colour);
122 break;
123 }
124
125
126}
127
138
149
160
171
180 // Call the correct get_pixel function based on the color depth
181 switch(m_color_depth) {
182 case 8:
183 return get_rendered_pixel_8_bit(x, y);
184 case 16:
185 return get_rendered_pixel_16_bit(x, y);
186 case 24:
187 return get_rendered_pixel_24_bit(x, y);
188 case 32:
189 return get_rendered_pixel_32_bit(x, y);
190 }
191
192 return colour_to_int(Colour(0xFF, 0xFF, 0xFF));
193}
194
205
216
227
238
246
247 switch(m_color_depth) {
248 case 8: {
249 uint32_t result = 0;
250 int mindistance = 0xfffffff;
251 for(uint32_t i = 0; i <= 255; ++i) {
252 Colour* c = &m_colour_pallet[i];
253 int distance =
254 ((int) colour.red - (int) c->red) * ((int) colour.red - (int) c->red)
255 + ((int) colour.green - (int) c->green) * ((int) colour.green - (int) c->green)
256 + ((int) colour.blue - (int) c->blue) * ((int) colour.blue - (int) c->blue);
257 if(distance < mindistance) {
259 result = i;
260 }
261 }
262 return result;
263 }
264 case 16: {
265 // 16-Bit colours RRRRRGGGGGGBBBBB
266 return ((uint16_t) (colour.red & 0xF8)) << 8
267 | ((uint16_t) (colour.green & 0xFC)) << 3
268 | ((uint16_t) (colour.blue & 0xF8) >> 3);
269 }
270 case 24: {
271 return (uint32_t) colour.red << 16
272 | (uint32_t) colour.green << 8
273 | (uint32_t) colour.blue;
274 }
275 default:
276 case 32: {
277 uint32_t red_hex = ((uint32_t) colour.red & 0xFF) << 16;
278 uint32_t green_hex = ((uint32_t) colour.green & 0xFF) << 8;
279 uint32_t blue_hex = (uint32_t) colour.blue & 0xFF;
280 uint32_t alpha_hex = ((uint32_t) colour.alpha & 0xFF) << 24;
281
283
284
285 return hex_value;
286 }
287 }
288}
289
297 switch(m_color_depth) {
298
299 case 8: {
300 // Return the colour from the palette
301 return m_colour_pallet[colour & 0xFF];
302 }
303
304 case 16: {
305 // 16-Bit Colour: 5 bits for red, 6 bits for green, 5 bits for blue (RRRRR,GGGGGG,BBBBB)
307
308 result.red = (colour & 0xF800) >> 8;
309 result.green = (colour & 0x07E0) >> 3;
310 result.blue = (colour & 0x001F) << 3;
311
312 return result;
313 }
314
315 case 24: {
316 // 24-Bit Colour: 8 bits for red, 8 bits for green, 8 bits for blue (RRRRRRRR,GGGGGGGG,BBBBBBBB)
318
319 result.red = (colour & 0xFF0000) >> 16;
320 result.green = (colour & 0x00FF00) >> 8;
321 result.blue = (colour & 0x0000FF);
322
323 return result;
324 }
325
326 default:
327 case 32: {
329
330 uint32_t hex_value = colour;
331 result.red = (hex_value >> 16) & 0xFF;
332 result.green = (hex_value >> 8) & 0xFF;
333 result.blue = hex_value & 0xFF;
334 result.alpha = (hex_value >> 24) & 0xFF;
335
336 return result;
337
338 }
339 }
340}
341
348 return m_width;
349}
350
357 return m_height;
358}
359
367
376
377 // Convert the colour to an integer and then print it
378 put_pixel(x, y, colour_to_int(colour));
379}
380
391
392 // Check bounds
393 if(x >= m_width || y >= m_height)
394 return;
395
396 // Render the pixel
397 render_pixel(x, mirror_y_axis ? m_height - y - 1 : y, colour);
398
399}
400
409
410 // Check bounds
411 if(x >= m_width || y >= m_height)
412 return { 0, 0, 0 };
413
414 // Get the pixel and convert it to a colour
417}
418
426
427 // Get the pixel
428 Colour colour = get_pixel(x, y);
429
430 // Invert the pixel
431 colour.red = 255 - colour.red;
432 colour.green = 255 - colour.green;
433 colour.blue = 255 - colour.blue;
434
435 // Render the pixel
436 put_pixel(x, y, colour);
437}
438
439
452
463
464 // Store the minimum and maximum y values
465 bool y_0_is_smaller = y0 < y1;
468
469 //Reverse the points to draw from left to right
470 if(x1 < x0) {
471 draw_line(x1, y1, x0, y0, colour);
472 return;
473 }
474
475 // Vertical line
476 if(x1 == x0) {
477 // Force the line to be within the screen
478 if(y_min < 0) y_min = 0;
479 if(y_max >= m_height)
480 y_max = m_height - 1;
481
482 // Mirror the Y axis as directly calling put_pixel will not do this
483 if(mirror_y_axis) {
485 y_max = m_height - y_min - 1;
486 y_min = m_height - temp - 1;
487 }
488
489 // Check that the line is within the screen
490 if(0 > x0 || x0 >= m_width) {
491 return;
492 }
493
494 // Draw the line
495 for(int32_t y = y_min; y <= y_max; ++y)
496 put_pixel(x0, y, colour);
497
498 return;
499 }
500
501 // Horizontal line
502 if(y1 == y0) {
503 // Ensure the line is within the screen
504 if(x0 < 0) x0 = 0;
505 if(x1 >= m_width) x1 = m_width - 1;
506
507 // Mirror the Y axis as directly calling put_pixel will not do this
508 if(mirror_y_axis)
509 y0 = m_height - y0 - 1;
510
511 // Check that the line is within the screen
512 if(0 > y0 || y0 >= m_height)
513 return;
514
515 // Draw the line
516 for(int32_t x = x0; x <= x1; ++x)
517 put_pixel(x, y0, colour);
518 }
519
520 // If the line is not horizontal or vertical then it must be a diagonal line
521 // Find the slope of the line
522// float slope = ((float) (y1 - y0)) / (x1 - x0);
523 int slope = 1; // replace when in userland
524
525 // A slope that is more horizontal should be drawn by incrementing x
526 if(-1 <= slope && slope <= 1) {
527 int y = y0;
528 for(int32_t x = x0; x <= x1; x++, y += slope)
529 put_pixel(x, (int32_t) y, colour);
530 }
531
532 // A slope that is more vertical should be drawn by incrementing y
533 else {
534 // Invert the slope
535 slope = 1 / slope;
536
537 int x = x0;
538 for(int32_t y = y_min; y <= y_max; x += slope, y++)
539 put_pixel((int32_t) x, y, colour);
540 }
541}
542
556
567
568 // Ensure x and y 0 is smaller than x and y 1
569 --y0;
570 --x0;
571
572 // Draw the rectangle
573 draw_line(x0, y0, x1, y0, colour); // Top
574 draw_line(x0, y1, x1, y1, colour); // Bottom
575 draw_line(x0, y0, x0, y1, colour); // Left
576 draw_line(x1, y0, x1, y1, colour); // Right
577
578}
579
592
603
604 // Draw from left to right
605 if(y1 < y0) {
606 fill_rectangle(x1, y1, x0, y0, colour);
607 return;
608 }
609
610 // Make sure the rectangle is within the height of the screen
611 if(y0 < 0) y0 = 0;
612 if(y1 > m_height) y1 = m_height;
613
614 // Make sure the rectangle is within the width of the screen
615 bool x_0_is_smaller = x0 < x1;
618
619 if(x_min < 0) x_min = 0;
620 if(x_max > m_width)
621 x_max = m_width;
622
623 // Mirror the Y axis as directly calling put_pixel will not do this
624 if(mirror_y_axis) {
625 int32_t temp = y1;
626 y1 = m_height - y0 - 1;
627 y0 = m_height - temp - 1;
628 }
629
630 // Draw the rectangle
631 for(int32_t y = y0; y < y1; ++y) {
632 for(int32_t x = x_min; x < x_max; ++x) {
633 put_pixel(x, y, colour);
634 }
635 }
636
637}
638
650
660
661 // Make sure the circle is with in the width and height of the screen
662 if(x0 < 0) x0 = 0;
663 if(x0 > m_width) x0 = m_width;
664 if(y0 < 0) y0 = 0;
665 if(y0 > m_height) y0 = m_height;
666
667 // Mirror the Y axis as directly calling put_pixel will not do this
668 if(mirror_y_axis)
669 y0 = m_height - y0 - 1;
670
671
672 // Begin drawing at the left most point of the circle and draw a line to the right most point of the circle
673 for(int32_t x = -radius; x <= radius; ++x) {
674
675 // Draw a line from the top most point of the circle to the bottom most point of the circle
676 for(int32_t y = -radius; y <= radius; ++y) {
677
678 // If the point is within the circle, draw it but make sure it is only part of the outline
679 if(x * x + y * y <= radius * radius && x * x + y * y >= (radius - 1) * (radius - 1))
680 put_pixel(x0 + x, y0 + y, colour);
681 }
682 }
683
684
685}
686
699
709
710 // Make sure the circle is with in the width and height of the screen
711 if(x0 < 0) x0 = 0;
712 if(x0 > m_width) x0 = m_width;
713 if(y0 < 0) y0 = 0;
714 if(y0 > m_height) y0 = m_height;
715
716 // Mirror the Y axis as directly calling put_pixel will not do this
717 if(mirror_y_axis)
718 y0 = m_height - y0 - 1;
719
720 // Draw the circle
721
722 // Begin drawing at the left most point of the circle and draw a line to the right most point of the circle
723 for(int32_t x = -radius; x <= radius; ++x) {
724
725 // Draw a line from the top most point of the circle to the bottom most point of the circle
726 for(int32_t y = -radius; y <= radius; ++y) {
727
728 // Only draw the pixel if it is within the circle
729 if(x * x + y * y <= radius * radius)
730 put_pixel(x0 + x, y0 + y, colour);
731 }
732 }
733}
734
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
uint8_t alpha
The amount of alpha 0-255 (0 is visible, 255 is invisible) todo: confirm.
Definition colour.h:102
uint8_t green
The amount of green 0-255.
Definition colour.h:100
uint8_t blue
The amount of blue 0-255.
Definition colour.h:101
uint8_t red
The amount of red 0-255.
Definition colour.h:99
void fill_circle(uint32_t x0, uint32_t y0, uint32_t radius, const Colour &colour)
Draws a circle on the screen, filled with a colour.
void put_pixel(uint32_t x, uint32_t y, const Colour &colour)
Renders a pixel to the screen (automatically converts the colour to an integer)
void draw_rectangle(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
Draws a rectangle on the screen.
virtual uint8_t get_rendered_pixel_8_bit(uint32_t x, uint32_t y)
Gets the colour of a pixel on the screen using the 8 bit color depth.
virtual void render_pixel_8_bit(uint32_t x, uint32_t y, uint8_t colour)
Renders a pixel to the screen using the 8 bit color depth.
uint32_t colour_to_int(const Colour &)
Converts a colour to an integer value based on the current color depth.
void invert_pixel(uint32_t x, uint32_t y)
Inverts a pixel.
uint64_t * framebuffer_address()
Gets the address of the context's framebuffer to draw on.
uint32_t m_color_depth
The color depth of the screen in bits per pixel.
Colour get_pixel(uint32_t x, uint32_t y)
Gets the colour of a pixel, or returns black if the pixel is outside the screen.
virtual void render_pixel_24_bit(uint32_t x, uint32_t y, uint32_t colour)
Renders a pixel to the screen using the 24 bit color depth.
Colour int_to_colour(uint32_t)
Converts an integer value to a colour based on the current color depth.
Colour m_colour_pallet[256]
The colour pallet for 8 bit color depth.
virtual uint32_t get_rendered_pixel_24_bit(uint32_t x, uint32_t y)
Gets the colour of a pixel on the screen using the 24 bit color depth.
virtual uint16_t get_rendered_pixel_16_bit(uint32_t x, uint32_t y)
Gets the colour of a pixel on the screen using the 16 bit color depth.
virtual uint32_t get_rendered_pixel(uint32_t x, uint32_t y)
Gets the colour of a pixel on the screen, automatically uses the correct color depth.
uint32_t width() const
Gets the width of the screen.
uint64_t * m_framebuffer_address
The address of the framebuffer.
virtual void render_pixel(uint32_t x, uint32_t y, uint32_t colour)
Renders a pixel to the screen based on the current color depth.
bool mirror_y_axis
Should the y axis be mirrored (0,0 is top left if false, bottom left if true)
uint32_t height() const
Gets the height of the screen.
uint32_t m_width
The width of the screen in pixels.
virtual void render_pixel_32_bit(uint32_t x, uint32_t y, uint32_t colour)
Renders a pixel to the screen using the 32 bit color depth.
GraphicsContext()
Constructs a GraphicsContext object and initializes the color palette.
void fill_rectangle(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
Draws a rectangle on the screen, filled with a colour.
void draw_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
Draws a line on the screen.
virtual void render_pixel_16_bit(uint32_t x, uint32_t y, uint16_t colour)
Renders a pixel to the screen using the 16 bit color depth.
virtual uint32_t get_rendered_pixel_32_bit(uint32_t x, uint32_t y)
Gets the colour of a pixel on the screen using the 32 bit color depth.
uint32_t color_depth() const
Gets the current color depth (bits per pixel)
void draw_circle(uint32_t x0, uint32_t y0, uint32_t radius, const Colour &colour)
Draws a circle on the screen.
uint32_t m_height
The height of the screen in pixels.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Defines a GraphicsContext class for drawing pixels, lines, rectangles and circles to the screen.