-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.hpp
118 lines (98 loc) · 3.25 KB
/
graphics.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef GRAPHICS_HPP_
#define GRAPHICS_HPP_
#include <stdint.h>
#include "bootparam.h"
extern unsigned char _binary_hankaku_bin_start[];
extern unsigned char _binary_hankaku_bin_end[];
extern unsigned char _binary_hankaku_bin_size[];
namespace bitnos::graphics
{
struct Point
{
int x, y;
};
inline Point operator +(const Point& lhs, const Point& rhs)
{
return {lhs.x + rhs.x, lhs.y + rhs.y};
}
struct RectSize
{
unsigned int width, height;
};
inline Point operator +(const Point& p, const RectSize& s)
{
return {
static_cast<decltype(p.x)>(p.x + s.width),
static_cast<decltype(p.y)>(p.y + s.width)
};
}
inline Point operator +(const RectSize& s, const Point& p)
{
return p + s;
}
struct Color
{
uint8_t r, g, b, a;
};
class PixelWriter
{
const uintptr_t frame_buffer_base_;
const uint32_t pixels_per_scan_line_;
const uint8_t bytes_per_pixel_;
protected:
uintptr_t CalcPixelAddr(const Point& pixel)
{
return frame_buffer_base_ +
bytes_per_pixel_ * (pixels_per_scan_line_ * pixel.y + pixel.x);
}
public:
PixelWriter(uintptr_t frame_buffer_base, uint32_t pixels_per_scan_line, uint32_t bytes_per_pixel)
: frame_buffer_base_(frame_buffer_base),
pixels_per_scan_line_(pixels_per_scan_line),
bytes_per_pixel_(bytes_per_pixel)
{}
virtual ~PixelWriter() = default;
PixelWriter(const PixelWriter&) = delete;
PixelWriter& operator =(const PixelWriter&) = delete;
PixelWriter(PixelWriter&&) = delete;
PixelWriter& operator =(PixelWriter&&) = delete;
virtual void Write(const Point& position, const Color& color) = 0;
};
class PixelWriterRedGreenBlueReserved8BitPerColor : public PixelWriter
{
public:
PixelWriterRedGreenBlueReserved8BitPerColor(const GraphicMode *mode)
: PixelWriter(mode->frame_buffer_base, mode->pixels_per_scan_line, 4)
{}
virtual void Write(const Point& position, const Color& color);
};
class PixelWriterBlueGreenRedReserved8BitPerColor : public PixelWriter
{
public:
PixelWriterBlueGreenRedReserved8BitPerColor(const GraphicMode *mode)
: PixelWriter(mode->frame_buffer_base, mode->pixels_per_scan_line, 4)
{}
virtual void Write(const Point& position, const Color& color);
};
/** @brief DrawAscii draws an ascii character to the given pixel writer.
* _binary_hankaku_bin_start font is used.
*
* @param w Pixel writer to draw a character
* @param position Pixel position
* @param ch Ascii code
*/
void DrawAscii(PixelWriter& w, const Point& position, char ch);
/** @brief DrawRect draws a rectangle to the given pixel writer.
*
* @param w Pixel writer to draw a rectangle
* @param position Pixel position
* @param size Size of the rectangle (x and y must be positive value)
* @param color Draw color
*/
void DrawRect(
PixelWriter& w,
const Point& position,
const RectSize& size,
const Color& color);
}
#endif // GRAPHICS_HPP_