StuBS
Loading...
Searching...
No Matches
Graphics Class Reference

Driver managing the video mode and synchronizing its buffer with the graphics printer. More...

#include <graphics.h>

Inheritance diagram for Graphics:
Collaboration diagram for Graphics:

Public Member Functions

 Graphics (unsigned size, void *frontbuffer, void *backbuffer)
 Constructor.
 
bool init (bool force=false)
 Initialize GraphicsPrinter according to the current video mode.
 
bool switchBuffers ()
 Switch front and back buffer (only if front buffer was already copied to video memory)
 
void scanoutFrontbuffer ()
 Copy current front buffer to the video memory.
 
void clear ()
 Clear all pixel of the current back buffer (set full screen to black)
 
bool valid (const Point &p)
 Check if a Point can be displayed at the current resolution.
 
unsigned height ()
 Number of vertical pixels in current resolution.
 
unsigned width ()
 Number of horizontal pixels in current resolution.
 
void pixel (const Point &p, const Color &color)
 Draw a pixel on the current back buffer.
 
void pixel (const Point &p, const ColorAlpha &color)
 Draw a pixel on the current back buffer.
 
void line (const Point &start, const Point &end, const Color &color)
 Draw a line on the current back buffer.
 
void line (const Point &start, const Point &end, const ColorAlpha &color)
 Draw a line on the current back buffer.
 
void rectangle (const Point &start, const Point &end, const Color &color, bool filled=true)
 Draw a rectangle on the current back buffer.
 
void rectangle (const Point &start, const Point &end, const ColorAlpha &color, bool filled=true)
 Draw a rectangle on the current back buffer.
 
void font (const Font &new_font)
 Change the current font for text output in video mode.
 
void text (const Point &p, const char *string, unsigned len, const Color &color, const Font *font=nullptr)
 Print text (without automatic word wrap) on the current back buffer.
 
void text (const Point &p, const char *string, unsigned len, const ColorAlpha &color, const Font *font=nullptr)
 Print text (without automatic word wrap) on the current back buffer.
 
void image (const Point &p, PNG &image, unsigned width=0, unsigned height=0, unsigned offset_x=0, unsigned offset_y=0)
 Draw a PNG image [detail] on the current back buffer.
 
void image (const Point &p, const GIMP &image, unsigned width=0, unsigned height=0, unsigned offset_x=0, unsigned offset_y=0)
 Draw a GIMP image [detail] on the current back buffer.
 
void image (const Point &p, const Color *image, unsigned width, unsigned height, unsigned offset_x=0, unsigned offset_y=0)
 Draw a sprite on the current back buffer.
 
void image (const Point &p, const ColorAlpha *image, unsigned width, unsigned height, unsigned offset_x=0, unsigned offset_y=0)
 Draw a sprite with alpha blending (transparency) on the current back buffer.
 

Private Attributes

AbstractGraphicsPrinterprinter
 Pointer to a GraphicsPrinter supporting the current video mode.
 
void * address
 Pointer to the physical address of the video memory (linear frame buffer)
 
unsigned size
 Video memory size required for a full screen picture.
 
unsigned buffer_size
 Size of the front (or back) buffer (which has to be at least size)
 
void *const buffer [2]
 Pointer to the two buffers (used alternately as front and back buffers)
 
int scanout_buffer
 Index of the current front buffer.
 
bool refresh
 Has the current front buffer already been drawn?
 

Detailed Description

Driver managing the video mode and synchronizing its buffer with the graphics printer.

This device detects the current video mode set by the Multiboot compliant boot loader and initializes a suitable GraphicsPrinter.

With the methods Graphics::switchBuffers() (to exchange front- and backbuffer) and Graphics::scanoutFrontbuffer() (copying the contents of the frontbuffer into the video memory) it provides some kind of manually triple buffering.

A typical usage is to fully prepare the back buffer before switching it with the front buffer

graphics.init();
while(true) {
// Draw on back buffer
// using the primitives provided by the driver
graphics.switchBuffers();
}
bool init(bool force=false)
Initialize GraphicsPrinter according to the current video mode.
Definition graphics.cc:85
bool switchBuffers()
Switch front and back buffer (only if front buffer was already copied to video memory)
Definition guarded_graphics.h:27

The method Graphics::scanoutFrontbuffer() has to be executed either inside the loop (right after Graphics::switchBuffers() in the example above) or at a predefined interval by employing the Watch.

Note
The driver requires Multiboot to initialize a video mode, which can be configured using the flags in boot/multiboot/config.inc.

Constructor & Destructor Documentation

◆ Graphics()

Graphics::Graphics ( unsigned size,
void * frontbuffer,
void * backbuffer )

Constructor.

Parameters
sizeSize of each buffer
frontbufferPointer to a reserved memory area with a sufficient size to transmit a full screen picture to the video memory at the current video mode / resolution.
backbufferPointer to a reserved memory area with the same size as the front buffer to prepare the picture.

Member Function Documentation

◆ font()

void Graphics::font ( const Font & new_font)
inline

Change the current font for text output in video mode.

Parameters
new_fontFont to be used on subsequent calls to text (without explicit font parameter)

◆ height()

unsigned Graphics::height ( )
inline

Number of vertical pixels in current resolution.

Returns
Height of the screen in current video mode

◆ image() [1/4]

void Graphics::image ( const Point & p,
const Color * image,
unsigned width,
unsigned height,
unsigned offset_x = 0,
unsigned offset_y = 0 )
inline

Draw a sprite on the current back buffer.

Each element in the source array will be displayed as a single pixel.

Parameters
pCoordinate of the sprites upper left corner
imageSource sprite to display
widthWidth of the sprite detail
heightHeight of the sprite detail
offset_xRight offset of the source sprite
offset_yTop offset of the source sprite

◆ image() [2/4]

void Graphics::image ( const Point & p,
const ColorAlpha * image,
unsigned width,
unsigned height,
unsigned offset_x = 0,
unsigned offset_y = 0 )
inline

Draw a sprite with alpha blending (transparency) on the current back buffer.

Each element in the source array will be displayed as a single pixel.

Parameters
pCoordinate of the sprites upper left corner
imageSource sprite to display
widthWidth of the sprite detail
heightHeight of the sprite detail
offset_xRight offset of the source sprite
offset_yTop offset of the source sprite

◆ image() [3/4]

void Graphics::image ( const Point & p,
const GIMP & image,
unsigned width = 0,
unsigned height = 0,
unsigned offset_x = 0,
unsigned offset_y = 0 )
inline

Draw a GIMP image [detail] on the current back buffer.

The image has to be exported as C-source (without Glib types!) in GIMP, alpha blending (transparency) is supported.

Parameters
pCoordinate of the images upper left corner
imageSource image to display
widthWidth of the image detail (full image width of the source image if zero/default value)
heightHeight of the image detail (full image height of the source if zero/default value)
offset_xRight offset of the source image
offset_yTop offset of the source image

◆ image() [4/4]

void Graphics::image ( const Point & p,
PNG & image,
unsigned width = 0,
unsigned height = 0,
unsigned offset_x = 0,
unsigned offset_y = 0 )
inline

Draw a PNG image [detail] on the current back buffer.

The image can has to be in a supported PNG format. Alpha blending (transparency) is supported.

Parameters
pCoordinate of the images upper left corner
imageSource image to display
widthWidth of the image detail (full image width of the source image if zero/default value)
heightHeight of the image detail (full image height of the source if zero/default value)
offset_xRight offset of the source image
offset_yTop offset of the source image

◆ init()

bool Graphics::init ( bool force = false)

Initialize GraphicsPrinter according to the current video mode.

Parameters
forceDo not check video attributes for the linear frame buffer (required on our test systems due to some strange VBE behaviour)
Returns
'true' if a suitable GraphicsPrinter was found for the video mode

◆ line() [1/2]

void Graphics::line ( const Point & start,
const Point & end,
const Color & color )
inline

Draw a line on the current back buffer.

Parameters
startCoordinates of the begin of the line
endCoordinates of the end of the line
colorColor of the line

◆ line() [2/2]

void Graphics::line ( const Point & start,
const Point & end,
const ColorAlpha & color )
inline

Draw a line on the current back buffer.

Parameters
startCoordinates of the begin of the line
endCoordinates of the end of the line
colorColor of the line

◆ pixel() [1/2]

void Graphics::pixel ( const Point & p,
const Color & color )
inline

Draw a pixel on the current back buffer.

Parameters
pCoordinates of the pixel
colorColor of the pixel

◆ pixel() [2/2]

void Graphics::pixel ( const Point & p,
const ColorAlpha & color )
inline

Draw a pixel on the current back buffer.

Parameters
pCoordinates of the pixel
colorColor of the pixel

◆ rectangle() [1/2]

void Graphics::rectangle ( const Point & start,
const Point & end,
const Color & color,
bool filled = true )
inline

Draw a rectangle on the current back buffer.

Parameters
startCoordinate of the rectangles upper left corner
endCoordinate of the rectangles lower right corner
colorColor of the rectangle
filledIf set, the rectangle will be filled with the same color. (otherwise only borders will be drawn)

◆ rectangle() [2/2]

void Graphics::rectangle ( const Point & start,
const Point & end,
const ColorAlpha & color,
bool filled = true )
inline

Draw a rectangle on the current back buffer.

Parameters
startCoordinate of the rectangles upper left corner
endCoordinate of the rectangles lower right corner
colorColor of the rectangle
filledIf set, the rectangle will be filled with the same color. (otherwise only borders will be drawn)

◆ switchBuffers()

bool Graphics::switchBuffers ( )

Switch front and back buffer (only if front buffer was already copied to video memory)

Returns
true if buffers have been switched, false if previous front buffer wasn't yet fully copied to video memory.

◆ text() [1/2]

void Graphics::text ( const Point & p,
const char * string,
unsigned len,
const Color & color,
const Font * font = nullptr )
inline

Print text (without automatic word wrap) on the current back buffer.

Parameters
pUpper left start position of the text
stringPointer to char array containing the text to be displayed
lenNumber of characters to be displayed
colorColor for the text characters
fontExplicit font – or nullptr to use default font (set by font method)

◆ text() [2/2]

void Graphics::text ( const Point & p,
const char * string,
unsigned len,
const ColorAlpha & color,
const Font * font = nullptr )
inline

Print text (without automatic word wrap) on the current back buffer.

Parameters
pUpper left start position of the text
stringPointer to char array containing the text to be displayed
lenNumber of characters to be displayed
colorColor for the text characters
fontExplicit font – or nullptr to use default font (set by font method)

◆ valid()

bool Graphics::valid ( const Point & p)
inline

Check if a Point can be displayed at the current resolution.

Parameters
pCoordinates to check
Returns
'true' if can be displayed

◆ width()

unsigned Graphics::width ( )
inline

Number of horizontal pixels in current resolution.

Returns
Width of the screen in current video mode

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