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>

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_t * framebuffer_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.
 

Static Public Member Functions

static uint32_t colour_to_int (const Colour &colour, uint32_t color_depth)
 Converts a colour to an integer value based on the current color depth.
 

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_t * m_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 25 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)
93 m_colour_pallet[color_code] = Colour(0, 0, 0);
94
95
96}
Colour m_colour_pallet[256]
The colour pallet for 8 bit color depth.

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 376 of file graphicsContext.cpp.

376 {
377 return m_color_depth;
378}
uint32_t m_color_depth
The color depth of the screen in bits per pixel.

References m_color_depth.

Referenced by colour_to_int().

◆ colour_to_int() [1/2]

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 return colour_to_int(colour, m_color_depth);
247}
static uint32_t colour_to_int(const Colour &colour, uint32_t color_depth)
Converts a colour to an integer value based on the current color depth.

References colour_to_int(), and m_color_depth.

◆ colour_to_int() [2/2]

uint32_t GraphicsContext::colour_to_int ( const Colour &  colour,
uint32_t  color_depth 
)
static

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

Parameters
colourThe colour class to convert
color_depthHow many bits can be used to store colour info
Returns
The integer value of the colour

Definition at line 256 of file graphicsContext.cpp.

256 {
257
258 switch(color_depth) {
259 case 8: {
260 uint32_t result = 0;
261 int mindistance = 0xfffffff;
262 for(uint32_t i = 0; i <= 255; ++i) {
263// Colour* c = &m_colour_pallet[i];
264 Colour* c = nullptr; //@todo fix this
265 int distance =
266 ((int) colour.red - (int) c->red) * ((int) colour.red - (int) c->red)
267 + ((int) colour.green - (int) c->green) * ((int) colour.green - (int) c->green)
268 + ((int) colour.blue - (int) c->blue) * ((int) colour.blue - (int) c->blue);
269 if(distance < mindistance) {
270 mindistance = distance;
271 result = i;
272 }
273 }
274 return result;
275 }
276 case 16: {
277 // 16-Bit colours RRRRRGGGGGGBBBBB
278 return ((uint16_t) (colour.red & 0xF8)) << 8
279 | ((uint16_t) (colour.green & 0xFC)) << 3
280 | ((uint16_t) (colour.blue & 0xF8) >> 3);
281 }
282 case 24: {
283 return (uint32_t) colour.red << 16
284 | (uint32_t) colour.green << 8
285 | (uint32_t) colour.blue;
286 }
287 default:
288 case 32: {
289 uint32_t red_hex = ((uint32_t) colour.red & 0xFF) << 16;
290 uint32_t green_hex = ((uint32_t) colour.green & 0xFF) << 8;
291 uint32_t blue_hex = (uint32_t) colour.blue & 0xFF;
292 uint32_t alpha_hex = ((uint32_t) colour.alpha & 0xFF) << 24;
293
294 uint32_t hex_value = red_hex | green_hex | blue_hex | alpha_hex;
295
296
297 return hex_value;
298 }
299 }
300}
uint32_t color_depth() const
Gets the current color depth (bits per pixel)

References color_depth().

Referenced by colour_to_int(), draw_circle(), draw_line(), draw_rectangle(), fill_circle(), fill_rectangle(), get_rendered_pixel(), MaxOS::console::FramebufferConsole::print_logo(), MaxOS::console::FramebufferConsole::put_character(), put_pixel(), MaxOS::console::FramebufferConsole::scroll_up(), and MaxOS::console::FramebufferConsole::update_progress_bar().

◆ 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 659 of file graphicsContext.cpp.

659 {
660 draw_circle(x0, y0, radius, colour_to_int(colour));
661}
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 671 of file graphicsContext.cpp.

671 {
672
673 // Make sure the circle is with in the width and height of the screen
674 if(x0 < 0) x0 = 0;
675 if(x0 > m_width) x0 = m_width;
676 if(y0 < 0) y0 = 0;
677 if(y0 > m_height) y0 = m_height;
678
679 // Mirror the Y axis as directly calling put_pixel will not do this
680 if(mirror_y_axis)
681 y0 = m_height - y0 - 1;
682
683
684 // Begin drawing at the left most point of the circle and draw a line to the right most point of the circle
685 for(int32_t x = -radius; x <= radius; ++x) {
686
687 // Draw a line from the top most point of the circle to the bottom most point of the circle
688 for(int32_t y = -radius; y <= radius; ++y) {
689
690 // If the point is within the circle, draw it but make sure it is only part of the outline
691 if(x * x + y * y <= radius * radius && x * x + y * y >= (radius - 1) * (radius - 1))
692 put_pixel(x0 + x, y0 + y, colour);
693 }
694 }
695
696
697}
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 461 of file graphicsContext.cpp.

461 {
462 draw_line(x0, y0, x1, y1, colour_to_int(colour));
463}
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 474 of file graphicsContext.cpp.

474 {
475
476 // Store the minimum and maximum y values
477 bool y_0_is_smaller = y0 < y1;
478 int32_t y_min = y_0_is_smaller ? y0 : y1;
479 int32_t y_max = y_0_is_smaller ? y1 : y0;
480
481 //Reverse the points to draw from left to right
482 if(x1 < x0) {
483 draw_line(x1, y1, x0, y0, colour);
484 return;
485 }
486
487 // Vertical line
488 if(x1 == x0) {
489 // Force the line to be within the screen
490 if(y_min < 0) y_min = 0;
491 if(y_max >= m_height)
492 y_max = m_height - 1;
493
494 // Mirror the Y axis as directly calling put_pixel will not do this
495 if(mirror_y_axis) {
496 int32_t temp = y_max;
497 y_max = m_height - y_min - 1;
498 y_min = m_height - temp - 1;
499 }
500
501 // Check that the line is within the screen
502 if(0 > x0 || x0 >= m_width) {
503 return;
504 }
505
506 // Draw the line
507 for(int32_t y = y_min; y <= y_max; ++y)
508 put_pixel(x0, y, colour);
509
510 return;
511 }
512
513 // Horizontal line
514 if(y1 == y0) {
515 // Ensure the line is within the screen
516 if(x0 < 0) x0 = 0;
517 if(x1 >= m_width) x1 = m_width - 1;
518
519 // Mirror the Y axis as directly calling put_pixel will not do this
520 if(mirror_y_axis)
521 y0 = m_height - y0 - 1;
522
523 // Check that the line is within the screen
524 if(0 > y0 || y0 >= m_height)
525 return;
526
527 // Draw the line
528 for(int32_t x = x0; x <= x1; ++x)
529 put_pixel(x, y0, colour);
530 }
531
532 // If the line is not horizontal or vertical then it must be a diagonal line
533 // Find the slope of the line
534// float slope = ((float) (y1 - y0)) / (x1 - x0);
535 int slope = 1; // replace when in userland
536
537 // A slope that is more horizontal should be drawn by incrementing x
538 if(-1 <= slope && slope <= 1) {
539 int y = y0;
540 for(int32_t x = x0; x <= x1; x++, y += slope)
541 put_pixel(x, (int32_t) y, colour);
542 }
543
544 // A slope that is more vertical should be drawn by incrementing y
545 else {
546 // Invert the slope
547 slope = 1 / slope;
548
549 int x = x0;
550 for(int32_t y = y_min; y <= y_max; x += slope, y++)
551 put_pixel((int32_t) x, y, colour);
552 }
553}

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 564 of file graphicsContext.cpp.

564 {
565 draw_rectangle(x0, y0, x1, y1, colour_to_int(colour));
566
567}
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 578 of file graphicsContext.cpp.

578 {
579
580 // Ensure x and y 0 is smaller than x and y 1
581 --y0;
582 --x0;
583
584 // Draw the rectangle
585 draw_line(x0, y0, x1, y0, colour); // Top
586 draw_line(x0, y1, x1, y1, colour); // Bottom
587 draw_line(x0, y0, x0, y1, colour); // Left
588 draw_line(x1, y0, x1, y1, colour); // Right
589
590}

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 707 of file graphicsContext.cpp.

707 {
708 fill_circle(x0, y0, radius, colour_to_int(colour));
709
710}
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 720 of file graphicsContext.cpp.

720 {
721
722 // Make sure the circle is with in the width and height of the screen
723 if(x0 < 0) x0 = 0;
724 if(x0 > m_width) x0 = m_width;
725 if(y0 < 0) y0 = 0;
726 if(y0 > m_height) y0 = m_height;
727
728 // Mirror the Y axis as directly calling put_pixel will not do this
729 if(mirror_y_axis)
730 y0 = m_height - y0 - 1;
731
732 // Draw the circle
733
734 // Begin drawing at the left most point of the circle and draw a line to the right most point of the circle
735 for(int32_t x = -radius; x <= radius; ++x) {
736
737 // Draw a line from the top most point of the circle to the bottom most point of the circle
738 for(int32_t y = -radius; y <= radius; ++y) {
739
740 // Only draw the pixel if it is within the circle
741 if(x * x + y * y <= radius * radius)
742 put_pixel(x0 + x, y0 + y, colour);
743 }
744 }
745}

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 601 of file graphicsContext.cpp.

601 {
602 fill_rectangle(x0, y0, x1, y1, colour_to_int(colour));
603}
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 614 of file graphicsContext.cpp.

614 {
615
616 // Draw from left to right
617 if(y1 < y0) {
618 fill_rectangle(x1, y1, x0, y0, colour);
619 return;
620 }
621
622 // Make sure the rectangle is within the height of the screen
623 if(y0 < 0) y0 = 0;
624 if(y1 > m_height) y1 = m_height;
625
626 // Make sure the rectangle is within the width of the screen
627 bool x_0_is_smaller = x0 < x1;
628 int32_t x_min = x_0_is_smaller ? x0 : x1;
629 int32_t x_max = x_0_is_smaller ? x1 : x0;
630
631 if(x_min < 0) x_min = 0;
632 if(x_max > m_width)
633 x_max = m_width;
634
635 // Mirror the Y axis as directly calling put_pixel will not do this
636 if(mirror_y_axis) {
637 int32_t temp = y1;
638 y1 = m_height - y0 - 1;
639 y0 = m_height - temp - 1;
640 }
641
642 // Draw the rectangle
643 for(int32_t y = y0; y < y1; ++y) {
644 for(int32_t x = x_min; x < x_max; ++x) {
645 put_pixel(x, y, colour);
646 }
647 }
648
649}

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 752 of file graphicsContext.cpp.

752 {
754}
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 420 of file graphicsContext.cpp.

420 {
421
422 // Check bounds
423 if(x >= m_width || y >= m_height)
424 return { 0, 0, 0 };
425
426 // Get the pixel and convert it to a colour
427 uint32_t translated_color = get_rendered_pixel(x, mirror_y_axis ? m_height - y - 1 : y);
428 return int_to_colour(translated_color);
429}
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 368 of file graphicsContext.cpp.

368 {
369 return m_height;
370}

References m_height.

◆ 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 308 of file graphicsContext.cpp.

308 {
309 switch(m_color_depth) {
310
311 case 8: {
312 // Return the colour from the palette
313 return m_colour_pallet[colour & 0xFF];
314 }
315
316 case 16: {
317 // 16-Bit Colour: 5 bits for red, 6 bits for green, 5 bits for blue (RRRRR,GGGGGG,BBBBB)
318 Colour result;
319
320 result.red = (colour & 0xF800) >> 8;
321 result.green = (colour & 0x07E0) >> 3;
322 result.blue = (colour & 0x001F) << 3;
323
324 return result;
325 }
326
327 case 24: {
328 // 24-Bit Colour: 8 bits for red, 8 bits for green, 8 bits for blue (RRRRRRRR,GGGGGGGG,BBBBBBBB)
329 Colour result;
330
331 result.red = (colour & 0xFF0000) >> 16;
332 result.green = (colour & 0x00FF00) >> 8;
333 result.blue = (colour & 0x0000FF);
334
335 return result;
336 }
337
338 default:
339 case 32: {
340 Colour result;
341
342 uint32_t hex_value = colour;
343 result.red = (hex_value >> 16) & 0xFF;
344 result.green = (hex_value >> 8) & 0xFF;
345 result.blue = hex_value & 0xFF;
346 result.alpha = (hex_value >> 24) & 0xFF;
347
348 return result;
349
350 }
351 }
352}

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 437 of file graphicsContext.cpp.

437 {
438
439 // Get the pixel
440 Colour colour = get_pixel(x, y);
441
442 // Invert the pixel
443 colour.red = 255 - colour.red;
444 colour.green = 255 - colour.green;
445 colour.blue = 255 - colour.blue;
446
447 // Render the pixel
448 put_pixel(x, y, colour);
449}
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 get_pixel(), and put_pixel().

◆ 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 387 of file graphicsContext.cpp.

387 {
388
389 // Convert the colour to an integer and then print it
390 put_pixel(x, y, colour_to_int(colour));
391}

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 402 of file graphicsContext.cpp.

402 {
403
404 // Check bounds
405 if(x >= m_width || y >= m_height)
406 return;
407
408 // Render the pixel
409 render_pixel(x, mirror_y_axis ? m_height - y - 1 : y, colour);
410
411}
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 359 of file graphicsContext.cpp.

359 {
360 return m_width;
361}

References m_width.

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 33 of file graphicsContext.h.

33{ 0 };

Referenced by color_depth(), colour_to_int(), get_rendered_pixel(), int_to_colour(), and render_pixel().

◆ 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 35 of file graphicsContext.h.

Referenced by 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 37 of file graphicsContext.h.

37{ nullptr };

Referenced by framebuffer_address().

◆ m_height

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

The height of the screen in pixels.

Definition at line 32 of file graphicsContext.h.

32{ 0 };

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

◆ m_width

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

The width of the screen in pixels.

Definition at line 31 of file graphicsContext.h.

31{ 0 };

Referenced by draw_circle(), draw_line(), fill_circle(), fill_rectangle(), get_pixel(), put_pixel(), 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 28 of file graphicsContext.h.

28 {
29 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: