Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::common::GraphicsContext Class Reference

Draws pixels to the screen, and handles drawing lines, rectangles and circles. More...

#include <graphicsContext.h>

Inheritance diagram for MaxOS::common::GraphicsContext:
MaxOS::drivers::video::VideoDriver MaxOS::drivers::video::VideoElectronicsStandardsAssociation MaxOS::drivers::video::VideoGraphicsArray

Public Member Functions

 GraphicsContext ()
 Constructs a GraphicsContext object and initializes the color palette.
 
uint32_t colour_to_int (const Colour &)
 Converts a colour to an integer value based on the current color depth.
 
Colour int_to_colour (uint32_t)
 Converts an integer value to a colour based on the current color depth.
 
uint32_t width () const
 Gets the width of the screen.
 
uint32_t height () const
 Gets the height of the screen.
 
uint32_t color_depth () const
 Gets the current color depth (bits per pixel)
 
uint64_tframebuffer_address ()
 Gets the address of the context's framebuffer to draw on.
 
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 put_pixel (uint32_t x, uint32_t y, uint32_t colour)
 Renders a pixel to the screen (automatically converts the colour to an integer), will not render the pixel if it is outside the screen.
 
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.
 
void invert_pixel (uint32_t x, uint32_t y)
 Inverts a pixel.
 
void draw_line (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
 Draws a line on the screen.
 
void draw_line (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t colour)
 Draws a line on the screen.
 
void draw_rectangle (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
 Draws a rectangle on the screen.
 
void draw_rectangle (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t colour)
 Draws a rectangle on the screen.
 
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 fill_rectangle (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t colour)
 Draws a rectangle on the screen, filled with a colour.
 
void draw_circle (uint32_t x0, uint32_t y0, uint32_t radius, const Colour &colour)
 Draws a circle on the screen.
 
void draw_circle (uint32_t x0, uint32_t y0, uint32_t radius, uint32_t colour)
 Draws a circle on the screen.
 
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 fill_circle (uint32_t x0, uint32_t y0, uint32_t radius, uint32_t colour)
 Draws a circle on the screen, filled with a colour.
 

Protected Member Functions

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.
 
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.
 
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 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.
 
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.
 
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.
 
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 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_24_bit (uint32_t x, uint32_t y)
 Gets the colour of a pixel on the screen using the 24 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.
 

Protected Attributes

bool mirror_y_axis
 Should the y axis be mirrored (0,0 is top left if false, bottom left if true)
 
uint32_t m_width { 0 }
 The width of the screen in pixels.
 
uint32_t m_height { 0 }
 The height of the screen in pixels.
 
uint32_t m_color_depth { 0 }
 The color depth of the screen in bits per pixel.
 
Colour m_colour_pallet [256]
 The colour pallet for 8 bit color depth.
 
uint64_tm_framebuffer_address { nullptr }
 The address of the framebuffer.
 

Detailed Description

Draws pixels to the screen, and handles drawing lines, rectangles and circles.

Definition at line 22 of file graphicsContext.h.

Constructor & Destructor Documentation

◆ GraphicsContext()

GraphicsContext::GraphicsContext ( )

Constructs a GraphicsContext object and initializes the color palette.

Definition at line 17 of file graphicsContext.cpp.

17 {
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}
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
Colour m_colour_pallet[256]
The colour pallet for 8 bit color depth.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22

References m_colour_pallet.

Member Function Documentation

◆ color_depth()

uint32_t GraphicsContext::color_depth ( ) const

Gets the current color depth (bits per pixel)

Returns
The color depth

Definition at line 364 of file graphicsContext.cpp.

364 {
365 return m_color_depth;
366}
uint32_t m_color_depth
The color depth of the screen in bits per pixel.

References m_color_depth.

Referenced by MaxOS::drivers::video::VideoDriver::set_mode(), and MaxOS::drivers::video::VideoElectronicsStandardsAssociation::supports_mode().

◆ colour_to_int()

uint32_t GraphicsContext::colour_to_int ( const Colour colour)

Converts a colour to an integer value based on the current color depth.

Parameters
colourThe colour class to convert
Returns
The integer value of the colour

Definition at line 245 of file graphicsContext.cpp.

245 {
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}
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

References MaxOS::common::Colour::alpha, MaxOS::common::Colour::blue, MaxOS::common::Colour::green, m_color_depth, m_colour_pallet, and MaxOS::common::Colour::red.

Referenced by draw_circle(), draw_line(), draw_rectangle(), fill_circle(), fill_rectangle(), get_rendered_pixel(), and put_pixel().

◆ draw_circle() [1/2]

void GraphicsContext::draw_circle ( uint32_t  x0,
uint32_t  y0,
uint32_t  radius,
const Colour colour 
)

Draws a circle on the screen.

Parameters
x0The x coordinate of the centre of the circle
y0The y coordinate of the centre of the circle
radiusThe radius of the circle
colourThe colour of the circle

Definition at line 647 of file graphicsContext.cpp.

647 {
649}
uint32_t colour_to_int(const Colour &)
Converts a colour to an integer value based on the current color depth.
void draw_circle(uint32_t x0, uint32_t y0, uint32_t radius, const Colour &colour)
Draws a circle on the screen.

References colour_to_int(), and draw_circle().

Referenced by draw_circle().

◆ draw_circle() [2/2]

void GraphicsContext::draw_circle ( uint32_t  x0,
uint32_t  y0,
uint32_t  radius,
uint32_t  colour 
)

Draws a circle on the screen.

Parameters
x0The x coordinate of the centre of the circle
y0The y coordinate of the centre of the circle
radiusThe radius of the circle
colourThe colour of the circle

Definition at line 659 of file graphicsContext.cpp.

659 {
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}
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)
bool mirror_y_axis
Should the y axis be mirrored (0,0 is top left if false, bottom left if true)
uint32_t m_width
The width of the screen in pixels.
uint32_t m_height
The height of the screen in pixels.

References m_height, m_width, mirror_y_axis, and put_pixel().

◆ draw_line() [1/2]

void GraphicsContext::draw_line ( uint32_t  x0,
uint32_t  y0,
uint32_t  x1,
uint32_t  y1,
const Colour colour 
)

Draws a line on the screen.

Parameters
x0The x coordinate of the m_first_memory_chunk point
y0The y coordinate of the m_first_memory_chunk point
x1The x coordinate of the final point
y1The y coordinate of the final point
colourThe colour of the line

Definition at line 449 of file graphicsContext.cpp.

449 {
450 draw_line(x0, y0, x1, y1, colour_to_int(colour));
451}
void draw_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
Draws a line on the screen.

References colour_to_int(), and draw_line().

Referenced by draw_line(), draw_line(), and draw_rectangle().

◆ draw_line() [2/2]

void GraphicsContext::draw_line ( uint32_t  x0,
uint32_t  y0,
uint32_t  x1,
uint32_t  y1,
uint32_t  colour 
)

Draws a line on the screen.

Parameters
x0The x coordinate of the m_first_memory_chunk point
y0The y coordinate of the m_first_memory_chunk point
x1The x coordinate of the final point
y1The y coordinate of the final point
colourThe colour of the line

Definition at line 462 of file graphicsContext.cpp.

462 {
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}

References draw_line(), m_height, m_width, mirror_y_axis, and put_pixel().

◆ draw_rectangle() [1/2]

void GraphicsContext::draw_rectangle ( uint32_t  x0,
uint32_t  y0,
uint32_t  x1,
uint32_t  y1,
const Colour colour 
)

Draws a rectangle on the screen.

Parameters
x0The x coordinate of the top left corner
y0The y coordinate of the top left corner
x1The x coordinate of the bottom right corner
y1The y coordinate of the bottom right corner
colourThe colour of the rectangle

Definition at line 552 of file graphicsContext.cpp.

552 {
553 draw_rectangle(x0, y0, x1, y1, colour_to_int(colour));
554
555}
void draw_rectangle(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, const Colour &colour)
Draws a rectangle on the screen.

References colour_to_int(), and draw_rectangle().

Referenced by draw_rectangle().

◆ draw_rectangle() [2/2]

void GraphicsContext::draw_rectangle ( uint32_t  x0,
uint32_t  y0,
uint32_t  x1,
uint32_t  y1,
uint32_t  colour 
)

Draws a rectangle on the screen.

Parameters
x0The x coordinate of the top left corner
y0The y coordinate of the top left corner
x1The x coordinate of the bottom right corner
y1The y coordinate of the bottom right corner
colourThe colour of the rectangle

Definition at line 566 of file graphicsContext.cpp.

566 {
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}

References draw_line().

◆ fill_circle() [1/2]

void GraphicsContext::fill_circle ( uint32_t  x0,
uint32_t  y0,
uint32_t  radius,
const Colour colour 
)

Draws a circle on the screen, filled with a colour.

Parameters
x0The x coordinate of the centre of the circle
y0The y coordinate of the centre of the circle
radiusThe radius of the circle
colourThe colour of the circle

Definition at line 695 of file graphicsContext.cpp.

695 {
697
698}
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.

References colour_to_int(), and fill_circle().

Referenced by fill_circle().

◆ fill_circle() [2/2]

void GraphicsContext::fill_circle ( uint32_t  x0,
uint32_t  y0,
uint32_t  radius,
uint32_t  colour 
)

Draws a circle on the screen, filled with a colour.

Parameters
x0The x coordinate of the centre of the circle
y0The y coordinate of the centre of the circle
radiusThe radius of the circle
colourThe colour of the circle

Definition at line 708 of file graphicsContext.cpp.

708 {
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}

References m_height, m_width, mirror_y_axis, and put_pixel().

◆ fill_rectangle() [1/2]

void GraphicsContext::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.

Parameters
x0The x coordinate of the top left corner
y0The y coordinate of the top left corner
x1The x coordinate of the bottom right corner
y1The y coordinate of the bottom right corner
colourThe colour of the rectangle

Definition at line 589 of file graphicsContext.cpp.

589 {
590 fill_rectangle(x0, y0, x1, y1, colour_to_int(colour));
591}
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.

References colour_to_int(), and fill_rectangle().

Referenced by fill_rectangle(), and fill_rectangle().

◆ fill_rectangle() [2/2]

void GraphicsContext::fill_rectangle ( uint32_t  x0,
uint32_t  y0,
uint32_t  x1,
uint32_t  y1,
uint32_t  colour 
)

Draws a rectangle on the screen, filled with a colour.

Parameters
x0The x coordinate of the top left corner
y0The y coordinate of the top left corner
x1The x coordinate of the bottom right corner
y1The y coordinate of the bottom right corner
colourThe colour of the rectangle

Definition at line 602 of file graphicsContext.cpp.

602 {
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}

References fill_rectangle(), m_height, m_width, mirror_y_axis, and put_pixel().

◆ framebuffer_address()

uint64_t * GraphicsContext::framebuffer_address ( )

Gets the address of the context's framebuffer to draw on.

Returns
The framebuffer address

Definition at line 740 of file graphicsContext.cpp.

740 {
742}
uint64_t * m_framebuffer_address
The address of the framebuffer.

References m_framebuffer_address.

◆ get_pixel()

Colour GraphicsContext::get_pixel ( uint32_t  x,
uint32_t  y 
)

Gets the colour of a pixel, or returns black if the pixel is outside the screen.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
Returns
The colour of the pixel or black if the pixel is outside the screen

Definition at line 408 of file graphicsContext.cpp.

408 {
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}
Colour int_to_colour(uint32_t)
Converts an integer value to a colour based on the current 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.

References get_rendered_pixel(), int_to_colour(), m_height, m_width, and mirror_y_axis.

Referenced by invert_pixel().

◆ get_rendered_pixel()

uint32_t GraphicsContext::get_rendered_pixel ( uint32_t  x,
uint32_t  y 
)
protectedvirtual

Gets the colour of a pixel on the screen, automatically uses the correct color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
Returns
The colour of the pixel or white if the pixel is not supported

Definition at line 179 of file graphicsContext.cpp.

179 {
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}
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 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_32_bit(uint32_t x, uint32_t y)
Gets the colour of a pixel on the screen using the 32 bit color depth.

References colour_to_int(), get_rendered_pixel_16_bit(), get_rendered_pixel_24_bit(), get_rendered_pixel_32_bit(), get_rendered_pixel_8_bit(), and m_color_depth.

Referenced by get_pixel().

◆ get_rendered_pixel_16_bit()

uint16_t GraphicsContext::get_rendered_pixel_16_bit ( uint32_t  x,
uint32_t  y 
)
protectedvirtual

Gets the colour of a pixel on the screen using the 16 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
Returns
The 16Bit colour of the pixel

Definition at line 213 of file graphicsContext.cpp.

213 {
214 return 0;
215}

Referenced by get_rendered_pixel().

◆ get_rendered_pixel_24_bit()

uint32_t GraphicsContext::get_rendered_pixel_24_bit ( uint32_t  x,
uint32_t  y 
)
protectedvirtual

Gets the colour of a pixel on the screen using the 24 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
Returns
The 24Bit colour of the pixel

Definition at line 224 of file graphicsContext.cpp.

224 {
225 return 0;
226}

Referenced by get_rendered_pixel().

◆ get_rendered_pixel_32_bit()

uint32_t GraphicsContext::get_rendered_pixel_32_bit ( uint32_t  x,
uint32_t  y 
)
protectedvirtual

Gets the colour of a pixel on the screen using the 32 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
Returns
The 32Bit colour of the pixel

Definition at line 235 of file graphicsContext.cpp.

235 {
236 return 0;
237}

Referenced by get_rendered_pixel().

◆ get_rendered_pixel_8_bit()

uint8_t GraphicsContext::get_rendered_pixel_8_bit ( uint32_t  x,
uint32_t  y 
)
protectedvirtual

Gets the colour of a pixel on the screen using the 8 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
Returns
The 8Bit colour of the pixel

Definition at line 202 of file graphicsContext.cpp.

202 {
203 return 0;
204}

Referenced by get_rendered_pixel().

◆ height()

uint32_t GraphicsContext::height ( ) const

Gets the height of the screen.

Returns
The height of the screen

Definition at line 356 of file graphicsContext.cpp.

356 {
357 return m_height;
358}

References m_height.

Referenced by MaxOS::drivers::video::VideoDriver::set_mode(), MaxOS::drivers::video::VideoGraphicsArray::supports_mode(), and MaxOS::drivers::video::VideoElectronicsStandardsAssociation::supports_mode().

◆ int_to_colour()

Colour GraphicsContext::int_to_colour ( uint32_t  colour)

Converts an integer value to a colour based on the current color depth.

Parameters
colourThe integer value to convert
Returns
The colour class of the integer value

Definition at line 296 of file graphicsContext.cpp.

296 {
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}

References m_color_depth, and m_colour_pallet.

Referenced by get_pixel().

◆ invert_pixel()

void GraphicsContext::invert_pixel ( uint32_t  x,
uint32_t  y 
)

Inverts a pixel.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel

Definition at line 425 of file graphicsContext.cpp.

425 {
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}
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.

References MaxOS::common::Colour::blue, get_pixel(), MaxOS::common::Colour::green, put_pixel(), and MaxOS::common::Colour::red.

Referenced by MaxOS::gui::Desktop::invert_mouse_cursor().

◆ put_pixel() [1/2]

void GraphicsContext::put_pixel ( uint32_t  x,
uint32_t  y,
const Colour colour 
)

Renders a pixel to the screen (automatically converts the colour to an integer)

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe colour of the pixel

Definition at line 375 of file graphicsContext.cpp.

375 {
376
377 // Convert the colour to an integer and then print it
378 put_pixel(x, y, colour_to_int(colour));
379}

References colour_to_int(), and put_pixel().

Referenced by draw_circle(), draw_line(), fill_circle(), fill_rectangle(), invert_pixel(), and put_pixel().

◆ put_pixel() [2/2]

void GraphicsContext::put_pixel ( uint32_t  x,
uint32_t  y,
uint32_t  colour 
)

Renders a pixel to the screen (automatically converts the colour to an integer), will not render the pixel if it is outside the screen.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe colour of the pixel
Todo:
make them uint32_t

Definition at line 390 of file graphicsContext.cpp.

390 {
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}
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.

References m_height, m_width, mirror_y_axis, and render_pixel().

◆ render_pixel()

void GraphicsContext::render_pixel ( uint32_t  x,
uint32_t  y,
uint32_t  colour 
)
protectedvirtual

Renders a pixel to the screen based on the current color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe colour of the pixel

Definition at line 107 of file graphicsContext.cpp.

107 {
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}
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.
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.
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.
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.

References m_color_depth, render_pixel_16_bit(), render_pixel_24_bit(), render_pixel_32_bit(), and render_pixel_8_bit().

Referenced by put_pixel().

◆ render_pixel_16_bit()

void GraphicsContext::render_pixel_16_bit ( uint32_t  x,
uint32_t  y,
uint16_t  colour 
)
protectedvirtual

Renders a pixel to the screen using the 16 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe 16Bit colour of the pixel

Definition at line 146 of file graphicsContext.cpp.

146 {
147
148}

Referenced by render_pixel().

◆ render_pixel_24_bit()

void GraphicsContext::render_pixel_24_bit ( uint32_t  x,
uint32_t  y,
uint32_t  colour 
)
protectedvirtual

Renders a pixel to the screen using the 24 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe 24Bit colour of the pixel

Definition at line 157 of file graphicsContext.cpp.

157 {
158
159}

Referenced by render_pixel().

◆ render_pixel_32_bit()

void GraphicsContext::render_pixel_32_bit ( uint32_t  x,
uint32_t  y,
uint32_t  colour 
)
protectedvirtual

Renders a pixel to the screen using the 32 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe 32Bit colour of the pixel

Definition at line 168 of file graphicsContext.cpp.

168 {
169
170}

Referenced by render_pixel().

◆ render_pixel_8_bit()

void GraphicsContext::render_pixel_8_bit ( uint32_t  x,
uint32_t  y,
uint8_t  colour 
)
protectedvirtual

Renders a pixel to the screen using the 8 bit color depth.

Parameters
xThe x coordinate of the pixel
yThe y coordinate of the pixel
colourThe 8Bit colour of the pixel

Definition at line 135 of file graphicsContext.cpp.

135 {
136
137}

Referenced by render_pixel().

◆ width()

uint32_t GraphicsContext::width ( ) const

Gets the width of the screen.

Returns
The width of the screen

Definition at line 347 of file graphicsContext.cpp.

347 {
348 return m_width;
349}

References m_width.

Referenced by MaxOS::drivers::video::VideoDriver::set_mode(), MaxOS::drivers::video::VideoGraphicsArray::supports_mode(), and MaxOS::drivers::video::VideoElectronicsStandardsAssociation::supports_mode().

Member Data Documentation

◆ m_color_depth

uint32_t MaxOS::common::GraphicsContext::m_color_depth { 0 }
protected

The color depth of the screen in bits per pixel.

Definition at line 30 of file graphicsContext.h.

30{ 0 };

Referenced by color_depth(), colour_to_int(), get_rendered_pixel(), int_to_colour(), render_pixel(), and MaxOS::drivers::video::VideoDriver::set_mode().

◆ m_colour_pallet

Colour MaxOS::common::GraphicsContext::m_colour_pallet[256]
protected

The colour pallet for 8 bit color depth.

Todo:
make const

Definition at line 32 of file graphicsContext.h.

Referenced by colour_to_int(), GraphicsContext(), and int_to_colour().

◆ m_framebuffer_address

uint64_t* MaxOS::common::GraphicsContext::m_framebuffer_address { nullptr }
protected

The address of the framebuffer.

Definition at line 34 of file graphicsContext.h.

34{ nullptr };

Referenced by framebuffer_address(), and MaxOS::drivers::video::VideoElectronicsStandardsAssociation::VideoElectronicsStandardsAssociation().

◆ m_height

uint32_t MaxOS::common::GraphicsContext::m_height { 0 }
protected

The height of the screen in pixels.

Definition at line 29 of file graphicsContext.h.

29{ 0 };

Referenced by draw_circle(), draw_line(), fill_circle(), fill_rectangle(), get_pixel(), height(), put_pixel(), and MaxOS::drivers::video::VideoDriver::set_mode().

◆ m_width

uint32_t MaxOS::common::GraphicsContext::m_width { 0 }
protected

The width of the screen in pixels.

Definition at line 28 of file graphicsContext.h.

28{ 0 };

Referenced by draw_circle(), draw_line(), fill_circle(), fill_rectangle(), get_pixel(), put_pixel(), MaxOS::drivers::video::VideoDriver::set_mode(), and width().

◆ mirror_y_axis

bool MaxOS::common::GraphicsContext::mirror_y_axis
protected
Initial value:
{
false }

Should the y axis be mirrored (0,0 is top left if false, bottom left if true)

Definition at line 25 of file graphicsContext.h.

25 {
26 false };

Referenced by draw_circle(), draw_line(), fill_circle(), fill_rectangle(), get_pixel(), and put_pixel().


The documentation for this class was generated from the following files: